|
|
|
|
@ -3,10 +3,12 @@
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Events\InsuranceEvent;
|
|
|
|
|
use App\InsuranceAction;
|
|
|
|
|
use App\InsuranceState;
|
|
|
|
|
use App\InsuranceSubscriptionState;
|
|
|
|
|
use App\Models\CountriesCurrency;
|
|
|
|
|
use App\Models\NhInsurance;
|
|
|
|
|
use App\Models\NhInsurancesSubscription;
|
|
|
|
|
use App\Models\NhInsurancesSubscriptionsHistory;
|
|
|
|
|
use App\Models\NhNetworksConfig;
|
|
|
|
|
use App\Traits\Helper;
|
|
|
|
|
@ -124,15 +126,14 @@ class InsuranceController extends Controller
|
|
|
|
|
* ref="#/components/schemas/ApiResponse",
|
|
|
|
|
* example = {
|
|
|
|
|
* "status" : 200,
|
|
|
|
|
* "response" : {{"id":3,"insurance_subscription_id":"3QM2DS9V4KEG","insured_id":"GJZF4VK69V6S","start_at":"2021-11-08T13:13:27.000000Z",
|
|
|
|
|
* "response" : {{"id":3,"insured_id":"GJZF4VK69V6S","start_at":"2021-11-08T13:13:27.000000Z",
|
|
|
|
|
* "end_at":"2022-02-08T13:13:27.000000Z","state":"PAY\u00c9E","created_at":"2021-11-08T12:13:27.000000Z","updated_at":"2021-11-08T12:13:27.000000Z",
|
|
|
|
|
* "subscription":{"id":2,"insurance_subscription_id":"3QM2DS9V4KEG","network_id":250,"user_id":349,"number_of_months":3,"bonus_amount":"150\u202f000\u00a0FCFA",
|
|
|
|
|
* "number_of_beneficiaries":2,"total_bonus_amount":"495\u202f000\u00a0FCFA","state":"ACCEPT\u00c9E","created_at":"2021-11-01T15:18:34.000000Z",
|
|
|
|
|
* "updated_at":"2021-11-05T16:43:00.000000Z","reason":"Quel est ton vrai age ?","network": { "id": 250, "name": "Cnamgs-pharmacies", "age_limit_of_insured_and_spouse": 30,
|
|
|
|
|
* "network_id":250,"user_id":349,"number_of_months":3,"bonus_amount":"150\u202f000\u00a0FCFA",
|
|
|
|
|
* "number_of_beneficiaries":2,"total_bonus_amount":"495\u202f000\u00a0FCFA","network": { "id": 250, "name": "Cnamgs-pharmacies", "age_limit_of_insured_and_spouse": 30,
|
|
|
|
|
* "age_limit_of_child_beneficiary": 25, "max_number_of_beneficiaries": 5 },"beneficiaries":{{"id":5,"insurance_subscription_id":"3QM2DS9V4KEG",
|
|
|
|
|
* "lastname":"Djery","firstname":"DI","gender":"M","birthdate":"2001-10-05T00:00:00.00000 0Z","affiliation":"CHILD","bonus_amount":"195\u202f000\u00a0FCFA",
|
|
|
|
|
* "birthdate_proof":"CERTIFIED_COPY","birthdate_proof_doc":"birth.jpg","justice_doc":"just.png","marriage_certificate_doc":null,"id_document_type":null,
|
|
|
|
|
* "id_document_front":null,"id_document_back":null,"deleted_at":null,"created_at":"2021-11-01T15:18:34.000000Z","updated_at":"2021-11-01T15:18:34.000000Z","affiliation_tr":"ENFANT"}}}}},
|
|
|
|
|
* "id_document_front":null,"id_document_back":null,"deleted_at":null,"created_at":"2021-11-01T15:18:34.000000Z","updated_at":"2021-11-01T15:18:34.000000Z","affiliation_tr":"ENFANT"}}}},
|
|
|
|
|
* "error":null
|
|
|
|
|
* }
|
|
|
|
|
* )
|
|
|
|
|
@ -149,9 +150,7 @@ class InsuranceController extends Controller
|
|
|
|
|
$userId = $request->input('user_id');
|
|
|
|
|
$type = $request->input('type');
|
|
|
|
|
|
|
|
|
|
$query = NhInsurance::with(['subscription', 'subscription.network:id,name', 'subscription.beneficiaries'])->whereHas('subscription', function ($query) use ($userId) {
|
|
|
|
|
return $query->where('user_id', $userId);
|
|
|
|
|
});
|
|
|
|
|
$query = NhInsurance::with(['network:id,name', 'beneficiaries'])->where('user_id', $userId);
|
|
|
|
|
|
|
|
|
|
if ($request->has('type') && $type == 'EDITABLE') {
|
|
|
|
|
$query = $query->whereIn('state', [InsuranceState::PAID]);
|
|
|
|
|
@ -164,17 +163,17 @@ class InsuranceController extends Controller
|
|
|
|
|
|
|
|
|
|
if ($type == 'EDITABLE') {
|
|
|
|
|
// Nécessaire seulement lors de la modification ( ajout de ayant droit)
|
|
|
|
|
$config = NhNetworksConfig::where('network_id', $insurance->subscription->network->id)->firstOrFail();
|
|
|
|
|
$insurance->subscription->network->age_limit_of_insured_and_spouse = $config->age_limit_of_insured_and_spouse;
|
|
|
|
|
$insurance->subscription->network->age_limit_of_child_beneficiary = $config->age_limit_of_child_beneficiary;
|
|
|
|
|
$insurance->subscription->network->max_number_of_beneficiaries = $config->max_number_of_beneficiaries;
|
|
|
|
|
$config = NhNetworksConfig::where('network_id', $insurance->network->id)->firstOrFail();
|
|
|
|
|
$insurance->network->age_limit_of_insured_and_spouse = $config->age_limit_of_insured_and_spouse;
|
|
|
|
|
$insurance->network->age_limit_of_child_beneficiary = $config->age_limit_of_child_beneficiary;
|
|
|
|
|
$insurance->network->max_number_of_beneficiaries = $config->max_number_of_beneficiaries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$currency_code = $this->getNetworkCurrency($insurance->subscription->network_id);
|
|
|
|
|
$insurance->subscription->state = trans('states.' . $insurance->subscription->state);
|
|
|
|
|
$insurance->subscription->bonus_amount = $this->toMoneyWithCurrencyCode($insurance->subscription->bonus_amount, $currency_code);
|
|
|
|
|
$insurance->subscription->total_bonus_amount = $this->toMoneyWithCurrencyCode($insurance->subscription->total_bonus_amount, $currency_code);
|
|
|
|
|
foreach ($insurance->subscription->beneficiaries as $b) {
|
|
|
|
|
$currency_code = $this->getNetworkCurrency($insurance->network_id);
|
|
|
|
|
$insurance->state = trans($insurance->state);
|
|
|
|
|
$insurance->bonus_amount = $this->toMoneyWithCurrencyCode($insurance->bonus_amount, $currency_code);
|
|
|
|
|
$insurance->total_bonus_amount = $this->toMoneyWithCurrencyCode($insurance->total_bonus_amount, $currency_code);
|
|
|
|
|
foreach ($insurance->beneficiaries as $b) {
|
|
|
|
|
$b->bonus_amount = $this->toMoneyWithCurrencyCode($b->bonus_amount, $currency_code);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -257,12 +256,14 @@ class InsuranceController extends Controller
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$insurance = NhInsurance::findOrFail($id);
|
|
|
|
|
$subscription = $insurance->subscription;
|
|
|
|
|
if ($subscription->state != InsuranceSubscriptionState::ACCEPTED) {
|
|
|
|
|
return $this->errorResponse(trans('errors.subscription_cannot_be_submitted'));
|
|
|
|
|
$latestSubscription = NhInsurancesSubscription::where('network_id', $request->input('network_id'))->where('user_id', $request->input('user_id'))
|
|
|
|
|
->whereNotIn('state', [InsuranceSubscriptionState::REJECTED])->orderBy('created_at', 'DESC')->first();
|
|
|
|
|
|
|
|
|
|
if (isset($latestSubscription)) {
|
|
|
|
|
return $this->errorResponse(trans('errors.subscription_cannot_be_submitted', ['state' => mb_strtolower(trans('states.' . $latestSubscription->state), 'UTF-8')]));
|
|
|
|
|
}
|
|
|
|
|
$user = $subscription->user;
|
|
|
|
|
$identification = $subscription->user->identification;
|
|
|
|
|
$user = $insurance->user;
|
|
|
|
|
$identification = $insurance->user->identification;
|
|
|
|
|
|
|
|
|
|
if (!isset($identification) || $identification->status == 0)
|
|
|
|
|
return $this->errorResponse(trans('errors.user_identification_required'));
|
|
|
|
|
@ -270,53 +271,40 @@ class InsuranceController extends Controller
|
|
|
|
|
if (!checkPassword($request->password, $user->encrypted_password, $user->salt))
|
|
|
|
|
return $this->errorResponse(trans('messages.incorrect_user_password'));
|
|
|
|
|
|
|
|
|
|
$nbOfBeneficiaries = $subscription->beneficiaries()->count();
|
|
|
|
|
$networkConfig = NhNetworksConfig::where('network_id', $subscription->network_id)->first();
|
|
|
|
|
$nbOfBeneficiaries = $insurance->beneficiaries()->count();
|
|
|
|
|
$networkConfig = NhNetworksConfig::where('network_id', $insurance->network_id)->first();
|
|
|
|
|
if ((sizeof($request->input('beneficiaries')) + $nbOfBeneficiaries) > $networkConfig->max_number_of_beneficiaries)
|
|
|
|
|
return $this->errorResponse(trans('errors.number_of_beneficiaries_exceeded'));
|
|
|
|
|
|
|
|
|
|
$monthPrice = $networkConfig->monthsPricesGrid()->where('number_of_months', $subscription->number_of_months)->first();
|
|
|
|
|
$monthPrice = $networkConfig->monthsPricesGrid()->where('number_of_months', $insurance->number_of_months)->first();
|
|
|
|
|
if (!isset($monthPrice))
|
|
|
|
|
return $this->errorResponse(trans('errors.incorrect_selected_amount'));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
$datetime = $this->getCurrentTimeByCountryCode($networkConfig->network->country->code_country);
|
|
|
|
|
// Creer une nouvelle subscription en clonant l'anciene
|
|
|
|
|
$newSubscription = $subscription->replicate();
|
|
|
|
|
$newSubscription->insurance_subscription_id = $this->generateSubscriptionID();
|
|
|
|
|
$newSubscription->number_of_beneficiaries += sizeof($request->input('beneficiaries'));
|
|
|
|
|
$newSubscription->push();
|
|
|
|
|
|
|
|
|
|
//reset relations on EXISTING MODEL (this way you can control which ones will be loaded
|
|
|
|
|
$subscription->setRelations([]);
|
|
|
|
|
//load relations on EXISTING MODEL
|
|
|
|
|
$subscription->load('beneficiaries');
|
|
|
|
|
foreach ($subscription->getRelations() as $relation => $items) {
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
|
$item->insurance_subscription_id = $newSubscription->insurance_subscription_id;
|
|
|
|
|
unset($item->id);
|
|
|
|
|
$newSubscription->{$relation}()->create($item->toArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Creer une nouvelle subscription
|
|
|
|
|
$newSubscription = NhInsurancesSubscription::create([
|
|
|
|
|
'network_id' => $insurance->network_id,
|
|
|
|
|
'user_id' => $insurance->user_id,
|
|
|
|
|
'insurance_subscription_id' => $this->generateSubscriptionID(),
|
|
|
|
|
'number_of_beneficiaries' => sizeof($request->input('beneficiaries')),
|
|
|
|
|
'number_of_months' => $monthPrice->number_of_months,
|
|
|
|
|
'bonus_amount' => $monthPrice->min_amount,
|
|
|
|
|
'insurance_action' => InsuranceAction::ADDITION_OF_BENEFICIARY
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Ajouter les nouveaux ayant droit
|
|
|
|
|
$beneficiariesBonus = $this->storeBeneficiariesAndGetBonus($newSubscription, $request, $networkConfig, $monthPrice, $datetime);
|
|
|
|
|
|
|
|
|
|
$newSubscription->total_bonus_amount += $beneficiariesBonus;
|
|
|
|
|
$newSubscription->total_bonus_amount = $newSubscription->bonus_amount + $beneficiariesBonus;
|
|
|
|
|
$newSubscription->created_at = $newSubscription->updated_at = $datetime;
|
|
|
|
|
$newSubscription->save();
|
|
|
|
|
|
|
|
|
|
$insurance->insurance_subscription_id = $newSubscription->insurance_subscription_id;
|
|
|
|
|
$insurance->state = InsuranceState::ADDITION_OF_BENEFICIARY;
|
|
|
|
|
$insurance->remaining_amount = $beneficiariesBonus;
|
|
|
|
|
$insurance->save();
|
|
|
|
|
|
|
|
|
|
NhInsurancesSubscriptionsHistory::create([
|
|
|
|
|
'action' => 'ADD',
|
|
|
|
|
'insurance_subscription_id' => $newSubscription->insurance_subscription_id,
|
|
|
|
|
'insurance_subscription_state' => $newSubscription->state,
|
|
|
|
|
'insurance_subscription' => json_encode($newSubscription),
|
|
|
|
|
'created_at' => $datetime, 'updated_at' => $datetime,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|