refactoring member model
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Builders\MemberBuilder;
|
||||
use App\Traits\Blameable;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
@@ -73,6 +75,19 @@ class Member extends Model
|
||||
'deleted_by',
|
||||
];
|
||||
|
||||
public static function query(): MemberBuilder
|
||||
{
|
||||
return parent::query();
|
||||
}
|
||||
|
||||
public function newEloquentBuilder($query): MemberBuilder
|
||||
{
|
||||
return new MemberBuilder($query);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* relationship */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
public function claims()
|
||||
{
|
||||
return $this->hasMany(Claim::class, 'member_id', 'id');
|
||||
@@ -111,7 +126,7 @@ class Member extends Model
|
||||
{
|
||||
// return $this->belongsToMany(Corporate::class, 'corporate_employees', 'corporate_id', 'member_id')
|
||||
// // ->withPivot([
|
||||
// // 'branch_code',
|
||||
// // 'branch_code',
|
||||
// // 'divison_id',
|
||||
// // 'nik',
|
||||
// // 'status',
|
||||
@@ -167,75 +182,69 @@ class Member extends Model
|
||||
->where('member_policies.end', '>', now());
|
||||
}
|
||||
|
||||
public function getAgeAttribute()
|
||||
{
|
||||
if ($this->birth_date) {
|
||||
return Carbon::parse($this->birth_date)->diffInYears(now());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function getFullNameAttribute()
|
||||
{
|
||||
if (!$this->person) {
|
||||
return null;
|
||||
}
|
||||
$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 implode(' ', $arr);
|
||||
}
|
||||
|
||||
public function getGenderCodeAttribute()
|
||||
{
|
||||
return $this->gender ? ($this->gender == 'female' ? 'F' : 'M') : $this->gender;
|
||||
}
|
||||
|
||||
public function getNameAttribute()
|
||||
{
|
||||
return $this->person->name ?? ($this->name ?? null);
|
||||
}
|
||||
|
||||
public function getBirthDateAttribute()
|
||||
{
|
||||
$date = $this->person->birth_date ?? ($this->birth_date ?? null);
|
||||
return !empty($date) ? Carbon::parse($date)->format('Y-m-d') : null;
|
||||
}
|
||||
|
||||
public function getGenderAttribute()
|
||||
{
|
||||
return $this->person->gender ?? null;
|
||||
}
|
||||
|
||||
public function scopeFilter($query, array $filters)
|
||||
{
|
||||
$query->when($filters['search'] ?? false, function ($query, $search) {
|
||||
return $query
|
||||
->where('member_id', 'like', "%" . $search . "%")
|
||||
->orWhere('payor_id', 'like', "%" . $search . "%")
|
||||
->orWhere('email', 'like', "%" . $search . "%")
|
||||
->orWhereHas('person', function ($query) use ($search) {
|
||||
$query->where('name', 'like', "%" . $search . "%");
|
||||
$query->orWhere('phone', 'like', "%" . $search . "%");
|
||||
})
|
||||
->orWhereHas('currentPlan', function ($query) use ($search) {
|
||||
$query->where('code', 'like', "%" . $search . "%");
|
||||
});
|
||||
// ->orWhereHas('corporatePlan', function ($query) use ($search) {
|
||||
// $query->where('code', 'like', "%" . $search . "%");
|
||||
// });
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
/* -------------------------------------------------------------------------- */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user