Hoan thien core finance v2
This commit is contained in:
@@ -33,6 +33,11 @@ class Contract extends Model
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
public function paymentTemplate()
|
||||
{
|
||||
return $this->belongsTo(PaymentTemplate::class);
|
||||
}
|
||||
|
||||
public function customers()
|
||||
{
|
||||
return $this->belongsToMany(Customer::class, 'contract_customers')
|
||||
@@ -73,6 +78,19 @@ class Contract extends Model
|
||||
return $this->hasMany(PaymentFine::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Giá trị sau chiết khấu.
|
||||
*/
|
||||
public function getFinalValueAttribute(): float
|
||||
{
|
||||
$result = \App\Services\DiscountEngine::calculate(
|
||||
(float) $this->total_value,
|
||||
$this->discount_details
|
||||
);
|
||||
|
||||
return $result['final_value'];
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::saving(function ($contract) {
|
||||
|
||||
@@ -20,6 +20,21 @@ class PaymentScheduleItem extends Model
|
||||
'due_date' => 'date',
|
||||
];
|
||||
|
||||
public function getPaidAmountAttribute(): float
|
||||
{
|
||||
// Nếu đã eager load payments, dùng collection sum để tránh query thêm
|
||||
if ($this->relationLoaded('payments')) {
|
||||
return (float) $this->payments->sum('amount');
|
||||
}
|
||||
|
||||
return (float) $this->payments()->sum('amount');
|
||||
}
|
||||
|
||||
public function getRemainingAmountAttribute(): float
|
||||
{
|
||||
return (float) $this->amount - $this->paid_amount;
|
||||
}
|
||||
|
||||
public function template()
|
||||
{
|
||||
return $this->belongsTo(PaymentTemplate::class);
|
||||
|
||||
@@ -7,12 +7,14 @@ use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Filament\Models\Contracts\FilamentUser;
|
||||
use Filament\Panel;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
class User extends Authenticatable implements FilamentUser
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
@@ -29,4 +31,9 @@ class User extends Authenticatable
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
public function canAccessPanel(Panel $panel): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user