152 lines
4.6 KiB
PHP
152 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Events\ClaimApproved;
|
|
use App\Events\ClaimRequested;
|
|
use App\Models\Claim;
|
|
use App\Models\ClaimRequest;
|
|
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{
|
|
|
|
public static function storeClaimRequest($code, $member, $paymentType, $serviceCode, $submissionDate = null, $status = 'requested')
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$claimRequestData = [
|
|
'code' => $code,
|
|
'member_id' => $member->id,
|
|
'submission_date' => $submissionDate ?? now(),
|
|
'status' => $status,
|
|
'payment_type' => $paymentType,
|
|
'service_code' => $serviceCode,
|
|
'policy_id' => $member->currentPolicy->id ?? null,
|
|
];
|
|
|
|
$claimRequest = ClaimRequest::create($claimRequestData);
|
|
|
|
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'],
|
|
'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(){
|
|
try {
|
|
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
|
|
throw new \Exception($error);
|
|
}
|
|
}
|
|
|
|
protected function validatePlanRow($row)
|
|
{
|
|
if (empty($row['member_id'])) {
|
|
throw new ImportRowException(__('Member ID Required'), 0, null, $row);
|
|
}
|
|
}
|
|
|
|
public function handleClaimRequestRow($row)
|
|
{
|
|
try {
|
|
$member = Member::where('member_id', $row['member_id'])->with(['currentPlan'])->first();
|
|
if(!$member){
|
|
throw new ImportRowException(__('Member Tidak ditemukan'), 0, null, $row);
|
|
};
|
|
$code = $row['client_claim_id'];
|
|
$submissionDate = Helper::formatDateDB($row['admission_date']);
|
|
$paymentType = $row['claim_type'];
|
|
$status = $row['status'];
|
|
$serviceCode = $row['coverage_type'];
|
|
|
|
$newClaimRequest = $this->storeClaimRequest(code: $code, member: $member, paymentType: $paymentType, serviceCode: $serviceCode, submissionDate: $submissionDate, status: $status);
|
|
|
|
$newlyCreatedID = $newClaimRequest->id;
|
|
|
|
$newClaimManangement = $this->storeClaimManagement($row, $member, $newlyCreatedID);
|
|
ClaimRequested::dispatch($newClaimRequest);
|
|
// Log History
|
|
$newClaimRequest->histories()->create([
|
|
'title' => 'New Claim Requested',
|
|
'description' => "Claim Requested for Member : {$member->member_id} - ({$member->full_name})",
|
|
'type' => 'info',
|
|
'system_origin' => 'import-internal-aso'
|
|
]);
|
|
|
|
$claim_request_data = $row;
|
|
|
|
// dd($claim_request_data['admission_date']);
|
|
|
|
$this->validatePlanRow($claim_request_data);
|
|
|
|
|
|
|
|
|
|
return $newClaimRequest;
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
} |