refactoring member model
This commit is contained in:
40
app/Builders/MemberBuilder.php
Normal file
40
app/Builders/MemberBuilder.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Builders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
|
class MemberBuilder extends Builder
|
||||||
|
{
|
||||||
|
public function filter(array $filters): static
|
||||||
|
{
|
||||||
|
return $this->when($filters['search'] ?? false, function (MemberBuilder $query, $search) {
|
||||||
|
$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 . "%");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function joinCorporateEmployees(string $value = 'join'): static
|
||||||
|
{
|
||||||
|
return match ($value) {
|
||||||
|
'join' => $this->join('corporate_employees', 'members.id', '=', 'corporate_employees.member_id'),
|
||||||
|
'left' => $this->leftJoin('corporate_employees', 'members.id', '=', 'corporate_employees.member_id')
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function joinCorporateDivisions(string $value = 'join'): static
|
||||||
|
{
|
||||||
|
return match ($value) {
|
||||||
|
'join' => $this->join('corporate_divisions', 'corporate_employees.division_id', '=', 'corporate_divisions.id'),
|
||||||
|
'left' => $this->leftJoin('corporate_divisions', 'corporate_employees.division_id', '=', 'corporate_divisions.id')
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Builders\MemberBuilder;
|
||||||
use App\Traits\Blameable;
|
use App\Traits\Blameable;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
@@ -73,6 +75,19 @@ class Member extends Model
|
|||||||
'deleted_by',
|
'deleted_by',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public static function query(): MemberBuilder
|
||||||
|
{
|
||||||
|
return parent::query();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function newEloquentBuilder($query): MemberBuilder
|
||||||
|
{
|
||||||
|
return new MemberBuilder($query);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* relationship */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
public function claims()
|
public function claims()
|
||||||
{
|
{
|
||||||
return $this->hasMany(Claim::class, 'member_id', 'id');
|
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')
|
// return $this->belongsToMany(Corporate::class, 'corporate_employees', 'corporate_id', 'member_id')
|
||||||
// // ->withPivot([
|
// // ->withPivot([
|
||||||
// // 'branch_code',
|
// // 'branch_code',
|
||||||
// // 'divison_id',
|
// // 'divison_id',
|
||||||
// // 'nik',
|
// // 'nik',
|
||||||
// // 'status',
|
// // 'status',
|
||||||
@@ -167,75 +182,69 @@ class Member extends Model
|
|||||||
->where('member_policies.end', '>', now());
|
->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()
|
public function division()
|
||||||
{
|
{
|
||||||
return $this->hasOneThrough(CorporateDivision::class, CorporateEmployee::class, 'member_id', 'id', 'id', 'division_id');
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,8 @@ class CorporateMemberService
|
|||||||
$limit = $request->has('perPage') ? $request->input('perPage') : 10;
|
$limit = $request->has('perPage') ? $request->input('perPage') : 10;
|
||||||
|
|
||||||
return Member::query()
|
return Member::query()
|
||||||
->select(['members.id', 'members.person_id', 'members.member_id', 'members.name', 'members.name_prefix', 'members.name_suffix', 'corporate_divisions.name AS division_name', 'members.active'])
|
->joinCorporateEmployees('left')
|
||||||
->leftJoin('corporate_employees', 'members.id', '=', 'corporate_employees.member_id')
|
->joinCorporateDivisions('left')
|
||||||
->leftJoin('corporate_divisions', 'corporate_employees.division_id', '=', 'corporate_divisions.id')
|
|
||||||
->with('currentPlan')
|
->with('currentPlan')
|
||||||
->withSum('claims', 'total_claim')
|
->withSum('claims', 'total_claim')
|
||||||
->whereHas('employeds', function (Builder $corporateEmployee) use ($corporateId) {
|
->whereHas('employeds', function (Builder $corporateEmployee) use ($corporateId) {
|
||||||
@@ -44,6 +43,7 @@ class CorporateMemberService
|
|||||||
$query->getQuery()->orderBy('corporate_divisions.name', $request->order);
|
$query->getQuery()->orderBy('corporate_divisions.name', $request->order);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
->select(['members.id', 'members.person_id', 'members.member_id', 'members.name', 'members.name_prefix', 'members.name_suffix', 'corporate_divisions.name AS division_name', 'members.active'])
|
||||||
->paginate($limit);
|
->paginate($limit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user