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 [
|
||||
|
||||
@@ -6,14 +6,19 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
||||
|
||||
#[Fillable(['name', 'email', 'phone', 'address', 'status'])]
|
||||
class Customer extends Model
|
||||
{
|
||||
public function products(): BelongsToMany
|
||||
/**
|
||||
* Các căn hộ khách hàng sở hữu (qua HĐ Ownership active)
|
||||
*/
|
||||
public function ownedProducts(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Product::class, 'customer_product')
|
||||
->withPivot('purchase_date')
|
||||
return $this->belongsToMany(Product::class, 'contracts')
|
||||
->wherePivot('category', 'ownership')
|
||||
->wherePivot('status', 'active')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class CustomerProduct extends Model
|
||||
{
|
||||
protected $table = 'customer_product';
|
||||
|
||||
protected $fillable = [
|
||||
'customer_id',
|
||||
'product_id',
|
||||
'purchase_date',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'purchase_date' => 'date',
|
||||
];
|
||||
}
|
||||
|
||||
public function customer(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Customer::class);
|
||||
}
|
||||
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
public function feedbacks(): HasMany
|
||||
{
|
||||
return $this->hasMany(Feedback::class);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['customer_id', 'customer_product_id', 'contract_id', 'feedback_channel_id', 'assigned_to', 'title', 'content', 'status', 'current_department_id', 'is_escalated'])]
|
||||
#[Fillable(['customer_id', 'contract_id', 'feedback_channel_id', 'assigned_to', 'title', 'content', 'status', 'current_department_id', 'is_escalated'])]
|
||||
class Feedback extends Model
|
||||
{
|
||||
public function customer(): BelongsTo
|
||||
@@ -16,11 +16,6 @@ class Feedback extends Model
|
||||
return $this->belongsTo(Customer::class);
|
||||
}
|
||||
|
||||
public function customerProduct(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CustomerProduct::class, 'customer_product_id');
|
||||
}
|
||||
|
||||
public function contract(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Contract::class);
|
||||
|
||||
@@ -4,19 +4,11 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['name', 'description', 'status'])]
|
||||
class Product extends Model
|
||||
{
|
||||
public function customers(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Customer::class, 'customer_product')
|
||||
->withPivot('purchase_date')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function contracts(): HasMany
|
||||
{
|
||||
return $this->hasMany(Contract::class);
|
||||
|
||||
Reference in New Issue
Block a user