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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
19
app/Models/ContractCustomer.php
Normal file
19
app/Models/ContractCustomer.php
Normal 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 = [];
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user