star-cloud/app/Models/Machine/MachineStockMovement.php
sky121113 8feca33f13 [FEAT] 優化機台日誌管理、新增庫存變動紀錄與取貨碼/通行碼介面改善
1. 新增機台異常日誌手動解除功能,包含資料庫欄位更新與 UI 操作按鈕。
2. 實作機台庫存變動紀錄系統 (Machine Stock Movements),支援追蹤補貨與銷售產生的庫存異動。
3. 重構取貨碼 (Pickup Code) 與通行碼 (Pass Code) 管理介面,採用極簡奢華風 UI 並拆分 Partial Views 提升可維護性。
4. 優化取貨操作日誌,明確區分「取貨成功」與「取貨失敗」,並加入顏色標籤增強辨識度。
5. 擴充多語系支援 (繁中、英文、日文),確保所有新功能與操作狀態均有對應翻譯。
6. 更新 IoT API 規格文件與相關 Service 邏輯,加強指令確認 (ACK) 處理機制。
2026-05-04 08:43:35 +08:00

110 lines
2.5 KiB
PHP

<?php
namespace App\Models\Machine;
use App\Traits\TenantScoped;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use App\Models\Product\Product;
use App\Models\System\User;
class MachineStockMovement extends Model
{
use TenantScoped;
protected $fillable = [
'company_id',
'machine_id',
'product_id',
'slot_no',
'type',
'quantity',
'before_qty',
'after_qty',
'reference_type',
'reference_id',
'note',
'created_by',
];
protected $appends = [
'translated_note',
];
/**
* 動態翻譯備註
*/
public function getTranslatedNoteAttribute(): ?string
{
if (empty($this->note)) {
return null;
}
return __($this->note, $this->context ?? []);
}
protected $casts = [
'quantity' => 'integer',
'before_qty' => 'integer',
'after_qty' => 'integer',
'context' => 'array',
];
// ─── 異動類型常數 ───
const TYPE_REPLENISHMENT = 'replenishment'; // 補貨單完成
const TYPE_PICKUP = 'pickup'; // 取貨碼核銷
const TYPE_REMOTE_DISPENSE = 'remote_dispense'; // 遠端出貨 (B055 樂觀扣除)
const TYPE_ADJUSTMENT = 'adjustment'; // B009 回報 / 後台手動修改
const TYPE_ROLLBACK = 'rollback'; // B055 出貨失敗回退
// ─── 關聯 ───
public function machine(): BelongsTo
{
return $this->belongsTo(Machine::class);
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
/**
* 多型關聯:補貨單、取貨碼操作記錄、遠端指令等
*/
public function reference(): MorphTo
{
return $this->morphTo();
}
// ─── 輔助方法 ───
/**
* 是否為增加庫存的類型
*/
public function isInbound(): bool
{
return in_array($this->type, [
self::TYPE_REPLENISHMENT,
self::TYPE_ROLLBACK,
]);
}
/**
* 是否為減少庫存的類型
*/
public function isOutbound(): bool
{
return in_array($this->type, [
self::TYPE_PICKUP,
self::TYPE_REMOTE_DISPENSE,
]);
}
}