update create e-prescription

This commit is contained in:
2024-04-30 10:11:11 +07:00
parent 2c4fe723dc
commit 8aa67c1864
19 changed files with 2234 additions and 28 deletions

View File

@@ -2,11 +2,30 @@
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\User;
use App\Models\OLDLMS\UserDetail;
use App\Models\OLDLMS\Prescription;
use App\Models\OLDLMS\PrescriptionItem;
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 DB;
class PrescriptionController extends Controller
{
@@ -15,21 +34,30 @@ class PrescriptionController extends Controller
* @param int|null $id
* @return \Illuminate\Http\JsonResponse
*/
public function index($id = null)
public function index(Request $request)
{
$query = Prescription::query();
if ($id !== null) {
$query->where('nID', $id);
$startDate = $request->startDate;
$endDate = $request->endDate;
$livechat = Livechat::with('doctor.user', 'doctor.speciality', 'appointment.appointmentDetail', 'healthCare', 'summary');
// ->where('nIDAppointment', '!=', null)
// ->where('nIDAppointment', '!=', '');
if ($startDate) {
$livechat = $livechat->where('dCreateOn', '>=', $startDate);
}
$prescriptions = $query->select('nID','nIDLiveChat', 'nIDLiveChatSummary', 'nIDDokter', 'sDokterName', 'dTanggalResep', 'sSource', 'nIDUser', 'sKodeResep', 'sDiagnose', 'sStatus')
->get();
// $prescriptions->toArray();
// dd($prescriptions);
if ($endDate) {
$endDate = date('Y-m-d', strtotime($endDate . ' +1 day'));
$livechat = $livechat->where('dCreateOn', '<', $endDate);
}
return response()->json($prescriptions);
// return response()->json(Helper::paginateResources(LivechatResource::collection($livechat)));
$livechat = $livechat->whereHas('summary', function ($query) {
$query->whereNotNull('nIDLiveChat');
});
$livechat = $livechat->latest()->paginate(15);
return response()->json(Helper::paginateResources(LivechatResource::collection($livechat)));
}
@@ -51,6 +79,94 @@ class PrescriptionController extends Controller
*/
public function store(Request $request)
{
$livechat = Livechat::where('nID', $request->id)->first();
$livechatSummary = LivechatSummary::where('nIDLivechat', $request->id)->first();
$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' => $livechat->nIDDokter,
'sDokterName' => $dokter,
'dTanggalResep' => date('Y-m-d H:i:s'),
'sSource' => 'lms',
'nIDUser' => $livechat->nIDUser,
'sRegID' => '',
'sKodeResep' => $kodeResep,
'sDiagnose' => $sDiagnosis,
'sKodeRS' => $hospital,
];
$prescription = Prescription::create($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
DB::beginTransaction();
foreach($medicine as $key => $value){
$drugData = Drug::where('id', $value['drug_id'])->first();
$drug = '';
if ($drugData){
$drug = $drugData->name;
}
$unitData = Unit::where('id', $value['unit_id'])->first();
$unit = '';
if ($unitData) {
$unit = $unitData->name;
}
$data = [
'nIDPrescription' => $prescription->id,
'sItemName' => $drug,
'nQty' => $value['qty'],
'sSatuan' => $unit,
'sSigna' => $value['signa'],
'sNote' => $value['note'],
];
// Insert Data
try {
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);
}
/**