Improve method to update health care sheet

This commit is contained in:
Djery-Tom 2021-12-27 12:02:56 +01:00
parent 41c4e33947
commit 8a5e1d9863
1 changed files with 132 additions and 36 deletions

View File

@ -565,7 +565,7 @@ class HealthCareSheetController extends Controller
return $this->errorResponse(trans('messages.incorrect_user_password')); return $this->errorResponse(trans('messages.incorrect_user_password'));
$beneficiary_id = $request->input('beneficiary_id'); $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(); $beneficiary = $insurance->beneficiaries()->where('nh_having_rights.id', $beneficiary_id)->first();
if (!isset($beneficiary)) { if (!isset($beneficiary)) {
return $this->errorResponse(trans('errors.beneficiary_not_found')); return $this->errorResponse(trans('errors.beneficiary_not_found'));
@ -933,9 +933,20 @@ class HealthCareSheetController extends Controller
* default = 349 * default = 349
* ), * ),
* in="query", * 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", * parameter="type",
* name="type", * name="type",
* description="Type des feuilles de soins", * description="Type des feuilles de soins",
@ -982,14 +993,24 @@ class HealthCareSheetController extends Controller
public function getHealthCareSheets(Request $request) public function getHealthCareSheets(Request $request)
{ {
$this->validate($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', 'type' => 'nullable|in:CONSULTATION,EXECUTION',
'state' => 'nullable|in:UNTREATED,TREATED,ACCEPTED,TO_BILL' 'state' => 'nullable|in:UNTREATED,TREATED,ACCEPTED,TO_BILL'
]); ]);
$type = $request->input('type'); $type = $request->input('type');
$state = $request->input('state'); $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') { if (!empty($state) && $state == 'TO_BILL') {
// Liste des feuilles de soins a afficher pour l'execution // Liste des feuilles de soins a afficher pour l'execution
@ -1306,7 +1327,7 @@ class HealthCareSheetController extends Controller
* ref="#/components/schemas/ApiResponse", * ref="#/components/schemas/ApiResponse",
* example = { * example = {
* "status" : 200, * "status" : 200,
* "response" : "Consultation ou prescription effectuée", * "response" : "Consultation ou prescription mis à jour",
* "error":null * "error":null
* } * }
* ) * )
@ -1319,7 +1340,6 @@ class HealthCareSheetController extends Controller
* @OA\Schema( * @OA\Schema(
* schema="update_performance", * schema="update_performance",
* title = "Modifier une prestation", * title = "Modifier une prestation",
* required={"id"},
* @OA\Property(property="id", * @OA\Property(property="id",
* type="integer", * type="integer",
* example = 1, * example = 1,
@ -1339,13 +1359,17 @@ class HealthCareSheetController extends Controller
* type="number", * type="number",
* example=5000, * example=5000,
* description="Frais de deplacement pour visiste à domicile " * description="Frais de deplacement pour visiste à domicile "
* ),
* @OA\Property(property="to_delete",
* type="boolean",
* example=true,
* description="A supprimer"
* ) * )
* ) * )
* *
* @OA\Schema( * @OA\Schema(
* schema="update_prescription", * schema="update_prescription",
* title = "Modifier une prescription medicale", * title = "Modifier une prescription medicale",
* required={"id"},
* @OA\Property(property="id", * @OA\Property(property="id",
* type="integer", * type="integer",
* example = 1, * example = 1,
@ -1370,13 +1394,17 @@ class HealthCareSheetController extends Controller
* type="number", * type="number",
* example=3000, * example=3000,
* description="Prix unitaire" * description="Prix unitaire"
* ),
* @OA\Property(property="to_delete",
* type="boolean",
* example=true,
* description="A supprimer"
* ) * )
* ) * )
* *
* @OA\Schema( * @OA\Schema(
* schema="update_exam", * schema="update_exam",
* title = "Modifier un examen", * title = "Modifier un examen",
* required={"id"},
* @OA\Property(property="id", * @OA\Property(property="id",
* type="integer", * type="integer",
* example = 1, * example = 1,
@ -1401,6 +1429,11 @@ class HealthCareSheetController extends Controller
* type="number", * type="number",
* example=5000, * example=5000,
* description="Prix unitaire" * 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_start_at' => 'nullable|date_format:Y-m-d|before:today',
'pregnancy_end_at' => 'nullable|date_format:Y-m-d|after:pregnancy_start_at', 'pregnancy_end_at' => 'nullable|date_format:Y-m-d|after:pregnancy_start_at',
'performances' => 'nullable|array', 'performances' => 'nullable|array',
'performances.*.id' => 'required|integer|exists:nh_performances,id', 'performances.*.id' => 'nullable|integer|exists:nh_performances,id',
'performances.*.act_id' => 'nullable|integer|exists:nh_acts,id', 'performances.*.act_id' => 'required_with:performances.*.id|integer|exists:nh_acts,id',
'performances.*.amount' => 'nullable|numeric', 'performances.*.amount' => 'required_with:performances.*.id|numeric',
'performances.*.home_visit_fees' => 'nullable|numeric', 'performances.*.home_visit_fees' => 'nullable|numeric',
'performances.*.to_delete' => 'nullable|boolean',
'prescriptions' => 'nullable|array', 'prescriptions' => 'nullable|array',
'prescriptions.*.id' => 'required|integer|exists:nh_medical_prescriptions,id', 'prescriptions.*.id' => 'nullable|integer|exists:nh_medical_prescriptions,id',
'prescriptions.*.drug_or_device_id' => 'nullable|integer|exists:nh_drugs_and_devices,id', 'prescriptions.*.drug_or_device_id' => 'required_with:prescriptions.*.id|integer|exists:nh_drugs_and_devices,id',
'prescriptions.*.dosage' => 'nullable|string', 'prescriptions.*.dosage' => 'required_with:prescriptions.*.id|string',
'prescriptions.*.quantity' => 'nullable|integer', 'prescriptions.*.quantity' => 'required_with:prescriptions.*.id|integer',
'prescriptions.*.unit_price' => 'nullable|numeric', 'prescriptions.*.unit_price' => 'nullable|numeric',
'prescriptions.*.to_delete' => 'nullable|boolean',
'exams' => 'nullable|array', 'exams' => 'nullable|array',
'exams.*.id' => 'required|integer|exists:nh_exams,id', 'exams.*.id' => 'nullable|integer|exists:nh_exams,id',
'exams.*.act_id' => 'nullable|integer|exists:nh_acts,id', 'exams.*.act_id' => 'required_with:exams.*.id|integer|exists:nh_acts,id',
'exams.*.description' => 'nullable|string', 'exams.*.description' => 'required_with:exams.*.id|string',
'exams.*.quantity' => 'nullable|integer', 'exams.*.quantity' => 'required_with:exams.*.id|integer',
'exams.*.unit_price' => 'nullable|numeric', 'exams.*.unit_price' => 'nullable|numeric',
'exams.*.to_delete' => 'nullable|boolean',
]); ]);
$sheet = NhHealthCareSheet::where('id', $id)->where('state', InsuranceSubscriptionState::UNDER_VALIDATION)->first(); $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'); $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(); $beneficiary = $sheet->insurance->beneficiaries()->where('nh_having_rights.id', $beneficiary_id)->first();
if (!isset($beneficiary)) { if (!isset($beneficiary)) {
return $this->errorResponse(trans('errors.beneficiary_not_found')); return $this->errorResponse(trans('errors.beneficiary_not_found'));
@ -1469,13 +1505,13 @@ class HealthCareSheetController extends Controller
$exams = $request->input('exams', []); $exams = $request->input('exams', []);
$performancesIds = array_map(function ($r) { $performancesIds = array_map(function ($r) {
return $r['id']; return $r['id'] ?? null;
}, $performances); }, $performances);
$prescriptionsIds = array_map(function ($r) { $prescriptionsIds = array_map(function ($r) {
return $r['id']; return $r['id'] ?? null;
}, $prescriptions); }, $prescriptions);
$examsIds = array_map(function ($r) { $examsIds = array_map(function ($r) {
return $r['id']; return $r['id'] ?? null;
}, $exams); }, $exams);
try { try {
@ -1499,11 +1535,23 @@ class HealthCareSheetController extends Controller
} }
foreach ($request->input('performances', []) as $p) { foreach ($request->input('performances', []) as $p) {
$shPerformance = NhHealthCareSheetsPerformance::where('sheet_id', $sheet->id)->where('performance_id', $p['id'])->first(); if (!empty($p['to_delete'])) {
if (!isset($shPerformance)) { NhPerformance::find($p['id'])->delete();
return $this->errorResponse(__('errors.performance_not_belong_to_sheet', ['id' => array_search($p['id'], $performancesIds) + 1])); 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->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; $performance->home_visit_fees = !empty($p['home_visit_fees']) ? $p['home_visit_fees'] : $performance->home_visit_fees;
if (!empty($p['amount'])) { if (!empty($p['amount'])) {
@ -1513,15 +1561,35 @@ class HealthCareSheetController extends Controller
} }
$performance->updated_at = $datetime; $performance->updated_at = $datetime;
$performance->save(); $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) { foreach ($request->input('prescriptions', []) as $p) {
$shPrescription = NhHealthCareSheetsPrescription::where('sheet_id', $sheet->id)->where('prescription_id', $p['id'])->first(); if (!empty($p['to_delete'])) {
if (!isset($shPrescription)) { NhMedicalPrescription::find($p['id'])->delete();
return $this->errorResponse(__('errors.prescription_not_belong_to_sheet', ['id' => array_search($p['id'], $prescriptionsIds) + 1])); 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) { 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->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->dosage = !empty($p['dosage']) ? $p['dosage'] : $prescription->dosage;
@ -1535,14 +1603,34 @@ class HealthCareSheetController extends Controller
} }
$prescription->updated_at = $datetime; $prescription->updated_at = $datetime;
$prescription->save(); $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) { foreach ($request->input('exams', []) as $p) {
$shExam = NhHealthCareSheetsExam::where('sheet_id', $sheet->id)->where('exam_id', $p['id'])->first(); if (!empty($p['to_delete'])) {
if (!isset($shExam)) { NhExam::find($p['id'])->delete();
return $this->errorResponse(__('errors.prescription_not_belong_to_sheet', ['id' => array_search($p['id'], $examsIds) + 1])); 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) { if ($sheet->type == HealthCareSheetType::CONSULTATION) {
$exam->act_id = !empty($p['act_id']) ? $p['act_id'] : $exam->act_id; $exam->act_id = !empty($p['act_id']) ? $p['act_id'] : $exam->act_id;
$exam->description = !empty($p['description']) ? $p['description'] : $exam->description; $exam->description = !empty($p['description']) ? $p['description'] : $exam->description;
@ -1556,6 +1644,14 @@ class HealthCareSheetController extends Controller
} }
$exam->updated_at = $datetime; $exam->updated_at = $datetime;
$exam->save(); $exam->save();
if (empty($p['id'])) {
NhHealthCareSheetsExam::create([
'sheet_id' => $sheet->id,
'exam_id' => $exam->id,
'created_at' => $datetime, 'updated_at' => $datetime,
]);
}
} }
$sheet->save(); $sheet->save();