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

100 lines
3.0 KiB
PHP

<?php
namespace Modules\Internal\Services;
use App\Exceptions\ImportRowException;
use App\Models\Benefit;
use App\Models\Corporate;
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([
'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['benefit_code'])) {
throw new ImportRowException(__('benefit.BENEFIT_CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['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 = Benefit::updateOrCreate([
'code' => $benefit_data['code'],
], $benefit_data);
return $plan;
} catch (\Exception $e) {
throw $e;
}
}
}