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',
|
2026-01-26 17:27:34 +08:00
|
|
|
'order_date',
|
2026-01-26 10:37:47 +08:00
|
|
|
'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 = [
|
2026-01-26 17:27:34 +08:00
|
|
|
'order_date' => 'date',
|
2026-01-26 10:37:47 +08:00
|
|
|
'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);
|
|
|
|
|
}
|
2026-02-06 15:32:12 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 檢查是否可以轉移至新狀態,並驗證權限。
|
|
|
|
|
*/
|
|
|
|
|
public function canTransitionTo(string $newStatus, $user = null): bool
|
|
|
|
|
{
|
|
|
|
|
$user = $user ?? auth()->user();
|
|
|
|
|
if (!$user) return false;
|
|
|
|
|
if ($user->hasRole('super-admin')) return true;
|
|
|
|
|
|
|
|
|
|
$currentStatus = $this->status;
|
|
|
|
|
|
|
|
|
|
// 定義合法的狀態轉移路徑與所需權限
|
|
|
|
|
$transitions = [
|
|
|
|
|
'draft' => [
|
|
|
|
|
'pending' => 'purchase_orders.view', // 基本檢視者即可送審
|
|
|
|
|
'cancelled' => 'purchase_orders.cancel',
|
|
|
|
|
],
|
|
|
|
|
'pending' => [
|
|
|
|
|
'approved' => 'purchase_orders.approve',
|
|
|
|
|
'draft' => 'purchase_orders.approve', // 退回草稿
|
|
|
|
|
'cancelled' => 'purchase_orders.cancel',
|
|
|
|
|
],
|
|
|
|
|
'approved' => [
|
|
|
|
|
'cancelled' => 'purchase_orders.cancel',
|
|
|
|
|
'partial' => null, // 系統自動轉移,不需手動權限點
|
|
|
|
|
],
|
|
|
|
|
'partial' => [
|
|
|
|
|
'completed' => null, // 系統自動轉移
|
|
|
|
|
'closed' => 'purchase_orders.approve', // 手動結案通常需要核准權限
|
|
|
|
|
'cancelled' => 'purchase_orders.cancel',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (!isset($transitions[$currentStatus])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!array_key_exists($newStatus, $transitions[$currentStatus])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$requiredPermission = $transitions[$currentStatus][$newStatus];
|
|
|
|
|
|
|
|
|
|
return $requiredPermission ? $user->can($requiredPermission) : true;
|
|
|
|
|
}
|
2026-01-26 10:37:47 +08:00
|
|
|
}
|