1. 在 StaffCardLog 模型中實作 booted() 事件監聽器,於刷卡日誌建立後自動更新關聯員工識別卡的「最後使用時間」與「最後使用機台」,並使用 withoutGlobalScopes 確保寫入安全。 2. 修正 StaffCardController 刪除方法(destroy),判斷 AJAX 請求以回傳 JSON,其餘一般表單刪除後則正常重導向回列表頁,解決刪除時跳轉至 JSON 純文字的問題。 3. 補齊多語系語系檔(zh_TW.json, en.json, ja.json)中有關「刪除員工識別卡」的翻譯對照,確保 UI 刪除警告在不同語言環境下均能完美呈現。
76 lines
1.7 KiB
PHP
76 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\TenantScoped;
|
|
use App\Models\Machine\Machine;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class StaffCardLog extends Model
|
|
{
|
|
use TenantScoped;
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $fillable = [
|
|
'company_id',
|
|
'staff_card_id',
|
|
'machine_id',
|
|
'payment_type',
|
|
'code_id',
|
|
'order_id',
|
|
'action',
|
|
'remark',
|
|
'raw_data',
|
|
];
|
|
|
|
protected $casts = [
|
|
'raw_data' => 'array',
|
|
'created_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* 註冊 Model 事件以自動更新 StaffCard 的最後使用狀態
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::created(function ($log) {
|
|
if ($log->staff_card_id) {
|
|
// 使用 withoutGlobalScopes 確保不論在任何 Session Context 下皆能正確寫入
|
|
$staffCard = StaffCard::withoutGlobalScopes()->find($log->staff_card_id);
|
|
if ($staffCard) {
|
|
$staffCard->update([
|
|
'last_used_at' => $log->created_at ?? now(),
|
|
'last_machine_id' => $log->machine_id,
|
|
]);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 取得關聯的員工卡片
|
|
*/
|
|
public function staffCard(): BelongsTo
|
|
{
|
|
return $this->belongsTo(StaffCard::class);
|
|
}
|
|
|
|
/**
|
|
* 取得刷卡的機台
|
|
*/
|
|
public function machine(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Machine::class);
|
|
}
|
|
|
|
/**
|
|
* 取得關聯的訂單
|
|
*/
|
|
public function order(): BelongsTo
|
|
{
|
|
return $this->belongsTo(\App\Models\Transaction\Order::class);
|
|
}
|
|
}
|