Change Benefit Table Structure

This commit is contained in:
R
2022-12-19 09:33:34 +07:00
parent 1baaf80b2b
commit 7c4886d57f
9 changed files with 407 additions and 312 deletions

View File

@@ -5,6 +5,7 @@ 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;
@@ -61,10 +62,10 @@ class CorporateService
if (empty($row['plan_code'])) {
throw new ImportRowException(__('benefit.PLAN_CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['benefit_code'])) {
if (empty($row['code'])) {
throw new ImportRowException(__('benefit.BENEFIT_CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['code'])) {
if (empty($row['corporate_benefit_code'])) {
throw new ImportRowException(__('benefit.CUSTOMER_BENEFIT_CODE_REQUIRED'), 0, null, $row);
}
if (empty($row['description'])) {
@@ -86,20 +87,28 @@ class CorporateService
try {
$benefit_data = $row;
$benefit_data["corporate_id"] = $corporate->id;
$this->validateBenefitRow($benefit_data);
$plan = Plan::where('corporate_id', $corporate->id)->where('corporate_plan_id', $benefit_data['plan_code'])->first();
$plan = $corporate->plans->where('corporate_plan_id', $benefit_data['plan_code'])->first();
$benefit_data['plan_code'] = $plan->id;
$benefit = Benefit::updateOrCreate([
'service_code' => $benefit_data['service_code'],
'plan_code' => $benefit_data['plan_code'],
'corporate_id' => $corporate->id,
'code' => $benefit_data['code'],
], $benefit_data);
'service_code' => $plan->service_code,
], [
'code' => $benefit_data['code'],
'service_code' => $plan->service_code,
'active' => true
]);
return $benefit;
$corporateBenefit = $corporate->corporateBenefits()->updateOrCreate([
'benefit_id' => $benefit->id,
'plan_id' => $plan->id
], $benefit_data );
return $corporateBenefit;
} catch (\Exception $e) {
dd($e->getMessage());
throw $e;
}
}