2026-01-26 10:37:47 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Modules\Procurement\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-01-26 14:59:24 +08:00
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
|
|
|
|
|
class PurchaseOrder extends Model
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<\Database\Factories\PurchaseOrderFactory> */
|
|
|
|
|
use HasFactory;
|
|
|
|
|
use \Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
2026-01-26 14:59:24 +08:00
|
|
|
'code',
|
2026-01-26 10:37:47 +08:00
|
|
|
'vendor_id',
|
|
|
|
|
'warehouse_id',
|
|
|
|
|
'user_id',
|
|
|
|
|
'expected_delivery_date',
|
|
|
|
|
'status',
|
|
|
|
|
'total_amount',
|
2026-01-26 14:59:24 +08:00
|
|
|
'tax_amount',
|
|
|
|
|
'grand_total',
|
|
|
|
|
'remark',
|
2026-01-26 10:37:47 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'expected_delivery_date' => 'date',
|
|
|
|
|
'total_amount' => 'decimal:2',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
|
|
|
|
|
{
|
|
|
|
|
return \Spatie\Activitylog\LogOptions::defaults()
|
|
|
|
|
->logAll()
|
|
|
|
|
->logOnlyDirty()
|
|
|
|
|
->dontSubmitEmptyLogs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
|
|
|
|
{
|
|
|
|
|
$snapshot = $activity->properties['snapshot'] ?? [];
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
$snapshot['po_number'] = $this->code;
|
2026-01-26 10:37:47 +08:00
|
|
|
|
|
|
|
|
if ($this->vendor) {
|
|
|
|
|
$snapshot['vendor_name'] = $this->vendor->name;
|
|
|
|
|
}
|
2026-01-26 14:59:24 +08:00
|
|
|
// Warehouse relation removed in Strict Mode. Snapshot should be set via manual hydration if needed,
|
|
|
|
|
// or during the procurement process where warehouse_id is known.
|
2026-01-26 10:37:47 +08:00
|
|
|
|
|
|
|
|
$activity->properties = $activity->properties->merge([
|
|
|
|
|
'snapshot' => $snapshot
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Vendor::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
|
|
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
|
|
|
|
|
public function items(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(PurchaseOrderItem::class);
|
|
|
|
|
}
|
|
|
|
|
}
|