Files
aso/Modules/Internal/Services/CorporateService.php
2022-12-19 09:33:34 +07:00

116 lines
3.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\Plan;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
class CorporateService
{
protected function validatePlanRow($row)
{
if (empty($row['service_code'])) {
throw new ImportRowException(__('plan.RECORD_TYPE_REQUIRED'), 0, null, $row);
}
if (empty($row['code'])) {
throw new ImportRowException(__('plan.CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['type'])) {
throw new ImportRowException(__('plan.TYPE_REQUIRED'), 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;
$plan_data["corporate_id"] = $corporate->id;
$this->validatePlanRow($plan_data);
$plan = Plan::updateOrCreate([
'service_code' => $plan_data['service_code'],
'corporate_id' => $corporate->id,
'code' => $plan_data['code'],
], $plan_data);
return $plan;
} catch (\Exception $e) {
throw $e;
}
}
protected function validateBenefitRow($row)
{
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);
}
}
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();
$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' => true
]);
$corporateBenefit = $corporate->corporateBenefits()->updateOrCreate([
'benefit_id' => $benefit->id,
'plan_id' => $plan->id
], $benefit_data );
return $corporateBenefit;
} catch (\Exception $e) {
dd($e->getMessage());
throw $e;
}
}
}