244 lines
8.3 KiB
PHP
244 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\Internal\Http\Controllers\Api\Linksehat;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Models\OLDLMS\Rujukan;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Internal\Transformers\ReportRujukanResource;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
|
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
|
|
|
class RujukanController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
* @return Renderable
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
// return $request->toArray();
|
|
$rujukan = Rujukan::query()
|
|
->with(['livechat', 'livechat.user']);
|
|
|
|
if ($request->has('search')) {
|
|
$search = $request->search;
|
|
$rujukan->where(function ($query) use ($search) {
|
|
$query->where('nID', $search)
|
|
->orWhereHas('user', function ($detail) use ($search) {
|
|
$detail->where('sFirstName', 'LIKE', '%' . $search . '%')
|
|
->orWhere('sLastName', 'LIKE', '%' . $search . '%');
|
|
})
|
|
->orWhereHas('healthCare', function ($detail) use ($search) {
|
|
$detail->where('sHealthCare', 'LIKE', '%' . $search . '%');
|
|
});
|
|
});
|
|
}
|
|
|
|
if (($request->has('rujukan_start') || $request->has('rujukan_end'))
|
|
&& !empty($request->rujukan_start)
|
|
&& !empty($request->rujukan_end)
|
|
) {
|
|
|
|
|
|
$rujukan = $rujukan->where(function($q) use ($request) {
|
|
$q->where('dCreateOn', '>=', $request->rujukan_start)
|
|
->where('dCreateOn', '<=', $request->rujukan_end);
|
|
});
|
|
}
|
|
|
|
if ($request->has('healthcare_id') && !empty($request->healthcare_id)) {
|
|
$rujukan->where('nIDHealthCare', $request->healthcare_id);
|
|
}
|
|
|
|
$rujukans = $rujukan->orderBy('dUpdateOn', 'DESC')
|
|
->paginate();
|
|
|
|
return Helper::responseJson(Helper::paginateResources(ReportRujukanResource::collection($rujukans)));
|
|
}
|
|
|
|
/**
|
|
* 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 Riwayat Rekam Medis';
|
|
// Membuat penulis entitas Spout
|
|
$writer = WriterEntityFactory::createXLSXWriter();
|
|
// Membuka penulis untuk menulis ke file
|
|
$writer->openToFile(public_path('files/Report-Riwayat-Rekam-Medis.xlsx'));
|
|
$headerArray = [
|
|
'Healthcare',
|
|
'Patient',
|
|
'Doctor',
|
|
'Speciality',
|
|
'Date',
|
|
'Keluhan (Subjective)',
|
|
'Pemerikasan Fisik Online (Objective)',
|
|
'Diagnosa (Assessment)',
|
|
'Tata Laksana (Plan)',
|
|
];
|
|
// Sheet 1
|
|
$writer->getCurrentSheet()->setName('Data');
|
|
$headers_map_to_table_fields = $headerArray;
|
|
$headerRow = WriterEntityFactory::createRowFromArray($headers_map_to_table_fields);
|
|
$writer->addRow($headerRow);
|
|
$rujukans = Livechat::query()
|
|
->with([ 'user', 'doctor', 'doctor.user', 'doctor.user.detail', 'doctor.speciality', 'appointment', 'healthCare', 'summary']);
|
|
|
|
if ($request->has('search')) {
|
|
$search = $request->search;
|
|
$rujukans->where(function ($query) use ($search) {
|
|
$query->where('nID', $search)
|
|
->orWhereHas('user', function ($detail) use ($search) {
|
|
$detail->where('sFirstName', 'LIKE', '%' . $search . '%')
|
|
->orWhere('sLastName', 'LIKE', '%' . $search . '%');
|
|
})
|
|
->orWhereHas('healthCare', function ($detail) use ($search) {
|
|
$detail->where('sHealthCare', 'LIKE', '%' . $search . '%');
|
|
});
|
|
});
|
|
}
|
|
|
|
if (($request->has('rujukan_start') || $request->has('rujukan_end'))
|
|
&& !empty($request->rujukan_start)
|
|
&& !empty($request->rujukan_end)
|
|
) {
|
|
$rujukans = $rujukans->where(function($q) use ($request) {
|
|
$q->where('dCreateOn', '>=', $request->rujukan_start)
|
|
->where('dCreateOn', '<=', $request->rujukan_end);
|
|
});
|
|
}
|
|
|
|
$rujukans = $rujukans->get();
|
|
|
|
if ($rujukans){
|
|
foreach ($rujukans as $index => $row) {
|
|
$doctor_name = '-';
|
|
if ($row->doctor && $row->doctor->user && $row->doctor->user->detail) {
|
|
$doctor_name = $row->doctor->user->detail->sTitlePrefix . ' ' . $row->doctor->user->fullname;
|
|
}
|
|
$speciality = $row->doctor->speciality->sSpesialis ?? '-';
|
|
// Process the plan
|
|
$plan = '-';
|
|
if ($row->summary && $row->summary->sPlan) {
|
|
$plan = $this->processPlan($row->summary->sPlan);
|
|
}
|
|
|
|
$rowData = [
|
|
$row->healthCare ? $row->healthCare->sHealthCare : '-',
|
|
$row->user ? $row->user->sFirstName : '-',
|
|
$doctor_name,
|
|
$speciality,
|
|
$row->summary ? Carbon::parse($row->dCreateOn)->format('Y-m-d H:i:s') : '-',
|
|
$row->summary ? $row->summary->sSubjective : '-',
|
|
$row->summary ? $row->summary->sObjective : '-',
|
|
$row->summary ? $row->summary->sAssessment : '-',
|
|
is_array($plan) ? implode(', ', $plan) : $plan, // Handle arrays from unserialized or JSON data
|
|
];
|
|
|
|
// 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 Riwayat Log " . date('Y-m-d h:i:s'),
|
|
"file_url" => url('files/Report-Riwayat-Rekam-Medis.xlsx')
|
|
]);
|
|
}
|
|
}
|