startDate; $endDate = $request->endDate; $livechat = Livechat::with('doctor.user', 'doctor.speciality', 'appointment.appointmentDetail', 'healthCare', 'summary'); // ->where('nIDAppointment', '!=', null) // ->where('nIDAppointment', '!=', ''); if ($startDate) { $livechat = $livechat->where('dCreateOn', '>=', $startDate); } if ($endDate) { $endDate = date('Y-m-d', strtotime($endDate . ' +1 day')); $livechat = $livechat->where('dCreateOn', '<', $endDate); } $livechat = $livechat->whereHas('summary', function ($query) { $query->whereNotNull('nIDLiveChat'); }); $livechat = $livechat->latest()->paginate(15); return response()->json(Helper::paginateResources(LivechatResource::collection($livechat))); } /** * Show the form for creating a new resource. * @return Renderable */ public function create() { return view('internal::create'); } /** * Store a newly created resource in storage. * @param Request $request * @return Renderable */ 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(); $userDokter = User::where('nID', $livechat->nIDDokter)->first(); $userDetailDokter = UserDetail::where('nIDUser', $userDokter->nID)->first(); $dokter = $userDetailDokter->sTitlePrefix . ' ' . $userDokter->sFirstName . ' ' . $userDokter->sLastName . ' ' . $userDetailDokter->sTitleSuffix; $kodeResep = 'LMS' . date('ymd') . rand(1,100); $diagnosis = explode(",",$request->diagnosis); if(isset($request->diagnosis) && is_array($diagnosis) && count($diagnosis) > 0) { foreach($diagnosis as $data){ $icd = Icd::where('code', $data)->first(); array_push($diagnosis, $icd->name); }; } $sDiagnosis = implode(", ",$diagnosis); $hospitalData = Organization::where('id', $request->hospital)->first(); $hospital = ''; if ($hospitalData) { $hospital = $hospitalData->code; } $data = [ 'nIDLivechat' => $request->id, 'nIDLivechatSummary' => $livechatSummary->nID, 'nIDDokter' => $livechat->nIDDokter, 'sDokterName' => $dokter, 'dTanggalResep' => date('Y-m-d H:i:s'), 'sSource' => 'lms', 'nIDUser' => $livechat->nIDUser, 'sRegID' => '', 'sKodeResep' => $kodeResep, 'sDiagnose' => $sDiagnosis, 'sKodeRS' => $hospital, ]; $prescription = Prescription::updateOrCreate([ 'nIDLivechat' => $request->id ],$data); $medicine = $request->medicine; $customMessages = [ 'required' => 'Kolom :attribute wajib diisi.', 'numeric' => 'Kolom :attribute harus berupa angka.', ]; $validator = Validator::make($request->all(), [ 'medicine' => 'required|array', 'medicine.*' => 'required', ], $customMessages); if ($validator->fails()) { 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 = ''; if ($drugData){ $drug = $drugData->name; } $unitData = Unit::where('id', $value['unit_id'])->first(); $unit = ''; if ($unitData) { $unit = $unitData->name; } $data = [ 'nIDPrescription' => $prescription->id, 'sItemName' => $drug, 'nQty' => $value['qty'], 'sSatuan' => $unit, '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(); return Helper::responseJson(status: 'failed', statusCode: 500, message: $th->getMessage()); } } DB::commit(); return Helper::responseJson(status: 'success', statusCode: 201, message: 'success', data: $request->toArray()); } return Helper::responseJson(status: 'success', statusCode: 200, message: 'Resep Online berhasil ajukan!', data: $prescription); } /** * Show the specified resource. * @param int $id * @return Renderable */ public function show($id) { } /** * Show the form for editing the specified resource. * @param int $id * @return Renderable */ public function edit($id) { return view('internal::edit'); } /** * Update the specified resource in storage. * @param Request $request * @param int $id * @return Renderable */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * @param int $id * @return Renderable */ public function destroy($id) { // } }