manage the case when we want to add a consultation to insure but the start date of his insurance has not been reached

This commit is contained in:
root 2026-01-07 18:32:21 +01:00
parent a43b4a1fb0
commit 3390a56c74
3 changed files with 73 additions and 46 deletions

View File

@ -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);
}
}

View File

@ -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"
];

View File

@ -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"
];