優化採購單與進貨單操作紀錄:新增品項明細、ID 轉名稱解析、前端多數量 key 通用顯示
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
ERP-Deploy-Production / deploy-production (push) Successful in 1m12s

- 重構 PurchaseOrder@tapActivity:支援 vendor_id/warehouse_id/user_id 自動解析為名稱
- 修改 PurchaseOrderController@store:改用 saveQuietly + 手動日誌,建立時紀錄品項明細
- 修正 PurchaseOrderController update/destroy snapshot 跨模組取值為 null 的問題
- 修改 GoodsReceiptService@store:改用 saveQuietly + 手動日誌,建立時紀錄品項明細
- 修改 ActivityDetailDialog.tsx:支援 quantity/quantity_received/requested_qty 多 key 通用渲染
- 新增項目顯示金額與備註,更新項目增加金額與備註變更對比
This commit is contained in:
2026-03-02 17:30:55 +08:00
parent 0a955fb993
commit 036f4a4fb6
5 changed files with 195 additions and 65 deletions

View File

@@ -38,10 +38,15 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
$data['user_id'] = auth()->id();
$data['status'] = GoodsReceipt::STATUS_DRAFT; // 預設草稿
// 2. Create Header
$goodsReceipt = GoodsReceipt::create($data);
// 2. 靜默建立以抑制自動日誌(後續手動發送含品項明細的日誌)
$goodsReceipt = new GoodsReceipt($data);
$goodsReceipt->saveQuietly();
// 3. 建立品項並收集 items_diff
$diff = ['added' => [], 'removed' => [], 'updated' => []];
$productIds = collect($data['items'])->pluck('product_id')->unique()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
// 3. Process Items
foreach ($data['items'] as $itemData) {
// Create GR Item
$grItem = new GoodsReceiptItem([
@@ -54,8 +59,39 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
'expiry_date' => $itemData['expiry_date'] ?? null,
]);
$goodsReceipt->items()->save($grItem);
$product = $products->get($itemData['product_id']);
$diff['added'][] = [
'product_name' => $product?->name ?? '未知商品',
'new' => [
'quantity_received' => (float)$itemData['quantity_received'],
'unit_price' => (float)$itemData['unit_price'],
'total_amount' => (float)($itemData['quantity_received'] * $itemData['unit_price']),
]
];
}
// 4. 手動發送高品質日誌(包含品項明細)
activity()
->performedOn($goodsReceipt)
->causedBy(auth()->user())
->event('created')
->withProperties([
'items_diff' => $diff,
'attributes' => [
'gr_number' => $goodsReceipt->code,
'type' => $goodsReceipt->type,
'warehouse_id' => $goodsReceipt->warehouse_id,
'vendor_id' => $goodsReceipt->vendor_id,
'purchase_order_id' => $goodsReceipt->purchase_order_id,
'received_date' => $goodsReceipt->received_date,
'status' => $goodsReceipt->status,
'remarks' => $goodsReceipt->remarks,
'user_id' => $goodsReceipt->user_id,
]
])
->log('created');
return $goodsReceipt;
});
}

View File

