From 3390a56c74bdc49e846acfb23bf5df101c76f46e Mon Sep 17 00:00:00 2001 From: root Date: Wed, 7 Jan 2026 18:32:21 +0100 Subject: [PATCH] manage the case when we want to add a consultation to insure but the start date of his insurance has not been reached --- app/Http/Controllers/InsuredController.php | 115 ++++++++++++--------- resources/lang/en/errors.php | 2 + resources/lang/fr/errors.php | 2 + 3 files changed, 73 insertions(+), 46 deletions(-) diff --git a/app/Http/Controllers/InsuredController.php b/app/Http/Controllers/InsuredController.php index d6c7339..7808816 100755 --- a/app/Http/Controllers/InsuredController.php +++ b/app/Http/Controllers/InsuredController.php @@ -87,52 +87,75 @@ class InsuredController extends Controller * ) * ) */ - public function getInsured(Request $request) - { - $insured_id = $request->input('insured_id'); - $name = $request->input('name'); - $phone = $request->input('phone'); - $network_id = $request->input('network_id'); - $network = Network::find($network_id); - if (!isset($network)) { - return $this->errorResponse(trans('errors.network_not_found')); - } +public function getInsured(Request $request) +{ + $insured_id = $request->input('insured_id'); + $name = $request->input('name'); + $phone = $request->input('phone'); + $network_id = $request->input('network_id'); - $currency_code = $network->country->currency_code; - $datetime = $this->getCurrentTimeByCountryCode($network->country->code_country); - - $q = NhInsurance::with(['user:id,firstname,lastname,phone,email', 'user.identification:firstname,lastname,user_image,document_image_front,document_image_back,id_user', - 'network:id,name', 'beneficiaries', 'nhNetworkConfig', 'monthsGrid'])->where('network_id', $network_id) - ->whereIn('state', [InsuranceState::PAID, InsuranceState::PARTIALLY_PAID, InsuranceState::SUSPENDED])->where('start_at', '<=', $datetime); - - if (!empty($insured_id)) { - $q = $q->where('insured_id', $insured_id); - } - - if (!empty($name)) { - $q = $q->whereHas('user', function ($query) use ($name) { - return $query->where('lastname', 'like', '%' . $name . '%')->orWhere('firstname', 'like', '%' . $name . '%'); - }); - } - - if (!empty($phone)) { - $q = $q->whereHas('user', function ($query) use ($phone) { - return $query->where('phone', 'like', '%' . $phone . '%'); - }); - } - - $insured = $q->get(); - foreach ($insured as $i) { - $monthPrice = $i->monthsGrid; - $i->insurance_consumed_amount = $this->toMoneyWithCurrencyCode($i->insurance_coverage_amount, $currency_code); - $i->insurance_remaining_amount = $this->toMoneyWithCurrencyCode(($monthPrice->max_insurance_coverage_amount ?? 0) - $i->insurance_coverage_amount, $currency_code); - foreach ($i->beneficiaries as $b) { - $b->insurance_consumed_amount = $this->toMoneyWithCurrencyCode($b->insurance_coverage_amount, $currency_code); - $b->insurance_remaining_amount = $this->toMoneyWithCurrencyCode(($monthPrice->max_insurance_coverage_amount ?? 0) - $b->insurance_coverage_amount, $currency_code); - } - - unset($i->nhNetworkConfig); - } - return $this->successResponse($insured); + $network = Network::find($network_id); + if (!isset($network)) { + return $this->errorResponse(trans('errors.network_not_found')); } + + $currency_code = $network->country->currency_code; + $datetime = $this->getCurrentTimeByCountryCode($network->country->code_country); + + $baseQuery = NhInsurance::with([ + 'user:id,firstname,lastname,phone,email', + 'user.identification:firstname,lastname,user_image,document_image_front,document_image_back,id_user', + 'network:id,name', + 'beneficiaries', + 'nhNetworkConfig', + 'monthsGrid' + ]) + ->where('network_id', $network_id) + ->whereIn('state', [InsuranceState::PAID, InsuranceState::PARTIALLY_PAID, InsuranceState::SUSPENDED]); + + if (!empty($insured_id)) { + $baseQuery->where('insured_id', $insured_id); + } + if (!empty($name)) { + $baseQuery->whereHas('user', function ($query) use ($name) { + return $query->where('lastname', 'like', '%' . $name . '%')->orWhere('firstname', 'like', '%' . $name . '%'); + }); + } + if (!empty($phone)) { + $baseQuery->whereHas('user', function ($query) use ($phone) { + return $query->where('phone', 'like', '%' . $phone . '%'); + }); + } + + // ESSAI 1 : On cherche les assurances déjà ACTIVES + $activeQuery = clone $baseQuery; + $insured = $activeQuery->where('start_at', '<=', $datetime)->get(); + + // GESTION DU CAS "FUTUR" : Si aucun assuré actif n'est trouvé + if ($insured->isEmpty()) { + // On vérifie s'il existe une assurance qui commence plus tard + $futureInsurance = $baseQuery->where('start_at', '>', $datetime)->first(); + + if ($futureInsurance) { + $dateDebut = date('d/m/Y', strtotime($futureInsurance->start_at)); + return $this->errorResponse(trans('errors.insurance_not_active_yet', ['dateDebut' => $dateDebut])); + } + + return $this->errorResponse(trans('errors.no_insured_found')); + } + + foreach ($insured as $i) { + $monthPrice = $i->monthsGrid; + $i->insurance_consumed_amount = $this->toMoneyWithCurrencyCode($i->insurance_coverage_amount, $currency_code); + $i->insurance_remaining_amount = $this->toMoneyWithCurrencyCode(($monthPrice->max_insurance_coverage_amount ?? 0) - $i->insurance_coverage_amount, $currency_code); + + foreach ($i->beneficiaries as $b) { + $b->insurance_consumed_amount = $this->toMoneyWithCurrencyCode($b->insurance_coverage_amount, $currency_code); + $b->insurance_remaining_amount = $this->toMoneyWithCurrencyCode(($monthPrice->max_insurance_coverage_amount ?? 0) - $b->insurance_coverage_amount, $currency_code); + } + unset($i->nhNetworkConfig); + } + + return $this->successResponse($insured); +} } diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index dc8fb69..e4b4709 100755 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -78,4 +78,6 @@ return [ 'payment_not_found' => "Payment not found", 'payment_invalid' => "Invalid payment", "transaction_already_completed" => "This transaction has already been completed", + 'insurance_not_active_yet' => "The insurance is paid, but it will only be active from :dateDebut", + 'no_insured_found' => "No insured found with these criteria" ]; diff --git a/resources/lang/fr/errors.php b/resources/lang/fr/errors.php index 8f194ce..d5f2722 100755 --- a/resources/lang/fr/errors.php +++ b/resources/lang/fr/errors.php @@ -81,4 +81,6 @@ return [ 'payment_not_found' => "Paiement non trouvé", 'payment_invalid' => "Paiement invalide", "transaction_already_completed" => "Cette transaction a déjà été éffectuée", + 'insurance_not_active_yet' => "L'assurance est bien payée, mais elle ne sera active qu'à partir du :dateDebut", + 'no_insured_found' => "Aucun assuré trouvé avec ces critères" ];