Xu ly giao dien San Pham

This commit is contained in:
2026-04-19 23:50:21 +00:00
parent 10eac55520
commit 91ff4a5e4d
18 changed files with 744 additions and 196 deletions

View File

@@ -30,7 +30,8 @@ class Contract extends Model
public function customers()
{
return $this->belongsToMany(Customer::class, 'contract_customers')
->withPivot('role', 'transfer_order')
->using(ContractCustomer::class)
->withPivot('id', 'role', 'transfer_order')
->withTimestamps();
}
@@ -68,4 +69,20 @@ class Contract extends Model
{
return $this->hasMany(PaymentFine::class);
}
protected static function booted()
{
static::creating(function ($contract) {
// Tự động lấy giá trị từ sản phẩm nếu chưa có
if (empty($contract->total_value) && !empty($contract->product_id)) {
$product = Product::find($contract->product_id);
if ($product) {
$contract->total_value = $product->total_price;
}
}
// Tính toán số tiền còn lại
$contract->remaining_amount = ($contract->total_value ?? 0) - ($contract->paid_amount ?? 0);
});
}
}