Files
aso/app/Models/CorporatePolicy.php
2022-12-05 13:07:20 +07:00

82 lines
2.0 KiB
PHP
Executable File

<?php
namespace App\Models;
use App\Traits\Blameable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class CorporatePolicy extends Model
{
use HasFactory, SoftDeletes, Blameable;
protected $fillable = [
'corporate_id',
// 'policy_id',
'code',
'total_premi',
'minimal_deposit_percentage',
'minimal_deposit_net',
'minimal_alert_percentage',
'minimal_alert_net',
'minimal_stop_service_percentage',
'minimal_stop_service_net',
'start',
'end',
'active',
];
protected $with = [
'latestLimitJournal'
];
protected $appends = [
'limit_balance'
];
public function corporate()
{
return $this->belongsTo(Corporate::class);
}
public function limitJournals()
{
return $this->morphMany(LimitJournal::class, 'journalable');
}
public function latestLimitJournal()
{
return $this->morphOne(LimitJournal::class, 'journalable')->latestOfMany();
}
public function setCodeAttribute($value)
{
$this->attributes['code'] = !empty($value) ? $value : Str::upper(Str::random('6'));
}
// public function setPolicyIdAttribute($value)
// {
// $this->attributes['policy_id'] = !empty($value) ? $value : Str::upper(Str::random('6'));
// }
public function setStartAttribute($value)
{
$this->attributes['start'] = !empty($value) ? Carbon::parse($value)->format('Y-m-d') : null;
}
public function setEndAttribute($value)
{
$this->attributes['end'] = !empty($value) ? Carbon::parse($value)->format('Y-m-d') : null;
}
public function getLimitBalanceAttribute()
{
$journal = $this->latestLimitJournal;
return $journal ? $journal->balance : (!empty($this->total_premi) ? $this->total_premi : "0");
}
}