Files
aso/Modules/Internal/Http/Controllers/Api/PrescriptionController.php
Server D3 Linksehat 1bf608b1ed Server 103 Commit
2024-07-18 16:05:33 +07:00

312 lines
10 KiB
PHP
Executable File

<?php
namespace Modules\Internal\Http\Controllers\Api;
use App\Helpers\Helper;
use App\Models\OLDLMS\Livechat;
use App\Models\OLDLMS\LivechatSummary;
use App\Models\OLDLMS\Appointment;
use App\Models\OLDLMS\Dokter;
use App\Models\OLDLMS\User;
use App\Models\OLDLMS\UserDetail;
use App\Models\OLDLMS\Prescription;
use App\Models\OLDLMS\PrescriptionItem;
use App\Models\Prescription as PrescriptionAso;
use App\Models\PrescriptionItem as PrescriptionItemAso;
use App\Models\Icd;
use App\Models\Organization;
use App\Models\Drug;
use App\Models\Unit;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Modules\Internal\Transformers\LivechatResource;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;
use Dompdf\Dompdf;
use Dompdf\Options;
use DB;
class PrescriptionController extends Controller
{
/**
* Display a listing of the resource.
* @param int|null $id
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
$startDate = $request->startDate;
$endDate = $request->endDate;
$livechat = Livechat::with('doctor.user', 'doctor.speciality', 'appointment.appointmentDetail', 'healthCare', 'summary')
->where('nTebusObat', '=', 1);
// ->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();
$dokterData = Dokter::where('nIDUser', $livechat->nIDDokter)->first();
$nIDDokter = $dokterData ? $dokterData->nID : $livechat->nIDDokter;
$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' => $nIDDokter,
'sDokterName' => $dokter,
'dTanggalResep' => date('Y-m-d H:i:s'),
'sSource' => 'lms',
'nIDUser' => $livechat->nIDUser,
'sRegID' => '',
'sKodeResep' => $kodeResep,
'sDiagnose' => $sDiagnosis,
'sKodeRS' => $hospital,
'sStatus' => 1, // bayar'
];
$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', $prescription->nID)->delete();
foreach($medicine as $key => $value){
$drugData = Drug::where('id', $value['drug_id'])->first();
$drug = '';
$drugCode = '';
$drugPrice = 0;
if ($drugData){
$drug = $drugData->name;
$drugCode = $drugData->code;
$drugPrice = $drugData->price;
}
$unitData = Unit::where('id', $value['unit_id'])->first();
$unit = '';
if ($unitData) {
$unit = $unitData->name;
}
// Insert Data
$dataAso = [
'prescription_id' => $prescriptionAso->id,
'drug_id' => $value['drug_id'],
'qty' => $value['qty'],
'unit_id' => $value['unit_id'],
'signa' => $value['signa'],
'note' => $value['note']
];
$data = [
'nIDPrescription' => $prescription->nID,
'sItemName' => $drug,
'sItemCode' => $drug,
'sOriginCode' => $drugCode,
'nQty' => $value['qty'],
'sSatuan' => $unit,
'sSigna' => $value['signa'],
'sNote' => $value['note'],
'nHarga' => $drugPrice
];
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)
{
//
}
public function downloadPrescription($id){
$pdf = new Dompdf();
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true);
$options->set(['isRemoteEnabled' => true]);
$pdf->setOptions($options);
$pdf->setPaper('A4', 'portrait');
$livechat = Livechat::with('doctor.user', 'doctor.speciality', 'appointment.appointmentDetail', 'healthCare')
->where('nIDAppointment', '!=', null)->where('nIDAppointment', '!=', '')
->where('nID', $id)
->first();
$prescription = Prescription::where('nIDLivechat', $id)->first();
$valid_date = date('d-m-Y', strtotime($prescription->dTanggalResep . ' +3 days'));
$prescriptionItem = PrescriptionItem::where('nIDPrescription', $prescription->nID)->get();
$user = User::where('nID', $livechat->nIDUser)->first();
$doctor = Dokter::where('nIDUser', $livechat->nIDDokter)->first();
$patient = [
'name' => $user->sFirstName. ' '. $user->sMiddleName. ' '. $user->sLastName,
'tgl_lahir' => date('d-m-Y', strtotime($user->dTanggalLahir)),
'kelamin' => $user->nIDJenisKelamin == 1 ? 'M' : 'F',
'umur' => Helper::calculateAge($user->dTanggalLahir)
];
// Memuat view pdf_view.php ke dalam variabel
$data = [
'doctor' => $doctor,
'items' => $prescriptionItem,
'tanggal_resep' => date('d-m-Y', strtotime($prescription->dTanggalResep)),
'pasien' => $patient,
'valid_date' => $valid_date,
];
// Halaman 1
$html1 = view('pdf.prescription', $data);
$htmlCombined = $html1 ;
$pdf->loadHtml($htmlCombined);
$pdf->render();
$headers = [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="file.pdf"',
];
return response($pdf->output(), 200, $headers);
}
}