validate($request, [ 'network_id' => 'required|integer', 'name' => 'required|string' ]); $drugs = NhDrugsAndDevice::where('network_id', $request->input('network_id')) ->where('name', 'like', '%' . $request->input('name') . '%')->get(); return $this->successResponse($drugs); } /** * @OA\Post( * path="/drugs-and-devices", * summary="Ajouter les medicaments / appareillages", * tags={"Médicaments / Appareillages"}, * security={{"api_key":{}}}, * @OA\RequestBody( * description="Corps de la requete", * required=true, * @OA\MediaType( * mediaType="application/json", * @OA\Schema( * @OA\Property( * property="network_id", * description = "ID du reseau", * type="integer", * example= 250 * ), * @OA\Property( * property="code", * description = "Code du médicament / appareillage", * type="string", * example= "ABD" * ), * @OA\Property( * property="name", * description = "Nom du médicament / appareillage", * type="string", * example= "Nivaquine" * ), * @OA\Property( * property="type", * description = "Type de médicament / appareillage", * type="string", * enum={"COMPRESSED","SYRUP","SOLUTION","SUPPOSITORY","DEVICE"}, * example= "COMPRESSED" * ), * @OA\Property( * property="on_prescription", * description = "Sous ordornance ou pas", * type="bool", * example= "false" * ) * ), * ), * ), * @OA\Response( * response=200, * description="OK", * @OA\JsonContent( * ref="#/components/schemas/ApiResponse", * example = { * "status" : 200, * "response" : "Médicament / Appareillage enregistré", * "error":null * } * ) * ) * ) */ public function storeDrugsAndDevices(Request $request) { $this->validate($request, [ 'network_id' => 'required|integer|exists:networks,id', 'name' => 'required|string', 'code' => 'required|string', 'type' => 'required|string|in:COMPRESSED,SYRUP,SOLUTION,SUPPOSITORY,DEVICE', 'on_prescription' => 'required|boolean' ]); $drug = NhDrugsAndDevice::where('network_id', $request->input('network_id')) ->where('code', $request->input('code'))->first(); if (isset($drug)) { return $this->errorResponse(trans('errors.drug_device_already_exists')); } NhDrugsAndDevice::create($request->all()); return $this->successResponse(trans('messages.drug_device_saved')); } public function storeHealthCareSheet(Request $request) { $this->validate($request, [ 'insured_id' => 'required|string', 'network_agent_id' => 'required|integer|exists:networks_agents,id', 'password' => 'required|string', 'beneficiary_id' => 'nullable|int|exists:nh_having_rights,id', 'practitioner_lastname' => 'required|string', 'practitioner_firstname' => 'nullable|string', 'practitioner_provider_class_id' => 'required|integer', 'care_condition' => 'required|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' => 'required_with:pregnancy_start_at|date_format:Y-m-d|after:pregnancy_start_at', 'performances' => 'nullable|array', 'performances.*.act_code' => 'required|string', 'performances.*.amount' => 'required|numeric', 'performances.*.home_visit_fees' => 'nullable|numeric', 'prescriptions' => 'nullable|array', 'prescriptions.*.drug_or_device_id' => 'required|integer|exists:nh_drugs_and_devices,id', 'prescriptions.*.dosage' => 'required|string', 'prescriptions.*.quantity' => 'required|integer', 'prescriptions.*.unit_price' => 'required|numeric' ]); $insurance = NhInsurance::where('insured_id', $request->input('insured_id'))->where('state', InsuranceState::PAID)->first(); if (!isset($insurance)) { return $this->errorResponse(trans('errors.not_insured')); } $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')); if ($request->has('beneficiary_id')) { $beneficiary = $insurance->beneficiaries()->where('nh_having_rights.id', $request->input('beneficiary_id'))->first(); if (!isset($beneficiary)) { return $this->errorResponse("Ce béneficiaire n'existe pas"); } } $nhConfig = NhNetworksConfig::where('network_id', $insurance->network_id)->first(); if (!isset($nhConfig)) { return $this->errorResponse(trans('errors.nano_health_not_activated')); } $insuredPart = 0; $insurerPart = 0; switch ($request->input('care_condition')) { case 'CURRENT_AFFECTION': $insurerPart = $nhConfig->current_affection_percentage_insurer / 100; $insuredPart = $nhConfig->current_affection_percentage_insured / 100; break; case 'LONG_TERM_AFFECTION': $insurerPart = $nhConfig->long_term_affection_percentage_insurer / 100; $insuredPart = $nhConfig->long_term_affection_percentage_insured / 100; break; case 'EXONERATION': $insurerPart = $nhConfig->exoneration_percentage_insurer / 100; $insuredPart = $nhConfig->exoneration_percentage_insured / 100; break; } try { DB::beginTransaction(); $datetime = $this->getCurrentTimeByCountryCode($insurance->network->country->code_country); $healthCareSheet = NhHealthCareSheet::create(array_merge($request->all(), [ 'health_care_sheet_id' => $this->generateSheetID(), 'insurance_id' => $insurance->id, 'patient_lastname' => isset($beneficiary) ? $beneficiary->lastname : $insurance->user->lastname, 'patient_firstname' => isset($beneficiary) ? $beneficiary->firstname : $insurance->user->firstname, 'patient_situation' => isset($beneficiary) ? 'HAVING_RIGHT' : 'INSURED', 'state' => InsuranceSubscriptionState::UNDER_VALIDATION ])); foreach ($request->input('performances', []) as $p) { $act = NhAct::where('code', $p['act_code'])->first(); $performance = NhPerformance::create([ 'act_id' => $act->id, 'amount' => $p['amount'], 'home_visit_fees' => $p['home_visit_fees'] ?? null, 'moderator_ticket' => $insuredPart * $p['amount'], // to calculate, 'insurance_amount' => $insurerPart * $p['amount'], // to calculate, montant de l'assurance 'created_at' => $datetime, 'updated_at' => $datetime, ]); NhHealthCareSheetsPerformance::create([ 'sheet_id' => $healthCareSheet->id, 'performance_id' => $performance->id, 'created_at' => $datetime, 'updated_at' => $datetime, ]); } foreach ($request->input('prescriptions', []) as $p) { $amount = $p['unit_price'] * $p['quantity']; $prescription = NhMedicalPrescription::create(array_merge($p, [ 'insured_paid_amount' => $insuredPart * $amount, // to calculate, 'insurer_paid_amount' => $insurerPart * $amount, // to calculate, montant de l'assurance 'created_at' => $datetime, 'updated_at' => $datetime, ])); NhHealthCareSheetsPrescription::create([ 'sheet_id' => $healthCareSheet->id, 'prescription_id' => $prescription->id, 'created_at' => $datetime, 'updated_at' => $datetime, ]); } NhHealthCareSheetsHistory::create([ 'action' => 'ADD', 'health_care_sheet_id' => $healthCareSheet->health_care_sheet_id, 'state' => $healthCareSheet->state, 'created_at' => $datetime, 'updated_at' => $datetime, ]); DB::commit(); return $this->successResponse(trans('messages.successful_transaction')); } 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 { $code = generateTransactionCode(); $codeCorrect = NhHealthCareSheet::where('health_care_sheet_id', $code)->count() < 0; } while ($codeCorrect); return $code; } }