startDate; $endDate = $request->endDate; $search = $request->search; $livechat = Livechat::with('doctor.user', 'doctor.speciality', 'appointment.appointmentDetail', 'healthCare', 'user'); // ->where('nIDAppointment', '!=', null) // ->where('nIDAppointment', '!=', ''); if ($search) { $livechat = $livechat->whereHas('user', function ($query) use ($search) { $query->where('sFirstName', 'like', '%' . $search . '%') ->orWhere('sLastName', 'like', '%' . $search . '%'); }); } 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->latest()->paginate(15); return response()->json(Helper::paginateResources(LivechatResource::collection($livechat))); } /** * Show the specified resource. * @param int $id * @return Renderable */ public function show($id) { $livechat = Livechat::with('doctor.user', 'doctor.speciality', 'appointment.appointmentDetail', 'healthCare') ->where('nIDAppointment', '!=', null)->where('nIDAppointment', '!=', '') ->where('nID', $id) ->first(); return response()->json(new LivechatResource($livechat)); } public function export(Request $request) { ini_set('memory_limit', '1G'); $startDate = $request->has('startDate') ? $request->input('startDate') : ''; $endDate = $request->has('endDate') ? $request->input('endDate') : ''; $type = $request->has('type') ? $request->input('type') : ''; if ($type == 1){ return $this->exportMonthly($startDate, $endDate); } else { $liveChats = Livechat::with('userInsurance', 'user:nID,sFirstName,sLastName,sEmail,sPhone,nIDUser,nIDHubunganKeluarga', 'user.detail:dTanggalLahir,nIDJenisKelamin', 'user.relation', 'doctor:nID,nIDSpesialis,nIDUser', 'doctor.user:nID,sFirstName,sLastName', 'doctor.user.detail', 'doctor.speciality', 'appointment:nID,sPaymentStatus,sPaymentMethod', 'appointment.appointmentDetail:nID,nIDAppointment,dTanggalAppointment,tTimeAppointment', 'healthCare:nID,sHealthCare', 'prescription', 'prescription.items', 'prescription.payment', 'rujukan', 'summary' ) ->whereHas('userInsurance', function (Builder $query) { // Kondisi pada relasi userInsurance $query->where('nIDInsurance', 107); // khusus inhealth }) ->where(function (Builder $query) use ($startDate, $endDate) { // $query->where('nIDAppointment', '!=', null); // $query->where('nIDAppointment', '!=', ''); if ($startDate) { $query->where('dCreateOn', '>=', $startDate); } if ($endDate) { $endDate = date('Y-m-d', strtotime($endDate . ' +1 day')); $query->where('dCreateOn', '<', $endDate); } }) ->orderBy('nID', 'desc') ->get(['nID', 'nIDUser', 'nIDDokter', 'nIDHealthCare', 'nIDAppointment', 'sStatus', 'sMediaDokter', 'sMedia', 'dCreateOn', 'sNoSpj', 'dRequestTime', 'dAcceptTime', 'dStartTime', 'dEndTime']); $headers = [ ['value' => 'No', 'cell' => 'A1', 'mergeCell' => true, 'mergeToCell' => 'A2'], ['value' => 'Kode TC', 'cell' => 'B1', 'mergeCell' => true, 'mergeToCell' => 'B2'], ['value' => 'Kode Livechat', 'cell' => 'C1', 'mergeCell' => true, 'mergeToCell' => 'C2'], ['value' => 'Tanggal', 'cell' => 'D1', 'mergeCell' => true, 'mergeToCell' => 'D2'], ['value' => 'Waktu', 'cell' => 'E1', 'mergeCell' => true, 'mergeToCell' => 'E2'], ['value' => 'Faskes', 'cell' => 'F1', 'mergeCell' => true, 'mergeToCell' => 'F2'], ['value' => 'Nama Dokter', 'cell' => 'G1', 'mergeCell' => true, 'mergeToCell' => 'G2'], ['value' => 'Spesialis', 'cell' => 'H1', 'mergeCell' => true, 'mergeToCell' => 'H2'], ['value' => 'Chat Via App/Website', 'cell' => 'I1', 'mergeCell' => true, 'mergeToCell' => 'J1'], ['value' => 'Pasien', 'cell' => 'I2', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Dokter', 'cell' => 'J2', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'nIDUser', 'cell' => 'K1', 'mergeCell' => true, 'mergeToCell' => 'K2'], ['value' => 'Nama Pasien', 'cell' => 'L1', 'mergeCell' => true, 'mergeToCell' => 'L2'], ['value' => 'No Telepon Pasien', 'cell' => 'M1', 'mergeCell' => true, 'mergeToCell' => 'M2'], ['value' => 'Email Pasien', 'cell' => 'N1', 'mergeCell' => true, 'mergeToCell' => 'N2'], ['value' => 'No Kartu Insurance', 'cell' => 'O1', 'mergeCell' => true, 'mergeToCell' => 'O2'], ['value' => 'Corporate ID', 'cell' => 'P1', 'mergeCell' => true, 'mergeToCell' => 'P2'], ['value' => 'Corporate Name', 'cell' => 'Q1', 'mergeCell' => true, 'mergeToCell' => 'Q2'], ['value' => 'Hubungan Pasien', 'cell' => 'R1', 'mergeCell' => true, 'mergeToCell' => 'R2'], ['value' => 'Chanel', 'cell' => 'S1', 'mergeCell' => true, 'mergeToCell' => 'S2'], ['value' => 'Status', 'cell' => 'T1', 'mergeCell' => true, 'mergeToCell' => 'T2'], ['value' => 'Request Time', 'cell' => 'U1', 'mergeCell' => true, 'mergeToCell' => 'U2'], ['value' => 'Accept Time', 'cell' => 'V1', 'mergeCell' => true, 'mergeToCell' => 'V2'], ['value' => 'Start Time', 'cell' => 'W1', 'mergeCell' => true, 'mergeToCell' => 'W2'], ['value' => 'End Time', 'cell' => 'X1', 'mergeCell' => true, 'mergeToCell' => 'X2'], ['value' => 'Kode Diagnosa', 'cell' => 'Y1', 'mergeCell' => true, 'mergeToCell' => 'Y2'], ['value' => 'Diagnosa', 'cell' => 'Z1', 'mergeCell' => true, 'mergeToCell' => 'Z2'], ['value' => 'Kode Resep', 'cell' => 'AA1', 'mergeCell' => true, 'mergeToCell' => 'AA2'], ['value' => 'Tanggal Resep', 'cell' => 'AB1', 'mergeCell' => true, 'mergeToCell' => 'AB2'], ['value' => 'Obat', 'cell' => 'AC1', 'mergeCell' => true, 'mergeToCell' => 'AC2'], ['value' => 'Tebus Resep', 'cell' => 'AD1', 'mergeCell' => true, 'mergeToCell' => 'AD2'], ['value' => 'Tanggal Bayar', 'cell' => 'AE1', 'mergeCell' => true, 'mergeToCell' => 'AE2'], ['value' => 'Provider Rujukan', 'cell' => 'AF1', 'mergeCell' => true, 'mergeToCell' => 'AF2'], ['value' => 'Poli', 'cell' => 'AG1', 'mergeCell' => true, 'mergeToCell' => 'AG2'], ['value' => 'Subjek', 'cell' => 'AH1', 'mergeCell' => true, 'mergeToCell' => 'AH2'], ['value' => 'No SJP', 'cell' => 'AI1', 'mergeCell' => true, 'mergeToCell' => 'AI2'], ]; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); foreach ($headers as $header) { $sheet->setCellValue($header['cell'], $header['value']); if ($header['mergeCell'] === true) { $sheet->mergeCells($header['cell'] . ':' . $header['mergeToCell']); } $sheet->getStyle($header['cell'])->getFont()->setBold(true); $sheet->getStyle($header['cell'])->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); $sheet->setCellValue($header['cell'], $header['value']); if ($header['mergeCell'] === true) { $sheet->mergeCells($header['cell'] . ':' . $header['mergeToCell']); } $sheet->getStyle($header['cell'])->getFont()->setBold(true); $sheet->getStyle($header['cell'])->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); } $startFrom = 3; foreach ($liveChats as $indexLiveChat => $liveChat) { if ( $liveChat->user->nID == 109370 || $liveChat->nIDDokter == 100120) { continue; // user testing / dokter testing } $summary = $liveChat->summary; $user = $liveChat->user; $phone = $liveChat->user->sPhone ?? '-'; $status = $liveChat->sStatus; $nIDUser = $liveChat->user->nIDUser ?? 0; // Principal or Dependent $paymentMethod = $liveChat->appointment ? Helper::sPaymentMethod($liveChat->appointment->sPaymentMethod) : 'N/A'; $fullNameDoctor = '-'; if (!empty($liveChat->doctor->user)) { $fullNameDoctor = ''; if ($liveChat->doctor->user->detail !== null) { if ($liveChat->doctor->user->detail->sTitlePrefix !== null) { $fullNameDoctor .= $liveChat->doctor->user->detail->sTitlePrefix . '. '; } if ($liveChat->doctor->user->full_name !== null) { $fullNameDoctor .= $liveChat->doctor->user->full_name . ' '; } if ($liveChat->doctor->user->detail->sTitleSuffix !== null) { $fullNameDoctor .= $liveChat->doctor->user->detail->sTitleSuffix; } } } $recordType = 'P'; if ($nIDUser){ $recordType = 'D'; } switch ($status) { case 0: $statusLivechat = "Request TC"; break; case 1: $statusLivechat = "Accepted by Doctor but User not Payment"; break; case 2: $statusLivechat = "Payment Success"; break; case 3: $statusLivechat = "Decline by Doctor"; break; case 4: $statusLivechat = "Payment Expired"; break; default: $statusLivechat = "Cancel by Patient"; } $requestTime = Carbon::parse($liveChat->dRequestTime); $acceptTime = Carbon::parse($liveChat->dAcceptTime); $startTime = Carbon::parse($liveChat->dStartTime); $endTime = Carbon::parse($liveChat->dEndTime); // Hitung selisih waktu response if ($requestTime && $acceptTime) { $diff = $requestTime->diff($acceptTime); $responseTimeDiff = sprintf('%02d:%02d:%02d', $diff->h, $diff->i, $diff->s); } else { $responseTimeDiff = '00:00:00'; // Default jika data kosong } // Hitung selisih waktu chat if ($startTime && $endTime) { $diffChatTime = $startTime->diff($endTime); $chatTimeDiff = sprintf('%02d:%02d:%02d', $diffChatTime->h, $diffChatTime->i, $diffChatTime->s); } else { $chatTimeDiff = '00:00:00'; // Default jika data kosong } // Set nilai responseTime dan chatTime $responseTime = $responseTimeDiff; $chatTime = $chatTimeDiff; // $diagnosa = '-'; // $diagnosaCode = '-'; // if($liveChat->summary){ // $diagnosis = explode(', ', $liveChat->summary->sAssessment); // if ($diagnosis) { // $diagnosaArray = []; // $diagnosaCodeArray = []; // foreach($diagnosis as $data){ // $diagnosaArray[] = Helper::diagnosisName($data); // Tambahkan diagnosis ke array // $diagnosaCodeArray[] = $data; // Tambahkan diagnosis ke array // } // $diagnosa = implode('; ', $diagnosaArray); // Gabungkan array dengan tanda koma // $diagnosaCode = implode('; ', $diagnosaCodeArray); // Gabungkan array dengan tanda koma // } // } $diagnosa = '-'; $diagnosaCode = '-'; if ($summary && $summary->sAssessment) { $diagnosisCodes = explode(', ', $summary->sAssessment); $diagnosaArray = array_map(fn($code) => Helper::diagnosisName($code), $diagnosisCodes); $diagnosa = implode('; ', $diagnosaArray); $diagnosaCode = implode('; ', $diagnosisCodes); } $tebusResep = 'Belum Ditebus'; $paymentTebus = '-'; if ($liveChat->prescription){ // Tanggal target (misalnya, dari database atau input) $tanggalResep = Carbon::parse($liveChat->prescription->dTanggalResep); // Tanggal hari ini $tanggalNow = Carbon::now(); // Menghitung selisih hari $selisihHari = $tanggalNow->diffInDays($tanggalResep); if ($selisihHari > 1){ $tebusResep = 'Resep Kadaluarsa'; } if ($liveChat->prescription->sIsDownload == 1){ $tebusResep = 'Offline'; } if($liveChat->prescription->payment){ $tebusResep = 'Online'; if ($liveChat->prescription->payment->sPaymentStatus == 'paid'){ $paymentTebus = Carbon::parse($liveChat->prescription->payment->dCreateOn)->format('d-m-Y H:i:s'); } }; } else { $tebusResep = '-'; } // switc $nIDHubunganKeluarga = match($user->nIDHubunganKeluarga) { 9 => "Peserta", 4 => "Istri", 5 => "Anak", 3 => "Suami", default => $user->relation->sHubunganKeluarga ?? '-' }; if ($liveChat->user->relation && $nIDHubunganKeluarga == '-'){ $nIDHubunganKeluarga = $liveChat->user->relation->sHubunganKeluarga; } $obat = '-'; if($liveChat->prescription){ if ($liveChat->prescription->items){ $obatArray = []; foreach($liveChat->prescription->items as $data){ $obatArray[] = $data->sItemName .' - '. $data->nQty; } $obat = implode('; ',$obatArray); } }; $kodeLivechat = '-'; if ($liveChat->dRequestTime && $liveChat->nID) { $createOnCarbon = Carbon::parse($liveChat->dRequestTime); $formattedDate = $createOnCarbon->format('ymdHis'); $kodeLivechat = 'LS-' . $formattedDate . $liveChat->nID; } $sheet->setCellValue('A' . $startFrom, $indexLiveChat + 1); $sheet->setCellValue('B' . $startFrom, $liveChat->nID ?? '-'); $sheet->setCellValue('C' . $startFrom, $kodeLivechat); $sheet->setCellValue('D' . $startFrom, Carbon::parse($liveChat->dCreateOn)->format('d-m-Y')); $sheet->setCellValue('E' . $startFrom, Carbon::parse($liveChat->dCreateOn)->format('H:i:s')); $sheet->setCellValue('F' . $startFrom, $liveChat->healthCare->sHealthCare ?? '-'); $sheet->setCellValue('G' . $startFrom, $fullNameDoctor); $sheet->setCellValue('H' . $startFrom, $liveChat->doctor->speciality->sSpesialis ?? '-'); $sheet->setCellValue('I' . $startFrom, $liveChat->sMedia ?? '-'); $sheet->setCellValue('J' . $startFrom, $liveChat->sMediaDokter ?? '-'); $sheet->setCellValue('K' . $startFrom, $liveChat->user->nID ?? '-'); $sheet->setCellValue('L' . $startFrom, $liveChat->user->full_name ?? '-'); $sheet->setCellValue('M' . $startFrom, preg_replace('/(\d{3})(\d{4})(\d{3})/', '$1$2$3', $phone)); $sheet->setCellValue('N' . $startFrom, $liveChat->user->sEmail ?? '-'); $sheet->setCellValue('O' . $startFrom, (string)($liveChat->userInsurance->sNoPolis ?? '-')); $sheet->setCellValue('P' . $startFrom, (string)($liveChat->userInsurance->sCorporateCode ?? '-')); $sheet->setCellValue('Q' . $startFrom, (string)($liveChat->userInsurance->sCorporateName ?? '-')); $sheet->setCellValue('R' . $startFrom, (string)($nIDHubunganKeluarga ?? '-')); $sheet->setCellValue('S' . $startFrom, (string)($liveChat->userInsurance->sChannel ?? '-')); $sheet->setCellValue('T' . $startFrom, $statusLivechat); $sheet->setCellValue('U' . $startFrom, $requestTime); // $sheet->setCellValue('V' . $startFrom, $recordType); // $sheet->setCellValue('W' . $startFrom, $nIDUser ?? '-'); // $sheet->setCellValue('X' . $startFrom, $paymentMethod ?? '-'); $sheet->setCellValue('V' . $startFrom, $acceptTime); $sheet->setCellValue('W' . $startFrom, $startTime); $sheet->setCellValue('X' . $startFrom, $endTime); $sheet->setCellValue('Y' . $startFrom, $diagnosaCode ?? '-'); $sheet->setCellValue('Z' . $startFrom, $diagnosa ?? '-'); $sheet->setCellValue('AA' . $startFrom, $liveChat->prescription->sKodeResep ?? '-'); $sheet->setCellValue('AB' . $startFrom, $liveChat->prescription->dCreateOn ?? '-'); $sheet->setCellValue('AC' . $startFrom, $obat); $sheet->setCellValue('AD' . $startFrom, $tebusResep); $sheet->setCellValue('AE' . $startFrom, $paymentTebus); $sheet->setCellValue('AF' . $startFrom, $liveChat->rujukan->nIDHealthcare ?? '-'); $sheet->setCellValue('AG' . $startFrom, $liveChat->rujukan->sDepartement ?? '-'); $sheet->setCellValue('AH' . $startFrom, $liveChat->summary->sSubjective ?? '-'); $sheet->setCellValue('AI' . $startFrom, $liveChat->sNoSpj ?? '-'); $startFrom++; } foreach (['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI'] as $header) { if ($header === 'A') { $spreadsheet->getActiveSheet()->getColumnDimension($header)->setWidth(35, 'px'); } elseif ($header === 'I' || $header === 'J') { $spreadsheet->getActiveSheet()->getColumnDimension($header)->setWidth(100, 'px'); } else { $spreadsheet->getActiveSheet()->getColumnDimension($header)->setAutoSize(true); } } $spreadsheet->getActiveSheet()->getStyle('A2:A' . $startFrom)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); $sheet->getDefaultRowDimension()->setRowHeight(-1); $sheet->setTitle('Live Chat Report'); $writer = new Xlsx($spreadsheet); ob_start(); $writer->save('php://output'); $content = ob_get_contents(); ob_end_clean(); $fileName = 'result-' . now()->getPreciseTimestamp(3) . '-livechat-report.xlsx'; Storage::disk('public')->put('temp/' . $fileName, $content); $fileUrl = url('storage/temp/' . $fileName); return Helper::responseJson([ "file_url" => $fileUrl ]); } } public function exportMonthly($startDate, $endDate) { $liveChats = Livechat::with('userInsurance', 'user:nID,sFirstName,sLastName,sEmail,sPhone,nIDUser,nIDHubunganKeluarga', 'user.detail', 'user.relation', 'doctor:nID,nIDSpesialis,nIDUser', 'doctor.user:nID,sFirstName,sLastName', 'doctor.user.detail', 'doctor.speciality', 'appointment:nID,sPaymentStatus,sPaymentMethod', 'appointment.appointmentDetail:nID,nIDAppointment,dTanggalAppointment,tTimeAppointment', 'healthCare:nID,sHealthCare', 'prescription', 'prescription.items', 'prescription.payment', 'summary', 'rujukan' ) ->whereHas('userInsurance', function (Builder $query) { // Kondisi pada relasi userInsurance $query->where('nIDInsurance', 107); // khusus inhealth }) ->where(function (Builder $query) use ($startDate, $endDate) { // $query->where('nIDAppointment', '!=', null); // $query->where('nIDAppointment', '!=', ''); if ($startDate) { $query->where('dCreateOn', '>=', $startDate); } if ($endDate) { $endDate = date('Y-m-d', strtotime($endDate . ' +1 day')); $query->where('dCreateOn', '<', $endDate); } }) ->orderBy('nID', 'desc') ->get(['nID', 'nIDUser', 'nIDDokter', 'nIDHealthCare', 'nIDAppointment', 'sStatus', 'sMediaDokter', 'sMedia', 'dCreateOn', 'sNoSpj', 'dRequestTime', 'dAcceptTime', 'dStartTime', 'dEndTime']); $headers = [ ['value' => 'ConsultationId', 'cell' => 'A1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'No SJP', 'cell' => 'B1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Kode Livechat', 'cell' => 'C1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'PKSKD', 'cell' => 'D1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'PKSNM', 'cell' => 'E1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Card Number', 'cell' => 'F1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'NamaPasien', 'cell' => 'G1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'JenisKelamin', 'cell' => 'H1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'TanggalLahir', 'cell' => 'I1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Usia', 'cell' => 'J1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Produk', 'cell' => 'K1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Plan', 'cell' => 'L1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'DependencyStatus', 'cell' => 'M1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'TanggalKonsultasi', 'cell' => 'N1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Keluhan', 'cell' => 'O1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Kode Diagnosa', 'cell' => 'P1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Diagnosa', 'cell' => 'Q1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'StartTime', 'cell' => 'R1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'EndTime', 'cell' => 'S1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'DurasiKonsultasi', 'cell' => 'T1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'StatusKonsultasi', 'cell' => 'U1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'PrescriptionNumber', 'cell' => 'V1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Nama Dokter', 'cell' => 'W1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Jenis Dokter', 'cell' => 'X1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Rujuk (Ya/Tidak)', 'cell' => 'Y1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Provider Rujukan', 'cell' => 'Z1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Poli', 'cell' => 'AA1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Total', 'cell' => 'AB1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Obat', 'cell' => 'AC1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Apotek', 'cell' => 'AD1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Nama Obat', 'cell' => 'AE1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Jumlah Obat', 'cell' => 'AF1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'By', 'cell' => 'AG1', 'mergeCell' => false, 'mergeToCell' => ''], ['value' => 'Frequency Livechat (1 Minggu)', 'cell' => 'AH1', 'mergeCell' => false, 'mergeToCell' => ''], ]; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->setActiveSheetIndex(0); $sheet2 = $spreadsheet->createSheet(); $sheet2 = $spreadsheet->setActiveSheetIndex(1); foreach ($headers as $header) { $sheet->setCellValue($header['cell'], $header['value']); // if ($header['mergeCell'] === true) { // $sheet->mergeCells($header['cell'] . ':' . $header['mergeToCell']); // } $sheet->getStyle($header['cell'])->getFont()->setBold(true); $sheet->getStyle($header['cell'])->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); $sheet2->setCellValue($header['cell'], $header['value']); $sheet2->getStyle($header['cell'])->getFont()->setBold(true); $sheet2->getStyle($header['cell'])->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); } $startFromSheet1 = 2; $startFromSheet2 = 2; $apotekList = Organization::pluck('name', 'id'); // Ambil semua nama apotek dulu (1 query) // dd('test'); foreach ($liveChats as $indexLiveChat => $liveChat) { //###################### SHEET 1 ########## $phone = $liveChat->user->sPhone ?? '-'; $status = $liveChat->sStatus; $nIDUser = $liveChat->user->nIDUser ?? 0; // Principal or Dependent $paymentMethod = $liveChat->appointment ? Helper::sPaymentMethod($liveChat->appointment->sPaymentMethod) : 'N/A'; $summary = $liveChat->summary; $user = $liveChat->user; $frequencyLivechat = $this->frequencyLivechat($user->nID); $fullNameDoctor = '-'; if (!empty($liveChat->doctor->user)) { $fullNameDoctor = 'dr. '; // if ($liveChat->doctor->user->detail !== null) { // if ($liveChat->doctor->user->detail->sTitlePrefix !== null) { // $fullNameDoctor .= $liveChat->doctor->user->detail->sTitlePrefix . '. '; // } if ($liveChat->doctor->user->full_name !== null) { $fullNameDoctor .= $liveChat->doctor->user->full_name . ' '; } // if ($liveChat->doctor->user->detail->sTitleSuffix !== null) { // $fullNameDoctor .= $liveChat->doctor->user->detail->sTitleSuffix; // } // } } $recordType = 'P'; if ($nIDUser){ $recordType = 'D'; } switch ($status) { case 0: $statusLivechat = "Request TC"; break; case 1: $statusLivechat = "Accepted by Doctor but User not Payment"; break; case 2: $statusLivechat = "Payment Success"; break; case 3: $statusLivechat = "Decline by Doctor"; break; case 4: $statusLivechat = "Payment Expired"; break; default: $statusLivechat = "Cancel by Patient"; } $requestTime = Carbon::parse($liveChat->dRequestTime); $acceptTime = Carbon::parse($liveChat->dAcceptTime); $startTime = Carbon::parse($liveChat->dStartTime); $endTime = Carbon::parse($liveChat->dEndTime); // Hitung selisih waktu response if ($requestTime && $acceptTime) { $diff = $requestTime->diff($acceptTime); $responseTimeDiff = sprintf('%02d:%02d:%02d', $diff->h, $diff->i, $diff->s); } else { $responseTimeDiff = '00:00:00'; // Default jika data kosong } // Hitung selisih waktu chat if ($startTime && $endTime) { $diffChatTime = $startTime->diff($endTime); $chatTimeDiff = sprintf('%02d:%02d:%02d', $diffChatTime->h, $diffChatTime->i, $diffChatTime->s); } else { $chatTimeDiff = '00:00:00'; // Default jika data kosong } // Set nilai responseTime dan chatTime $responseTime = $responseTimeDiff; $chatTime = $chatTimeDiff; // $diagnosa = '-'; // $diagnosaCode = '-'; // if($liveChat->summary){ // $diagnosis = explode(', ', $liveChat->summary->sAssessment); // if ($diagnosis) { // $diagnosaArray = []; // $diagnosaCodeArray = []; // foreach($diagnosis as $data){ // $diagnosaArray[] = Helper::diagnosisName($data); // Tambahkan diagnosis ke array // $diagnosaCodeArray[] = $data; // Tambahkan diagnosis ke array // } // $diagnosa = implode('; ', $diagnosaArray); // Gabungkan array dengan tanda koma // $diagnosaCode = implode('; ', $diagnosaCodeArray); // Gabungkan array dengan tanda koma // } // } $diagnosa = '-'; $diagnosaCode = '-'; if ($summary && $summary->sAssessment) { $diagnosisCodes = explode(', ', $summary->sAssessment); $diagnosaArray = array_map(fn($code) => Helper::diagnosisName($code), $diagnosisCodes); $diagnosa = implode('; ', $diagnosaArray); $diagnosaCode = implode('; ', $diagnosisCodes); } $tebusResep = 'Belum Ditebus'; $paymentTebus = '-'; $obat = '-'; $obatQty = '-'; $qtyTotal = 0; // Nama Apotek $apotek = '-'; if ($liveChat->prescription){ // Tanggal target (misalnya, dari database atau input) $tanggalResep = Carbon::parse($liveChat->prescription->dTanggalResep); // Tanggal hari ini $tanggalNow = Carbon::now(); // Menghitung selisih hari $selisihHari = $tanggalNow->diffInDays($tanggalResep); if ($selisihHari > 1){ $tebusResep = 'Resep Kadaluarsa'; } if ($liveChat->prescription->sIsDownload == 1){ $tebusResep = 'Offline'; } if($liveChat->prescription->payment){ $tebusResep = 'Online'; // $apotek = Organization::where('code', $liveChat->prescription->payment->nIDApotek)->first(); // if ($apotek){ // $apotek = $apotek->name; // } else { // $apotek = '-'; // } $apotek = $apotekList[$liveChat->prescription->payment->nIDApotek] ?? '-'; if ($liveChat->prescription->payment->sPaymentStatus == 'paid'){ $paymentTebus = Carbon::parse($liveChat->prescription->payment->dCreateOn)->format('d-m-Y H:i:s'); } }; if ($liveChat->prescription->items){ $obatArray = []; $obatQtyArray = []; foreach($liveChat->prescription->items as $data){ $obatArray[] = $data->sItemName; $obatQtyArray[] = $data->nQty; $qtyTotal += $data->nQty; } $obat = implode('; ',$obatArray); $obatQty = implode('; ',$obatQtyArray); } } else { $tebusResep = '-'; } $nIDHubunganKeluarga = match($user->nIDHubunganKeluarga) { 9 => "Peserta", 4 => "Istri", 5 => "Anak", 3 => "Suami", default => $user->relation->sHubunganKeluarga ?? '-' }; if ($liveChat->user->relation && $nIDHubunganKeluarga == '-'){ $nIDHubunganKeluarga = $liveChat->user->relation->sHubunganKeluarga; } $kodeLivechat = '-'; if ($liveChat->dRequestTime && $liveChat->nID) { $createOnCarbon = Carbon::parse($liveChat->dRequestTime); $formattedDate = $createOnCarbon->format('ymdHis'); $kodeLivechat = 'LS-' . $formattedDate . $liveChat->nID; } $sheet->setCellValue('A' . $startFromSheet1, $liveChat->nID ?? '-'); $sheet->setCellValue('B' . $startFromSheet1, $liveChat->sNoSpj ?? '-'); $sheet->setCellValue('C' . $startFromSheet1, (string)($kodeLivechat) ?? '-'); $sheet->setCellValue('D' . $startFromSheet1, (string)($liveChat->userInsurance->sCorporateCode ?? '-')); $sheet->setCellValue('E' . $startFromSheet1, (string)($liveChat->userInsurance->sCorporateName ?? '-')); $sheet->setCellValue('F' . $startFromSheet1, (string)($liveChat->userInsurance->sNoPolis ?? '-')); $sheet->setCellValue('G' . $startFromSheet1, $liveChat->user->full_name ?? '-'); $sheet->setCellValue('H' . $startFromSheet1, $liveChat->user->detail->nIDJenisKelamin == 1 ? 'Laki-laki' : 'Wanita'); $sheet->setCellValue('I' . $startFromSheet1, $liveChat->user->detail->dTanggalLahir ?? '-'); $sheet->setCellValue('J' . $startFromSheet1, Helper::calculateAge($liveChat->user->detail->dTanggalLahir, true) ?? '-'); $sheet->setCellValue('K' . $startFromSheet1, (string)($liveChat->userInsurance->sProductCode ?? '-')); $sheet->setCellValue('L' . $startFromSheet1, (string)($liveChat->userInsurance->sPlanCode ?? '-')); $sheet->setCellValue('M' . $startFromSheet1, $nIDHubunganKeluarga ?? '-'); $sheet->setCellValue('N' . $startFromSheet1, $requestTime->format('Y-m-d')); $sheet->setCellValue('O' . $startFromSheet1, $liveChat->summary->sSubjective ?? '-'); $sheet->setCellValue('P' . $startFromSheet1, $diagnosaCode ?? '-'); $sheet->setCellValue('Q' . $startFromSheet1, $diagnosa ?? '-'); $sheet->setCellValue('R' . $startFromSheet1, $startTime->format('H:i:s')); $sheet->setCellValue('S' . $startFromSheet1, $endTime->format('H:i:s')); $sheet->setCellValue('T' . $startFromSheet1, $chatTime); // $sheet->setCellValue('O' . $startFromSheet1, $recordType); // $sheet->setCellValue('P' . $startFromSheet1, $nIDUser ?? '-'); // $sheet->setCellValue('Q' . $startFromSheet1, $paymentMethod ?? '-'); $sheet->setCellValue('U' . $startFromSheet1, $statusLivechat); $sheet->setCellValue('V' . $startFromSheet1, $liveChat->prescription->sKodeResep ?? '-'); $sheet->setCellValue('W' . $startFromSheet1, $fullNameDoctor); $sheet->setCellValue('X' . $startFromSheet1, $liveChat->doctor->speciality->sSpesialis ?? '-'); $sheet->setCellValue('Y' . $startFromSheet1, $liveChat->rujukan ? 'Ya' : 'Tidak'); $sheet->setCellValue('Z' . $startFromSheet1, $liveChat->rujukan->nIDHealthcare ?? '-'); $sheet->setCellValue('AA' . $startFromSheet1, $liveChat->rujukan->sDepartement ?? '-'); $sheet->setCellValue('AB' . $startFromSheet1, $qtyTotal); $sheet->setCellValue('AC' . $startFromSheet1, $tebusResep); $sheet->setCellValue('AD' . $startFromSheet1, $apotek); $sheet->setCellValue('AE' . $startFromSheet1, $obat); $sheet->setCellValue('AF' . $startFromSheet1, $obatQty); $sheet->setCellValue('AG' . $startFromSheet1, 'LMS'); $sheet->setCellValue('AH' . $startFromSheet1, $frequencyLivechat); // $sheet->setCellValue('AC' . $startFrom, $liveChat->prescription->dCreateOn ?? '-'); // $sheet->setCellValue('AD' . $startFrom, $obat); // $sheet->setCellValue('AE' . $startFrom, $tebusResep); // $sheet->setCellValue('AF' . $startFrom, $paymentTebus); // $sheet->setCellValue('AG' . $startFrom, $liveChat->rujukan->nIDHealthcare ?? '-'); // $sheet->setCellValue('AH' . $startFrom, $liveChat->rujukan->sDepartement ?? '-'); // $sheet->setCellValue('AI' . $startFrom, $liveChat->summary->sSubjective ?? '-'); // $sheet->setCellValue('AJ' . $startFrom, $liveChat->sNoSpj ?? '-'); $startFromSheet1++; //###################### END SHEET 1 ########## if (!$liveChat->prescription || !$liveChat->prescription->items) { continue; } else { if ($liveChat->prescription->items){ $obatArray = []; $obatQtyArray = []; foreach($liveChat->prescription->items as $data){ $phone = $liveChat->user->sPhone ?? '-'; $status = $liveChat->sStatus; $nIDUser = $liveChat->user->nIDUser ?? 0; // Principal or Dependent $paymentMethod = $liveChat->appointment ? Helper::sPaymentMethod($liveChat->appointment->sPaymentMethod) : 'N/A'; $fullNameDoctor = '-'; if (!empty($liveChat->doctor->user)) { $fullNameDoctor = ''; if ($liveChat->doctor->user->detail !== null) { if ($liveChat->doctor->user->detail->sTitlePrefix !== null) { $fullNameDoctor .= $liveChat->doctor->user->detail->sTitlePrefix . '. '; } if ($liveChat->doctor->user->full_name !== null) { $fullNameDoctor .= $liveChat->doctor->user->full_name . ' '; } if ($liveChat->doctor->user->detail->sTitleSuffix !== null) { $fullNameDoctor .= $liveChat->doctor->user->detail->sTitleSuffix; } } } $recordType = 'P'; if ($nIDUser){ $recordType = 'D'; } switch ($status) { case 0: $statusLivechat = "Request TC"; break; case 1: $statusLivechat = "Accepted by Doctor but User not Payment"; break; case 2: $statusLivechat = "Payment Success"; break; case 3: $statusLivechat = "Decline by Doctor"; break; case 4: $statusLivechat = "Payment Expired"; break; default: $statusLivechat = "Cancel by Patient"; } $requestTime = Carbon::parse($liveChat->dRequestTime); $acceptTime = Carbon::parse($liveChat->dAcceptTime); $startTime = Carbon::parse($liveChat->dStartTime); $endTime = Carbon::parse($liveChat->dEndTime); // Hitung selisih waktu response if ($requestTime && $acceptTime) { $diff = $requestTime->diff($acceptTime); $responseTimeDiff = sprintf('%02d:%02d:%02d', $diff->h, $diff->i, $diff->s); } else { $responseTimeDiff = '00:00:00'; // Default jika data kosong } // Hitung selisih waktu chat if ($startTime && $endTime) { $diffChatTime = $startTime->diff($endTime); $chatTimeDiff = sprintf('%02d:%02d:%02d', $diffChatTime->h, $diffChatTime->i, $diffChatTime->s); } else { $chatTimeDiff = '00:00:00'; // Default jika data kosong } // Set nilai responseTime dan chatTime $responseTime = $responseTimeDiff; $chatTime = $chatTimeDiff; // $diagnosa = '-'; // $diagnosaCode = '-'; // if($liveChat->summary){ // $diagnosis = explode(', ', $liveChat->summary->sAssessment); // if ($diagnosis) { // $diagnosaArray = []; // $diagnosaCodeArray = []; // foreach($diagnosis as $dataDiagnosa){ // $diagnosaArray[] = Helper::diagnosisName($dataDiagnosa); // Tambahkan diagnosis ke array // $diagnosaCodeArray[] = $dataDiagnosa; // Tambahkan diagnosis ke array // } // $diagnosa = implode('; ', $diagnosaArray); // Gabungkan array dengan tanda koma // $diagnosaCode = implode('; ', $diagnosaCodeArray); // Gabungkan array dengan tanda koma // } // } $diagnosa = '-'; $diagnosaCode = '-'; if ($summary && $summary->sAssessment) { $diagnosisCodes = explode(', ', $summary->sAssessment); $diagnosaArray = array_map(fn($code) => Helper::diagnosisName($code), $diagnosisCodes); $diagnosa = implode('; ', $diagnosaArray); $diagnosaCode = implode('; ', $diagnosisCodes); } $tebusResep = 'Belum Ditebus'; $paymentTebus = '-'; if ($liveChat->prescription){ // Tanggal target (misalnya, dari database atau input) $tanggalResep = Carbon::parse($liveChat->prescription->dTanggalResep); // Tanggal hari ini $tanggalNow = Carbon::now(); // Menghitung selisih hari $selisihHari = $tanggalNow->diffInDays($tanggalResep); if ($selisihHari > 1){ $tebusResep = 'Resep Kadaluarsa'; } if ($liveChat->prescription->sIsDownload == 1){ $tebusResep = 'Offline'; } if($liveChat->prescription->payment){ $tebusResep = 'Online'; // $apotek = Organization::where('id', $liveChat->prescription->payment->nIDApotek)->first(); // if ($apotek){ // $apotek = $apotek->name; // } else { // $apotek = '-'; // } $apotek = $apotekList[$liveChat->prescription->payment->nIDApotek] ?? '-'; if ($liveChat->prescription->payment->sPaymentStatus == 'paid'){ $paymentTebus = Carbon::parse($liveChat->prescription->payment->dCreateOn)->format('d-m-Y H:i:s'); } }; } else { $tebusResep = '-'; } // switch ($liveChat->user->nIDHubunganKeluarga) { // case 9: // $nIDHubunganKeluarga = "Peserta"; // Parent // break; // case 4: // $nIDHubunganKeluarga = 'Istri'; // Spouse // break; // case 5: // $nIDHubunganKeluarga = 'Anak'; // Child // break; // case 3: // $nIDHubunganKeluarga = 'Suami'; // Husband // break; // default: // $nIDHubunganKeluarga = '-'; // No need to set $nIDHubunganKeluarga as it's already set to default value // break; // } $nIDHubunganKeluarga = match($user->nIDHubunganKeluarga) { 9 => "Peserta", 4 => "Istri", 5 => "Anak", 3 => "Suami", default => $user->relation->sHubunganKeluarga ?? '-' }; if ($liveChat->user->relation && $nIDHubunganKeluarga == '-'){ $nIDHubunganKeluarga = $liveChat->user->relation->sHubunganKeluarga; } $obat = $data->sItemName; $obatQty = $data->nQty; $sheet2->setCellValue('A' . $startFromSheet2, $liveChat->nID ?? '-'); $sheet2->setCellValue('B' . $startFromSheet2, $liveChat->sNoSpj ?? '-'); $sheet2->setCellValue('C' . $startFromSheet2, (string)($kodeLivechat) ?? '-'); $sheet2->setCellValue('D' . $startFromSheet2, (string)($liveChat->userInsurance->sCorporateCode ?? '-')); $sheet2->setCellValue('E' . $startFromSheet2, (string)($liveChat->userInsurance->sCorporateName ?? '-')); $sheet2->setCellValue('F' . $startFromSheet2, (string)($liveChat->userInsurance->sNoPolis ?? '-')); $sheet2->setCellValue('G' . $startFromSheet2, $liveChat->user->full_name ?? '-'); $sheet2->setCellValue('H' . $startFromSheet2, $liveChat->user->detail->dTanggalLahir ?? '-'); $sheet2->setCellValue('I' . $startFromSheet2, $liveChat->user->detail->nIDJenisKelamin == 1 ? 'Laki-laki' : 'Wanita'); $sheet2->setCellValue('J' . $startFromSheet2, Helper::calculateAge($liveChat->user->detail->dTanggalLahir) ?? '-'); $sheet2->setCellValue('K' . $startFromSheet2, (string)($liveChat->userInsurance->sProductCode ?? '-')); $sheet2->setCellValue('L' . $startFromSheet2, (string)($liveChat->userInsurance->sPlanCode ?? '-')); $sheet2->setCellValue('M' . $startFromSheet2, $nIDHubunganKeluarga ?? '-'); $sheet2->setCellValue('N' . $startFromSheet2, $requestTime->format('Y-m-d')); $sheet2->setCellValue('O' . $startFromSheet2, $liveChat->summary->sSubjective ?? '-'); $sheet2->setCellValue('P' . $startFromSheet2, $diagnosaCode ?? '-'); $sheet2->setCellValue('Q' . $startFromSheet2, $diagnosa ?? '-'); $sheet2->setCellValue('R' . $startFromSheet2, $startTime->format('H:i:s')); $sheet2->setCellValue('S' . $startFromSheet2, $endTime->format('H:i:s')); $sheet2->setCellValue('T' . $startFromSheet2, $chatTime); // $sheet2->setCellValue('O' . $startFromSheet2, $recordType); // $sheet2->setCellValue('P' . $startFromSheet2, $nIDUser ?? '-'); // $sheet2->setCellValue('Q' . $startFromSheet2, $paymentMethod ?? '-'); $sheet2->setCellValue('U' . $startFromSheet2, $statusLivechat); $sheet2->setCellValue('V' . $startFromSheet2, $liveChat->prescription->sKodeResep ?? '-'); $sheet2->setCellValue('W' . $startFromSheet2, $fullNameDoctor); $sheet2->setCellValue('X' . $startFromSheet2, $liveChat->doctor->speciality->sSpesialis ?? '-'); $sheet2->setCellValue('Y' . $startFromSheet2, $liveChat->rujukan ? 'Ya' : 'Tidak'); $sheet2->setCellValue('Z' . $startFromSheet2, $liveChat->rujukan->nIDHealthcare ?? '-'); $sheet2->setCellValue('AA' . $startFromSheet2, $liveChat->rujukan->sDepartement ?? '-'); $sheet2->setCellValue('AB' . $startFromSheet2, '-'); $sheet2->setCellValue('AC' . $startFromSheet2, $tebusResep); $sheet2->setCellValue('AD' . $startFromSheet2, $apotek); $sheet2->setCellValue('AE' . $startFromSheet2, $obat); $sheet2->setCellValue('AF' . $startFromSheet2, $obatQty); $sheet2->setCellValue('AG' . $startFromSheet2, 'LMS'); // $sheet->setCellValue('AC' . $startFromSheet2, $liveChat->prescription->dCreateOn ?? '-'); // $sheet->setCellValue('AD' . $startFromSheet2, $obat); // $sheet->setCellValue('AE' . $startFromSheet2, $tebusResep); // $sheet->setCellValue('AF' . $startFromSheet2, $paymentTebus); // $sheet->setCellValue('AG' . $startFromSheet2, $liveChat->rujukan->nIDHealthcare ?? '-'); // $sheet->setCellValue('AH' . $startFromSheet2, $liveChat->rujukan->sDepartement ?? '-'); // $sheet->setCellValue('AI' . $startFromSheet2, $liveChat->summary->sSubjective ?? '-'); // $sheet->setCellValue('AJ' . $startFromSheet2, $liveChat->sNoSpj ?? '-'); $startFromSheet2++; } } } } foreach (['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF'] as $header) { // if ($header === 'A') { // $spreadsheet->getActiveSheet()->getColumnDimension($header)->setWidth(35, 'px'); // } elseif ($header === 'H' || $header === 'I') { // $spreadsheet->getActiveSheet()->getColumnDimension($header)->setWidth(100, 'px'); // } else { $spreadsheet->getActiveSheet(0)->getColumnDimension($header)->setAutoSize(true); $spreadsheet->getActiveSheet(1)->getColumnDimension($header)->setAutoSize(true); // } } // $spreadsheet->getActiveSheet()->getStyle('A2:A' . $startFrom)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); $sheet->getDefaultRowDimension()->setRowHeight(-1); $sheet->setTitle('Live Chat Report'); $sheet2->getDefaultRowDimension()->setRowHeight(-1); $sheet2->setTitle('DATA KHUSUS OBAT'); $writer = new Xlsx($spreadsheet); ob_start(); $writer->save('php://output'); $content = ob_get_contents(); ob_end_clean(); $fileName = 'result-' . now()->getPreciseTimestamp(3) . '-livechat-report.xlsx'; Storage::disk('public')->put('temp/' . $fileName, $content); $fileUrl = url('storage/temp/' . $fileName); return Helper::responseJson([ "file_url" => $fileUrl ]); } public function frequencyLivechat($nIdUser) { $liveChats = Livechat::where('sStatus', 2) ->where('dRequestTime', '>=', Carbon::now()->subDays(7)) ->where('nIDUser', $nIdUser) ->count(); return $liveChats; } }