From 104e5d4f7817300b6a816c27cc6b73e1aa66288a Mon Sep 17 00:00:00 2001 From: Djery-Tom Date: Tue, 21 Dec 2021 14:50:05 +0100 Subject: [PATCH] Add enpoint to update health care sheet --- .../Controllers/HealthCareSheetController.php | 364 ++++++++++++++++++ resources/lang/en/errors.php | 7 +- resources/lang/en/messages.php | 1 + resources/lang/fr/errors.php | 13 +- resources/lang/fr/messages.php | 1 + routes/web.php | 1 + 6 files changed, 382 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/HealthCareSheetController.php b/app/Http/Controllers/HealthCareSheetController.php index 2995f8f..b1d8a9a 100755 --- a/app/Http/Controllers/HealthCareSheetController.php +++ b/app/Http/Controllers/HealthCareSheetController.php @@ -1212,6 +1212,370 @@ class HealthCareSheetController extends Controller return $result; } + /** + * @OA\Put( + * path="/health-care-sheets/{id}", + * summary="Modifier une feuille de soins", + * tags={"Feuilles de soins"}, + * security={{"api_key":{}}}, + * @OA\RequestBody( + * description="Corps de la requete", + * required=true, + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema( + * schema="update_health_care_sheet", + * title = "Modfiier une feuille de soins", + * required={"insured_id", "network_agent_id", "password", "practitioner_lastname", "practitioner_provider_class_id"}, + * @OA\Property( + * property="network_agent_id", + * description = "ID de l'agent qui saisit la feuille de soin dans le reseau", + * type="integer", + * example= 4325 + * ), + * @OA\Property( + * property="password", + * description = "Mot de passe de l'agent", + * type="string", + * example= "password" + * ), + * @OA\Property( + * property="beneficiary_id", + * description = "ID du beneficiaire , s'il s'agit d'une feuille de soins pour beneficiaire", + * type="integer", + * example= 4 + * ), + * @OA\Property( + * property="practitioner_lastname", + * description = "Nom du pratricien", + * type="string", + * example= "Dr DIETCHI" + * ), + * @OA\Property( + * property="practitioner_firstname", + * description = "Prenom du pratricien", + * type="string", + * example= "Djery" + * ), + * @OA\Property( + * property="practitioner_provider_class_id", + * description = "ID de la classe de prestataire du praticien", + * type="integer", + * example= 12 + * ), + * @OA\Property( + * property="care_condition", + * description = "Condition de prise en charge", + * type="string", + * enum={"CURRENT_AFFECTION","LONG_TERM_AFFECTION","EXONERATION"}, + * example= "CURRENT_AFFECTION" + * ), + * @OA\Property( + * property="accident_date", + * description = "Date de l'accident en cas d'accident", + * type="string", + * example= "2021-10-01" + * ), + * @OA\Property( + * property="pregnancy_start_at", + * description = "Date de début de la grossesse (si grossesse)", + * type="string", + * example= "2021-01-01" + * ), + * @OA\Property( + * property="pregnancy_end_at", + * description = "Date de fin de la grossese (si grossesse)", + * type="string", + * example= "2021-09-01" + * ), + * @OA\Property(property="performances", + * type="array", + * description="Listes des prestations", + * @OA\Items(ref="#/components/schemas/update_performance") + * ), + * @OA\Property(property="prescriptions", + * type="array", + * description="Listes des prescriptions medicales", + * @OA\Items(ref="#/components/schemas/update_prescription") + * ), + * @OA\Property(property="exams", + * type="array", + * description="Listes des examens", + * @OA\Items(ref="#/components/schemas/update_exam") + * ) + * ), + * ), + * ), + * @OA\Response( + * response=200, + * description="OK", + * @OA\JsonContent( + * ref="#/components/schemas/ApiResponse", + * example = { + * "status" : 200, + * "response" : "Consultation ou prescription effectuée", + * "error":null + * } + * ) + * ) + * ) + */ + public function updateHealthCareSheet(Request $request, $id) + { + /** + * @OA\Schema( + * schema="update_performance", + * title = "Modifier une prestation", + * required={"id"}, + * @OA\Property(property="id", + * type="integer", + * example = 1, + * description="ID de la prestation" + * ), + * @OA\Property(property="act_id", + * type="integer", + * example = 2, + * description="ID de l'acte " + * ), + * @OA\Property(property="amount", + * type="number", + * example=30000, + * description="Montant de la prestation" + * ), + * @OA\Property(property="home_visit_fees", + * type="number", + * example=5000, + * description="Frais de deplacement pour visiste à domicile " + * ) + * ) + * + * @OA\Schema( + * schema="update_prescription", + * title = "Modifier une prescription medicale", + * required={"id"}, + * @OA\Property(property="id", + * type="integer", + * example = 1, + * description="ID de la prescription medicale" + * ), + * @OA\Property(property="drug_or_device_id", + * type="integer", + * example = 2, + * description="ID du medicament ou appareillage" + * ), + * @OA\Property(property="dosage", + * type="string", + * example="3 fois / jour pendant 1e semaine", + * description="Posologie ou frequence de consommation" + * ), + * @OA\Property(property="quantity", + * type="int", + * example=5, + * description="Quantité" + * ), + * @OA\Property(property="unit_price", + * type="number", + * example=3000, + * description="Prix unitaire" + * ) + * ) + * + * @OA\Schema( + * schema="update_exam", + * title = "Modifier un examen", + * required={"id"}, + * @OA\Property(property="id", + * type="integer", + * example = 1, + * description="ID de l'examen" + * ), + * @OA\Property(property="act_id", + * type="integer", + * example = 2, + * description="ID de l'acte" + * ), + * @OA\Property(property="description", + * type="string", + * example="Une descrition de l'examen", + * description="Description de l'examene" + * ), + * @OA\Property(property="quantity", + * type="int", + * example=5, + * description="Quantité" + * ), + * @OA\Property(property="unit_price", + * type="number", + * example=5000, + * description="Prix unitaire" + * ) + * ) + */ + $this->validate($request, [ + 'network_agent_id' => 'required|integer|exists:networks_agents,id', + 'password' => 'required|string', + 'beneficiary_id' => 'nullable|int|exists:nh_having_rights,id', + 'practitioner_lastname' => 'nullable|string', + 'practitioner_firstname' => 'nullable|string', + 'practitioner_provider_class_id' => 'nullable|integer', + 'care_condition' => 'nullable|in:CURRENT_AFFECTION,LONG_TERM_AFFECTION,EXONERATION', + 'accident_date' => 'nullable|date_format:Y-m-d|before:today', + '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.*.home_visit_fees' => 'nullable|numeric', + '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.*.unit_price' => 'nullable|numeric', + '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.*.unit_price' => 'nullable|numeric', + ]); + + $sheet = NhHealthCareSheet::where('id', $id)->where('state', InsuranceSubscriptionState::UNDER_VALIDATION)->first(); + if (!isset($sheet)) { + return $this->errorResponse(__('errors.sheet_cannot_be_modified')); + } + + if ($sheet->network_agent_id != $request->input('network_agent_id')) { + return $this->errorResponse(__('errors.unauthorized_to_update_sheet'), 403); + } + + $agent = AgentPlus::where('network_agent_id', $request->input('network_agent_id'))->first(); + if (!checkPassword($request->input('password'), $agent->encrypted_password, $agent->salt)) + return $this->errorResponse(trans('messages.incorrect_user_password')); + + + $beneficiary_id = $request->input('beneficiary_id'); + if (isset($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')); + } + $sheet->beneficiary_id = $beneficiary_id; + } + + $nhConfig = NhNetworksConfig::where('network_id', $sheet->insurance->network_id)->first(); + if (!isset($nhConfig)) { + return $this->errorResponse(trans('errors.nano_health_not_activated')); + } + + $parts = $this->getConfigInsuranceParts($nhConfig, $request->input('care_condition', $sheet->care_condition)); + + $performances = $request->input('performances', []); + $prescriptions = $request->input('prescriptions', []); + $exams = $request->input('exams', []); + + $performancesIds = array_map(function ($r) { + return $r['id']; + }, $performances); + $prescriptionsIds = array_map(function ($r) { + return $r['id']; + }, $prescriptions); + $examsIds = array_map(function ($r) { + return $r['id']; + }, $exams); + + try { + DB::beginTransaction(); + + $datetime = $this->getCurrentTimeByCountryCode($sheet->insurance->network->country->code_country); + $sheet->updated_at = $datetime; + $sheet->practitioner_lastname = $request->input('practitioner_lastname', $sheet->practitioner_lastname); + $sheet->practitioner_firstname = $request->input('practitioner_firstname', $sheet->practitioner_firstname); + $sheet->practitioner_provider_class_id = $request->input('practitioner_provider_class_id', $sheet->practitioner_provider_class_id); + + if ($sheet->type == HealthCareSheetType::CONSULTATION) { + $sheet->care_condition = $request->input('care_condition', $sheet->care_condition); + $sheet->accident_date = $request->input('accident_date', $sheet->accident_date); + $sheet->pregnancy_start_at = $request->input('pregnancy_start_at', $sheet->pregnancy_start_at); + $sheet->pregnancy_end_at = $request->input('pregnancy_end_at', $sheet->pregnancy_end_at); + if (isset($beneficiary)) { + $sheet->patient_lastname = $beneficiary->lastname; + $sheet->patient_firstname = $beneficiary->firstname; + $sheet->patient_situation = 'HAVING_RIGHT'; + } + + 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])); + } + $performance = NhPerformance::findOrFail($p['id']); + $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'])) { + $performance->amount = $p['amount']; + $performance->moderator_ticket = $parts->insured_part * $p['amount']; // to calculate, + $performance->insurance_amount = $parts->insurer_part * $p['amount']; // to calculate, montant de l'assurance + } + $performance->updated_at = $datetime; + $performance->save(); + } + } + + 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])); + } + $prescription = NhMedicalPrescription::findOrFail($p['id']); + 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; + $prescription->quantity = !empty($p['quantity']) ? $p['quantity'] : $prescription->quantity; + } 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); + } + } + $prescription->updated_at = $datetime; + $prescription->save(); + } + + 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])); + } + $exam = NhExam::findOrFail($p['id']); + 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; + $exam->quantity = !empty($p['quantity']) ? $p['quantity'] : $exam->quantity; + } else { + if (!empty($p['unit_price'])) { + $exam->unit_price = $p['unit_price']; + $exam->insured_paid_amount = $parts->insured_part * $exam->unit_price * ($exam->quantity ?? 1); + $exam->insurer_paid_amount = $parts->insurer_part * $exam->unit_price * ($exam->quantity ?? 1); + } + } + $exam->updated_at = $datetime; + $exam->save(); + } + + $sheet->save(); + + DB::commit(); + return $this->successResponse(trans('messages.consultation_or_prescription_updated')); + } catch (Throwable $e) { + Log::error($e->getMessage() . '\n' . $e->getTraceAsString()); + DB::rollBack(); + return $this->errorResponse(trans('errors.unexpected_error'), 500); + } + } + public function generateSheetID(): string { do { diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index 14b4100..81b7536 100755 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -37,5 +37,10 @@ return [ "care_sheet_already_been_processed" => "This care sheet has already been processed", "unauthorized" => "You are not authorized to perform this operation", "prescription_already_invoiced" => "The prescription :id has already been invoiced", - "exam_already_invoiced" => "The exam:id has already been invoiced" + "exam_already_invoiced" => "The exam:id has already been invoiced", + "performance_not_belong_to_sheet" => "The service: id does not belong to the care sheet", + "exam_not_belong_to_sheet" => "The exam: id does not belong to the care sheet", + "prescription_not_belong_to_sheet" => "The prescription: id does not belong to the care sheet", + "sheet_cannot_be_modified" => "This care sheet cannot be modified", + "unauthorized_to_update_sheet" => "You are not authorized to modify this care sheet" ]; diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 75040ea..2028609 100755 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -113,4 +113,5 @@ A new consultation or prescription has been made with your insurance. "care_sheet_rejected" => "The care sheet has been rejected", "care_sheet_resubmitted" => "The care sheet has been resubmitted", 'execution_carried_out' => "Execution of prescription carried out", + 'consultation_or_prescription_updated' => "Consultation or prescription updated", ]; diff --git a/resources/lang/fr/errors.php b/resources/lang/fr/errors.php index 6ca9884..7e424dd 100755 --- a/resources/lang/fr/errors.php +++ b/resources/lang/fr/errors.php @@ -18,7 +18,7 @@ return [ 'transaction_not_exist' => 'Cette transaction n\'existe pas', 'withdrawal_already_made' => 'Retrait déjà éffectué', 'incorrect_withdrawal_amount' => 'Montant de retrait incorrect', - 'operation_cannot_performed_in_country' => 'Cette operation ne peut pas etre effectuée dans ce pays', + 'operation_cannot_performed_in_country' => 'Cette operation ne peut pas être effectuée dans ce pays', 'user_identification_required' => 'L\'identification de l\'utilisateur est requise pour continuer l\'operation', 'validation_user_identification_required' => 'La validation de l\'identification de l\'utilisateur est requise pour continuer l\'operation', 'incorrect_net_amount' => 'Le montant net est inférieur à zéro', @@ -27,8 +27,8 @@ return [ 'incorrect_selected_amount' => 'Le montant choisi est incorrect', 'minimal_age_required' => "Votre âge est supérieur à l'âge limite requis pour souscrire à cette assurance", 'cannot_subscribe_again' => "Vous ne pouvez plus souscrire à cette assurance. Vous avez déjà une demande :state", - "subscription_cannot_be_updated" => "Cette demande de souscription ne peut etre modifiée", - "subscription_cannot_be_paid" => "Cette demande de souscription ne peut etre payée", + "subscription_cannot_be_updated" => "Cette demande de souscription ne peut être modifiée", + "subscription_cannot_be_paid" => "Cette demande de souscription ne peut être payée", 'subscription_be_already_paid' => "Cette souscription a déjà été payée", "subscription_cannot_be_submitted" => "Votre demande précédente :state. Vous ne pouvez pas en soumettre une autre pour l'instant", "not_insured" => "Vous n'êtes pas assuré", @@ -37,5 +37,10 @@ return [ "care_sheet_already_been_processed" => "Cette feuille de soins a deja été traitée", "unauthorized" => "Vous n'etes pas autorisé à effectuer cette operation", "prescription_already_invoiced" => "La prescription :id a deja été facturée", - "exam_already_invoiced" => "L'examen :id a deja été facturé" + "exam_already_invoiced" => "L'examen :id a deja été facturé", + "performance_not_belong_to_sheet" => "La prestation :id n'appartient pas à la feuille de soins", + "exam_not_belong_to_sheet" => "L'examen :id n'appartient pas à la feuille de soins", + "prescription_not_belong_to_sheet" => "La prescription :id n'appartient pas à la feuille de soins", + "sheet_cannot_be_modified" => "Cette feuille de soins ne peut être modifiée", + "unauthorized_to_update_sheet" => "Vous n'êtes pas autorisé à modifier cette feuille de soins" ]; diff --git a/resources/lang/fr/messages.php b/resources/lang/fr/messages.php index 0ca8b3f..5cb86bb 100755 --- a/resources/lang/fr/messages.php +++ b/resources/lang/fr/messages.php @@ -129,4 +129,5 @@ Une nouvelle execution de prescription vient d'etre effectuée sur votre assuran Connectez-vous à l'application pour avoir plus de details et valider cette opération. ", + 'consultation_or_prescription_updated' => "Consultation ou prescription mis à jour", ]; diff --git a/routes/web.php b/routes/web.php index d260485..1f5ccfc 100644 --- a/routes/web.php +++ b/routes/web.php @@ -48,6 +48,7 @@ $router->group(['prefix' => '', 'middleware' => 'auth'], function () use ($route $router->post('health-care-sheets/consultation', 'HealthCareSheetController@storeHealthCareSheetConsultation'); $router->post('health-care-sheets/execution', 'HealthCareSheetController@storeHealthCareSheetExecution'); $router->get('health-care-sheets/{id}', 'HealthCareSheetController@getOneHealthCareSheets'); + $router->put('health-care-sheets/{id}', 'HealthCareSheetController@updateHealthCareSheet'); //QRCode for agents $router->get('qrcode/generate/{id_user}', 'QRCodeController@generate');