502 lines
17 KiB
PHP
Executable File
502 lines
17 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Internal\Http\Controllers\Api;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Models\OLDLMS\Livechat;
|
|
use App\Models\OLDLMS\Dokter;
|
|
use Illuminate\Http\Request;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
* @return Renderable
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$start_date = $request->start_date
|
|
? Carbon::parse($request->start_date)->startOfDay()->toDateTimeString()
|
|
: null;
|
|
|
|
$end_date = $request->end_date
|
|
? Carbon::parse($request->end_date)->endOfDay()->toDateTimeString()
|
|
: null;
|
|
|
|
$type = $request->type; // 0 = harian, 1 = mingguan, 2 = bulanan
|
|
$status = $request->status; // 0 = semua, 1 = berhasil, 2 = abandon, 3 = gagal
|
|
|
|
// Menyesuaikan filter berdasarkan type (harian, mingguan, bulanan)
|
|
// if ($type == 1) {
|
|
// // Filter mingguan
|
|
// $start_date = Carbon::now()->startOfWeek()->toDateTimeString();
|
|
// $end_date = Carbon::now()->endOfWeek()->toDateTimeString();
|
|
|
|
// } elseif ($type == 2) {
|
|
// // Filter bulanan
|
|
// $start_date = Carbon::now()->startOfMonth()->toDateTimeString();
|
|
// $end_date = Carbon::now()->endOfMonth()->toDateTimeString();
|
|
// }
|
|
|
|
// Query awal
|
|
$query = Livechat::query();
|
|
|
|
// Filter berdasarkan tanggal
|
|
if ($start_date && $end_date) {
|
|
$query->whereBetween('dRequestTime', [$start_date, $end_date]);
|
|
}
|
|
|
|
// Filter berdasarkan status
|
|
if ($status == 1) {
|
|
$query->where('sStatus', '2'); // Berhasil
|
|
} elseif ($status == 2) {
|
|
$query->where('sStatus', '1'); // Abandon
|
|
} elseif ($status == 3) {
|
|
$query->whereNotIn('sStatus', ['1', '2']); // Gagal (selain 1 dan 2)
|
|
}
|
|
|
|
$liveChat = $query->get();
|
|
|
|
// Mapping status transaksi
|
|
$statusMapping = [
|
|
"2" => "Berhasil",
|
|
"1" => "Abandon",
|
|
];
|
|
|
|
// Inisialisasi counter status
|
|
$statusCount = [
|
|
"Berhasil" => 0,
|
|
"Abandon" => 0,
|
|
"Gagal" => 0,
|
|
];
|
|
|
|
// Hitung jumlah status
|
|
foreach ($liveChat as $chat) {
|
|
$statusLabel = isset($statusMapping[$chat->sStatus])
|
|
? $statusMapping[$chat->sStatus]
|
|
: "Gagal";
|
|
$statusCount[$statusLabel]++;
|
|
}
|
|
|
|
// Format response seperti yang diminta
|
|
$transaksiData = [
|
|
["name" => "Berhasil", "value" => $statusCount["Berhasil"], "color" => "#4CAF50"],
|
|
["name" => "Gagal", "value" => $statusCount["Gagal"], "color" => "#F44336"],
|
|
["name" => "Abandon", "value" => $statusCount["Abandon"], "color" => "#9E9E9E"],
|
|
];
|
|
|
|
return response()->json($transaksiData);
|
|
}
|
|
|
|
public function listBarChart(Request $request)
|
|
{
|
|
$start_date = $request->start_date
|
|
? Carbon::parse($request->start_date)->startOfDay()
|
|
: Carbon::now()->startOfMonth();
|
|
|
|
$end_date = $request->end_date
|
|
? Carbon::parse($request->end_date)->endOfDay()
|
|
: Carbon::now()->endOfMonth();
|
|
|
|
$status = $request->status; // 0 = semua, 1 = berhasil, 2 = abandon, 3 = gagal
|
|
$type = $request->type; // 0 = harian, 1 = mingguan, 2 = bulanan
|
|
|
|
// Query awal
|
|
$query = Livechat::query();
|
|
|
|
// Filter berdasarkan rentang tanggal yang dimasukkan user
|
|
$query->whereBetween('dRequestTime', [$start_date, $end_date]);
|
|
|
|
// Filter berdasarkan status
|
|
if ($status == 1) {
|
|
$query->where('sStatus', '2'); // Berhasil
|
|
} elseif ($status == 2) {
|
|
$query->where('sStatus', '1'); // Abandon
|
|
} elseif ($status == 3) {
|
|
$query->whereNotIn('sStatus', ['1', '2']); // Gagal (selain 1 dan 2)
|
|
}
|
|
|
|
$liveChat = $query->get();
|
|
|
|
// Mengelompokkan data berdasarkan tipe request (harian, mingguan, atau bulanan)
|
|
$groupedData = [];
|
|
|
|
foreach ($liveChat as $chat) {
|
|
if ($type == 1) {
|
|
// Mingguan (contoh: "01 Jan 2025 - 07 Jan 2025")
|
|
$weekStart = Carbon::parse($chat->dRequestTime)->startOfWeek();
|
|
$weekEnd = Carbon::parse($chat->dRequestTime)->endOfWeek();
|
|
$groupKey = $weekStart->format('d M Y') . ' - ' . $weekEnd->format('d M Y');
|
|
} elseif ($type == 2) {
|
|
// Bulanan (contoh: "Jan 2025")
|
|
$groupKey = Carbon::parse($chat->dRequestTime)->translatedFormat('M Y');
|
|
} else {
|
|
// Harian (format "1 Jan 2025 - 2 Feb 2025")
|
|
$groupKey = Carbon::parse($chat->dRequestTime)->format('j M Y');
|
|
}
|
|
|
|
if (!isset($groupedData[$groupKey])) {
|
|
$groupedData[$groupKey] = [
|
|
"date" => $groupKey,
|
|
"Berhasil" => 0,
|
|
"Abandon" => 0,
|
|
"Gagal" => 0,
|
|
];
|
|
}
|
|
|
|
if ($chat->sStatus == "2") {
|
|
$groupedData[$groupKey]["Berhasil"]++;
|
|
} elseif ($chat->sStatus == "1") {
|
|
$groupedData[$groupKey]["Abandon"]++;
|
|
} else {
|
|
$groupedData[$groupKey]["Gagal"]++;
|
|
}
|
|
}
|
|
|
|
// Konversi hasil ke dalam array untuk response JSON
|
|
$result = array_values($groupedData);
|
|
|
|
return response()->json($result);
|
|
}
|
|
|
|
|
|
public function listDokter(Request $request)
|
|
{
|
|
$idDokter = [
|
|
'120866',
|
|
'107922',
|
|
'107921',
|
|
'107920',
|
|
'101192',
|
|
'99232',
|
|
'99230',
|
|
]; // List dokter
|
|
|
|
$listDokters = Dokter::with([])->whereIn('nIDUser', $idDokter)->get();
|
|
|
|
$result = $listDokters->map(function ($dokter) {
|
|
return [
|
|
'id' => $dokter->nIDUser,
|
|
'code' => $dokter->nIDUser,
|
|
'name' => $dokter->user ? $dokter->user->fullName : '-',
|
|
'online' => $dokter->sIsOnline,
|
|
];
|
|
});
|
|
|
|
return response()->json($result);
|
|
}
|
|
public function listPerformaDokter(Request $request)
|
|
{
|
|
$start_date = $request->start_date
|
|
? Carbon::parse($request->start_date)->startOfDay()
|
|
: Carbon::now()->startOfMonth();
|
|
|
|
$end_date = $request->end_date
|
|
? Carbon::parse($request->end_date)->endOfDay()
|
|
: Carbon::now()->endOfMonth();
|
|
|
|
$status = $request->status; // 0 = semua, 1 = berhasil, 2 = abandon, 3 = gagal
|
|
$type = $request->type; // 0 = harian, 1 = mingguan, 2 = bulanan
|
|
|
|
$nIDDokter = $request->nIDDokter;
|
|
|
|
// Query awal
|
|
$query = Livechat::with('doctor');
|
|
|
|
// Filter berdasarkan rentang tanggal yang dimasukkan user
|
|
$query->whereBetween('dRequestTime', [$start_date, $end_date]);
|
|
|
|
if (!empty($nIDDokter)) {
|
|
$query->whereIn('nIDDokter', $nIDDokter);
|
|
}
|
|
|
|
// Filter berdasarkan status
|
|
if ($status == 1) {
|
|
$query->where('sStatus', '2'); // Berhasil
|
|
} elseif ($status == 2) {
|
|
$query->where('sStatus', '1'); // Abandon
|
|
} elseif ($status == 3) {
|
|
$query->whereNotIn('sStatus', ['1', '2']); // Gagal
|
|
}
|
|
|
|
// Ambil data livechat
|
|
$liveChats = $query->get();
|
|
|
|
// Data akhir yang akan dikembalikan
|
|
$groupedData = [];
|
|
|
|
foreach ($liveChats as $chat) {
|
|
$dokterId = $chat->nIDDokter;
|
|
$dokterName = $chat->doctor->user->fullName ?? 'Unknown'; // Ambil nama dokter dari relasi
|
|
if (!isset($groupedData[$dokterId])) {
|
|
$groupedData[$dokterId] = [
|
|
"name" => $dokterName,
|
|
"Berhasil" => 0,
|
|
"Abandon" => 0,
|
|
"Gagal" => 0,
|
|
];
|
|
}
|
|
|
|
if ($chat->sStatus == "2") {
|
|
$groupedData[$dokterId]["Berhasil"]++;
|
|
} elseif ($chat->sStatus == "1") {
|
|
$groupedData[$dokterId]["Abandon"]++;
|
|
} else {
|
|
$groupedData[$dokterId]["Gagal"]++;
|
|
}
|
|
}
|
|
|
|
// Konversi hasil ke dalam array untuk response JSON
|
|
$result = array_values($groupedData);
|
|
|
|
return response()->json($result);
|
|
}
|
|
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
//
|
|
}
|
|
|
|
public function search(Request $request)
|
|
{
|
|
return Icd::when($request->search ?? null, function($icd, $search) {
|
|
$icd->where('name', 'LIKE', '%'.$search.'%')
|
|
->orWhere('code', 'LIKE', '%'.$search.'%');
|
|
})->limit(10)->get();
|
|
}
|
|
|
|
public function import(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'file' => 'required|file|mimes:xls,xlsx,csv,txt',
|
|
]);
|
|
$file_name = now()->getPreciseTimestamp(3).'-'.$request->file('file')->getClientOriginalName();
|
|
$file = $request->file('file')->storeAs('temp', $file_name);
|
|
|
|
$import = new ImportService();
|
|
$import->read(Storage::path('temp/'.$file_name));
|
|
$import->write(Storage::disk('public')->path('temp/result-'.$file_name), 'xsls');
|
|
|
|
$imported_icd_data = 0;
|
|
$failed_icd_data = [];
|
|
foreach ($import->sheetsIterator() as $sheetIndex => $sheet) {
|
|
$doc_headers_indexes = [];
|
|
foreach ($sheet->getRowIterator() as $index => $row) {
|
|
if ($index == 1) { // First Row Must be Header
|
|
foreach ($row->getCells() as $index => $cell) {
|
|
$title = $cell->getValue();
|
|
$title = preg_replace( "/\r|\n/", " ", $title );
|
|
$title = preg_replace('/\xc2\xa0/', " ", $title );
|
|
$title = rtrim($title);
|
|
$title = ltrim($title);
|
|
$doc_headers_indexes[$index] = $title;
|
|
}
|
|
|
|
// Write Header to File
|
|
$result_headers = array_merge($doc_headers_indexes, ['Ingest Code', 'Ingest Note']);
|
|
$import->addArrayToRow($result_headers);
|
|
|
|
// TODO Validate if First Row not Header
|
|
} else { // Next Row Should be Data
|
|
$row_data = [];
|
|
$row_map = [
|
|
0 => 'ICD_Code',
|
|
1 => 'Description',
|
|
];
|
|
|
|
foreach ($row->getCells() as $header_index => $cell) {
|
|
if (isset($row_map[$header_index])) {
|
|
$value = $cell->getValue();
|
|
$value = preg_replace( "/\r|\n/", " ", $value );
|
|
$value = preg_replace('/\xc2\xa0/', " ", $value );
|
|
$value = rtrim($value);
|
|
$value = ltrim($value);
|
|
$row_data[$row_map[$header_index]] = $cell->getValue();
|
|
}
|
|
}
|
|
|
|
try { // Process the Row Data
|
|
if (
|
|
empty($row_data['ICD_Code']) &&
|
|
empty($row_data['Description'])
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
// Save the Row
|
|
$icdService = new IcdService();
|
|
$icdService->handleIcdRow($row_data, $id);
|
|
|
|
// Write Success Result to File
|
|
$import->addArrayToRow(array_merge($row_data, [
|
|
'Ingest Code' => 200,
|
|
'Ingest Note' => 'Success',
|
|
]), $sheet->getName());
|
|
$imported_icd_data++;
|
|
|
|
} catch (ImportRowException $e) {
|
|
// Write Data Validation Error to File
|
|
$import->addArrayToRow(array_merge($row_data, [
|
|
'Ingest Code' => $e->getCode(),
|
|
'Ingest Note' => $e->getMessage(),
|
|
]), $sheet->getName());
|
|
$failed_icd_data[] = ['row_number' => $index, 'error' => $e->getMessage(), 'data' => $row_data];
|
|
} catch (\Exception $e) {
|
|
throw new \Exception($e);
|
|
// Write Server Error to File
|
|
$import->addArrayToRow(array_merge($row_data, [
|
|
'Ingest Code' => 500,
|
|
'Ingest Note' => env('APP_DEBUG') ? $e->getMessage() : 'Server Error',
|
|
]), $sheet->getName());
|
|
$failed_icd_data[] = ['row_number' => $index, 'error' => $e->getMessage(), 'data' => $row_data];
|
|
}
|
|
}
|
|
}
|
|
|
|
break; // Only Read First Row
|
|
}
|
|
$import->reader->close();
|
|
Storage::delete('temp/'.$file_name);
|
|
$import->writer->close();
|
|
|
|
return [
|
|
'total_successed_row' => $imported_icd_data,
|
|
'total_failed_row' => count($failed_icd_data),
|
|
'failed_row' => $failed_icd_data,
|
|
'result_file' => [
|
|
'url' => Storage::disk('public')->url('temp/result-'.$file_name),
|
|
'name' => 'result-'.$file_name,
|
|
]
|
|
];
|
|
}
|
|
|
|
public function activation(Request $request, $diagnosis_id)
|
|
{
|
|
$request->validate([
|
|
'active' => 'required'
|
|
]);
|
|
|
|
$Icd = Icd::findOrFail($diagnosis_id);
|
|
$Icd->active = $request->active == '1';
|
|
|
|
if ($Icd->save()) {
|
|
return response()->json([
|
|
'icd' => $Icd,
|
|
'message' => 'Status Updated Successfully'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function generateIcdList(Request $request, $diagnosis_id){
|
|
// Mendapatkan data yang akan diekspor (misalnya, dari database)
|
|
$data = Icd::where('icd_template_id', $diagnosis_id)->get()->toArray();
|
|
|
|
// Membuat penulis entitas Spout
|
|
$writer = WriterEntityFactory::createXLSXWriter();
|
|
|
|
// Membuka penulis untuk menulis ke file
|
|
$writer->openToFile(public_path('files/TemplateICDList.xlsx'));
|
|
/** Create a style with the StyleBuilder */
|
|
$style = (new StyleBuilder())
|
|
->setFontBold()
|
|
->build();
|
|
|
|
// Menulis header kolom
|
|
$headers_map_to_table_fields = $this->icdService->listing_doc_headers;
|
|
$headerRow = WriterEntityFactory::createRowFromArray($headers_map_to_table_fields, $style);
|
|
|
|
$writer->addRow($headerRow);
|
|
|
|
// Menulis data
|
|
if (!empty($data)) {
|
|
foreach ($data as $item) {
|
|
$rowData = [
|
|
// $item['rev'], // Rev
|
|
// $item['version'], // Version
|
|
$item['code'], // Code
|
|
// $item['parent_code'], // Parent Code
|
|
$item['name'], // Name
|
|
// $item['description'], // Description
|
|
// $item['active'] == 1 ? 'Active' : 'Inactive', // Status
|
|
// $item['type'], // Type
|
|
];
|
|
|
|
$row = WriterEntityFactory::createRowFromArray($rowData);
|
|
$writer->addRow($row);
|
|
}
|
|
}
|
|
|
|
// Menutup penulis
|
|
$writer->close();
|
|
|
|
// Mengembalikan response untuk mengunduh file
|
|
$filePath = public_path('files/TemplateICDList.xlsx');
|
|
|
|
return Helper::responseJson([
|
|
'file_name' => "Diagnosis ICD List " . date('Y-m-d h:i:s'),
|
|
"file_url" => url('files/TemplateICDList.xlsx')
|
|
]);
|
|
|
|
}
|
|
}
|