120 lines
3.6 KiB
PHP
120 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Internal\Services;
|
|
|
|
use App\Models\Claim;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ClaimService
|
|
{
|
|
public static function checkMemberEligibility($member, $benefit, $diagnosis, $totalClaim = 0)
|
|
{
|
|
$currentPlan = $member->currentPlan;
|
|
$policy = $member->currentPolicy;
|
|
$corporate = $policy->corporate;
|
|
|
|
|
|
$isEligible = true;
|
|
$validationErrors = [];
|
|
|
|
// Eligibility Validation
|
|
|
|
if (!in_array($member->marital_status, explode(',', $benefit->msc))) {
|
|
$validationErrors[] = [
|
|
'error' => 'msc',
|
|
'message' => 'Only '.$benefit->msc
|
|
];
|
|
$isEligible = false;
|
|
}
|
|
|
|
if (!in_array($member->gender_code, explode(',', $benefit->genders))) {
|
|
$validationErrors[] = [
|
|
'error' => 'genders',
|
|
'message' => 'Only '.$benefit->genders
|
|
];
|
|
$isEligible = false;
|
|
}
|
|
|
|
if (!empty($benefit->min_age) && $member->age < $benefit->min_age) {
|
|
$validationErrors[] = [
|
|
'error' => 'min_age',
|
|
'message' => 'Minimum Age is '.$benefit->min_age
|
|
];
|
|
$isEligible = false;
|
|
}
|
|
|
|
if (!empty($benefit->max_age) && $member->age > $benefit->max_age) {
|
|
$validationErrors[] = [
|
|
'error' => 'max_age',
|
|
'message' => 'Maximum Age is '.$benefit->min_age
|
|
];
|
|
$isEligible = false;
|
|
}
|
|
|
|
// TODO complete validations
|
|
|
|
// Limit Validation
|
|
if ($totalClaim > 0) {
|
|
if (bcsub($corporate->limit_balance, $totalClaim) < 0) {
|
|
$validationErrors[] = [
|
|
'error' => 'corporate_limit',
|
|
'message' => 'Corporate Limit cannot cover this'
|
|
];
|
|
$isEligible = false;
|
|
}
|
|
|
|
if (bcsub($benefit->limit_amount, $totalClaim) < 0) {
|
|
$validationErrors[] = [
|
|
'error' => 'benefit_limit',
|
|
'message' => 'Benefit Limit cannot cover this'
|
|
];
|
|
$isEligible = false;
|
|
}
|
|
|
|
// TODO complete validations
|
|
|
|
}
|
|
|
|
return [
|
|
'isEligible' => $isEligible,
|
|
'errors' => $validationErrors
|
|
];
|
|
}
|
|
|
|
public static function storeClaim($member, $diagnosis, $totalClaim, $benefit, $status)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$claimData = [
|
|
'member_id' => $member->id,
|
|
'diagnosis_id' => $diagnosis->id,
|
|
'total_claim' => $totalClaim,
|
|
'currency' => 'IDR',
|
|
'plan_id' => $member->currentPlan->id,
|
|
'benefit_id' => $benefit->id,
|
|
'status' => $status
|
|
];
|
|
$claimData[$status.'_at'] = now();
|
|
$claimData[$status.'_by'] = auth()->user()->id ?? null;
|
|
|
|
$claim = Claim::create($claimData);
|
|
|
|
$policy = $member->currentPolicy;
|
|
$policy->limitJournals()->create([
|
|
'previous_balance' => $policy->limit_balance,
|
|
'total_credit' => $totalClaim,
|
|
'type' => 'credit',
|
|
'balance' => bcsub($policy->limit_balance, $totalClaim),
|
|
'description' => 'Log for Claim #'. $claim->code,
|
|
]);
|
|
|
|
DB::commit();
|
|
return $claim;
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
|
|
throw new \Exception($error);
|
|
}
|
|
}
|
|
} |