Files
aso/app/Services/ClaimRequestService.php
Server D3 Linksehat 1bf608b1ed Server 103 Commit
2024-07-18 16:05:33 +07:00

244 lines
9.9 KiB
PHP
Executable File

<?php
namespace App\Services;
use App\Events\ClaimApproved;
use App\Events\ClaimRequested;
use App\Models\Claim;
use App\Models\ClaimRequest;
use App\Models\RequestLog;
use App\Models\Benefit;
use App\Models\RequestLogBenefit;
use App\Models\Organization;
use App\Helpers\Helper;
use App\Models\Icd;
use App\Models\Member;
use Carbon\Carbon;
use App\Exceptions\ImportRowException;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use DB;
use Str;
class ClaimRequestService{
private static $code_prefix = 'CLAIM';
public static function storeClaimRequest($row = null, $code, $member, $paymentType, $serviceCode, $requestLogID = null, $submissionDate = null, $status = 'requested', $organization_code = null)
{
// try {
$organization = False;
if($organization_code){
$organization = Organization::where('code', $organization_code)->first();
if (!$organization){
throw new ImportRowException(__('Code Provider Tidak ditemukan', [
'attribute' => 'provider_code',
'code' => $row['provider_code']
]), 403, null, $row);
}
}
DB::beginTransaction();
if ($status == 'submission'){
$claimManagement = 1;
$submissionDateClaimManagement = $submissionDate;
$submissionByClaimManagement = auth()->user()->id;
$statusClaim = 'received';
} else {
$claimManagement = 0;
$submissionDateClaimManagement = null;
$submissionByClaimManagement = null;
$statusClaim = null;
}
if (count($row)>0){
if($row['total_billing']) {
$data = [
'code' => $code,
'request_log_id' => $requestLogID ?? 0,
'member_id' => $member->id,
'submission_date' => $submissionDate ?? now(),
'status' => $status,
'claim_management' => $claimManagement,
'status_claim_management' => $statusClaim,
'submission_date_claim_management' => $submissionDateClaimManagement,
'submission_by_claim_management' => $submissionByClaimManagement,
'payment_type' => $paymentType,
'service_code' => $serviceCode,
'policy_id' => $member->currentPolicy->id ?? null,
'organization_id' => $organization ? $organization->id : 0,
];
} else {
$data = [];
}
$claimRequest = ClaimRequest::updateOrCreate(['request_log_id' => $requestLogID],$data);
$benefitData = Benefit::where('code', $row['benefit_code'])->first();
$requestLogData = RequestLogBenefit::updateOrCreate(
[
'request_log_id' => $requestLogID,
'benefit_id' => $benefitData->id,
],[
'request_log_id' => $requestLogID,
'benefit_id' => $benefitData->id,
'amount_incurred' => $row['amount_incurred'],
'amount_approved' => $row['amount_apporve'],
'amount_not_approved' => $row['amount_not_apporve'],
'excess_paid' => $row['excess_paid'],
]);
} else { // input from hospital portal
$data = [
'code' => $code,
'request_log_id' => $requestLogID ?? 0,
'member_id' => $member->id,
'submission_date' => $submissionDate ?? now(),
'status' => $status,
'claim_management' => $claimManagement,
'status_claim_management' => $statusClaim,
'submission_date_claim_management' => $submissionDateClaimManagement,
'submission_by_claim_management' => $submissionByClaimManagement,
'payment_type' => $paymentType,
'service_code' => $serviceCode,
'policy_id' => $member->currentPolicy->id ?? null,
'organization_id' => $organization ? $organization->id : 0,
];
$claimRequest = ClaimRequest::updateOrCreate(['request_log_id' => $requestLogID],$data);
}
DB::commit();
return $claimRequest;
// } catch (\Exception $error) {
// DB::rollBack();
// throw new \Exception($error);
// }
}
public static function storeClaimManagement($row, $member, $claim_request_id){
try {
$organization = 0;
if($row['provider_code']){
$organization = Organization::where('code', $row['provider_code'])->first();
if (!$organization){
throw new ImportRowException(__('Provider Tidak ditemukan'), 0, null, $row);
}
};
if(!$member){
throw new ImportRowException(__('Member Tidak ditemukan'), 0, null, $row);
};
DB::beginTransaction();
$data = [
'member_id' => $member->id,
'currency' => 'IDR',
'plan_id' => $member->currentPlan->id,
'total_claim' => $row['tot_amt_insurred'] ? $row['tot_amt_insurred'] : 0,
'benefit_code' => $row['benefit_code'] ? $row['benefit_code'] : '-',
'benefit_desc' => $row['benefit_desc'] ? $row['benefit_desc'] : '-',
'amount_incurred' => $row['tot_amt_insurred'] ? $row['tot_amt_insurred'] : 0,
'amount_approved' => $row['tot_amt_approved'] ? $row['tot_amt_approved'] : 0,
'amount_not_approved' => $row['tot_amt_not_approved'] ? $row['tot_amt_not_approved'] :0,
'excess_paid' => $row['tot_excess_paid'] ? $row['tot_excess_paid'] : 0,
'claim_request_id' => $claim_request_id,
'organization_id' => $organization ? $organization->id : NULL,
'status' => 'requested'
];
$claimManagement = Claim::create($data);
// update client id di claim request
ClaimRequest::where('id', $claim_request_id)->update([
'claim_id' => $claimManagement->id,
]);
DB::commit();
return $claimManagement;
} catch (\Exception $error) {
DB::rollBack();
throw new \Exception($error);
}
}
public static function updateClaimRequest($reason, $submission_date, $claim_request_id){
try {
$claimRequest = ClaimRequest::find($claim_request_id);
if (!$claimRequest) {
throw new \Exception("Claim request not found");
}
$claimRequest->submission_date = $submission_date;
$claimRequest->reason = $reason;
$claimRequest->save();
return $claimRequest;
} catch (\Exception $error) {
throw new \Exception($error->getMessage());
}
}
public function handleClaimRequestRow($row)
{
try {
$requestLog = RequestLog::where('code', $row['code'])->first();
if(!$requestLog){
throw new ImportRowException(__('LOG Tidak ditemukan'), 0, null, $row);
};
if ($requestLog->status != 'approved' && $requestLog != 'approved'){
throw new ImportRowException(__('Request LOG / Final LOG Belum di Approved'), 0, null, $row);
}
$organization = Organization::where('id', $requestLog->organization_id)->first();
// Create Code
$last_numeric_code = ClaimRequest::select(DB::raw('MAX(CAST(SUBSTRING_INDEX(code, ".", -1) AS SIGNED)) as max_numeric_code'))
->whereRaw('SUBSTRING_INDEX(code, ".", -1) REGEXP "^[0-9]+$"')
->value('max_numeric_code');
// $next_number = 1;
if ($last_numeric_code) {
// // Jika ada kode sebelumnya, pecah kode dan tambahkan 1 ke angka terakhir
// $parts = explode('-', $last_code);
// $last_number = (int) end($parts);
$next_number = $last_numeric_code + 1;
} else {
$next_number = 1;
}
// make code
$member = Member::with('currentCorporate')->where(['id' => $requestLog->member_id])->first();
$sparator = '.';
$date = date('ymd');
// Menghasilkan kode dengan format yang diinginkan
$code = 'CLAIM' . $sparator. 'I' . $sparator. $organization->code . $sparator. $date. $sparator . $member->currentPolicy->code . $sparator. $member->member_id . $sparator. str_pad($next_number, 5, '0', STR_PAD_LEFT);
$submissionDate = Helper::dateParser($row['date_submission']);
$paymentType = $requestLog->payment_type;
if ($row['qc'] == 'Y'){
$status = 'submission';
} else {
$status = 'requested';
}
$serviceCode = $requestLog->service_code;
$newClaimRequest = $this->storeClaimRequest(
row: $row,
code: $code,
member: $member,
paymentType: $paymentType,
serviceCode: $serviceCode,
requestLogID: $requestLog->id,
submissionDate: $submissionDate,
status: $status,
organization_code: $organization->code
);
$newlyCreatedID = $newClaimRequest->id;
return $newClaimRequest;
} catch (\Exception $e) {
throw $e;
}
}
}