Files
aso/app/Models/Corporate.php
2023-02-17 14:29:50 +07:00

151 lines
3.3 KiB
PHP
Executable File

<?php
namespace App\Models;
use App\Traits\Blameable;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Corporate extends Model
{
use HasFactory, SoftDeletes, Blameable;
protected $fillable = [
'type',
'parent_id',
'code',
'name',
'welcome_message',
'help_text',
'active',
'linking_rules',
];
protected $casts = [
'linking_rules' => AsArrayObject::class,
];
protected $appends = [
'avatar_url',
];
protected $hidden = [
'created_at',
'updated_at',
'deleted_at',
'created_by',
'updated_by',
'deleted_by',
];
public function imports()
{
return $this->morphMany(ImportLog::class, 'importable');
}
public function files()
{
return $this->morphMany(File::class, 'fileable');
}
public function avatar()
{
return $this->morphOne(File::class, 'fileable')->where('type', 'avatar')->latest();
}
public function getAvatarUrlAttribute()
{
return $this->avatar ? $this->avatar->url : null;
}
public function plans()
{
return $this->hasMany(Plan::class, 'corporate_id', 'id');
}
public function policies()
{
return $this->hasMany(CorporatePolicy::class);
}
public function currentPolicy()
{
return $this->hasOne(CorporatePolicy::class)
// ->where('start', '<=', now())
// ->where('end', '>=', now())
->where('active', true)
->latestOfMany();
}
public function corporatePlans()
{
return $this->hasMany(CorporatePlan::class, 'corporate_id');
}
public function corporateBenefits()
{
return $this->hasMany(CorporateBenefit::class, 'corporate_id');
}
public function corporateDivisions()
{
return $this->hasMany(CorporateDivision::class, 'corporate_id');
}
public function employees()
{
return $this->belongsToMany(Member::class, 'corporate_employees', 'corporate_id', 'member_id')->withPivot([
'branch_code',
'division_id',
'nik',
'status',
'start',
'end'
]);
}
// public function
public function importLogs()
{
return $this->morphMany(ImportLog::class, 'importable');
}
public function limitJournals()
{
return $this->morphMany(LimitJournal::class, 'journalable');
}
public function services()
{
return $this->hasManyThrough(CorporateService::class, Service::class, 'corporate_id', 'service_code', 'id', 'service_code');
}
// public function claims()
// {
// return $this->hasManyThrough()
// }
public function corporateServices()
{
return $this->hasMany(CorporateService::class, 'corporate_id');
}
public function parent()
{
return $this->belongsTo(Corporate::class, 'parent_id');
}
public function subCorporates()
{
return $this->hasMany(Corporate::class, 'parent_id');
}
public function getLimitBalanceAttribute()
{
return $this->currentPolicy->limit_balance;
}
}