Files
aso/Modules/Internal/Services/CorporateService.php

219 lines
7.7 KiB
PHP
Executable File

<?php
namespace Modules\Internal\Services;
use App\Exceptions\ImportRowException;
use App\Models\Benefit;
use App\Models\Corporate;
use App\Models\CorporateBenefit;
use App\Models\CorporatePolicy;
use App\Models\Plan;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
class CorporateService
{
protected function validatePlanRow($row)
{
// $corporate_policy = CorporatePolicy::where('corporate_id', $corporateId)->get();
/*
Refer to Coverage code :
MEDIVAC = Medical evacuation
MSO = Medical second opinion
PAC = Personal Assistant Concierge
OPDE = Outpatient Dental
IP=Inpatient
OP=Outpatient
MA=Maternity
ANC = Ante/Post Natal Care
DE=Dental
GL=Glasses
SP=Special Treatment
PF=Pooled Fund
MCU=Medical Check Up
KB-VACC = Family Planning/Vaccination
LAB = Test Diagnostic
PHAR = Pharmacy
**/
$service_code = ['MEDIVAC', 'MSO', 'PAC', 'OPDE', 'IP', 'OP', 'MA', 'ANC', 'DE', 'GL', 'SP', 'PF', 'MCU', 'KB-VACC', 'LAB', 'PHAR'];
$plan_code = [1, 2, 3, 4];
$prorate_type = [0, 1, 2];
$family_plan = ['F', 'S', 'N'];
if (empty($row['service_code'])) {
throw new ImportRowException(__('plan.REQUIRED', [
'attribute' => 'Service Code'
] ), 0, null, $row);
} else if (!in_array($row['service_code'], $service_code)){
throw new ImportRowException(__('plan.NOT_MATCH', [
'attribute' => 'Service Code',
'code' => $row['service_code']
]), 0, null, $row);
}
if (empty($row['corporate_plan_id'])) {
throw new ImportRowException(__('plan.REQUIRED', [
'attribute' => 'Plan'
]), 0, null, $row);
}
if (empty($row['code'])) {
throw new ImportRowException(__('plan.REQUIRED', [
'attribute' => 'Customer Plan'
]), 0, null, $row);
}
if (!in_array($row['type'], $plan_code)){
throw new ImportRowException(__('plan.NOT_MATCH', [
'attribute' => 'Plan Type',
'code' => $row['type']
]), 0, null, $row);
} else if ($row['type'] == 2 && $row['limit_rules'] != 999999999){
throw new ImportRowException(__('plan.LIMIT_PLAN'), 0, null, $row);
}
// if (!empty($row['start'])) {
// throw new ImportRowException(__('plan.REQUIRED', [
// 'attribute' => 'Start Date of Plan'
// ]), 0, null, $row);
// }
// if (empty($row['type'])) {
// throw new ImportRowException(__('plan.TYPE_REQUIRED'), 0, null, $row);
// } else if (!in_array($row['type'],$plan_code)){
// throw new ImportRowException(__('plan.NOT_MATCH', [
// 'attribute' => 'Plan Type',
// 'code' => $row['code']
// ]), 0, null, $row);
// }
if (!empty($row['family_plan']) && !in_array($family_plan)) {
throw new ImportRowException(__('plan.NOT_MATCH', [
'attribute' => 'Family Plan',
'code' => $row['family_plan']
]), 0, null, $row);
}
if (!empty($row['family_plan']) && !in_array($family_plan)) {
throw new ImportRowException(__('plan.NOT_MATCH', [
'attribute' => 'Family Plan',
'code' => $row['family_plan']
]), 0, null, $row);
}
if (!empty($row['prorate_type']) && !in_array($prorate_type)) {
throw new ImportRowException(__('plan.NOT_MATCH', [
'attribute' => 'Prorate Type',
'code' => $row['prorate_type']
]), 0, null, $row);
}
if (!empty($row['prorate_lookup']) && !in_array($prorate_type)) {
throw new ImportRowException(__('plan.NOT_MATCH', [
'attribute' => 'Prorate Lookup',
'code' => $row['prorate_lookup']
]), 0, null, $row);
}
if (empty($row['limit_rules'])) {
throw new ImportRowException(__('plan.PLAN_LIMIT_REQUIRED'), 0, null, $row);
}
if (empty($row['msc'])) {
throw new ImportRowException(__('plan.MSC_REQUIRED'), 0, null, $row);
}
}
public function handlePlanRow(Corporate $corporate, $row)
{
try {
$plan_data = $row;
$this->validatePlanRow($plan_data);
$plan_data["corporate_id"] = $corporate->id;
$plan = $corporate->plans()->updateOrCreate([
'corporate_plan_id' => $plan_data['corporate_plan_id'],
// 'active' => 0,
], $plan_data);
return $plan;
} catch (\Exception $e) {
throw $e;
}
}
protected function validateBenefitRow($row)
{
$max_frequence = 7;
$budget_aso = [1,2];
if (empty($row['service_code'])) {
throw new ImportRowException(__('benefit.SERVICE_CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['plan_code'])) {
throw new ImportRowException(__('benefit.PLAN_CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['code'])) {
throw new ImportRowException(__('benefit.BENEFIT_CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['corporate_benefit_code'])) {
throw new ImportRowException(__('benefit.CUSTOMER_BENEFIT_CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['description'])) {
throw new ImportRowException(__('benefit.DESCRIPTION_REQUIRED'), 0, null, $row);
}
if (empty($row['limit_amount'])) {
throw new ImportRowException(__('benefit.LIMIT_AMOUNT_REQUIRED'), 0, null, $row);
}
if (empty($row['msc'])) {
throw new ImportRowException(__('benefit.MSC_REQUIRED'), 0, null, $row);
}
if (empty($row['genders'])) {
throw new ImportRowException(__('benefit.GENDER_REQUIRED'), 0, null, $row);
}
if (!empty($row['max_frequency_period']) && $row['max_frequency_period'] >= $max_frequence){ // jenis frequence
throw new ImportRowException(__('benefit.MAX_FREQUENCY'), 0, null, $row);
}
if (!empty($row['budget']) && !in_array($budget_aso)) {
}
}
public function handleBenefitRow(Corporate $corporate, $row)
{
try {
$benefit_data = $row;
$benefit_data["corporate_id"] = $corporate->id;
$this->validateBenefitRow($benefit_data);
$plan = $corporate->plans()
->where('corporate_plan_id', $benefit_data['plan_code'])
->first();
// $corporate->plans->where('corporate_plan_id', $benefit_data['plan_code'])->first();
$benefit_data['plan_code'] = $plan->id;
$benefit = Benefit::updateOrCreate([
'code' => $benefit_data['code'],
'service_code' => $plan->service_code,
], [
'code' => $benefit_data['code'],
'service_code' => $plan->service_code,
'active' => 1,
'description' => $benefit_data['description'],
]);
$corporateBenefit = $corporate->corporateBenefits()->updateOrCreate([
'benefit_id' => $benefit->id,
'plan_id' => $plan->id
], $benefit_data);
return $corporateBenefit;
} catch (\Exception $e) {
// dd($e->getMessage());
throw $e;
}
}
}