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);
});
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Relations\Pivot;
class ContractCustomer extends Pivot
{
use HasUuids;
protected $table = 'contract_customers';
public $incrementing = false;
protected $keyType = 'string';
protected $guarded = [];
}

View File

@@ -21,7 +21,8 @@ class Customer extends Model
public function contracts()
{
return $this->belongsToMany(Contract::class, 'contract_customers')
->withPivot('role', 'transfer_order')
->using(ContractCustomer::class)
->withPivot('id', 'role', 'transfer_order')
->withTimestamps();
}
}

View File

@@ -11,12 +11,17 @@ class Project extends Model
use HasUuids, HasFactory;
protected $guarded = [];
public function products()
{
return $this->hasMany(Product::class);
}
public function paymentTemplate()
{
return $this->belongsTo(PaymentTemplate::class);
}
public function paymentTemplates()
{
return $this->hasMany(PaymentTemplate::class);