@@ -230,7 +230,8 @@ class PurchaseOrderController extends Controller
$userId = $user->id;
}
$order = PurchaseOrder::create([
// 靜默建立以抑制自動日誌(後續手動發送含品項明細的日誌)
$order = new PurchaseOrder([
'code' => $code,
'vendor_id' => $validated['vendor_id'],
'warehouse_id' => $validated['warehouse_id'],
@@ -246,6 +247,12 @@ class PurchaseOrderController extends Controller
'invoice_date' => $validated['invoice_date'] ?? null,
'invoice_amount' => $validated['invoice_amount'] ?? null,
]);
$order->saveQuietly();
// 建立品項並收集 items_diff
$diff = ['added' => [], 'removed' => [], 'updated' => []];
$productIds = collect($validated['items'])->pluck('productId')->unique()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
foreach ($validated['items'] as $item) {
// 反算單價
@@ -258,8 +265,43 @@ class PurchaseOrderController extends Controller
'unit_price' => $unitPrice,
'subtotal' => $item['subtotal'],
]);
$product = $products->get($item['productId']);
$diff['added'][] = [
'product_name' => $product?->name ?? '未知商品',
'new' => [
'quantity' => (float)$item['quantity'],
'subtotal' => (float)$item['subtotal'],
]
];
}
// 手動發送高品質日誌(包含品項明細)
activity()
->performedOn($order)
->causedBy($userId)
->event('created')
->withProperties([
'items_diff' => $diff,
'attributes' => [
'po_number' => $order->code,
'vendor_id' => $order->vendor_id,
'warehouse_id' => $order->warehouse_id,
'user_id' => $order->user_id,
'status' => $order->status,
'order_date' => $order->order_date,
'expected_delivery_date' => $order->expected_delivery_date,
'total_amount' => $order->total_amount,
'tax_amount' => $order->tax_amount,
'grand_total' => $order->grand_total,
'remark' => $order->remark,
'invoice_number' => $order->invoice_number,
'invoice_date' => $order->invoice_date,
'invoice_amount' => $order->invoice_amount,
]
])
->log('created');
DB::commit();
} finally {
$lock->release();
@@ -619,8 +661,6 @@ class PurchaseOrderController extends Controller
'snapshot' => [
'po_number' => $order->code,
'vendor_name' => $order->vendor?->name,
'warehouse_name' => $order->warehouse?->name,
'user_name' => $order->user?->name,
]
])
->log('updated');
@@ -673,8 +713,6 @@ class PurchaseOrderController extends Controller
'snapshot' => [
'po_number' => $order->code,
'vendor_name' => $order->vendor?->name,
'warehouse_name' => $order->warehouse?->name,
'user_name' => $order->user?->name,
]
])
->log('deleted');

View File

@@ -45,19 +45,52 @@ class PurchaseOrder extends Model
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$snapshot = $activity->properties['snapshot'] ?? [];
$snapshot['po_number'] = $this->code;
if ($this->vendor) {
$snapshot['vendor_name'] = $this->vendor->name;
}
// Warehouse relation removed in Strict Mode. Snapshot should be set via manual hydration if needed,
// or during the procurement process where warehouse_id is known.
// 🚩 核心:轉換為陣列以避免 Indirect modification error
$properties = $activity->properties instanceof \Illuminate\Support\Collection
? $activity->properties->toArray()
: $activity->properties;
$activity->properties = $activity->properties->merge([
'snapshot' => $snapshot
]);
// 1. Snapshot 快照
$snapshot = $properties['snapshot'] ?? [];
$snapshot['po_number'] = $this->code;
$snapshot['vendor_name'] = $this->vendor?->name;
// 倉庫名稱需透過服務取得(跨模組),若已在 snapshot 中則保留
if (!isset($snapshot['warehouse_name']) && $this->warehouse_id) {
$warehouse = app(\App\Modules\Inventory\Contracts\InventoryServiceInterface::class)->getWarehouse($this->warehouse_id);
$snapshot['warehouse_name'] = $warehouse?->name ?? null;
}
$properties['snapshot'] = $snapshot;
// 2. 名稱解析:自動將 attributes 與 old 中的 ID 換成人名/物名
$resolver = function (&$data) {
if (empty($data) || !is_array($data)) return;
// 使用者 ID 轉換
foreach (['user_id', 'created_by', 'updated_by'] as $f) {
if (isset($data[$f]) && is_numeric($data[$f])) {
$data[$f] = \App\Modules\Core\Models\User::find($data[$f])?->name ?? $data[$f];
}
}
// 廠商 ID 轉換
if (isset($data['vendor_id']) && is_numeric($data['vendor_id'])) {
$data['vendor_id'] = Vendor::find($data['vendor_id'])?->name ?? $data['vendor_id'];
}
// 倉庫 ID 轉換(跨模組,透過服務)
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
$warehouse = app(\App\Modules\Inventory\Contracts\InventoryServiceInterface::class)->getWarehouse($data['warehouse_id']);
$data['warehouse_id'] = $warehouse?->name ?? $data['warehouse_id'];
}
};
if (isset($properties['attributes'])) $resolver($properties['attributes']);
if (isset($properties['old'])) $resolver($properties['old']);
// 3. 合併 activityProperties (手動傳入的 items_diff 等)
if (!empty($this->activityProperties)) {
$properties = array_merge($properties, $this->activityProperties);
}
$activity->properties = $properties;
}
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo