Fix insurance invoice pay and improve GET insurance invoices response payload

This commit is contained in:
Djery-Tom 2022-05-04 11:52:01 +01:00
parent 50fd541415
commit 26df499acd
6 changed files with 43 additions and 15 deletions

View File

@ -676,7 +676,7 @@ class InsuranceController extends Controller
}
if (NhInsurancesInvoice::where('insurance_id', $id)->where('state', InsuranceInvoiceState::UNPAID)->count() > 0) {
return $this->errorResponse("Vous avez des factures non payés");
return $this->errorResponse(trans('errors.unpaid_bill'));
}
$user = $insurance->user;

View File

@ -103,11 +103,12 @@ class InsuranceInvoiceController extends Controller
* example = {
* "status" : 200,
* "response" : {{"id":1,"invoice_id":"SFSF6565656","insurance_id":5,"subscription_id":2,"amount":"495\u202f000 FCFA","next_payment_deadline":"2022-04-08 17:31:52","next_payment_reminder":"2022-04-05 17:31:52","state":"NON PAY\u00c9E",
* "reason":"ACTIVATION DE L'ASSURANCE","created_at":"2022-03-29T16:31:52.000000Z","updated_at":"2022-03-29T16:31:52.000000Z","insurance":{"id":5,"network_id":250,"user_id":349,"insured_id":"5DSTKZ7PQZX4",
* "months_grid_id":77,"bonus_amount":"150000.00","number_of_beneficiaries":2,"total_bonus_amount":"495000.00","insurance_coverage_amount":"0.00","start_at":null,"end_at":null,"state":"UNDER_ACTIVATION",
* "reason":"ACTIVATION DE L'ASSURANCE","created_at":"2022-03-29T16:31:52.000000Z","updated_at":"2022-03-29T16:31:52.000000Z","insurance":{"id":5,"network_id":250,"user_id":349,"insured_id":"5DSTKZ7PQZX4","remaining_amount": "0 FCFA",
"paid_amount": "495000 FCFA","months_grid_id":77,"bonus_amount":"150000.00","number_of_beneficiaries":2,"total_bonus_amount":"495000.00","insurance_coverage_amount":"0.00","start_at":null,"end_at":null,"state":"UNDER_ACTIVATION",
* "deadlines":1,"amount_last_payment":"495000.00","amount_per_split":"495000.00","paid_deadlines":0,"created_at":"2022-03-29T16:31:52.000000Z","updated_at":"2022-03-29T16:31:52.000000Z"},
* "subscription":{"id":2,"insurance_subscription_id":"CX36UNA2VVOP","network_id":250,"user_id":349,"months_grid_id":77,"bonus_amount":"150000.00","number_of_beneficiaries":2,"
* total_bonus_amount":"495000.00","state":"ACCEPTED","insurance_action":"ACTIVATION","created_at":"2021-11-10T16:52:32.000000Z","updated_at":"2022-03-29T17:31:52.000000Z","reason":null}}},
* total_bonus_amount":"495000.00","state":"ACCEPTED","insurance_action":"ACTIVATION","created_at":"2021-11-10T16:52:32.000000Z","updated_at":"2022-03-29T17:31:52.000000Z","reason":null},"payments": {{ "id": 8, "payment_id": "RRS63SV7VP", "invoice_id": 7,
* "amount": "200000 FCFA", "created_at": "2022-04-29T16:09:59.000000Z", "updated_at": "2022-04-29T16:09:59.000000Z" }}}},
* "error":null
* }
* )
@ -126,7 +127,7 @@ class InsuranceInvoiceController extends Controller
$currency_code = $user->network->country->currency_code;
$pagination = $request->input('pagination');
$state = $request->input('state');
$datetime = $this->getCurrentTimeByCountryCode($user->network->country->code_country);
// $datetime = $this->getCurrentTimeByCountryCode($user->network->country->code_country);
$query = NhInsurancesInvoice::with(['insurance', 'subscription'])
->whereHas('insurance', function ($q) use ($user_id) {
@ -140,6 +141,8 @@ class InsuranceInvoiceController extends Controller
// } else {
$query = $query->where('state', $state);
// }
} else {
$query = $query->with(['payments']);
}
if ($pagination) {
@ -150,9 +153,18 @@ class InsuranceInvoiceController extends Controller
$array = $pagination ? $invoices->items() : $invoices;
foreach ($array as $invoice) {
$paid_amount = floatval($invoice->paid_amount_non_formatted);
$invoice->state = trans('states.' . $invoice->state);
$invoice->reason = trans('states.' . $invoice->reason);
$invoice->remaining_amount = $this->toMoneyWithCurrencyCode($invoice->amount - $paid_amount, $currency_code);
$invoice->paid_amount = $this->toMoneyWithCurrencyCode($paid_amount, $currency_code);
$invoice->amount = $this->toMoneyWithCurrencyCode($invoice->amount, $currency_code);
if (empty($state)) {
foreach ($invoice->payments as $payment) {
$payment->amount = $this->toMoneyWithCurrencyCode($payment->amount, $currency_code);
}
}
}
return $this->successResponse($invoices);
}
@ -191,7 +203,7 @@ class InsuranceInvoiceController extends Controller
* description="Montant à payer"
* )
* ),
* example = {"password":"adbc1215448"}
* example = {"password":"adbc1215448", "amount" : 50000 }
* )
* ),
* @OA\Response(
@ -273,10 +285,15 @@ class InsuranceInvoiceController extends Controller
'amount' => $amountToPaid
]);
// Calcul de l'equivalence en nombre d'echeance
$paid_deadlines = $amountToPaid < $invoice->amount_per_split ? 1 : intval($amountToPaid / $invoice->amount_per_split);
$invoice->paid_deadlines += $paid_deadlines;
$invoice->next_payment_deadlines_to_paid -= $paid_deadlines;
// Calcul de l'équivalence en nombre d'échéance
if (($total_remains_amount - $amountToPaid) == 0) {
$invoice->paid_deadlines = $invoice->deadlines;
$invoice->next_payment_deadlines_to_paid = 0;
} else {
$paid_deadlines = $amountToPaid < $invoice->amount_per_split ? 1 : intval($amountToPaid / $invoice->amount_per_split);
$invoice->paid_deadlines += $paid_deadlines;
$invoice->next_payment_deadlines_to_paid -= $paid_deadlines;
}
if ($invoice->next_payment_deadlines_to_paid < 0) {
$invoice->next_payment_deadlines_to_paid = 0;
@ -335,7 +352,7 @@ class InsuranceInvoiceController extends Controller
trans('messages.insurance_paid_mail', ['name' => $invoice->insurance->user->lastname, 'insured_id' => $invoice->insurance->insured_id,
'bonus_amount' => $this->toMoneyWithCurrencyCode($invoice->insurance->bonus_amount, $currency), 'total_bonus_amount' => $this->toMoneyWithCurrencyCode($invoice->insurance->total_bonus_amount, $currency), 'number_of_beneficiaries' => $invoice->insurance->number_of_beneficiaries,
'gender' => trans('states.' . $invoice->insurance->user->identification->gender), 'insurance_name' => $invoice->insurance->network->name, 'months' => $invoice->insurance->monthsGrid->number_of_months, 'invoice_id' => $invoice->invoice_id,
'amount' => $this->toMoneyWithCurrencyCode($invoice->amount, $currency), 'paid_amount' => $this->toMoneyWithCurrencyCode($total_paid_amount + $amountToPaid, $currency), 'remains_amount' => $this->toMoneyWithCurrencyCode($total_remains_amount - $amountToPaid, $currency),
'amount' => $this->toMoneyWithCurrencyCode($amountToPaid, $currency), 'paid_amount' => $this->toMoneyWithCurrencyCode($total_paid_amount + $amountToPaid, $currency), 'remains_amount' => $this->toMoneyWithCurrencyCode($total_remains_amount - $amountToPaid, $currency),
'payment_period' => trans('states.' . $invoice->insurance->monthsGrid->payment_period), 'reason' => trans('states.' . $invoice->reason), 'title' => $isPartialPayment ? trans('messages.insurance_partially_paid_title') : trans('messages.insurance_fully_paid_title'),
'waiting_days' => empty($invoice->insurance->monthsGrid->waiting_period_days) ? trans('messages.none') : trans('messages.n_days', ['n' => $invoice->insurance->monthsGrid->waiting_period_days]),
'start_at' => $invoice->insurance->start_at, 'payment_id' => $payment->payment_id

View File

@ -20,6 +20,7 @@ class NhInsurancesInvoice extends Model
];
protected $guarded = ['id'];
protected $appends = ['paid_amount_non_formatted'];
public function insurance()
{
@ -30,4 +31,14 @@ class NhInsurancesInvoice extends Model
{
return $this->belongsTo(NhInsurancesSubscription::class, 'subscription_id');
}
public function payments()
{
return $this->hasMany(NhInsurancesPayment::class, 'invoice_id');
}
public function getPaidAmountNonFormattedAttribute()
{
return $this->payments()->sum('amount');
}
}

View File

@ -15,7 +15,6 @@ class NhInsurancesPayment extends Model
protected $casts = [
'invoice_id' => 'int',
'amount' => 'float'
];
protected $guarded = ['id'];

View File

@ -71,5 +71,6 @@ return [
'prescription_ordered_quantity_must_not_greater_than_ordered_quantity' => "Prescription quantity :id must not be greater than :quantity",
'consultation_prescription_not_found' => "The consultation attached to this prescription does not exist",
'minimum amount_to_paid' => "The minimum amount to pay is :amount",
'maximum amount_to_paid' => "The maximum amount to pay is :amount"
'maximum amount_to_paid' => "The maximum amount to pay is :amount",
"unpaid_bill" => "You have an unpaid bill"
];

View File

@ -74,6 +74,6 @@ return [
'sheet_exams_duplicata' => "La feuille de soins a des duplicata d'examens",
'sheet_prescriptions_duplicata' => "La feuille de soins a des duplicata de prescriptions médicales",
'minimum_amount_to_paid' => "Le montant minimum à payer est de :amount",
'maximum_amount_to_paid' => "Le montant maximum à payer est de :amount"
'maximum_amount_to_paid' => "Le montant maximum à payer est de :amount",
"unpaid_bill" => "Vous avez une facture non payée"
];