72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
|
|
class Contract extends Model
|
|
{
|
|
use HasUuids, HasFactory;
|
|
|
|
protected $guarded = [];
|
|
|
|
protected $casts = [
|
|
'metadata' => 'array',
|
|
'total_value' => 'decimal:2',
|
|
'paid_amount' => 'decimal:2',
|
|
'remaining_amount' => 'decimal:2',
|
|
'excess_amount' => 'decimal:2',
|
|
'signing_date' => 'date',
|
|
];
|
|
|
|
public function product()
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function customers()
|
|
{
|
|
return $this->belongsToMany(Customer::class, 'contract_customers')
|
|
->withPivot('role', 'transfer_order')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function appendices()
|
|
{
|
|
return $this->hasMany(Appendix::class);
|
|
}
|
|
|
|
public function paymentSchedule()
|
|
{
|
|
return $this->hasOne(PaymentSchedule::class);
|
|
}
|
|
|
|
/**
|
|
* Lấy trực tiếp các đợt thanh toán của hợp đồng này
|
|
*/
|
|
public function scheduleItems(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
PaymentScheduleItem::class,
|
|
PaymentSchedule::class,
|
|
'contract_id', // Khóa ngoại trên bảng PaymentSchedule
|
|
'schedule_id', // Khóa ngoại trên bảng PaymentScheduleItem
|
|
'id', // Khóa chính trên bảng Contract
|
|
'id' // Khóa chính trên bảng PaymentSchedule
|
|
);
|
|
}
|
|
|
|
public function payments()
|
|
{
|
|
return $this->hasMany(Payment::class);
|
|
}
|
|
|
|
public function paymentFines()
|
|
{
|
|
return $this->hasMany(PaymentFine::class);
|
|
}
|
|
}
|