Fix Employee Data - Client Portal

This commit is contained in:
Muhammad Fajar
2024-01-06 11:54:34 +07:00
parent 2e852f7203
commit 562f3121c5
2 changed files with 100 additions and 114 deletions

View File

@@ -6,7 +6,6 @@ use App\Helpers\Helper;
use App\Models\Member; use App\Models\Member;
use App\Models\Claim; use App\Models\Claim;
use App\Models\ClaimRequest; use App\Models\ClaimRequest;
use App\Models\CorporateEmployee;
use App\Services\CorporateMemberService; use App\Services\CorporateMemberService;
use Illuminate\Contracts\Support\Renderable; use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@@ -18,11 +17,8 @@ use Modules\Client\Transformers\Dashboard\MemberResources as ClaimSubmitMemberRe
use Modules\Client\Transformers\Dashboard\MemberResources as DashboardMemberResources; use Modules\Client\Transformers\Dashboard\MemberResources as DashboardMemberResources;
use Modules\Client\Transformers\Dashboard\MemberAlarmCenterResources as DashboardMemberAlarmResources; use Modules\Client\Transformers\Dashboard\MemberAlarmCenterResources as DashboardMemberAlarmResources;
use Modules\Client\Transformers\Dashboard\MemberEmployeeDataResources as DashboardMemberEmployeeDataResources; use Modules\Client\Transformers\Dashboard\MemberEmployeeDataResources as DashboardMemberEmployeeDataResources;
use Modules\Client\Transformers\DataMemberResource;
use Illuminate\Support\Facades\DB;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory; use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Modules\Client\Transformers\EmployeeData\UserProfile\DataMemberResource as EmployeeDataProfileMemberResource;
use Modules\Internal\Services\MemberEnrollmentService; use Modules\Internal\Services\MemberEnrollmentService;
class CorporateMemberController extends Controller class CorporateMemberController extends Controller
@@ -62,53 +58,23 @@ class CorporateMemberController extends Controller
} }
} }
public function show($corporate_id, $person_id) public function show(int $corporate_id, int $person_id)
{ {
$data = Member::with(['claims', 'person', 'employeds', 'currentPlan.benefits', 'person.currentAddress']) $data = Member::query()
->with(['person'])
->where('person_id', $person_id) ->where('person_id', $person_id)
->whereHas('employeds', function ($query) use ($corporate_id) { ->whereHas('currentEmployeds', function ($query) use ($corporate_id) {
$query->where('corporate_id', $corporate_id); $query->where('corporate_id', $corporate_id);
}) })
->first(); ->firstOrFail();
// Get Families
$totalClaims = $data->claims->sum('total_claim'); $data->families = Member::query()
$data->total_claims = $totalClaims; ->with(['person'])
//Get Family
$data_family = DB::table('members')
->join('persons', 'members.person_id', '=', 'persons.id')
->select('members.*', 'persons.phone')
->where('principal_id', $data->member_id) ->where('principal_id', $data->member_id)
->get(); ->get();
if ($data_family->isEmpty()) {
$principal_id = DB::table('members')
->where('member_id', $data->member_id)
->select('principal_id')
->first();
$data_family = DB::table('members')
->join('persons', 'members.person_id', '=', 'persons.id')
->select('members.*', 'persons.phone')
->where('principal_id', $principal_id->principal_id)
->where('members.member_id', '<>', $data->member_id)
->orWhere('members.member_id', $principal_id->principal_id)
->get();
}
$data->family = $data_family; return response()->json(EmployeeDataProfileMemberResource::make($data));
//Claim History
$data_claim_history = DB::table('claim_requests')
->join('claims', 'claims.claim_request_id', '=', 'claim_requests.id')
->join('claim_items', 'claim_items.claim_id', '=', 'claims.id')
->join('benefits', 'benefits.id', '=', 'claim_items.claim_itemable_id')
->select('claim_requests.status', 'claim_requests.submission_date', 'benefits.description')
->where('claim_requests.member_id', $data->id)
->get();
$data->claim_history = $data_claim_history;
return response()->json(DataMemberResource::make($data));
} }
public function generateMemberList($corporate_id) public function generateMemberList($corporate_id)

