star-cloud/app/Models/Machine/MachineStockMovement.php
sky121113 34f1e823f0 [FIX] 修正庫存異動類型缺失與統一交易扣庫邏輯
1. 補全 machine_stock_movements 資料表 type 欄位缺失的 sale 與 decommission 列舉值,修復 SQL 寫入失敗問題。
2. 重構 TransactionService:移除過時的 remote_dispense 判斷,將所有經由交易流程(含支付代碼 100)的出貨統一記錄為 sale 異動。
3. 更新 MachineService:將 B009 貨道同步時的移除動作類型由 adjustment 改為正確的 decommission。
4. 語系更新:於 zh_TW.json 與 en.json 補全硬體狀態碼 0205 (貨道有物) 的翻譯。
2026-05-11 17:13:41 +08:00

120 lines
2.9 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',
'context',
'created_by',
];
protected $appends = [
'translated_note',
'translated_type',
];
public function getTranslatedNoteAttribute(): ?string
{
if (empty($this->note)) {
return null;
}
return __($this->note, $this->context ?? []);
}
/**
* 動態翻譯類型
*/
public function getTranslatedTypeAttribute(): string
{
return __("movement.type.{$this->type}");
}
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_SALE = 'sale'; // 一般訂單銷售
const TYPE_ADJUSTMENT = 'adjustment'; // B009 回報 / 後台手動修改
const TYPE_ROLLBACK = 'rollback'; // B055 出貨失敗回退
const TYPE_DECOMMISSION = 'decommission'; // B009 全量同步時移除不在清單的貨道
// ─── 關聯 ───
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,
self::TYPE_SALE,
]);
}
}