Files
aso/app/Models/Corporate.php
2022-12-08 08:53:58 +07:00

117 lines
2.6 KiB
PHP
Executable File

<?php
namespace App\Models;
use App\Traits\Blameable;
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' => 'array',
];
public function imports()
{
return $this->morphMany(ImportLog::class, 'importable');
}
public function files()
{
return $this->morphMany(File::class, 'fileable');
}
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 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;
}
}