feat: 修正 BOM 單位顯示與完工入庫彈窗 UI 統一規範
This commit is contained in:
@@ -11,6 +11,14 @@ class ProductionOrder extends Model
|
||||
{
|
||||
use HasFactory, LogsActivity;
|
||||
|
||||
// 狀態常數
|
||||
const STATUS_DRAFT = 'draft';
|
||||
const STATUS_PENDING = 'pending';
|
||||
const STATUS_APPROVED = 'approved';
|
||||
const STATUS_IN_PROGRESS = 'in_progress';
|
||||
const STATUS_COMPLETED = 'completed';
|
||||
const STATUS_CANCELLED = 'cancelled';
|
||||
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'product_id',
|
||||
@@ -25,6 +33,51 @@ class ProductionOrder extends Model
|
||||
'remark',
|
||||
];
|
||||
|
||||
/**
|
||||
* 檢查是否可以轉移至新狀態,並驗證權限。
|
||||
*/
|
||||
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 = [
|
||||
self::STATUS_DRAFT => [
|
||||
self::STATUS_PENDING => 'production_orders.view', // 基本檢視者即可送審
|
||||
self::STATUS_CANCELLED => 'production_orders.cancel',
|
||||
],
|
||||
self::STATUS_PENDING => [
|
||||
self::STATUS_APPROVED => 'production_orders.approve',
|
||||
self::STATUS_DRAFT => 'production_orders.approve', // 退回草稿
|
||||
self::STATUS_CANCELLED => 'production_orders.cancel',
|
||||
],
|
||||
self::STATUS_APPROVED => [
|
||||
self::STATUS_IN_PROGRESS => 'production_orders.edit', // 啟動製作需要編輯權限
|
||||
self::STATUS_CANCELLED => 'production_orders.cancel',
|
||||
],
|
||||
self::STATUS_IN_PROGRESS => [
|
||||
self::STATUS_COMPLETED => 'production_orders.edit', // 完成製作需要編輯權限
|
||||
self::STATUS_CANCELLED => 'production_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;
|
||||
}
|
||||
|
||||
protected $casts = [
|
||||
'production_date' => 'date',
|
||||
'expiry_date' => 'date',
|
||||
|
||||
Reference in New Issue
Block a user