fitur import request log dan final log
This commit is contained in:
272
Modules/Internal/Services/RequestLogService.php
Normal file
272
Modules/Internal/Services/RequestLogService.php
Normal file
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Internal\Services;
|
||||
|
||||
use App\Exceptions\ImportRowException;
|
||||
use App\Models\Member;
|
||||
use App\Models\Benefit;
|
||||
use App\Models\RequestLog;
|
||||
use App\Models\RequestLogBenefit;
|
||||
use App\Models\Corporate;
|
||||
use App\Models\Service;
|
||||
use App\Models\Plan;
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
use Box\Spout\Common\Entity\Row;
|
||||
use Carbon\Carbon;
|
||||
use DateTime;
|
||||
use DB;
|
||||
|
||||
class RequestLogService
|
||||
{
|
||||
private static $code_prefix = 'LOG';
|
||||
|
||||
public $doc_headers_to_field_map = [
|
||||
"Date Of Request" => "submission_date",
|
||||
"Date Addmission" => "submission_date",
|
||||
"Member ID Peserta" => "member_id",
|
||||
"Type of patient" => "service",
|
||||
"Provider Name" => "organization_id",
|
||||
"End Of Claim Numbers" => "code",
|
||||
"Remarks" => "keterangan",
|
||||
"Diagnosis" => "catatan",
|
||||
"Tgl Billing dari RS" => "approved_final_log_at",
|
||||
"Benefit Item" => "benefit_id",
|
||||
"Total Billing" => "total_billing",
|
||||
"Amount Approval" => "amount_approval",
|
||||
"Amount Not Approval" => "amount_not_approval",
|
||||
"QC 1" => "status_final_log",
|
||||
"Ingestion Code" => "ingestion_code", // TODO I think this should not be here because if user uploading result then ingestion code and status will be filled
|
||||
"Ingestion Status" => "ingestion_status",
|
||||
|
||||
];
|
||||
|
||||
public $field_to_doc_headers_map = [
|
||||
"submission_date" => "Date Of Request",
|
||||
"submission_date" => "Date Addmission",
|
||||
"member_id" => "Member ID Peserta",
|
||||
"service" => "Type of patient",
|
||||
"organization_id" => "Provider Name",
|
||||
"code" => "End Of Claim Numbers",
|
||||
"keterangan" => "Remarks",
|
||||
"catatan" => "Diagnosis",
|
||||
"approved_final_log_at" => "Tgl Billing dari RS",
|
||||
"benefit_id" => "Benefit Item",
|
||||
"total_billing" => "Total Billing",
|
||||
"amount_approval" => "Amount Approval",
|
||||
"amount_not_approval" => "Amount Not Approval",
|
||||
"status_final_log" => "QC 1" ,
|
||||
"ingestion_code" => "Ingestion Code",
|
||||
"ingestion_status" => "Ingestion Status",
|
||||
];
|
||||
|
||||
public $result_doc_headers = [
|
||||
"Date Of Request",
|
||||
"Date Addmission",
|
||||
"Member ID Peserta",
|
||||
"Type of patient",
|
||||
"Provider Name",
|
||||
"End Of Claim Numbers",
|
||||
"Remarks",
|
||||
"Diagnosis",
|
||||
"Tgl Billing dari RS",
|
||||
"Benefit Item",
|
||||
"Total Billing",
|
||||
"Amount Approval",
|
||||
"Amount Not Approval",
|
||||
"QC 1",
|
||||
"Ingestion Code",
|
||||
"Ingestion Status",
|
||||
];
|
||||
|
||||
public $listing_doc_headers = [
|
||||
"Date Of Request",
|
||||
"Date Addmission",
|
||||
"Member ID Peserta",
|
||||
"Type of patient",
|
||||
"Provider Name",
|
||||
"End Of Claim Numbers",
|
||||
"Remarks",
|
||||
"Diagnosis",
|
||||
"Tgl Billing dari RS",
|
||||
"Benefit Item",
|
||||
"Total Billing",
|
||||
"Amount Approval",
|
||||
"Amount Not Approval",
|
||||
"QC 1",
|
||||
"Ingestion Code",
|
||||
"Ingestion Status",
|
||||
];
|
||||
|
||||
public function dateParser($date_from_row) {
|
||||
|
||||
if ($date_from_row instanceof DateTime) {
|
||||
return $date_from_row->format('Y-m-d');
|
||||
} else if ($date_from_row != null) {
|
||||
return date('Y-m-d', strtotime($date_from_row));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function dateParserCode($date_from_row) {
|
||||
|
||||
if ($date_from_row instanceof DateTime) {
|
||||
return $date_from_row->format('ymd');
|
||||
} else if ($date_from_row != null) {
|
||||
return date('ymd', strtotime($date_from_row));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function handleImportRow($row)
|
||||
{
|
||||
try {
|
||||
// Memulai transaksi
|
||||
DB::beginTransaction();
|
||||
$member = Member::with('currentCorporate')->where(['member_id' => $row['member_id']])->first();
|
||||
|
||||
// // Validate If Exist Member
|
||||
if (!$member) {
|
||||
throw new ImportRowException(__('MEMBER_NOT_FOUND', [
|
||||
'member_id' => $row['member_id'],
|
||||
]), 0, null, $row);
|
||||
}
|
||||
// Membuat singkatan dari nama rumah sakit
|
||||
$singkatan = "";
|
||||
$words = explode(' ', $row['organization_id']);
|
||||
|
||||
foreach ($words as $word) {
|
||||
$singkatan .= strtoupper(substr($word, 0, 1));
|
||||
}
|
||||
|
||||
// Membuat kode organisasi
|
||||
$kodeOrganisasi = "ORG000" . $singkatan;
|
||||
// Insert data ke tabel organizations
|
||||
$organization = DB::table('organizations')->where('code', $kodeOrganisasi)->first();
|
||||
// dd( $organization);
|
||||
if ($organization) {
|
||||
$organization_id = $organization->id;
|
||||
} else {
|
||||
$organization_id = DB::table('organizations')
|
||||
->insertGetId([
|
||||
'name' => $row['organization_id'],
|
||||
'code' => $kodeOrganisasi,
|
||||
'type' => 'hospital',
|
||||
'created_at' => now(),
|
||||
'created_by' => auth()->user()->id
|
||||
]);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'source' => 'H',
|
||||
'provideCode' => $kodeOrganisasi ,
|
||||
'date' => $this->dateParserCode($row['submission_date']),
|
||||
'policy' => $member->currentPolicy->code,
|
||||
'member_code' => $row['member_id']
|
||||
];
|
||||
|
||||
$code = $this->makeCode($row['code'], $data);
|
||||
$status = $row['status_final_log'] == 'Y' ? 'approved' : 'requested';
|
||||
$service = Service::where('name', $row['service'])->first();
|
||||
if ($service){
|
||||
$serviceCode = $service->code;
|
||||
} else {
|
||||
$serviceCode = 'Unk';
|
||||
}
|
||||
|
||||
$benefit = Benefit::where('code', $row['benefit_id'])->first();
|
||||
|
||||
$requestLog = RequestLog::updateOrCreate(
|
||||
[
|
||||
'code' => $code
|
||||
],
|
||||
[
|
||||
'code' => $code,
|
||||
'member_id' => $member->id,
|
||||
'submission_date' => $row['submission_date'],
|
||||
'discharge_date' => $row['submission_date'],
|
||||
'payment_type' => 'cashless',
|
||||
'status' => $status,
|
||||
'status_final_log' => $status,
|
||||
'final_log' =>$row['status_final_log'] == 'Y' ? 1 : 0,
|
||||
'import_system' =>TRUE,
|
||||
'catatan' => $row['catatan'],
|
||||
'policy_id' => $member->currentPolicy->id ?? null,
|
||||
'organization_id' => $organization_id,
|
||||
'service_code' => $serviceCode,
|
||||
'approved_final_log_at' => $row['approved_final_log_at'],
|
||||
]);
|
||||
|
||||
$requestLogBenefit = RequestLogBenefit::updateOrCreate(
|
||||
[
|
||||
'request_log_id' => $requestLog->id,
|
||||
],
|
||||
[
|
||||
'request_log_id' => $requestLog->id,
|
||||
'benefit_id' => $benefit->id,
|
||||
'amount_incurred' => $row['total_billing'],
|
||||
'amount_approved' => $row['amount_approval'],
|
||||
'amount_not_approved' => $row['amount_not_approval'],
|
||||
'excess_paid' => $row['amount_not_approval'],
|
||||
'created_by' => auth()->user()->id,
|
||||
|
||||
]);
|
||||
|
||||
// Commit transaksi
|
||||
DB::commit();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
throw new ImportRowException($e->getMessage(), $e->getCode(), $e, $row);
|
||||
}
|
||||
|
||||
$row['ingestion_code'] = "200";
|
||||
$row['ingestion_status'] = "SUCCESS";
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
// This returning row with format or order as it is
|
||||
public function makeResultRow($row_data)
|
||||
{
|
||||
$cells = [];
|
||||
foreach ($row_data as $cellValue) {
|
||||
$cells[] = WriterEntityFactory::createCell($cellValue);
|
||||
}
|
||||
|
||||
return $cells;
|
||||
}
|
||||
|
||||
// This returning row with format or order following $result_doc_headers
|
||||
public function makeResultRowWithResultFormat($row_data)
|
||||
{
|
||||
$cells = [];
|
||||
foreach ($this->result_doc_headers as $header) {
|
||||
$value = $row_data[$this->doc_headers_to_field_map[$header]] ?? null;
|
||||
if (is_string($value)) {
|
||||
$cells[] = WriterEntityFactory::createCell($value);
|
||||
}
|
||||
else if ($value instanceof DateTime) {
|
||||
$cells[] = WriterEntityFactory::createCell(Carbon::parse($value)->format('Ymd'));
|
||||
}
|
||||
else {
|
||||
$cells[] = WriterEntityFactory::createCell($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $cells;
|
||||
}
|
||||
|
||||
|
||||
public function makeCode($next_number, $data)
|
||||
{
|
||||
$sparator = '.';
|
||||
// Pastikan $next_number adalah integer positif
|
||||
$next_number = max(1, (int) $next_number);
|
||||
// Menghasilkan kode dengan format yang diinginkan
|
||||
return self::$code_prefix . $sparator. $data['source'] . $sparator. $data['provideCode'] . $sparator. $data['date'] . $sparator . $data['policy'] . $sparator. $data['member_code'] . $sparator. str_pad($next_number, 5, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user