Version 2.0
This commit is contained in:
@@ -7,9 +7,30 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['product_id', 'customer_id', 'type', 'start_date', 'end_date', 'status', 'printed_at', 'signed_status'])]
|
||||
#[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 [
|
||||
|
||||
Reference in New Issue
Block a user