Merge branch 'staging' of itcorp.primaya.id:rajif/aso into staging

This commit is contained in:
R
2023-03-24 14:41:29 +07:00
24 changed files with 2839 additions and 2848 deletions

View 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')
};
}
}

View File

@@ -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
);
}
/* -------------------------------------------------------------------------- */
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Services;
use App\Models\Member;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
class CorporateMemberService
{
public function getAllDashboardMembers(int $corporateId, Request $request)
{
$limit = $request->has('perPage') ? $request->input('perPage') : 10;
return Member::query()
->joinCorporateEmployees('left')
->joinCorporateDivisions('left')
->with('currentPlan')
->withSum('claims', 'total_claim')
->whereHas('employeds', function (Builder $corporateEmployee) use ($corporateId) {
$corporateEmployee->where('corporate_id', $corporateId);
})
->when($request->input('search'), function (Builder $query, $search) {
$query->where('member_id', 'like', "%" . $search . "%")
->orWhere('name', 'like', "%" . $search . "%");
})
->when($request->input('division'), function (Builder $division, $division_id) {
$division->whereHas('division', function ($corporateEmployee) use ($division_id) {
$corporateEmployee->where('division_id', $division_id);
});
})
->when($request->has('orderBy'), function (Builder $query) use ($request) {
$orderBy = match ($request->orderBy) {
'memberId' => 'member_id',
'fullName' => 'name',
'status' => 'active',
default => ''
};
if (in_array($orderBy, ['member_id', 'name', 'active'])) {
$query->getQuery()->orderBy($orderBy, $request->order);
} elseif ($request->orderBy === 'division') {
$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);
}
}