[WIP] Store Limit
This commit is contained in:
115
Modules/Internal/Services/ClaimService.php
Normal file
115
Modules/Internal/Services/ClaimService.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?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)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$claim = Claim::create([
|
||||
'member_id' => $member->id,
|
||||
'diagnosis_id' => $diagnosis->id,
|
||||
'total_claim' => $totalClaim,
|
||||
'currency' => 'IDR',
|
||||
'plan_id' => $member->currentPlan->id,
|
||||
'benefit_id' => $benefit->id,
|
||||
]);
|
||||
|
||||
$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();
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user