58 lines
1.8 KiB
PHP
58 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
#[Fillable(['product_id', 'customer_id', 'type', 'category', 'start_date', 'end_date', 'status', 'printed_at', 'signed_status', 'representative_name', 'representative_phone', 'representative_email'])]
|
|
class Contract extends Model
|
|
{
|
|
protected static function booted(): void
|
|
{
|
|
static::saving(function (self $contract) {
|
|
if ($contract->status === 'active') {
|
|
$exists = self::where('product_id', $contract->product_id)
|
|
->where('category', $contract->category)
|
|
->where('status', 'active')
|
|
->when($contract->exists, fn ($q) => $q->where('id', '!=', $contract->id))
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
throw new \RuntimeException(
|
|
__('feedback.contract_duplicate_body', [
|
|
'product' => $contract->product?->name ?? $contract->product_id,
|
|
'customer' => $contract->customer?->name ?? '?',
|
|
])
|
|
);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'start_date' => 'date',
|
|
'end_date' => 'date',
|
|
'printed_at' => 'date',
|
|
];
|
|
}
|
|
|
|
public function product(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function customer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
public function feedbacks(): HasMany
|
|
{
|
|
return $this->hasMany(Feedback::class);
|
|
}
|
|
}
|