優化採購單與進貨單操作紀錄:新增品項明細、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

View File

@@ -573,50 +573,76 @@ export default function ActivityDetailDialog({ open, onOpenChange, activity }: P
{!Array.isArray(activity.properties?.items_diff) && (
<>
{/* 更新項目 */}
{activity.properties?.items_diff?.updated?.map((item: any, idx: number) => (
<TableRow key={`upd-${idx}`} className="bg-blue-50/10 hover:bg-blue-50/20">
<TableCell className="font-medium">{item.product_name}</TableCell>
<TableCell className="text-center">
<Badge variant="outline" className="bg-blue-50 text-blue-700 border-blue-200"></Badge>
</TableCell>
<TableCell className="text-sm">
<div className="space-y-1 text-xs">
{item.old?.quantity !== item.new?.quantity && item.old?.quantity !== undefined && (
<div>: <span className="text-gray-500 line-through">{item.old.quantity}</span> <span className="text-blue-700 font-bold">{item.new.quantity}</span></div>
)}
{item.old?.counted_qty !== item.new?.counted_qty && item.old?.counted_qty !== undefined && (
<div>: <span className="text-gray-500 line-through">{item.old.counted_qty ?? '未盤'}</span> <span className="text-blue-700 font-bold">{item.new.counted_qty ?? '未盤'}</span></div>
)}
</div>
</TableCell>
</TableRow>
)) || null}
{activity.properties?.items_diff?.updated?.map((item: any, idx: number) => {
const getQty = (obj: any) => obj?.quantity ?? obj?.quantity_received ?? obj?.requested_qty;
const getAmt = (obj: any) => obj?.subtotal ?? obj?.total_amount;
const oldQty = getQty(item.old);
const newQty = getQty(item.new);
const oldAmt = getAmt(item.old);
const newAmt = getAmt(item.new);
return (
<TableRow key={`upd-${idx}`} className="bg-blue-50/10 hover:bg-blue-50/20">
<TableCell className="font-medium">{item.product_name}</TableCell>
<TableCell className="text-center">
<Badge variant="outline" className="bg-blue-50 text-blue-700 border-blue-200"></Badge>
</TableCell>
<TableCell className="text-sm">
<div className="space-y-1 text-xs">
{oldQty !== newQty && oldQty !== undefined && (
<div>: <span className="text-gray-500 line-through">{oldQty}</span> <span className="text-blue-700 font-bold">{newQty}</span></div>
)}
{oldAmt !== newAmt && oldAmt !== undefined && (
<div>: <span className="text-gray-500 line-through">{oldAmt}</span> <span className="text-blue-700 font-bold">{newAmt}</span></div>
)}
{item.old?.counted_qty !== item.new?.counted_qty && item.old?.counted_qty !== undefined && (
<div>: <span className="text-gray-500 line-through">{item.old.counted_qty ?? '未盤'}</span> <span className="text-blue-700 font-bold">{item.new.counted_qty ?? '未盤'}</span></div>
)}
{item.old?.remark !== item.new?.remark && item.old?.remark !== undefined && (
<div>: <span className="text-gray-500 line-through">{item.old.remark || '無'}</span> <span className="text-blue-700 font-bold">{item.new.remark || '無'}</span></div>
)}
</div>
</TableCell>
</TableRow>
);
}) || null}
{/* 新增項目 */}
{activity.properties?.items_diff?.added?.map((item: any, idx: number) => (
<TableRow key={`add-${idx}`} className="bg-green-50/10 hover:bg-green-50/20">
<TableCell className="font-medium">{item.product_name}</TableCell>
<TableCell className="text-center">
<Badge variant="outline" className="bg-green-50 text-green-700 border-green-200"></Badge>
</TableCell>
<TableCell className="text-sm">
: {item.new?.quantity ?? item.quantity} {item.unit_name || item.new?.unit_name || ''}
</TableCell>
</TableRow>
)) || null}
{activity.properties?.items_diff?.added?.map((item: any, idx: number) => {
const qty = item.new?.quantity ?? item.new?.quantity_received ?? item.new?.requested_qty ?? item.quantity;
const amt = item.new?.subtotal ?? item.new?.total_amount;
const remark = item.new?.remark;
return (
<TableRow key={`add-${idx}`} className="bg-green-50/10 hover:bg-green-50/20">
<TableCell className="font-medium">{item.product_name}</TableCell>
<TableCell className="text-center">
<Badge variant="outline" className="bg-green-50 text-green-700 border-green-200"></Badge>
</TableCell>
<TableCell className="text-sm">
<div className="space-y-0.5 text-xs">
<div>: {qty} {item.unit_name || item.new?.unit_name || ''}</div>
{amt !== undefined && <div>: {amt}</div>}
{remark && <div>: {remark}</div>}
</div>
</TableCell>
</TableRow>
);
}) || null}
{/* 移除項目 */}
{activity.properties?.items_diff?.removed?.map((item: any, idx: number) => (
<TableRow key={`rem-${idx}`} className="bg-red-50/10 hover:bg-red-50/20">
<TableCell className="font-medium text-gray-400 line-through">{item.product_name}</TableCell>
<TableCell className="text-center">
<Badge variant="outline" className="bg-red-50 text-red-700 border-red-200"></Badge>
</TableCell>
<TableCell className="text-sm text-gray-400">
: {item.old?.quantity ?? item.quantity} {item.unit_name || item.old?.unit_name || ''}
</TableCell>
</TableRow>
)) || null}
{activity.properties?.items_diff?.removed?.map((item: any, idx: number) => {
const qty = item.old?.quantity ?? item.old?.quantity_received ?? item.old?.requested_qty ?? item.quantity ?? item.quantity_received;
return (
<TableRow key={`rem-${idx}`} className="bg-red-50/10 hover:bg-red-50/20">
<TableCell className="font-medium text-gray-400 line-through">{item.product_name}</TableCell>
<TableCell className="text-center">
<Badge variant="outline" className="bg-red-50 text-red-700 border-red-200"></Badge>
</TableCell>
<TableCell className="text-sm text-gray-400">
: {qty} {item.unit_name || item.old?.unit_name || ''}
</TableCell>
</TableRow>
);
}) || null}
</>
)}

View File

@@ -106,10 +106,7 @@ export default function CreatePurchaseOrder({
return;
}
if (!expectedDate) {
toast.error("請選擇預計到貨日期");
return;
}
if (items.length === 0) {
toast.error("請至少新增一項採購商品");
@@ -140,7 +137,7 @@ export default function CreatePurchaseOrder({
vendor_id: supplierId,
warehouse_id: warehouseId,
order_date: orderDate,
expected_delivery_date: expectedDate,
expected_delivery_date: expectedDate || null,
remark: notes,
status: status,
invoice_number: invoiceNumber || null,