feat(inventory): 重構庫存盤點流程與優化操作日誌

1. 重構盤點流程:實作自動狀態轉換(盤點中/盤點完成)、整合按鈕為「儲存盤點結果」、更名 UI 狀態標籤。
2. 優化操作日誌:
   - 實作全域 ID 轉名稱邏輯(倉庫、使用者)。
   - 合併單次操作的日誌記錄,避免重複產生。
   - 修復日誌產生過程中的 Collection 修改錯誤。
3. 修正 TypeScript lint 錯誤(Index, Show 頁面)。
This commit is contained in:
2026-02-04 15:12:10 +08:00
parent f4f597e96d
commit 702af0a259
9 changed files with 291 additions and 59 deletions

View File

@@ -84,7 +84,7 @@ class CountDocController extends Controller
);
// 自動執行快照
$this->countService->snapshot($doc);
$this->countService->snapshot($doc, false);
return redirect()->route('inventory.count.show', [$doc->id])
->with('success', '已建立盤點單並完成庫存快照');
@@ -173,14 +173,37 @@ class CountDocController extends Controller
$this->countService->updateCount($doc, $validated['items']);
}
// 如果是按了 "完成盤點"
if ($request->input('action') === 'complete') {
$this->countService->complete($doc, auth()->id());
// 重新讀取以獲取最新狀態
$doc->refresh();
if ($doc->status === 'completed') {
return redirect()->route('inventory.count.index')
->with('success', '盤點單已完成');
->with('success', '盤點完成,單據已自動存檔並完成');
}
return redirect()->back()->with('success', '暫存成功');
return redirect()->back()->with('success', '盤點資料已暫存');
}
public function reopen(InventoryCountDoc $doc)
{
// 權限檢查 (通常僅允許有權限者執行,例如 inventory.adjust)
// 注意:前端已經用 <Can> 保護按鈕,後端這裡最好也加上檢查
if (!auth()->user()->can('inventory.adjust')) {
abort(403);
}
if ($doc->status !== 'completed') {
return redirect()->back()->with('error', '僅能針對已完成的盤點單重新開啟盤點');
}
// 執行取消核准邏輯
$doc->update([
'status' => 'counting', // 回復為盤點中
'completed_at' => null, // 清除完成時間
'completed_by' => null, // 清除完成者
]);
return redirect()->back()->with('success', '已重新開啟盤點,單據回復為盤點中狀態');
}
public function destroy(InventoryCountDoc $doc)
@@ -189,18 +212,7 @@ class CountDocController extends Controller
return redirect()->back()->with('error', '已完成的盤點單無法刪除');
}
// 記錄活動
activity()
->performedOn($doc)
->causedBy(auth()->user())
->event('deleted')
->withProperties([
'snapshot' => [
'doc_no' => $doc->doc_no,
'warehouse_name' => $doc->warehouse?->name,
]
])
->log('deleted');
// Activity Log handled by Model Trait
$doc->items()->delete();
$doc->delete();

View File

@@ -7,10 +7,13 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Modules\Core\Models\User;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class InventoryCountDoc extends Model
{
use HasFactory;
use LogsActivity;
protected $fillable = [
'doc_no',
@@ -75,4 +78,65 @@ class InventoryCountDoc extends Model
{
return $this->belongsTo(User::class, 'completed_by');
}
public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
{
return \Spatie\Activitylog\LogOptions::defaults()
->logAll()
->logOnlyDirty()
->dontSubmitEmptyLogs();
}
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
// 確保為陣列以進行修改
$properties = $activity->properties instanceof \Illuminate\Support\Collection
? $activity->properties->toArray()
: $activity->properties;
$snapshot = $properties['snapshot'] ?? [];
// Snapshot key information
$snapshot['doc_no'] = $this->doc_no;
$snapshot['warehouse_name'] = $this->warehouse ? $this->warehouse->name : null;
$snapshot['completed_at'] = $this->completed_at ? $this->completed_at->format('Y-m-d H:i:s') : null;
$snapshot['status'] = $this->status;
$snapshot['created_by_name'] = $this->createdBy ? $this->createdBy->name : null;
$snapshot['completed_by_name'] = $this->completedBy ? $this->completedBy->name : null;
$properties['snapshot'] = $snapshot;
// 全域 ID 轉名稱邏輯 (用於 attributes 與 old)
$convertIdsToNames = function (&$data) {
if (empty($data) || !is_array($data)) return;
// 倉庫 ID 轉換
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
$warehouse = \App\Modules\Inventory\Models\Warehouse::find($data['warehouse_id']);
if ($warehouse) {
$data['warehouse_id'] = $warehouse->name;
}
}
// 使用者 ID 轉換
$userFields = ['created_by', 'updated_by', 'completed_by'];
foreach ($userFields as $field) {
if (isset($data[$field]) && is_numeric($data[$field])) {
$user = \App\Modules\Core\Models\User::find($data[$field]);
if ($user) {
$data[$field] = $user->name;
}
}
}
};
if (isset($properties['attributes'])) {
$convertIdsToNames($properties['attributes']);
}
if (isset($properties['old'])) {
$convertIdsToNames($properties['old']);
}
$activity->properties = $properties;
}
}

