Kimi chinh sua

This commit is contained in:
2026-04-24 08:58:53 +00:00
parent 91ff4a5e4d
commit 86216ef872
43 changed files with 2868 additions and 597 deletions

View File

@@ -15,11 +15,17 @@ class Contract extends Model
protected $casts = [
'metadata' => 'array',
'discount_details' => 'array',
'total_value' => 'decimal:2',
'land_value' => 'decimal:2',
'foundation_value' => 'decimal:2',
'total_value_with_foundation' => 'decimal:2',
'paid_amount' => 'decimal:2',
'remaining_amount' => 'decimal:2',
'excess_amount' => 'decimal:2',
'signing_date' => 'date',
'sale_date' => 'date',
'hql_confirmation_date' => 'date',
];
public function product()
@@ -45,18 +51,15 @@ class Contract extends Model
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
'contract_id',
'schedule_id',
'id',
'id'
);
}
@@ -72,17 +75,22 @@ class Contract extends Model
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)) {
static::saving(function ($contract) {
// Bảo vệ tính toán tài chính: total_value luôn bằng land_value + foundation_value
$landValue = (float) ($contract->land_value ?? 0);
$foundationValue = (float) ($contract->foundation_value ?? 0);
if ($landValue > 0 || $foundationValue > 0) {
$contract->total_value = $landValue + $foundationValue;
} elseif ($contract->exists === false && empty($contract->total_value) && !empty($contract->product_id)) {
// Fallback khi tạo mới và chưa có giá trị tài chính chi tiết
$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);
$contract->remaining_amount = (float) ($contract->total_value ?? 0) - (float) ($contract->paid_amount ?? 0);
});
}
}

View File

@@ -5,6 +5,8 @@ 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\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Customer extends Model
{
@@ -12,12 +14,28 @@ class Customer extends Model
protected $guarded = [];
// Ép kiểu để Laravel tự động dịch JSON thành Mảng khi hiển thị lên Form
protected $casts = [
'address' => 'array',
'secondary_phones' => 'array',
'dob' => 'date',
'id_issue_date' => 'date',
];
/**
* Lấy các công ty khách hàng này đại diện
*/
public function representedCompanies(): HasMany
{
return $this->hasMany(Customer::class, 'representative_id');
}
/**
* Lấy người đại diện của công ty này
*/
public function representative(): BelongsTo
{
return $this->belongsTo(Customer::class, 'representative_id');
}
public function contracts()
{
return $this->belongsToMany(Contract::class, 'contract_customers')
@@ -25,4 +43,4 @@ class Customer extends Model
->withPivot('id', 'role', 'transfer_order')
->withTimestamps();
}
}
}