fix division when member import

This commit is contained in:
Muhammad Fajar
2022-12-06 18:48:17 +07:00
parent 405ca24c6a
commit b26ccdd0e6
7 changed files with 261 additions and 204 deletions

View File

@@ -0,0 +1,37 @@
<?php
namespace Modules\Client\Transformers;
use Illuminate\Http\Resources\Json\JsonResource;
class DashboardResources extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
$myLimitBalance = (int)$this->currentPolicy->limit_balance;
$myLimitTotal = (int)$this->currentPolicy->total_premi;
$lockBalance = (int)$this->currentPolicy->minimal_stop_service_net;
$lockPercentage = (int)$this->currentPolicy->minimal_stop_service_percentage;
return [
'policy' => [
'myLimit' => [
'balance' => $myLimitBalance,
'total' => $myLimitTotal,
'percentage' => ($myLimitBalance / $myLimitTotal) * 100,
],
'lockLimit' => [
'balance' => $lockBalance,
'percentage' => $lockPercentage
]
],
];
}
}

View File

@@ -27,13 +27,12 @@ class CorporateController extends Controller
{ {
$corporates = Corporate::query() $corporates = Corporate::query()
->when($request->search, function ($query, $search) { ->when($request->search, function ($query, $search) {
return $query->where('name', 'LIKE', '%'.$search.'%') return $query->where('name', 'LIKE', '%' . $search . '%')
->orWhere('code', 'LIKE', '%'.$search.'%'); ->orWhere('code', 'LIKE', '%' . $search . '%');
}) })
->with('currentPolicy', 'subCorporates') ->with('currentPolicy', 'subCorporates')
->withCount([ ->withCount([
'employees', 'employees',
'corporatePlans',
'corporateBenefits', 'corporateBenefits',
]) ])
->where('type', 'corporate') ->where('type', 'corporate')
@@ -48,10 +47,10 @@ class CorporateController extends Controller
*/ */
public function create() public function create()
{ {
$corporateGroups = Corporate::whereNull('parent_id')->get()->map(function($corporate) { $corporateGroups = Corporate::whereNull('parent_id')->get()->map(function ($corporate) {
return [ return [
'value' => $corporate->id, 'value' => $corporate->id,
'label' => $corporate->name.' ('.$corporate->code.')', 'label' => $corporate->name . ' (' . $corporate->code . ')',
]; ];
}); });
@@ -199,7 +198,8 @@ class CorporateController extends Controller
// //
} }
public function activation(Request $request, $corporate_id) { public function activation(Request $request, $corporate_id)
{
$request->validate([ $request->validate([
'active' => 'required' 'active' => 'required'
]); ]);
@@ -223,16 +223,16 @@ class CorporateController extends Controller
'file' => 'required|file|mimes:xls,xlsx,csv,txt', 'file' => 'required|file|mimes:xls,xlsx,csv,txt',
]); ]);
// dd($request->toArray()); // dd($request->toArray());
$file_name = now()->getPreciseTimestamp(3).'-'.$request->file('file')->getClientOriginalName(); $file_name = now()->getPreciseTimestamp(3) . '-' . $request->file('file')->getClientOriginalName();
$file = $request->file('file')->storeAs('temp', $file_name); $file = $request->file('file')->storeAs('temp', $file_name);
$corporate = Corporate::findOrFail($corporate_id); $corporate = Corporate::findOrFail($corporate_id);
$import = new ImportService(); $import = new ImportService();
$import->read(Storage::path('temp/'.$file_name)); $import->read(Storage::path('temp/' . $file_name));
$import->write(Storage::disk('public')->path('temp/result-'.$file_name), 'xsls'); $import->write(Storage::disk('public')->path('temp/result-' . $file_name), 'xsls');
foreach ($import->sheetsIterator() as $sheetIndex => $sheet) { foreach ($import->sheetsIterator() as $sheetIndex => $sheet) {
if ( !in_array($sheet->getName(), ['Plan', 'Benefit']) ) { if (!in_array($sheet->getName(), ['Plan', 'Benefit'])) {
continue; continue;
} else { } else {
if ($sheetIndex == 1) { // Rename First Sheet to Writer if ($sheetIndex == 1) { // Rename First Sheet to Writer
@@ -243,9 +243,9 @@ class CorporateController extends Controller
$nextWriterSheet->setName($sheet->getName()); $nextWriterSheet->setName($sheet->getName());
} }
if ( $sheet->getName() == 'Plan' ) { if ($sheet->getName() == 'Plan') {
$headers_map_to_table_fields = Plan::$doc_headers_to_field_map; $headers_map_to_table_fields = Plan::$doc_headers_to_field_map;
} else if ( $sheet->getName() == 'Benefit' ) { } else if ($sheet->getName() == 'Benefit') {
$headers_map_to_table_fields = Benefit::$doc_headers_to_field_map; $headers_map_to_table_fields = Benefit::$doc_headers_to_field_map;
} }
@@ -253,7 +253,6 @@ class CorporateController extends Controller
$result_headers = array_keys($headers_map_to_table_fields); $result_headers = array_keys($headers_map_to_table_fields);
$result_headers = array_merge($result_headers, ['Ingest Code', 'Ingest Note']); $result_headers = array_merge($result_headers, ['Ingest Code', 'Ingest Note']);
$import->addArrayToRow($result_headers); $import->addArrayToRow($result_headers);
} }
$doc_headers_indexes = []; $doc_headers_indexes = [];
@@ -261,8 +260,8 @@ class CorporateController extends Controller
if ($index == 1) { // First Row Must be Header if ($index == 1) { // First Row Must be Header
foreach ($row->getCells() as $index => $cell) { foreach ($row->getCells() as $index => $cell) {
$title = $cell->getValue(); $title = $cell->getValue();
$title = preg_replace( "/\r|\n/", " ", $title ); $title = preg_replace("/\r|\n/", " ", $title);
$title = preg_replace('/\xc2\xa0/', " ", $title ); $title = preg_replace('/\xc2\xa0/', " ", $title);
$title = rtrim($title); $title = rtrim($title);
$title = ltrim($title); $title = ltrim($title);
$doc_headers_indexes[$index] = $title; $doc_headers_indexes[$index] = $title;
@@ -278,9 +277,9 @@ class CorporateController extends Controller
try { // Process the Row Data try { // Process the Row Data
$corporateService = new CorporateService(); $corporateService = new CorporateService();
if ( $sheet->getName() == 'Plan' ) { if ($sheet->getName() == 'Plan') {
$corporateService->handlePlanRow($corporate, $row_data); $corporateService->handlePlanRow($corporate, $row_data);
} else if ( $sheet->getName() == 'Benefit' ) { } else if ($sheet->getName() == 'Benefit') {
$corporateService->handleBenefitRow($corporate, $row_data); $corporateService->handleBenefitRow($corporate, $row_data);
} }
@@ -289,7 +288,6 @@ class CorporateController extends Controller
'Ingest Code' => 200, 'Ingest Code' => 200,
'Ingest Note' => 'Success', 'Ingest Note' => 'Success',
]), $sheet->getName()); ]), $sheet->getName());
} catch (ImportRowException $e) { } catch (ImportRowException $e) {
// Write Data Validation Error to File // Write Data Validation Error to File
$import->addArrayToRow(array_merge($row_data, [ $import->addArrayToRow(array_merge($row_data, [
@@ -308,7 +306,7 @@ class CorporateController extends Controller
} }
} }
$import->reader->close(); $import->reader->close();
Storage::delete('temp/'.$file_name); Storage::delete('temp/' . $file_name);
$import->writer->close(); $import->writer->close();
return [ return [
@@ -316,8 +314,8 @@ class CorporateController extends Controller
// 'total_failed_row' => count($failed_plan_data), // 'total_failed_row' => count($failed_plan_data),
// 'failed_row' => $failed_plan_data, // 'failed_row' => $failed_plan_data,
'result_file' => [ 'result_file' => [
'url' => Storage::disk('public')->url('temp/result-'.$file_name), 'url' => Storage::disk('public')->url('temp/result-' . $file_name),
'name' => 'result-'.$file_name, 'name' => 'result-' . $file_name,
] ]
]; ];
} }

View File

@@ -35,9 +35,9 @@ class CorporateMemberController extends Controller
->with([ ->with([
'employeds', 'employeds',
'currentPolicy', 'currentPolicy',
'claims' => function ($claim) { // 'claims' => function ($claim) {
return $claim->used(); // return $claim->used();
} // }
]) ])
->with('currentPlan') ->with('currentPlan')
->paginate() ->paginate()
@@ -114,14 +114,14 @@ class CorporateMemberController extends Controller
]); ]);
$corporate = Corporate::findOrFail($corporate_id)->load('currentPolicy'); $corporate = Corporate::findOrFail($corporate_id)->load('currentPolicy');
$file_name = now()->getPreciseTimestamp(3).'-'.$request->file('file')->getClientOriginalName(); $file_name = now()->getPreciseTimestamp(3) . '-' . $request->file('file')->getClientOriginalName();
$file = $request->file('file')->storeAs('temp', $file_name); $file = $request->file('file')->storeAs('temp', $file_name);
$reader = ReaderEntityFactory::createReaderFromFile(Storage::path('temp/'.$file_name)); $reader = ReaderEntityFactory::createReaderFromFile(Storage::path('temp/' . $file_name));
$reader->open(Storage::path('temp/'.$file_name)); $reader->open(Storage::path('temp/' . $file_name));
$writer = WriterEntityFactory::createXLSXWriter(); $writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile(Storage::disk('public')->path('temp/result-'.$file_name)); $writer->openToFile(Storage::disk('public')->path('temp/result-' . $file_name));
$headers_map_to_table_fields = $this->memberEnrollmentService->doc_headers_to_field_map; $headers_map_to_table_fields = $this->memberEnrollmentService->doc_headers_to_field_map;
@@ -139,8 +139,8 @@ class CorporateMemberController extends Controller
foreach ($row->getCells() as $index => $cell) { foreach ($row->getCells() as $index => $cell) {
// Clear up the string and remove all spaces // Clear up the string and remove all spaces
$title = $cell->getValue(); $title = $cell->getValue();
$title = preg_replace( "/\r|\n/", " ", $title ); $title = preg_replace("/\r|\n/", " ", $title);
$title = preg_replace('/\xc2\xa0/', " ", $title ); $title = preg_replace('/\xc2\xa0/', " ", $title);
$title = rtrim($title); $title = rtrim($title);
$title = ltrim($title); $title = ltrim($title);
$doc_headers_indexes[$index] = $title; $doc_headers_indexes[$index] = $title;
@@ -182,14 +182,13 @@ class CorporateMemberController extends Controller
$failed_member_data[] = ['row_number' => $index, 'error' => $e->getMessage()]; $failed_member_data[] = ['row_number' => $index, 'error' => $e->getMessage()];
} }
} }
} }
break; //only read first sheet break; //only read first sheet
} }
$reader->close(); $reader->close();
$writer->close(); $writer->close();
Storage::delete('temp/'.$file_name); Storage::delete('temp/' . $file_name);
// throw(404); // throw(404);
return [ return [
@@ -197,8 +196,8 @@ class CorporateMemberController extends Controller
'total_failed_row' => count($failed_member_data), 'total_failed_row' => count($failed_member_data),
'failed_row' => $failed_member_data, 'failed_row' => $failed_member_data,
'result_file' => [ 'result_file' => [
'url' => Storage::disk('public')->url('temp/result-'.$file_name), 'url' => Storage::disk('public')->url('temp/result-' . $file_name),
'name' => 'result-'.$file_name, 'name' => 'result-' . $file_name,
] ]
]; ];
} }

