From 9168af67b6b663a9f92e78a49c97393142fe1db9 Mon Sep 17 00:00:00 2001 From: Tb Fajri Date: Tue, 30 Apr 2024 17:03:44 +0700 Subject: [PATCH] fixing add prescription --- .../Api/PrescriptionController.php | 33 +++- .../Transformers/LivechatResource.php | 11 ++ .../Controllers/Api/LivechatController.php | 2 + app/Models/OLDLMS/Livechat.php | 4 + app/Models/OLDLMS/Prescription.php | 2 + app/Models/OLDLMS/PrescriptionItem.php | 1 + app/Models/Prescription.php | 18 ++ app/Models/PrescriptionItem.php | 19 +++ ...0705_add_column_to_table_practitioners.php | 3 +- ...add_column_to_table_practitioner_roles.php | 3 +- ...04_30_132417_create_prescription_table.php | 34 ++++ ...132747_create_prescription_items_table.php | 36 ++++ ...add_column_to_prescription_items_table.php | 32 ++++ frontend/dashboard/src/@types/doctor.tsx | 2 +- .../src/pages/EPrescription/Livechat/View.tsx | 156 +++++++++++------- 15 files changed, 294 insertions(+), 62 deletions(-) create mode 100644 app/Models/Prescription.php create mode 100644 app/Models/PrescriptionItem.php create mode 100644 database/migrations/2024_04_30_132417_create_prescription_table.php create mode 100644 database/migrations/2024_04_30_132747_create_prescription_items_table.php create mode 100644 database/migrations/2024_04_30_150511_add_column_to_prescription_items_table.php diff --git a/Modules/Internal/Http/Controllers/Api/PrescriptionController.php b/Modules/Internal/Http/Controllers/Api/PrescriptionController.php index 4d36f639..e8b52477 100644 --- a/Modules/Internal/Http/Controllers/Api/PrescriptionController.php +++ b/Modules/Internal/Http/Controllers/Api/PrescriptionController.php @@ -10,6 +10,9 @@ use App\Models\OLDLMS\User; use App\Models\OLDLMS\UserDetail; use App\Models\OLDLMS\Prescription; use App\Models\OLDLMS\PrescriptionItem; + +use App\Models\Prescription as PrescriptionAso; +use App\Models\PrescriptionItem as PrescriptionItemAso; use App\Models\Icd; use App\Models\Organization; use App\Models\Drug; @@ -79,6 +82,17 @@ class PrescriptionController extends Controller */ public function store(Request $request) { + // Insert atau Update ke table prescription di ASO + $data = [ + 'livechat_id' => $request->id, + 'organization_id' => $request->hospital, + 'icd_code' => $request->diagnosis, + ]; + $prescriptionAso = PrescriptionAso::updateOrCreate([ + 'livechat_id' => $request->id + ], $data); + + // Insert ke table tx_prescription di Linksehat $livechat = Livechat::where('nID', $request->id)->first(); $livechatSummary = LivechatSummary::where('nIDLivechat', $request->id)->first(); @@ -117,7 +131,9 @@ class PrescriptionController extends Controller 'sKodeRS' => $hospital, ]; - $prescription = Prescription::create($data); + $prescription = Prescription::updateOrCreate([ + 'nIDLivechat' => $request->id + ],$data); $medicine = $request->medicine; $customMessages = [ @@ -134,7 +150,10 @@ class PrescriptionController extends Controller return Helper::responseJson([$request->all()],'error', 400, $validator->errors()); } else { // BeginTransaction + // delete item DB::beginTransaction(); + PrescriptionItemAso::where('prescription_id', $prescriptionAso->id)->delete(); + PrescriptionItem::where('nIDPrescription', $prescriptionAso->id)->delete(); foreach($medicine as $key => $value){ $drugData = Drug::where('id', $value['drug_id'])->first(); $drug = ''; @@ -154,8 +173,20 @@ class PrescriptionController extends Controller 'sSigna' => $value['signa'], 'sNote' => $value['note'], ]; + + $dataAso = [ + 'prescription_id' => $prescriptionAso->id, + 'drug_id' => $value['drug_id'], + 'qty' => $value['qty'], + 'unit_id' => $value['unit_id'], + 'signa' => $value['signa'], + 'note' => $value['note'] + ]; // Insert Data try { + // Insert to ASO + PrescriptionItemAso::create($dataAso); + // Insert to Linksehat PrescriptionItem::create($data); } catch (\Throwable $th) { DB::rollBack(); diff --git a/Modules/Internal/Transformers/LivechatResource.php b/Modules/Internal/Transformers/LivechatResource.php index 1dd3954e..3c2585d9 100644 --- a/Modules/Internal/Transformers/LivechatResource.php +++ b/Modules/Internal/Transformers/LivechatResource.php @@ -5,6 +5,8 @@ namespace Modules\Internal\Transformers; use Carbon\Carbon; use Illuminate\Http\Resources\Json\JsonResource; use App\Helpers\Helper; +use App\Models\Prescription; +use App\Models\PrescriptionItem; class LivechatResource extends JsonResource { @@ -16,6 +18,12 @@ class LivechatResource extends JsonResource */ public function toArray($request) { + $prescription = Prescription::where('livechat_id', $this->nID)->first(); + $diagnosis = $prescription ? $prescription->icd_code : ''; + $hospital = $prescription ? $prescription->organization_id : ''; + + $prescriptionItem = $prescription ? PrescriptionItem::where('prescription_id', $prescription->id)->get() : []; + $livechat = [ 'id' => $this->nID, 'doctor_name' => isset($this->doctor->user->sFirstName) ? $this->doctor->user->detail->sTitlePrefix . '. ' . $this->doctor->user->sFirstName . ' ' . $this->doctor->user->sLastName . ' ' . $this->doctor->user->detail->sTitleSuffix : null, @@ -36,6 +44,9 @@ class LivechatResource extends JsonResource 'appointment_media' => $this->appointment->sMedia ?? null, 'status_chat' => $this->status_name ?? null, 'payment_method' => $this->appointment->payment_method ?? null, + 'diagnosis' => $diagnosis, + 'hospital' => $hospital, + 'medicine' => $prescriptionItem ]; $start_time = $this->dStartTime; diff --git a/Modules/Linksehat/Http/Controllers/Api/LivechatController.php b/Modules/Linksehat/Http/Controllers/Api/LivechatController.php index e6b255e3..7671e376 100644 --- a/Modules/Linksehat/Http/Controllers/Api/LivechatController.php +++ b/Modules/Linksehat/Http/Controllers/Api/LivechatController.php @@ -15,6 +15,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Http; use Modules\Linksehat\Transformers\Livechat\LivechatResource; use Illuminate\Support\Facades\Validator; +use App\Http\Controllers\DuitkuController; use DB; use Str; @@ -191,6 +192,7 @@ class LivechatController extends Controller ]; return Helper::responseJson(data: $data); } + public function consultation_payment_choose($id){ $livechat = Livechat::where('id', $id)->with(['doctor', 'practitioner'])->first(); $practitionerRole = PractitionerRole::where('id',$livechat->practitioner->id)->first(); diff --git a/app/Models/OLDLMS/Livechat.php b/app/Models/OLDLMS/Livechat.php index 5c0ae646..19b35fa7 100644 --- a/app/Models/OLDLMS/Livechat.php +++ b/app/Models/OLDLMS/Livechat.php @@ -70,4 +70,8 @@ class Livechat extends Model { return $this->belongsTo(Healthcare::class, 'nIDHealthCare', 'nID'); } + + public function summary(){ + return $this->belongsTo(LivechatSummary::class, 'nID', 'nIDLivechat'); + } } diff --git a/app/Models/OLDLMS/Prescription.php b/app/Models/OLDLMS/Prescription.php index bdb36ef6..5652aa73 100644 --- a/app/Models/OLDLMS/Prescription.php +++ b/app/Models/OLDLMS/Prescription.php @@ -41,6 +41,8 @@ class Prescription extends Model protected $table = 'tx_prescriptions'; + protected $primaryKey = 'nID'; + // protected $appends = [ // 'status_name', // ]; diff --git a/app/Models/OLDLMS/PrescriptionItem.php b/app/Models/OLDLMS/PrescriptionItem.php index 7654f78e..4851e29c 100644 --- a/app/Models/OLDLMS/PrescriptionItem.php +++ b/app/Models/OLDLMS/PrescriptionItem.php @@ -48,4 +48,5 @@ class PrescriptionItem extends Model 'dTanggalResep' => 'datetime', ]; + protected $primaryKey = 'nID'; } diff --git a/app/Models/Prescription.php b/app/Models/Prescription.php new file mode 100644 index 00000000..27c48ffe --- /dev/null +++ b/app/Models/Prescription.php @@ -0,0 +1,18 @@ +dropColumn('str_number'); + $table->dropColumn('exp_date_str'); }); } }; diff --git a/database/migrations/2024_04_23_162213_add_column_to_table_practitioner_roles.php b/database/migrations/2024_04_23_162213_add_column_to_table_practitioner_roles.php index 3c4daa2f..d80eb1b4 100644 --- a/database/migrations/2024_04_23_162213_add_column_to_table_practitioner_roles.php +++ b/database/migrations/2024_04_23_162213_add_column_to_table_practitioner_roles.php @@ -27,7 +27,8 @@ return new class extends Migration public function down() { Schema::table('practitioner_roles', function (Blueprint $table) { - // + $table->dropColumn('sip_number'); + $table->dropColumn('exp_date_sip'); }); } }; diff --git a/database/migrations/2024_04_30_132417_create_prescription_table.php b/database/migrations/2024_04_30_132417_create_prescription_table.php new file mode 100644 index 00000000..69b52c24 --- /dev/null +++ b/database/migrations/2024_04_30_132417_create_prescription_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignId('livechat_id'); + $table->foreignId('organization_id'); + $table->string('icd_code'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('prescriptions'); + } +}; diff --git a/database/migrations/2024_04_30_132747_create_prescription_items_table.php b/database/migrations/2024_04_30_132747_create_prescription_items_table.php new file mode 100644 index 00000000..57585342 --- /dev/null +++ b/database/migrations/2024_04_30_132747_create_prescription_items_table.php @@ -0,0 +1,36 @@ +id(); + $table->foreignId('prescription_id'); + $table->foreignId('drug_id'); + $table->integer('qty'); + $table->foreignId('unit_id'); + $table->string('note'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('prescription_items'); + } +}; diff --git a/database/migrations/2024_04_30_150511_add_column_to_prescription_items_table.php b/database/migrations/2024_04_30_150511_add_column_to_prescription_items_table.php new file mode 100644 index 00000000..ec8ec019 --- /dev/null +++ b/database/migrations/2024_04_30_150511_add_column_to_prescription_items_table.php @@ -0,0 +1,32 @@ +string('signa'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('prescription_items', function (Blueprint $table) { + $table->dropColumn('signa'); + }); + } +}; diff --git a/frontend/dashboard/src/@types/doctor.tsx b/frontend/dashboard/src/@types/doctor.tsx index 034365ee..d672f44e 100644 --- a/frontend/dashboard/src/@types/doctor.tsx +++ b/frontend/dashboard/src/@types/doctor.tsx @@ -83,7 +83,7 @@ export type Appointment = { doctor_id:number; organizations:Organizations[]; specialities:Specialities[]; - diagonis:string; + diagnosis:string; hospital:string; medicine: Medicine[]; } diff --git a/frontend/dashboard/src/pages/EPrescription/Livechat/View.tsx b/frontend/dashboard/src/pages/EPrescription/Livechat/View.tsx index 8b4467fa..b3f3e4fa 100644 --- a/frontend/dashboard/src/pages/EPrescription/Livechat/View.tsx +++ b/frontend/dashboard/src/pages/EPrescription/Livechat/View.tsx @@ -124,15 +124,16 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) { organizations: currentAppointment?.organizations || [], specialities: currentAppointment?.specialities || [], diagnosis: currentAppointment?.diagnosis || '', - hospital: currentAppointment?.hospital || '', - medicine : [{ - id: 0, - drug_id: 0, - qty: 0, - signa: '', - unit_id: 0, - note: '', // input to database - }], + hospital: currentAppointment?.hospital || null, + medicine : currentAppointment?.medicine || [ + { + drug_id: 0, + qty: 0, + signa: '', + unit_id: 0, + note: '', // input to database + } + ], }), // eslint-disable-next-line react-hooks/exhaustive-deps [currentAppointment] @@ -150,14 +151,17 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) { const [icdOptions, setIcdOptions] = useState([ { value: '-', label: '-' } ]); + const codes = defaultValues.diagnosis.split(','); const [selectedIcdOptions, setSelectedIcdOptions] = useState([]); useEffect(() => { const selectedCodes = icdOptions.filter((icd) => { - // Logika pemilihan sesuai kebutuhan + return codes.includes(icd.value); }); setSelectedIcdOptions(selectedCodes); - }, [icdOptions]); + setValue('diagnosis', selectedCodes); + }, [icdOptions, defaultValues]); + useEffect(() => { // Ambil data dari API dan atur opsi ICD @@ -189,10 +193,14 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) { // Set default value saat hospitalOptions berubah useEffect(() => { - if (hospitalOptions.length > 0 && !selectedHospitalOption) { - setSelectedHospitalOption({value: '-', label: '-'}); - } - }, [hospitalOptions, selectedHospitalOption]); + const selectedId = hospitalOptions.find((hospital) => { + + return hospital.value == defaultValues.hospital + }); + setSelectedHospitalOption(selectedId); + setValue('hospital', defaultValues.hospital) + + }, [hospitalOptions, defaultValues]); // Autocomplite drugs const [drugOptions, setDrugsOptions] = useState([]); @@ -240,6 +248,26 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) { }); }, []); // useEffect dijalankan hanya sekali saat komponen dimount + useEffect(() => { + if (defaultValues.medicine.length > 0) { + defaultValues.medicine.map((med, index) => { + const selectedDrugId = drugOptions.find((drug) => { + return drug.value == med.drug_id + }); + handleAutocompleteChange(selectedDrugId,index) + + const selectedUnitId = unitOptions.find((unit) => { + return unit.value == med.unit_id + }); + handleAutocompleteChangeUnit(selectedUnitId,index) + + // Contoh: Lakukan tindakan lainnya sesuai kebutuhan Anda + }); + } else { + console.log('Medicine is empty'); + } + }, [defaultValues.medicine]); + const { reset, @@ -265,40 +293,40 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) { // eslint-disable-next-line react-hooks/exhaustive-deps }, [isEdit, currentAppointment]); - const onSubmit = async (data: FormValuesProps) => { - try { - const formData = new FormData(); - formData.append('id', data.id); - formData.append('diagnosis', data.diagnosis); - formData.append('hospital', data.hospital); - - // Iterasi melalui setiap objek dalam array medicine dan menambahkannya ke FormData - data.medicine.forEach((medicineObj, index) => { - // Anda dapat menambahkan setiap properti dari objek medicine ke FormData - formData.append(`medicine[${index}][drug_id]`, medicineObj.drug_id); - formData.append(`medicine[${index}][qty]`, medicineObj.qty); - formData.append(`medicine[${index}][unit_id]`, medicineObj.unit_id); - formData.append(`medicine[${index}][signa]`, medicineObj.signa); - formData.append(`medicine[${index}][note]`, medicineObj.note); - }); + const onSubmit = async (data: FormValuesProps) => { + try { + const formData = new FormData(); + formData.append('id', data.id); + formData.append('diagnosis', data.diagnosis); + formData.append('hospital', data.hospital); + + // Iterasi melalui setiap objek dalam array medicine dan menambahkannya ke FormData + data.medicine.forEach((medicineObj, index) => { + // Anda dapat menambahkan setiap properti dari objek medicine ke FormData + formData.append(`medicine[${index}][drug_id]`, medicineObj.drug_id); + formData.append(`medicine[${index}][qty]`, medicineObj.qty); + formData.append(`medicine[${index}][unit_id]`, medicineObj.unit_id); + formData.append(`medicine[${index}][signa]`, medicineObj.signa); + formData.append(`medicine[${index}][note]`, medicineObj.note); + }); - const response = await axios.post('/prescription', formData); - reset(); - enqueueSnackbar('Berhasil menambahkan resep', { - variant: 'success', - }); - navigate('/e-prescription/live-chat'); - } catch (error: any) { - console.log(error, 'submit') - enqueueSnackbar(error.message ?? 'Failed Processing Request', { variant: 'error' }); - } + const response = await axios.post('/prescription', formData); + reset(); + enqueueSnackbar('Berhasil menambahkan resep', { + variant: 'success', + }); + navigate('/e-prescription/live-chat'); + } catch (error: any) { + console.log(error, 'submit') + enqueueSnackbar(error.message ?? 'Failed Processing Request', { variant: 'error' }); + } - const ascent = document?.querySelector('ascent'); - if (ascent != null) { - ascent.innerHTML = ''; - } - }; + const ascent = document?.querySelector('ascent'); + if (ascent != null) { + ascent.innerHTML = ''; + } + }; return ( @@ -508,9 +536,6 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) { Obat - @@ -582,14 +607,29 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) { fullWidth /> - - {index !== fields.length - 1 && ( - - remove(index)}> - - - - )} + { + index === 0 ? ( + + append({medicine_name: '', medicine_price: 0, request_log_id: 1 })}> + + + + ) : ( + index == (fields.length-1) ? ( + + remove(index)}> + + + + ) : ( + + append({medicine_name: '', medicine_price: 0, request_log_id: 1 })}> + + + + ) + ) + } ))} @@ -601,7 +641,7 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) { size="large" loading={isSubmitting} > - {!isEdit ? 'Save' : 'Save'} + {!isEdit ? 'Save' : 'Update'}