diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index e5bc913..eba6065 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -176,7 +176,8 @@ class Kernel extends ConsoleKernel DB::beginTransaction(); $datetime = new DateTime(); - $insurances = NhInfosInsurances::with(['network:id,name', 'user.identification', 'monthsGrid'])->whereIn('state', [InsuranceState::PAID, InsuranceState::PARTIALLY_PAID])->where('end_at', '<=', $datetime)->get(); + $insurances = NhInsurance::with(['network:id,name', 'user.identification', 'monthsGrid'])->whereIn('state', [InsuranceState::PAID, InsuranceState::PARTIALLY_PAID]) + ->where('end_at', '<=', $datetime)->get(); NhInsurance::whereIn('state', [InsuranceState::PAID, InsuranceState::PARTIALLY_PAID])->where('end_at', '<=', $datetime)->update([ 'state' => InsuranceState::EXPIRED diff --git a/app/Http/Controllers/HealthCareSheetController.php b/app/Http/Controllers/HealthCareSheetController.php index d3d02e1..3191b39 100755 --- a/app/Http/Controllers/HealthCareSheetController.php +++ b/app/Http/Controllers/HealthCareSheetController.php @@ -928,11 +928,12 @@ class HealthCareSheetController extends Controller 'prescriptions' => 'required_without:exams|array', 'prescriptions.*.id' => 'required|integer|exists:nh_medical_prescriptions,id', 'prescriptions.*.unit_price' => 'required|numeric', + 'prescriptions.*.quantity' => 'nullable|integer|min:1', 'exams' => 'required_without:prescriptions|array', 'exams.*.id' => 'required|integer|exists:nh_exams,id', 'exams.*.quantity' => 'required|integer|min:1', 'exams.*.unit_quantity' => 'nullable|integer|min:1', - 'exams.*.unit_price' => 'nullable|numeric', + 'exams.*.unit_price' => 'nullable|numeric|min:1', ]); $prescriptions = $request->input('prescriptions', []); @@ -1009,6 +1010,17 @@ class HealthCareSheetController extends Controller DB::rollback(); return $this->errorResponse(__('errors.prescription_already_invoiced', ['id' => $itemIndex + 1])); } + if (isset($prescriptions[$itemIndex]['quantity'])) { + $quantity_to_bill = $item->quantity - ($item->billed_quantity ?? 0); + if ($prescriptions[$itemIndex]['quantity'] > $quantity_to_bill) { + DB::rollback(); + return $this->errorResponse(__('errors.prescription_ordered_quantity_must_not_greater_than_ordered_quantity', + ['id' => $itemIndex + 1, 'quantity' => $quantity_to_bill])); + } + + $item->quantity = $prescriptions[$itemIndex]['quantity']; + $i->billed_quantity += $item->quantity; + } $item->unit_price = $prescriptions[$itemIndex]['unit_price']; } @@ -1022,13 +1034,13 @@ class HealthCareSheetController extends Controller return $this->errorResponse(__('errors.exam_already_invoiced', ['id' => $itemIndex + 1])); } // Fetch exam unit price - if ($i->act->billing_type == ActBillingType::UNIT_PRICE && empty($exams[$itemIndex]['unit_quantity'])) { + if ($i->act->billing_type == ActBillingType::UNIT_PRICE && !isset($exams[$itemIndex]['unit_quantity'])) { DB::rollBack(); return $this->errorResponse(trans('errors.act_unit_quantity_required', ['act_name' => $i->act->name])); } if ($i->act->billing_type == ActBillingType::FREE) { - if (empty($exams[$itemIndex]['unit_price'])) { + if (!isset($exams[$itemIndex]['unit_price'])) { DB::rollBack(); return $this->errorResponse(trans('errors.act_unit_price_required', ['act_name' => $i->act->name])); } @@ -1046,9 +1058,14 @@ class HealthCareSheetController extends Controller $item->insurer_paid_amount = $parts->insurer_part * $item->unit_price * ($item->quantity ?? 1) * ($item->unit_quantity ?? 1); $item->created_at = $item->updated_at = $datetime; $item->billed = true; + $item->billed_quantity = null; $item->push(); - $i->billed = true; + if (!empty($i->billed_quantity) && $i->billed_quantity != $i->quantity) { + $i->billed = false; + } else { + $i->billed = true; + } $i->save(); @@ -1742,7 +1759,7 @@ class HealthCareSheetController extends Controller 'exams.*.act_id' => 'required_with:exams.*.id|integer|exists:nh_acts,id', 'exams.*.description' => 'required_with:exams.*.id|string', 'exams.*.quantity' => 'nullable|integer', - 'exams.*.unit_price' => 'nullable|numeric', + 'exams.*.unit_price' => 'nullable|numeric|min:1', 'exams.*.unit_quantity' => 'nullable|integer|min:1', 'exams.*.to_delete' => 'nullable|boolean', ]); @@ -1916,10 +1933,10 @@ class HealthCareSheetController extends Controller $exam->description = !empty($p['description']) ? $p['description'] : $exam->description; } else { $exam->quantity = !empty($p['quantity']) ? $p['quantity'] : $exam->quantity; - if (!empty($p['unit_price'])) { + if (isset($p['unit_price'])) { $exam->unit_price = $p['unit_price']; } - if (!empty($p['unit_quantity'])) { + if (isset($p['unit_quantity'])) { $exam->unit_quantity = $p['unit_quantity']; } $exam->insured_paid_amount = $parts->insured_part * $exam->unit_price * ($exam->quantity ?? 1) * ($exam->unit_quantity ?? 1); diff --git a/app/Traits/Helper.php b/app/Traits/Helper.php index 1477230..6445fd8 100644 --- a/app/Traits/Helper.php +++ b/app/Traits/Helper.php @@ -438,7 +438,7 @@ trait Helper ]))); } - $invoices = NhInsurancesInvoice::with(['insurance'])->where('state', InsuranceInvoiceState::UNPAID) + $invoices = NhInsurancesInvoice::whereHas('insurance')->with(['insurance'])->where('state', InsuranceInvoiceState::UNPAID) ->whereDate('payment_deadline', Carbon::now()->subDay())->get(); foreach ($invoices as $invoice) { diff --git a/database/migrations/2022_04_11_145002_add_billed_quantity_to_nh_medical_prescriptions.php b/database/migrations/2022_04_11_145002_add_billed_quantity_to_nh_medical_prescriptions.php new file mode 100644 index 0000000..5360f8c --- /dev/null +++ b/database/migrations/2022_04_11_145002_add_billed_quantity_to_nh_medical_prescriptions.php @@ -0,0 +1,32 @@ +tinyInteger('billed_quantity')->nullable()->after('quantity'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('nh_medical_prescriptions', function (Blueprint $table) { + $table->dropColumn('billed_quantity'); + }); + } +} diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index bf83a2f..eab4492 100755 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -68,4 +68,5 @@ return [ 'act_quantity_required' => "The quantity is required for the act :act_name", 'act_authorization_request_required' => "An authorization is required to apply the act :act_name", 'act_unit_quantity_required' => "The unit quantity is required for the act :act_name", + 'prescription_ordered_quantity_must_not_greater_than_ordered_quantity' => "Prescription quantity :id must not be greater than :quantity" ]; diff --git a/resources/lang/fr/errors.php b/resources/lang/fr/errors.php index cf30507..607c407 100755 --- a/resources/lang/fr/errors.php +++ b/resources/lang/fr/errors.php @@ -68,4 +68,5 @@ return [ 'act_quantity_required' => "La quantité est requise pour l'acte :act_name", 'act_authorization_request_required' => "Une autorisation est requise pour appliquer l'acte :act_name", 'act_unit_quantity_required' => "La quantité de l'unité est requise pour l'acte :act_name", + 'prescription_ordered_quantity_must_not_greater_than_ordered_quantity' => "La quantité de la prescription :id ne doit pas être supérieure à :quantity" ];