toArray(); $prescription = Prescription::query() ->with(['livechat', 'user', 'items']) ->join('tx_prescription_orders', 'tx_prescriptions.nID', '=', 'tx_prescription_orders.nIDPrescription') ->select( 'tx_prescriptions.*', 'tx_prescription_orders.sStatus as status_prescription', 'tx_prescription_orders.sAddress as kirim_ke', 'tx_prescription_orders.sDeliveryPrice as ongkir', 'tx_prescription_orders.sDeliveryMethod as delivery', 'tx_prescription_orders.sPaymentStatus', 'tx_prescription_orders.nIDApotek', ) ->whereNotNull('nIDUser'); if ($request->has('search')) { $search = $request->search; $prescription->where(function ($query) use ($search) { $query->where('sDokterName', 'LIKE', '%' . $search . "%") ->orWhere('sKodeResep', 'LIKE', '%' . $search . "%") ->orWhere('tx_prescription_orders.sPenerima', 'LIKE', '%' . $search . "%"); }); } if (($request->has('prescription_start') || $request->has('prescription_end')) && !empty($request->prescription_start) && !empty($request->prescription_end) ) { $prescription = $prescription->where(function($q) use ($request) { $q->where('dTanggalResep', '>=', $request->prescription_start) ->where('dTanggalResep', '<=', $request->prescription_end); }); } $prescriptions = $prescription->orderBy('dUpdateOn', 'DESC') ->paginate(); return Helper::responseJson(Helper::paginateResources(ReportPrescriptionResource::collection($prescriptions))); } /** * 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) { // } /** * Show the specified resource. * @param int $id * @return Renderable */ public function show($id) { return view('internal::show'); } /** * 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) { // } // Function to determine if a string is serialized private function is_serialized($string) { return ($string == 'b:0;' || @unserialize($string) !== false); } // Function to determine if a string is JSON private function is_json($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } // Function to safely process the plan private function processPlan($sPlan) { if ($this->is_serialized($sPlan)) { $unserializedPlan = @unserialize($sPlan); if ($unserializedPlan !== false || $sPlan === 'b:0;') { return $unserializedPlan; } } elseif ($this->is_json($sPlan)) { $jsonPlan = json_decode($sPlan, true); if (json_last_error() == JSON_ERROR_NONE) { return $jsonPlan; } } return $sPlan; // Treat as plain text if not serialized or JSON } public function generateExcel(Request $request) { Helper::setCustomPHPIniSettings(); $file_name = 'Data Report Resep Online'; // Membuat penulis entitas Spout $writer = WriterEntityFactory::createXLSXWriter(); // Membuka penulis untuk menulis ke file $writer->openToFile(public_path('files/Report-Resep-Online.xlsx')); $headerArray = [ 'No', 'Prescription Code', 'Date Consultation', 'Patient', 'Doctor', 'Status', 'Nama Apotek', 'Harga Ongkir', 'Jenis Obat (Drugs)', 'Jumlah Obat (QTY)', 'Cara Minum Obat', ]; // Sheet 1 $writer->getCurrentSheet()->setName('Data'); $headerRow = WriterEntityFactory::createRowFromArray($headerArray); $writer->addRow($headerRow); // Query prescription data $prescriptionQuery = Prescription::query() ->with(['livechat', 'user', 'items']) ->join('tx_prescription_orders', 'tx_prescriptions.nID', '=', 'tx_prescription_orders.nIDPrescription') ->select( 'tx_prescriptions.*', 'tx_prescription_orders.sStatus as status_prescription', 'tx_prescription_orders.sAddress as kirim_ke', 'tx_prescription_orders.sDeliveryPrice as ongkir', 'tx_prescription_orders.sDeliveryMethod as delivery', 'tx_prescription_orders.nIDApotek', ) ->whereNotNull('nIDUser'); if ($request->has('search')) { $search = $request->search; $prescriptionQuery->where(function ($query) use ($search) { $query->where('sDokterName', 'LIKE', '%' . $search . '%') ->orWhere('sKodeResep', 'LIKE', '%' . $search . '%'); }); } if ($request->has('prescription_start') && $request->has('prescription_end') && !empty($request->prescription_start) && !empty($request->prescription_end)) { $prescriptionQuery->whereBetween('dTanggalResep', [$request->prescription_start, $request->prescription_end]); } $prescriptions = $prescriptionQuery->get(); if ($prescriptions->isNotEmpty()) { $no = 1; foreach ($prescriptions as $index => $row) { $patientName = $row->user ? $row->user->sFirstName .' '. $row->user->sLastName : '-'; if ($row->items->isNotEmpty()) { $rowData = [ $no++, $row->sKodeResep ?? '-', $row->dTanggalResep ? Carbon::parse($row->dTanggalResep)->format('Y-m-d') : '-', $row->user->fullName ?? '-', $row->sDokterName ?? '-', $row->status_prescription ?? '-', $row->nIDApotek ? Helper::getOrganization($row->nIDApotek) : '-', $row->ongkir ?? '-', $row->sDeliveryPrice ?? '-', ]; // Create a row from the array and add it to the writer $rowEntity = WriterEntityFactory::createRowFromArray($rowData); $writer->addRow($rowEntity); foreach ($row->items as $item) { $rowSubData = [ '', '', '', '', '', '', '', '', $item->sItemName ?? '-', $item->nQty ?? '-', $item->sSigna ?? '-' ]; $subData = WriterEntityFactory::createRowFromArray($rowSubData); $writer->addRow($subData); } } else { $rowData = [ $no++, $row->sKodeResep ?? '-', $row->dTanggalResep ? Carbon::parse($row->dTanggalResep)->format('Y-m-d') : '-', $row->user->name ?? '-', $row->sDokterName ?? '-', ]; // Create a row from the array and add it to the writer $rowEntity = WriterEntityFactory::createRowFromArray($rowData); $writer->addRow($rowEntity); } } } $writer->close(); return Helper::responseJson([ 'file_name' => "Data Resep Online " . date('Y-m-d h:i:s'), 'file_url' => url('files/Report-Resep-Online.xlsx') ]); } }