View File

@@ -44,7 +44,6 @@ class DivisionController extends Controller
$request->validate([ $request->validate([
'code' => [ 'code' => [
'required', 'required',
Rule::unique('corporate_plans')->where('corporate_id', $corporate_id)
], ],
'name' => 'required' 'name' => 'required'
]); ]);

View File

@@ -27,7 +27,7 @@ class MemberEnrollmentService
"Halodoc Member ID" => "halodoc_member_id", "Halodoc Member ID" => "halodoc_member_id",
"Corporate ID" => "corporate_id", "Corporate ID" => "corporate_id",
"NIK" => "nik", "NIK" => "nik",
"Division" => "division_code", "Division" => "division_name",
"Branch Code" => "branch_code", "Branch Code" => "branch_code",
"Bank Info" => "banks_info", "Bank Info" => "banks_info",
"Language" => "language", "Language" => "language",
@@ -106,7 +106,7 @@ class MemberEnrollmentService
"halodoc_member_id" => "Halodoc Member ID", "halodoc_member_id" => "Halodoc Member ID",
"corporate_id" => "Corporate ID", "corporate_id" => "Corporate ID",
"nik" => "NIK", "nik" => "NIK",
"division_code" => "Division", "division_name" => "Division",
"branch_code" => "Branch Code", "branch_code" => "Branch Code",
"banks_info" => "Bank Info", "banks_info" => "Bank Info",
"language" => "Language", "language" => "Language",
@@ -306,12 +306,12 @@ class MemberEnrollmentService
throw new ImportRowException(__('enrollment.INVALID_MARITAL_STATUS'), 0, null, $row); throw new ImportRowException(__('enrollment.INVALID_MARITAL_STATUS'), 0, null, $row);
} }
if (empty($row['member_effective_date']) ) { if (empty($row['member_effective_date'])) {
throw new ImportRowException(__('enrollment.MEMBER_EFFECTIVE_REQUIRED'), 0, null, $row); throw new ImportRowException(__('enrollment.MEMBER_EFFECTIVE_REQUIRED'), 0, null, $row);
} }
// TODO EFFECTIVE DATE VALIDATION // TODO EFFECTIVE DATE VALIDATION
if (empty($row['member_expiry_date']) ) { if (empty($row['member_expiry_date'])) {
throw new ImportRowException(__('enrollment.MEMBER_EXPIRY_REQUIRED'), 0, null, $row); throw new ImportRowException(__('enrollment.MEMBER_EXPIRY_REQUIRED'), 0, null, $row);
} }
// TODO EFFECTIVE DATE VALIDATION // TODO EFFECTIVE DATE VALIDATION
@@ -327,13 +327,17 @@ class MemberEnrollmentService
throw new ImportRowException(__('enrollment.NAME_REQUIRED'), 0, null, $row); throw new ImportRowException(__('enrollment.NAME_REQUIRED'), 0, null, $row);
} }
if (!empty($row['telephone_mobile']) if (
&& !(substr($row['telephone_mobile'], 0, 4) == '+628' || substr($row['telephone_mobile'], 0, 3) == '628')) { !empty($row['telephone_mobile'])
&& !(substr($row['telephone_mobile'], 0, 4) == '+628' || substr($row['telephone_mobile'], 0, 3) == '628')
) {
throw new ImportRowException(__('enrollment.PHONE_INVALID'), 0, null, $row); throw new ImportRowException(__('enrollment.PHONE_INVALID'), 0, null, $row);
} }
if (!empty($row['email']) if (
&& !filter_var($row['email'], FILTER_VALIDATE_EMAIL)) { !empty($row['email'])
&& !filter_var($row['email'], FILTER_VALIDATE_EMAIL)
) {
throw new ImportRowException(__('enrollment.EMAIL_INVALID'), 0, null, $row); throw new ImportRowException(__('enrollment.EMAIL_INVALID'), 0, null, $row);
} }
@@ -449,8 +453,28 @@ class MemberEnrollmentService
]); ]);
$memberPolicy->save(); $memberPolicy->save();
if (!empty($row['division'])) { if (!empty($row['division_name'])) {
$division_id = CorporateDivision::where('code', $row['division_code'])->where('')->pluck('id'); $division_id = CorporateDivision::query()->where('code', $row['division_name'])->pluck('id')->first();
if (empty($division_id)) {
$corporateCodeArray = explode(' ', $row['division_name']);
if (!empty($corporateCodeArray[1])) {
$corporateCode = substr($corporateCodeArray[0], 0, 1) . substr($corporateCodeArray[1], 0, 1);
} elseif (!empty($corporateCodeArray[2])) {
$corporateCode = substr($corporateCodeArray[0], 0, 1) . substr($corporateCodeArray[1], 0, 1);
} else {
$corporateCode = substr($row['division_name'], 0, 1);
}
$division = CorporateDivision::query()->create([
'corporate_id' => $corporate->id,
'name' => $row['division_name'],
'code' => $corporateCode,
]);
$division_id = $division->id;
}
} }
$member->employeds()->create([ $member->employeds()->create([
@@ -583,7 +607,8 @@ class MemberEnrollmentService
throw new ImportRowException(__('enrollment.MEMBER_EXPIRY_DATE_INVALID'), 0, null, $row); throw new ImportRowException(__('enrollment.MEMBER_EXPIRY_DATE_INVALID'), 0, null, $row);
} }
if (Carbon::parse($memberPolicy->end) > Carbon::parse(strtotime($row['member_expiry_date'])) if (
Carbon::parse($memberPolicy->end) > Carbon::parse(strtotime($row['member_expiry_date']))
|| $memberPolicy->end > Carbon::parse(strtotime($row['member_expiry_date'])) || $memberPolicy->end > Carbon::parse(strtotime($row['member_expiry_date']))
) { ) {
throw new ImportRowException(__('enrollment.MEMBER_RENEWAL_STILL_ACTIVE'), 0, null, $row); throw new ImportRowException(__('enrollment.MEMBER_RENEWAL_STILL_ACTIVE'), 0, null, $row);
@@ -625,7 +650,8 @@ class MemberEnrollmentService
throw new ImportRowException(__('enrollment.MEMBER_EXPIRY_DATE_INVALID'), 0, null, $row); throw new ImportRowException(__('enrollment.MEMBER_EXPIRY_DATE_INVALID'), 0, null, $row);
} }
if (Carbon::parse($memberPolicy->end) > Carbon::parse(strtotime($row['member_expiry_date'])) if (
Carbon::parse($memberPolicy->end) > Carbon::parse(strtotime($row['member_expiry_date']))
|| $memberPolicy->end > Carbon::parse(strtotime($row['member_expiry_date'])) || $memberPolicy->end > Carbon::parse(strtotime($row['member_expiry_date']))
) { ) {
throw new ImportRowException(__('enrollment.MEMBER_RENEWAL_STILL_ACTIVE'), 0, null, $row); throw new ImportRowException(__('enrollment.MEMBER_RENEWAL_STILL_ACTIVE'), 0, null, $row);
@@ -743,7 +769,7 @@ class MemberEnrollmentService
try { try {
DB::beginTransaction(); DB::beginTransaction();
if ( !empty($record_type_new) ) { if (!empty($record_type_new)) {
$member = $memberPolicy->member; $member = $memberPolicy->member;
$member->record_type = $record_type_new; $member->record_type = $record_type_new;
$member->principal_id = $row['principal_id']; $member->principal_id = $row['principal_id'];
@@ -751,7 +777,7 @@ class MemberEnrollmentService
$member->save(); $member->save();
} }
if ( !empty($corp_code_new) ) { if (!empty($corp_code_new)) {
$oldCorporate = Corporate::where('code', $corp_code_old)->first(); $oldCorporate = Corporate::where('code', $corp_code_old)->first();
$newCorporate = Corporate::where('code', $corp_code_new)->first(); $newCorporate = Corporate::where('code', $corp_code_new)->first();
@@ -761,22 +787,24 @@ class MemberEnrollmentService
$corporateEmployee = CorporateEmployee::where('corporate_id', $oldCorporate->id) $corporateEmployee = CorporateEmployee::where('corporate_id', $oldCorporate->id)
->where('member_id', $memberPolicy->member->id) ->where('member_id', $memberPolicy->member->id)
->first(); ->first();
$newCorporateEmployee = CorporateEmployee::updateOrCreate([ $newCorporateEmployee = CorporateEmployee::updateOrCreate(
[
'corporate_id' => $oldCorporate->id, 'corporate_id' => $oldCorporate->id,
'member_id' => $memberPolicy->member->id 'member_id' => $memberPolicy->member->id
], ],
[ [
'corporate_id' => $newCorporate->id, 'corporate_id' => $newCorporate->id,
'member_id' => $memberPolicy->member->id 'member_id' => $memberPolicy->member->id
]); ]
);
} }
if ( !empty($policy_number_new) ) { if (!empty($policy_number_new)) {
$memberPolicy->policy_id = $policy_number_new; $memberPolicy->policy_id = $policy_number_new;
$memberPolicy->save(); $memberPolicy->save();
} }
if ( !empty($member_id_new) ) { if (!empty($member_id_new)) {
$memberPolicy->member_id = $member_id_new; $memberPolicy->member_id = $member_id_new;
$memberPolicy->save(); $memberPolicy->save();

View File

@@ -89,7 +89,7 @@ class Member extends Model
->belongsToMany(Corporate::class, 'corporate_employees', 'corporate_id', 'member_id') ->belongsToMany(Corporate::class, 'corporate_employees', 'corporate_id', 'member_id')
->withPivot([ ->withPivot([
'branch_code', 'branch_code',
'divison_id', 'division_id',
'nik', 'nik',
'status', 'status',
'start', 'start',
@@ -176,8 +176,7 @@ class Member extends Model
return $query return $query
->where('member_id', 'like', "%" . $search . "%") ->where('member_id', 'like', "%" . $search . "%")
->orWhere('payor_id', 'like', "%" . $search . "%") ->orWhere('payor_id', 'like', "%" . $search . "%")
->orWhere('name', 'like', "%" . $search . "%") ->orWhere('name', 'like', "%" . $search . "%");
;
// ->orWhereHas('corporatePlan', function ($query) use ($search) { // ->orWhereHas('corporatePlan', function ($query) use ($search) {
// $query->where('code', 'like', "%" . $search . "%"); // $query->where('code', 'like', "%" . $search . "%");
// }); // });

View File

@@ -32,9 +32,6 @@ return new class extends Migration
$table->dateTime('approved_at')->nullable(); $table->dateTime('approved_at')->nullable();
$table->unsignedBigInteger('approved_by')->nullable()->index(); $table->unsignedBigInteger('approved_by')->nullable()->index();
$table->dateTime('approved_at')->nullable();
$table->unsignedBigInteger('approved_by')->nullable()->index();
$table->dateTime('paid_at')->nullable(); $table->dateTime('paid_at')->nullable();
$table->unsignedBigInteger('paid_by')->nullable()->index(); $table->unsignedBigInteger('paid_by')->nullable()->index();