2025-12-30 15:03:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2026-01-16 17:36:37 +08:00
|
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
|
use Spatie\Activitylog\LogOptions;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
class Vendor extends Model
|
|
|
|
|
{
|
2026-01-16 17:36:37 +08:00
|
|
|
use LogsActivity;
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
protected $fillable = [
|
|
|
|
|
'code',
|
|
|
|
|
'name',
|
|
|
|
|
'short_name',
|
|
|
|
|
'tax_id',
|
|
|
|
|
'owner',
|
|
|
|
|
'contact_name',
|
|
|
|
|
'tel',
|
|
|
|
|
'phone',
|
|
|
|
|
'email',
|
|
|
|
|
'address',
|
|
|
|
|
'remark'
|
|
|
|
|
];
|
|
|
|
|
public function products(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Product::class, 'product_vendor')
|
|
|
|
|
->withPivot('last_price')
|
|
|
|
|
->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function purchaseOrders(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(PurchaseOrder::class);
|
|
|
|
|
}
|
2026-01-16 17:36:37 +08:00
|
|
|
|
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
|
|
|
{
|
|
|
|
|
return LogOptions::defaults()
|
|
|
|
|
->logAll()
|
|
|
|
|
->logOnlyDirty()
|
|
|
|
|
->dontSubmitEmptyLogs();
|
|
|
|
|
}
|
2026-01-19 15:32:41 +08:00
|
|
|
|
|
|
|
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
|
|
|
|
{
|
|
|
|
|
$properties = $activity->properties;
|
|
|
|
|
|
|
|
|
|
// Store name in 'snapshot' for context, keeping 'attributes' clean
|
|
|
|
|
$snapshot = $properties['snapshot'] ?? [];
|
|
|
|
|
// Only set name if it's not already set (e.g. by controller for specific context like supply product)
|
|
|
|
|
if (!isset($snapshot['name'])) {
|
|
|
|
|
$snapshot['name'] = $this->name;
|
|
|
|
|
}
|
|
|
|
|
$properties['snapshot'] = $snapshot;
|
|
|
|
|
|
|
|
|
|
$activity->properties = $properties;
|
|
|
|
|
}
|
2025-12-30 15:03:19 +08:00
|
|
|
}
|