View File

@@ -57,7 +57,6 @@ class Member extends Model
"endorsement_date", "endorsement_date",
"members_effective_date", "members_effective_date",
"members_expire_date", "members_expire_date",
"employee_status",
"activation_date", "activation_date",
"terminated_date", "terminated_date",
"remarks", "remarks",
@@ -94,7 +93,7 @@ class Member extends Model
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* relationship */ /* Relationship */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
public function claims() public function claims()
{ {
@@ -154,36 +153,18 @@ class Member extends Model
public function currentPlans() public function currentPlans()
{ {
return $this->hasManyThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id'); return $this->hasManyThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id');
// ->latest(); // TODO Fix This
} }
public function currentPlan() public function currentPlan()
{ {
return $this->hasOneThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id',) return $this->hasOneThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id',)
->latest(); ->latest();
// ->where('plans.service_code', $this->claimRequest->service_code); // TODO Fix This
} }
// public function currentPlan()
// {
// return $this->hasOneThrough(
// Plan::class,
// MemberPlan::class,
// 'member_id',
// 'id',
// 'id',
// 'plan_id'
// )
// ->join('claim_requests', 'claim_requests.service_code', '=', 'plans.service_code')
// ->latest('claim_requests.created_at') // Atau sesuaikan dengan kolom timestamp yang sesuai
// ->select('plans.*');
// }
public function currentEmployeds() public function currentEmployeds()
{ {
return $this->hasOneThrough(CorporateEmployee::class, Person::class, 'nik', 'id', 'id', 'nik') return $this->hasOneThrough(CorporateEmployee::class, Person::class, 'nik', 'id', 'id', 'nik')
->latest(); // TODO Fix This ->latest();
} }
public function policies() public function policies()
@@ -196,7 +177,6 @@ class Member extends Model
return $this->hasOneThrough(CorporatePolicy::class, MemberPolicy::class, 'member_id', 'code', 'member_id', 'policy_id') return $this->hasOneThrough(CorporatePolicy::class, MemberPolicy::class, 'member_id', 'code', 'member_id', 'policy_id')
->where('status', 'active') ->where('status', 'active')
->orderBy('end', 'DESC'); ->orderBy('end', 'DESC');
// return $this->hasOne(MemberPolicy::class, 'member_id', 'member_id')->where('status', 'active')->latestOfMany();
} }
public function currentActivePolicy() public function currentActivePolicy()
@@ -228,18 +208,20 @@ class Member extends Model
{ {
$arr = []; $arr = [];
if (!empty($this->person->name_prefix)) { if ($this->relationLoaded('person')) {
$arr[] = $this->person->name_prefix; if (!empty($this->person->name_prefix)) {
} $arr[] = $this->person->name_prefix;
}
$arr[] = $this->person->name ?? '-'; $arr[] = $this->person->name ?? '-';
if (!empty($this->person->name_suffix)) { if (!empty($this->person->name_suffix)) {
$arr[] = $this->person->name_suffix; $arr[] = $this->person->name_suffix;
}
} }
return Attribute::make( return Attribute::make(
get: fn () => !$this->person ? null : implode(' ', $arr) get: fn () => !empty($arr) ? ucwords(strtolower(implode(' ', $arr))) : null
); );
} }
@@ -252,46 +234,90 @@ class Member extends Model
protected function name(): Attribute protected function name(): Attribute
{ {
if ($this->relationLoaded('person')) {
return Attribute::make(
get: fn () => $this->person->name ?? ($this->name ?? null)
);
} else {
return Attribute::make(
get: fn () => $this->name ?? null
);
}
}
protected function maritalStatus(): Attribute
{
if ($this->relationLoaded('person')) {
$marital_status = $this->person->marital_status ?? ($this->marital_status ?? null);
if ($marital_status === 'M') {
$marital_status = 'Menikah';
}
return Attribute::make(
get: fn () => $marital_status
);
} else {
$marital_status = $this->marital_status ?? null;
if ($marital_status === 'M') {
$marital_status = 'Menikah';
}
return Attribute::make(
get: fn () => $marital_status
);
}
}
protected function relationship(): Attribute
{
$relation = null;
if ($this->relation_with_principal === 'S') {
$relation = 'Son';
} elseif ($this->relation_with_principal === 'H') {
$relation = 'Husband';
} elseif ($this->relation_with_principal === 'D') {
$relation = 'Daughter';
} elseif ($this->relation_with_principal === 'Wife') {
$relation = 'Wife';
}
return Attribute::make( return Attribute::make(
get: fn () => $this->person->name ?? ($this->name ?? null) get: fn () => $relation
); );
} }
// protected function birthDate(): Attribute protected function status(): Attribute
// { {
// // $date = $this->person->birth_date ?? ($this->birth_date ?? null); return Attribute::make(
// $date = $this->birth_date; get: fn () => $this->active ? ($this->active == 1 ? 'Active' : 'Inactive') : null
// if ($date){ );
// $date = ($this->birth_date ?? $this->person->birth_date); }
// return Attribute::make(
// get: fn () => !empty($date) ? Carbon::parse($date)->format('d / M / Y') : '-' protected function birthDateCard(): Attribute
// );
// } else {
// return Attribute::make(
// get: fn () => '-'
// );
// }
// }
protected function birthDateeCard(): Attribute
{ {
// $date = $this->person->birth_date ?? ($this->birth_date ?? null);
$date = $this->birth_date; $date = $this->birth_date;
if ($date) { if ($date) {
$date = ($this->birth_date ?? $this->person->birth_date); return Attribute::make(
get: fn () => !empty($date) ? Carbon::parse($date)->format('d / M / Y') : '-'
);
} else if ($this->relationLoaded('person')) {
$date = $this->person->birth_date;
return Attribute::make( return Attribute::make(
get: fn () => !empty($date) ? Carbon::parse($date)->format('d / M / Y') : '-' get: fn () => !empty($date) ? Carbon::parse($date)->format('d / M / Y') : '-'
); );
} else { } else {
return Attribute::make( return Attribute::make(
get: fn () => '-' get: fn () => '-'
); );
} }
} }
protected function startDate(): Attribute protected function startDate(): Attribute
{ {
// $date = $this->person->birth_date ?? ($this->birth_date ?? null);
$date = $this->members_effective_date; $date = $this->members_effective_date;
return Attribute::make( return Attribute::make(
get: fn () => !empty($date) ? Carbon::parse($date)->format('d / M / Y') : null get: fn () => !empty($date) ? Carbon::parse($date)->format('d / M / Y') : null
@@ -300,34 +326,28 @@ class Member extends Model
protected function endDate(): Attribute protected function endDate(): Attribute
{ {
// $date = $this->person->birth_date ?? ($this->birth_date ?? null);
$date = $this->members_expire_date; $date = $this->members_expire_date;
return Attribute::make( return Attribute::make(
get: fn () => !empty($date) ? Carbon::parse($date)->format('d / M / Y') : null get: fn () => !empty($date) ? Carbon::parse($date)->format('d / M / Y') : null
); );
} }
// protected function gender(): Attribute
// {
// return Attribute::make(
// get: fn () => $this->person->gender ? ucfirst($this->person->gender) : '-'
// // get: fn () => '-'
// );
// }
protected function corporateLogo(): Attribute protected function corporateLogo(): Attribute
{ {
$avatar = File::where(['type' => 'avatar', 'fileable_id' => $this->currentPolicy->corporate->id])->orderBy('id', 'desc')->get()->first(); $avatar = null;
if ($avatar) {
$path = $_ENV['LMS_APP_STORAGE'] . $avatar->path ? $avatar->path : ''; if ($this->relationLoaded('currentPolicy')) {
return Attribute::make( $corporateId = $this->currentPolicy->corporate->id;
get: fn () => $avatar ? $path : null $avatar = File::where(['type' => 'avatar', 'fileable_id' => $corporateId])
); ->orderBy('id', 'desc')
} else { ->first();
return Attribute::make(
get: fn () => null
);
} }
$path = $avatar ? $_ENV['LMS_APP_STORAGE'] . $avatar->path : '';
return Attribute::make(
get: fn () => $path
);
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
} }