hasMany(Claim::class, 'member_id', 'id'); } public function claimRequest() { return $this->hasMany(ClaimRequest::class, 'member_id', 'id')->where('claim_id', '!=', null); } public function postponedClaims() { return $this->hasMany(Claim::class, 'member_id', 'id')->where('status', 'postpone'); } public function person() { return $this->belongsTo(Person::class, 'person_id', 'id'); } public function employeds() { return $this->hasMany(CorporateEmployee::class, 'member_id'); } public function corporates() { return $this ->belongsToMany(Corporate::class, 'corporate_employees', 'corporate_id', 'member_id') ->withPivot([ 'branch_code', 'division_id', 'nik', 'status', 'start', 'end' ]); } public function currentCorporate() { return $this->hasOneThrough(Corporate::class, CorporateEmployee::class, 'member_id', 'id', 'id', 'corporate_id'); } public function memberPlans() { return $this->hasMany(MemberPlan::class, 'member_id'); } public function plans() { return $this->hasManyThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id'); } public function currentPlans() { return $this->hasManyThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id'); // ->latest(); // TODO Fix This } public function currentPlan() { return $this->hasOneThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id', ) ->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() { return $this->hasOneThrough(CorporateEmployee::class, Person::class, 'nik', 'id', 'id', 'nik') ->latest(); // TODO Fix This } public function policies() { return $this->hasMany(MemberPolicy::class, 'member_id', 'member_id'); } public function currentPolicy() { return $this->hasOneThrough(CorporatePolicy::class, MemberPolicy::class, 'member_id', 'code', 'member_id', 'policy_id') ->where('status', 'active') ->orderBy('end', 'DESC'); // return $this->hasOne(MemberPolicy::class, 'member_id', 'member_id')->where('status', 'active')->latestOfMany(); } public function currentActivePolicy() { return $this->hasOneThrough(CorporatePolicy::class, MemberPolicy::class, 'member_id', 'code', 'member_id', 'policy_id') ->where('corporate_policies.start', '<', now()) ->where('corporate_policies.end', '>', now()) ->where('member_policies.start', '<', now()) ->where('member_policies.end', '>', now()); } public function division() { return $this->hasOneThrough(CorporateDivision::class, CorporateEmployee::class, 'member_id', 'id', 'id', 'division_id'); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* Accessors & Mutators */ /* -------------------------------------------------------------------------- */ protected function age(): Attribute { return Attribute::make( get: fn () => Carbon::parse($this->birth_date)->diffInYears(now()) ); } protected function fullName(): Attribute { $arr = []; if (!empty($this->person->name_prefix)) { $arr[] = $this->person->name_prefix; } $arr[] = $this->person->name; if (!empty($this->person->name_suffix)) { $arr[] = $this->person->name_suffix; } return Attribute::make( get: fn () => !$this->person ? null : implode(' ', $arr) ); } protected function genderCode(): Attribute { return Attribute::make( get: fn () => $this->gender ? ($this->gender == 'female' ? 'F' : 'M') : $this->gender ); } protected function name(): Attribute { return Attribute::make( get: fn () => $this->person->name ?? ($this->name ?? null) ); } protected function birthDate(): Attribute { // $date = $this->person->birth_date ?? ($this->birth_date ?? null); $date = $this->person->birth_date ?? ($this->birth_date ?? null); return Attribute::make( get: fn () => !empty($date) ? Carbon::parse($date)->format('Y-m-d') : null ); } protected function gender(): Attribute { return Attribute::make( get: fn () => $this->person->gender ?? null ); } protected function corporateLogo(): Attribute { $avatar = File::where(['type' => 'avatar', 'fileable_id' => $this->currentPolicy->corporate->id])->orderBy('id', 'desc')->get()->first(); if ($avatar){ $path = $_ENV['LMS_APP_STORAGE'] . $avatar->path ? $avatar->path :''; return Attribute::make( get: fn () => $avatar ? $path : null ); } else { return Attribute::make( get: fn () => null ); } } /* -------------------------------------------------------------------------- */ }