Fix Corporate Member

This commit is contained in:
R
2022-12-05 13:07:20 +07:00
parent d3a806ff0b
commit 3bc8877740
6 changed files with 56 additions and 10 deletions

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CorporateManager extends Model
{
use HasFactory;
protected $table = 'corporate_manager';
}

View File

@@ -29,6 +29,14 @@ class CorporatePolicy extends Model
'active',
];
protected $with = [
'latestLimitJournal'
];
protected $appends = [
'limit_balance'
];
public function corporate()
{
return $this->belongsTo(Corporate::class);
@@ -39,6 +47,11 @@ class CorporatePolicy extends Model
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'));
@@ -61,7 +74,7 @@ class CorporatePolicy extends Model
public function getLimitBalanceAttribute()
{
$journal = $this->limitJournals()->latest()->first();
$journal = $this->latestLimitJournal;
return $journal ? $journal->balance : (!empty($this->total_premi) ? $this->total_premi : "0");
}