Xu ly giao dien San Pham
This commit is contained in:
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user