From 8a5e1d98637c4dfa8253ddaab6e27274fc3dc4e6 Mon Sep 17 00:00:00 2001 From: Djery-Tom Date: Mon, 27 Dec 2021 12:02:56 +0100 Subject: [PATCH] Improve method to update health care sheet --- .../Controllers/HealthCareSheetController.php | 168 ++++++++++++++---- 1 file changed, 132 insertions(+), 36 deletions(-) diff --git a/app/Http/Controllers/HealthCareSheetController.php b/app/Http/Controllers/HealthCareSheetController.php index ed3f707..f61b7e9 100755 --- a/app/Http/Controllers/HealthCareSheetController.php +++ b/app/Http/Controllers/HealthCareSheetController.php @@ -565,7 +565,7 @@ class HealthCareSheetController extends Controller return $this->errorResponse(trans('messages.incorrect_user_password')); $beneficiary_id = $request->input('beneficiary_id'); - if (isset($beneficiary_id)) { + if (!empty($beneficiary_id)) { $beneficiary = $insurance->beneficiaries()->where('nh_having_rights.id', $beneficiary_id)->first(); if (!isset($beneficiary)) { return $this->errorResponse(trans('errors.beneficiary_not_found')); @@ -933,9 +933,20 @@ class HealthCareSheetController extends Controller * default = 349 * ), * in="query", - * required=true + * required=false * ), - * * @OA\Parameter( + * @OA\Parameter( + * parameter="network_agent_id", + * name="network_agent_id", + * description="ID de l'agent dans le reseau", + * @OA\Schema( + * type="integer", + * default = 43510 + * ), + * in="query", + * required=false + * ), + * @OA\Parameter( * parameter="type", * name="type", * description="Type des feuilles de soins", @@ -982,14 +993,24 @@ class HealthCareSheetController extends Controller public function getHealthCareSheets(Request $request) { $this->validate($request, [ - 'user_id' => 'required|integer|exists:users,id', + 'user_id' => 'required_without:network_agent_id|integer|exists:users,id', + 'network_agent_id' => 'required_without:user_id|integer|exists:networks_agents,id', 'type' => 'nullable|in:CONSULTATION,EXECUTION', 'state' => 'nullable|in:UNTREATED,TREATED,ACCEPTED,TO_BILL' ]); $type = $request->input('type'); $state = $request->input('state'); - $query = NhInfosHealthCareSheets::with(['performances.act:id,code,name', 'exams.act:id,code,name', 'prescriptions.drug_or_device:id,name'])->where('user_id', $request->input('user_id')); + $user_id = $request->input('user_id'); + $network_agent_id = $request->input('network_agent_id'); + + if (!empty($user_id)) { + $query = NhInfosHealthCareSheets::where('user_id', $user_id); + } else { + $query = NhInfosHealthCareSheets::where('network_agent_id', $network_agent_id); + } + + $query = $query->with(['performances.act:id,code,name', 'exams.act:id,code,name', 'prescriptions.drug_or_device:id,name']); if (!empty($state) && $state == 'TO_BILL') { // Liste des feuilles de soins a afficher pour l'execution @@ -1306,7 +1327,7 @@ class HealthCareSheetController extends Controller * ref="#/components/schemas/ApiResponse", * example = { * "status" : 200, - * "response" : "Consultation ou prescription effectuée", + * "response" : "Consultation ou prescription mis à jour", * "error":null * } * ) @@ -1319,7 +1340,6 @@ class HealthCareSheetController extends Controller * @OA\Schema( * schema="update_performance", * title = "Modifier une prestation", - * required={"id"}, * @OA\Property(property="id", * type="integer", * example = 1, @@ -1339,13 +1359,17 @@ class HealthCareSheetController extends Controller * type="number", * example=5000, * description="Frais de deplacement pour visiste à domicile " + * ), + * @OA\Property(property="to_delete", + * type="boolean", + * example=true, + * description="A supprimer" * ) * ) * * @OA\Schema( * schema="update_prescription", * title = "Modifier une prescription medicale", - * required={"id"}, * @OA\Property(property="id", * type="integer", * example = 1, @@ -1370,13 +1394,17 @@ class HealthCareSheetController extends Controller * type="number", * example=3000, * description="Prix unitaire" + * ), + * @OA\Property(property="to_delete", + * type="boolean", + * example=true, + * description="A supprimer" * ) * ) * * @OA\Schema( * schema="update_exam", * title = "Modifier un examen", - * required={"id"}, * @OA\Property(property="id", * type="integer", * example = 1, @@ -1401,6 +1429,11 @@ class HealthCareSheetController extends Controller * type="number", * example=5000, * description="Prix unitaire" + * ), + * @OA\Property(property="to_delete", + * type="boolean", + * example=true, + * description="A supprimer" * ) * ) */ @@ -1416,22 +1449,25 @@ class HealthCareSheetController extends Controller 'pregnancy_start_at' => 'nullable|date_format:Y-m-d|before:today', 'pregnancy_end_at' => 'nullable|date_format:Y-m-d|after:pregnancy_start_at', 'performances' => 'nullable|array', - 'performances.*.id' => 'required|integer|exists:nh_performances,id', - 'performances.*.act_id' => 'nullable|integer|exists:nh_acts,id', - 'performances.*.amount' => 'nullable|numeric', + 'performances.*.id' => 'nullable|integer|exists:nh_performances,id', + 'performances.*.act_id' => 'required_with:performances.*.id|integer|exists:nh_acts,id', + 'performances.*.amount' => 'required_with:performances.*.id|numeric', 'performances.*.home_visit_fees' => 'nullable|numeric', + 'performances.*.to_delete' => 'nullable|boolean', 'prescriptions' => 'nullable|array', - 'prescriptions.*.id' => 'required|integer|exists:nh_medical_prescriptions,id', - 'prescriptions.*.drug_or_device_id' => 'nullable|integer|exists:nh_drugs_and_devices,id', - 'prescriptions.*.dosage' => 'nullable|string', - 'prescriptions.*.quantity' => 'nullable|integer', + '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.*.unit_price' => 'nullable|numeric', + 'prescriptions.*.to_delete' => 'nullable|boolean', 'exams' => 'nullable|array', - 'exams.*.id' => 'required|integer|exists:nh_exams,id', - 'exams.*.act_id' => 'nullable|integer|exists:nh_acts,id', - 'exams.*.description' => 'nullable|string', - 'exams.*.quantity' => 'nullable|integer', + 'exams.*.id' => 'nullable|integer|exists:nh_exams,id', + 'exams.*.act_id' => 'required_with:exams.*.id|integer|exists:nh_acts,id', + 'exams.*.description' => 'required_with:exams.*.id|string', + 'exams.*.quantity' => 'required_with:exams.*.id|integer', 'exams.*.unit_price' => 'nullable|numeric', + 'exams.*.to_delete' => 'nullable|boolean', ]); $sheet = NhHealthCareSheet::where('id', $id)->where('state', InsuranceSubscriptionState::UNDER_VALIDATION)->first(); @@ -1449,7 +1485,7 @@ class HealthCareSheetController extends Controller $beneficiary_id = $request->input('beneficiary_id'); - if (isset($beneficiary_id)) { + if (!empty($beneficiary_id)) { $beneficiary = $sheet->insurance->beneficiaries()->where('nh_having_rights.id', $beneficiary_id)->first(); if (!isset($beneficiary)) { return $this->errorResponse(trans('errors.beneficiary_not_found')); @@ -1469,13 +1505,13 @@ class HealthCareSheetController extends Controller $exams = $request->input('exams', []); $performancesIds = array_map(function ($r) { - return $r['id']; + return $r['id'] ?? null; }, $performances); $prescriptionsIds = array_map(function ($r) { - return $r['id']; + return $r['id'] ?? null; }, $prescriptions); $examsIds = array_map(function ($r) { - return $r['id']; + return $r['id'] ?? null; }, $exams); try { @@ -1499,11 +1535,23 @@ class HealthCareSheetController extends Controller } foreach ($request->input('performances', []) as $p) { - $shPerformance = NhHealthCareSheetsPerformance::where('sheet_id', $sheet->id)->where('performance_id', $p['id'])->first(); - if (!isset($shPerformance)) { - return $this->errorResponse(__('errors.performance_not_belong_to_sheet', ['id' => array_search($p['id'], $performancesIds) + 1])); + if (!empty($p['to_delete'])) { + NhPerformance::find($p['id'])->delete(); + NhHealthCareSheetsPerformance::where('sheet_id', $sheet->id)->where('performance_id', $p['id'])->delete(); + continue; } - $performance = NhPerformance::findOrFail($p['id']); + + if (!empty($p['id'])) { + $shPerformance = NhHealthCareSheetsPerformance::where('sheet_id', $sheet->id)->where('performance_id', $p['id'])->first(); + if (!isset($shPerformance)) { + return $this->errorResponse(__('errors.performance_not_belong_to_sheet', ['id' => array_search($p['id'], $performancesIds) + 1])); + } + $performance = NhPerformance::findOrFail($p['id']); + } else { + $performance = new NhPerformance(); + $performance->created_at = $datetime; + } + $performance->act_id = !empty($p['act_id']) ? $p['act_id'] : $performance->act_id; $performance->home_visit_fees = !empty($p['home_visit_fees']) ? $p['home_visit_fees'] : $performance->home_visit_fees; if (!empty($p['amount'])) { @@ -1513,15 +1561,35 @@ class HealthCareSheetController extends Controller } $performance->updated_at = $datetime; $performance->save(); + + if (empty($p['id'])) { + NhHealthCareSheetsPerformance::create([ + 'sheet_id' => $sheet->id, + 'performance_id' => $performance->id, + 'created_at' => $datetime, 'updated_at' => $datetime, + ]); + } } } foreach ($request->input('prescriptions', []) as $p) { - $shPrescription = NhHealthCareSheetsPrescription::where('sheet_id', $sheet->id)->where('prescription_id', $p['id'])->first(); - if (!isset($shPrescription)) { - return $this->errorResponse(__('errors.prescription_not_belong_to_sheet', ['id' => array_search($p['id'], $prescriptionsIds) + 1])); + if (!empty($p['to_delete'])) { + NhMedicalPrescription::find($p['id'])->delete(); + NhHealthCareSheetsPrescription::where('sheet_id', $sheet->id)->where('prescription_id', $p['id'])->delete(); + continue; } - $prescription = NhMedicalPrescription::findOrFail($p['id']); + + if (!empty($p['id'])) { + $shPrescription = NhHealthCareSheetsPrescription::where('sheet_id', $sheet->id)->where('prescription_id', $p['id'])->first(); + if (!isset($shPrescription)) { + return $this->errorResponse(__('errors.prescription_not_belong_to_sheet', ['id' => array_search($p['id'], $prescriptionsIds) + 1])); + } + $prescription = NhMedicalPrescription::findOrFail($p['id']); + } else { + $prescription = new NhMedicalPrescription(); + $prescription->created_at = $datetime; + } + if ($sheet->type == HealthCareSheetType::CONSULTATION) { $prescription->drug_or_device_id = !empty($p['drug_or_device_id']) ? $p['drug_or_device_id'] : $prescription->drug_or_device_id; $prescription->dosage = !empty($p['dosage']) ? $p['dosage'] : $prescription->dosage; @@ -1535,14 +1603,34 @@ class HealthCareSheetController extends Controller } $prescription->updated_at = $datetime; $prescription->save(); + + if (empty($p['id'])) { + NhHealthCareSheetsPrescription::create([ + 'sheet_id' => $sheet->id, + 'prescription_id' => $prescription->id, + 'created_at' => $datetime, 'updated_at' => $datetime, + ]); + } } foreach ($request->input('exams', []) as $p) { - $shExam = NhHealthCareSheetsExam::where('sheet_id', $sheet->id)->where('exam_id', $p['id'])->first(); - if (!isset($shExam)) { - return $this->errorResponse(__('errors.prescription_not_belong_to_sheet', ['id' => array_search($p['id'], $examsIds) + 1])); + if (!empty($p['to_delete'])) { + NhExam::find($p['id'])->delete(); + NhHealthCareSheetsExam::where('sheet_id', $sheet->id)->where('exam_id', $p['id'])->delete(); + continue; } - $exam = NhExam::findOrFail($p['id']); + + if (!empty($p['id'])) { + $shExam = NhHealthCareSheetsExam::where('sheet_id', $sheet->id)->where('exam_id', $p['id'])->first(); + if (!isset($shExam)) { + return $this->errorResponse(__('errors.prescription_not_belong_to_sheet', ['id' => array_search($p['id'], $examsIds) + 1])); + } + $exam = NhExam::findOrFail($p['id']); + } else { + $exam = new NhExam(); + $exam->created_at = $datetime; + } + if ($sheet->type == HealthCareSheetType::CONSULTATION) { $exam->act_id = !empty($p['act_id']) ? $p['act_id'] : $exam->act_id; $exam->description = !empty($p['description']) ? $p['description'] : $exam->description; @@ -1556,6 +1644,14 @@ class HealthCareSheetController extends Controller } $exam->updated_at = $datetime; $exam->save(); + + if (empty($p['id'])) { + NhHealthCareSheetsExam::create([ + 'sheet_id' => $sheet->id, + 'exam_id' => $exam->id, + 'created_at' => $datetime, 'updated_at' => $datetime, + ]); + } } $sheet->save();