View File

@@ -20,7 +20,8 @@ class CountService
return DB::transaction(function () use ($warehouseId, $remarks, $userId) {
$doc = InventoryCountDoc::create([
'warehouse_id' => $warehouseId,
'status' => 'draft',
'status' => 'counting',
'snapshot_date' => now(),
'remarks' => $remarks,
'created_by' => $userId,
]);
@@ -32,9 +33,9 @@ class CountService
/**
* 執行快照:鎖定當前庫存量
*/
public function snapshot(InventoryCountDoc $doc): void
public function snapshot(InventoryCountDoc $doc, bool $updateDoc = true): void
{
DB::transaction(function () use ($doc) {
DB::transaction(function () use ($doc, $updateDoc) {
// 清除舊的 items (如果有)
$doc->items()->delete();
@@ -62,10 +63,12 @@ class CountService
InventoryCountItem::insert($items);
}
$doc->update([
'status' => 'counting',
'snapshot_date' => now(),
]);
if ($updateDoc) {
$doc->update([
'status' => 'counting',
'snapshot_date' => now(),
]);
}
});
}
@@ -91,19 +94,111 @@ class CountService
public function updateCount(InventoryCountDoc $doc, array $itemsData): void
{
DB::transaction(function () use ($doc, $itemsData) {
$updatedItems = [];
$hasChanges = false;
$oldDocAttributes = [
'status' => $doc->status,
'completed_at' => $doc->completed_at ? $doc->completed_at->format('Y-m-d H:i:s') : null,
'completed_by' => $doc->completed_by,
];
foreach ($itemsData as $data) {
$item = $doc->items()->find($data['id']);
$item = $doc->items()->with('product')->find($data['id']);
if ($item) {
$countedQty = $data['counted_qty'];
$diff = is_numeric($countedQty) ? ($countedQty - $item->system_qty) : 0;
$oldQty = $item->counted_qty;
$newQty = $data['counted_qty'];
$oldNotes = $item->notes;
$newNotes = $data['notes'] ?? $item->notes;
$item->update([
'counted_qty' => $countedQty,
'diff_qty' => $diff,
'notes' => $data['notes'] ?? $item->notes,
]);
$isQtyChanged = $oldQty != $newQty;
$isNotesChanged = $oldNotes !== $newNotes;
if ($isQtyChanged || $isNotesChanged) {
$updatedItems[] = [
'product_name' => $item->product->name,
'old' => [
'counted_qty' => $oldQty,
'notes' => $oldNotes,
],
'new' => [
'counted_qty' => $newQty,
'notes' => $newNotes,
]
];
$countedQty = $data['counted_qty'];
$diff = is_numeric($countedQty) ? ($countedQty - $item->system_qty) : 0;
$item->update([
'counted_qty' => $countedQty,
'diff_qty' => $diff,
'notes' => $newNotes,
]);
$hasChanges = true;
}
}
}
// 檢查是否完成
$doc->refresh();
$isAllCounted = $doc->items()->whereNull('counted_qty')->count() === 0;
$newDocAttributesLog = [];
if ($isAllCounted) {
if ($doc->status !== 'completed') {
$doc->status = 'completed';
$doc->completed_at = now();
$doc->completed_by = auth()->id();
$doc->saveQuietly();
$doc->refresh(); // 獲取更新後的屬性 (如時間)
$newDocAttributesLog = [
'status' => 'completed',
'completed_at' => $doc->completed_at->format('Y-m-d H:i:s'),
'completed_by' => $doc->completed_by,
];
$hasChanges = true;
}
} else {
if ($doc->status === 'completed') {
$doc->status = 'counting';
$doc->completed_at = null;
$doc->completed_by = null;
$doc->saveQuietly();
$newDocAttributesLog = [
'status' => 'counting',
'completed_at' => null,
'completed_by' => null,
];
$hasChanges = true;
}
}
// 記錄操作日誌
if ($hasChanges) {
$properties = [
'items_diff' => [
'added' => [],
'removed' => [],
'updated' => $updatedItems,
],
];
// 如果有文件層級的屬性變更 (狀態),併入 log
if (!empty($newDocAttributesLog)) {
$properties['attributes'] = $newDocAttributesLog;
$properties['old'] = array_intersect_key($oldDocAttributes, $newDocAttributesLog);
}
activity()
->performedOn($doc)
->causedBy(auth()->user())
->event('updated')
->withProperties($properties)
->log('updated');
}
});
}
}