star-cloud/app/Models/Transaction/PickupCode.php
sky121113 772c7a62e8 [STYLE] 庫存管理響應式 UI 優化與卡片顯示格式修正
1. 優化「目前庫存」分頁:修復平板模式下卡片展開導致的高度拉伸問題,並確保 Alpine.js 變數 (localCardOpen) 作用域隔離。
2. 優化「異動紀錄」分頁:將時間戳記從獨立行移入 2x2 內容網格,補上「時間」標題並修正字體顏色。
3. 優化「進貨單管理」分頁:卡片日期顯示格式修正為完整時分秒 (Y-m-d H:i:s)。
4. 統一搜尋佈局:將庫存管理各分頁與機台補貨單的「搜尋」與「重置」按鈕群組化,確保在窄螢幕換行時能保持連動。
5. 修正進貨單詳情:優化側邊欄 (Slide-over) 的資料載入邏輯與多語系顯示。
2026-04-27 15:40:57 +08:00

72 lines
1.5 KiB
PHP

<?php
namespace App\Models\Transaction;
use App\Models\Machine\Machine;
use App\Models\User;
use App\Traits\TenantScoped;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PickupCode extends Model
{
use TenantScoped;
protected $fillable = [
'company_id',
'machine_id',
'slot_no',
'code',
'expires_at',
'used_at',
'status',
'created_by',
];
protected $casts = [
'expires_at' => 'datetime',
'used_at' => 'datetime',
];
/**
* 關聯機台
*/
public function machine(): BelongsTo
{
return $this->belongsTo(Machine::class);
}
/**
* 關聯建立者
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
/**
* 判斷是否可用
*/
public function isValid(): bool
{
return $this->status === 'active' && $this->expires_at->isFuture();
}
/**
* 產生唯一的取貨碼 (4位)
*/
public static function generateUniqueCode(int $machineId): string
{
do {
$code = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);
$exists = self::where('machine_id', $machineId)
->where('code', $code)
->where('status', 'active')
->where('expires_at', '>', now())
->exists();
} while ($exists);
return $code;
}
}