Improve update of health care sheet and add duplicata validation while store health care sheet

This commit is contained in:
Djery-Tom 2022-04-13 07:46:28 +01:00
parent 5b3923557a
commit c12ef5ea18
5 changed files with 96 additions and 9 deletions

View File

@ -50,3 +50,10 @@ if (!function_exists('generateTransactionCode')) {
return $randomString;
}
}
if (!function_exists('array_has_dupes')) {
function array_has_dupes(array $array)
{
return count($array) !== count(array_unique($array));
}
}

View File

@ -624,6 +624,9 @@ class HealthCareSheetController extends Controller
]);
$performances = $request->input('performances', []);
$prescriptions = $request->input('prescriptions', []);
$exams = $request->input('exams', []);
$insurance = NhInsurance::where('insured_id', $request->input('insured_id'))->whereIn('state', [InsuranceState::PAID, InsuranceState::PARTIALLY_PAID])->first();
if (!isset($insurance)) {
return $this->errorResponse(trans('errors.not_insured'));
@ -646,6 +649,28 @@ class HealthCareSheetController extends Controller
return $this->errorResponse(trans('errors.nano_health_not_activated'));
}
//Verifier les duplicata
$performancesActsIds = array_map(function ($r) {
return $r['act_id'];
}, $performances);
if (array_has_dupes($performancesActsIds)) {
return $this->errorResponse(trans('errors.sheet_performances_duplicata'));
}
$examsActsIds = array_map(function ($r) {
return $r['act_id'];
}, $exams);
if (array_has_dupes($examsActsIds)) {
return $this->errorResponse(trans('errors.sheet_exams_duplicata'));
}
$prescriptionsDrugsIds = array_map(function ($r) {
return $r['drug_or_device_id'];
}, $prescriptions);
if (array_has_dupes($prescriptionsDrugsIds)) {
return $this->errorResponse(trans('errors.sheet_prescriptions_duplicata'));
}
// Fetch performances amounts
foreach ($performances as $i => $p) {
$act = NhAct::find($p['act_id']);
@ -1751,7 +1776,7 @@ class HealthCareSheetController extends Controller
'prescriptions.*.id' => 'nullable|integer|exists:nh_medical_prescriptions,id',
'prescriptions.*.drug_or_device_id' => 'required_with:prescriptions.*.id|integer|exists:nh_drugs_and_devices,id',
'prescriptions.*.dosage' => 'required_with:prescriptions.*.id|string',
'prescriptions.*.quantity' => 'required_with:prescriptions.*.id|integer',
'prescriptions.*.quantity' => 'required_with:prescriptions.*.id|integer|min:1',
'prescriptions.*.unit_price' => 'nullable|numeric',
'prescriptions.*.to_delete' => 'nullable|boolean',
'exams' => 'nullable|array',
@ -1789,13 +1814,36 @@ class HealthCareSheetController extends Controller
return $this->errorResponse(trans('errors.nano_health_not_activated'));
}
$parts = $this->getConfigInsuranceParts($nhConfig, $request->input('care_condition', $sheet->care_condition));
$currency_code = $this->getNetworkCurrency($sheet->insurance->network_id);
$performances = $request->input('performances', []);
$prescriptions = $request->input('prescriptions', []);
$exams = $request->input('exams', []);
//Verifier les duplicata
$performancesActsIds = array_merge($sheet->performances->pluck('act_id')->toArray(), array_map(function ($r) {
return $r['act_id'] ?? null;
}, $performances));
if (array_has_dupes($performancesActsIds)) {
return $this->errorResponse(trans('errors.sheet_performances_duplicata'));
}
$examsActsIds = array_map(function ($r) {
return $r['act_id'] ?? null;
}, $exams);
if (array_has_dupes($examsActsIds)) {
return $this->errorResponse(trans('errors.sheet_exams_duplicata'));
}
$prescriptionsDrugsIds = array_map(function ($r) {
return $r['drug_or_device_id'] ?? null;
}, $prescriptions);
if (array_has_dupes($prescriptionsDrugsIds)) {
return $this->errorResponse(trans('errors.sheet_prescriptions_duplicata'));
}
$parts = $this->getConfigInsuranceParts($nhConfig, $request->input('care_condition', $sheet->care_condition));
$currency_code = $this->getNetworkCurrency($sheet->insurance->network_id);
$performancesIds = array_map(function ($r) {
return $r['id'] ?? null;
}, $performances);
@ -1869,7 +1917,7 @@ class HealthCareSheetController extends Controller
}
}
foreach ($request->input('prescriptions', []) as $p) {
foreach ($request->input('prescriptions', []) as $i => $p) {
if (!empty($p['to_delete'])) {
NhMedicalPrescription::find($p['id'])->delete();
NhHealthCareSheetsPrescription::where('sheet_id', $sheet->id)->where('prescription_id', $p['id'])->delete();
@ -1894,9 +1942,29 @@ class HealthCareSheetController extends Controller
} else {
if (!empty($p['unit_price'])) {
$prescription->unit_price = $p['unit_price'];
$prescription->insured_paid_amount = $parts->insured_part * $prescription->unit_price * ($prescription->quantity ?? 1);
$prescription->insurer_paid_amount = $parts->insurer_part * $prescription->unit_price * ($prescription->quantity ?? 1);
}
if (!empty($p['quantity'])) {
$quantity_diff = $p['quantity'] - $prescription->quantity;
// Update billed quantity in the previous sheets
$previous_prescription = $sheet->prescriptionSheet->prescriptions()->where('drug_or_device_id', $prescription->drug_or_device_id)->first();
if (!isset($previous_prescription)) {
DB::rollback();
return $this->errorResponse(__('errors.consultation_prescription_not_found'));
}
$quantity_to_bill = $previous_prescription->quantity - ($previous_prescription->billed_quantity ?? 0);
if ($quantity_diff > $quantity_to_bill) {
DB::rollback();
return $this->errorResponse(__('errors.prescription_ordered_quantity_must_not_greater_than_ordered_quantity',
['id' => $i + 1, 'quantity' => $quantity_to_bill + $prescription->quantity]));
}
$previous_prescription->billed_quantity += $quantity_diff;
$previous_prescription->billed = ($previous_prescription->billed_quantity == $previous_prescription->quantity);
$previous_prescription->save();
$prescription->quantity = $p['quantity'];
}
$prescription->insured_paid_amount = $parts->insured_part * $prescription->unit_price * ($prescription->quantity ?? 1);
$prescription->insurer_paid_amount = $parts->insurer_part * $prescription->unit_price * ($prescription->quantity ?? 1);
}
$prescription->updated_at = $datetime;
$prescription->save();

View File

@ -112,4 +112,10 @@ class NhHealthCareSheet extends Model
return $this->hasManyThrough(NhMedicalPrescription::class, NhHealthCareSheetsPrescription::class,
'sheet_id', 'id', 'id', 'prescription_id');
}
public function prescriptionSheet()
{
return $this->belongsTo(NhHealthCareSheet::class, 'prescription_sheet_id');
}
}

View File

@ -68,5 +68,6 @@ 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"
'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"
];

View File

@ -68,5 +68,10 @@ 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"
'prescription_ordered_quantity_must_not_greater_than_ordered_quantity' => "La quantité de la prescription :id ne doit pas être supérieure à :quantity",
'consultation_prescription_not_found' => "La consultation rattaché à cette prescription n'existe pas",
'sheet_performances_duplicata' => "La feuille de soins a des duplicata de prestations",
'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"
];