優化採購單與進貨單操作紀錄:新增品項明細、ID 轉名稱解析、前端多數量 key 通用顯示
- 重構 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:
@@ -38,10 +38,15 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
|||||||
$data['user_id'] = auth()->id();
|
$data['user_id'] = auth()->id();
|
||||||
$data['status'] = GoodsReceipt::STATUS_DRAFT; // 預設草稿
|
$data['status'] = GoodsReceipt::STATUS_DRAFT; // 預設草稿
|
||||||
|
|
||||||
// 2. Create Header
|
// 2. 靜默建立以抑制自動日誌(後續手動發送含品項明細的日誌)
|
||||||
$goodsReceipt = GoodsReceipt::create($data);
|
$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) {
|
foreach ($data['items'] as $itemData) {
|
||||||
// Create GR Item
|
// Create GR Item
|
||||||
$grItem = new GoodsReceiptItem([
|
$grItem = new GoodsReceiptItem([
|
||||||
@@ -54,8 +59,39 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
|||||||
'expiry_date' => $itemData['expiry_date'] ?? null,
|
'expiry_date' => $itemData['expiry_date'] ?? null,
|
||||||
]);
|
]);
|
||||||
$goodsReceipt->items()->save($grItem);
|
$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;
|
return $goodsReceipt;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -230,7 +230,8 @@ class PurchaseOrderController extends Controller
|
|||||||
$userId = $user->id;
|
$userId = $user->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
$order = PurchaseOrder::create([
|
// 靜默建立以抑制自動日誌(後續手動發送含品項明細的日誌)
|
||||||
|
$order = new PurchaseOrder([
|
||||||
'code' => $code,
|
'code' => $code,
|
||||||
'vendor_id' => $validated['vendor_id'],
|
'vendor_id' => $validated['vendor_id'],
|
||||||
'warehouse_id' => $validated['warehouse_id'],
|
'warehouse_id' => $validated['warehouse_id'],
|
||||||
@@ -246,6 +247,12 @@ class PurchaseOrderController extends Controller
|
|||||||
'invoice_date' => $validated['invoice_date'] ?? null,
|
'invoice_date' => $validated['invoice_date'] ?? null,
|
||||||
'invoice_amount' => $validated['invoice_amount'] ?? 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) {
|
foreach ($validated['items'] as $item) {
|
||||||
// 反算單價
|
// 反算單價
|
||||||
@@ -258,8 +265,43 @@ class PurchaseOrderController extends Controller
|
|||||||
'unit_price' => $unitPrice,
|
'unit_price' => $unitPrice,
|
||||||
'subtotal' => $item['subtotal'],
|
'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();
|
DB::commit();
|
||||||
} finally {
|
} finally {
|
||||||
$lock->release();
|
$lock->release();
|
||||||
@@ -619,8 +661,6 @@ class PurchaseOrderController extends Controller
|
|||||||
'snapshot' => [
|
'snapshot' => [
|
||||||
'po_number' => $order->code,
|
'po_number' => $order->code,
|
||||||
'vendor_name' => $order->vendor?->name,
|
'vendor_name' => $order->vendor?->name,
|
||||||
'warehouse_name' => $order->warehouse?->name,
|
|
||||||
'user_name' => $order->user?->name,
|
|
||||||
]
|
]
|
||||||
])
|
])
|
||||||
->log('updated');
|
->log('updated');
|
||||||
@@ -673,8 +713,6 @@ class PurchaseOrderController extends Controller
|
|||||||
'snapshot' => [
|
'snapshot' => [
|
||||||
'po_number' => $order->code,
|
'po_number' => $order->code,
|
||||||
'vendor_name' => $order->vendor?->name,
|
'vendor_name' => $order->vendor?->name,
|
||||||
'warehouse_name' => $order->warehouse?->name,
|
|
||||||
'user_name' => $order->user?->name,
|
|
||||||
]
|
]
|
||||||
])
|
])
|
||||||
->log('deleted');
|
->log('deleted');
|
||||||
|
|||||||
@@ -45,19 +45,52 @@ class PurchaseOrder extends Model
|
|||||||
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
{
|
{
|
||||||
$snapshot = $activity->properties['snapshot'] ?? [];
|
// 🚩 核心:轉換為陣列以避免 Indirect modification error
|
||||||
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
|
// 1. Snapshot 快照
|
||||||
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
$snapshot['po_number'] = $this->code;
|
$snapshot['po_number'] = $this->code;
|
||||||
|
$snapshot['vendor_name'] = $this->vendor?->name;
|
||||||
if ($this->vendor) {
|
// 倉庫名稱需透過服務取得(跨模組),若已在 snapshot 中則保留
|
||||||
$snapshot['vendor_name'] = $this->vendor->name;
|
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;
|
||||||
}
|
}
|
||||||
// Warehouse relation removed in Strict Mode. Snapshot should be set via manual hydration if needed,
|
$properties['snapshot'] = $snapshot;
|
||||||
// or during the procurement process where warehouse_id is known.
|
|
||||||
|
|
||||||
$activity->properties = $activity->properties->merge([
|
// 2. 名稱解析:自動將 attributes 與 old 中的 ID 換成人名/物名
|
||||||
'snapshot' => $snapshot
|
$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
|
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
|||||||
@@ -573,7 +573,14 @@ export default function ActivityDetailDialog({ open, onOpenChange, activity }: P
|
|||||||
{!Array.isArray(activity.properties?.items_diff) && (
|
{!Array.isArray(activity.properties?.items_diff) && (
|
||||||
<>
|
<>
|
||||||
{/* 更新項目 */}
|
{/* 更新項目 */}
|
||||||
{activity.properties?.items_diff?.updated?.map((item: any, idx: number) => (
|
{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">
|
<TableRow key={`upd-${idx}`} className="bg-blue-50/10 hover:bg-blue-50/20">
|
||||||
<TableCell className="font-medium">{item.product_name}</TableCell>
|
<TableCell className="font-medium">{item.product_name}</TableCell>
|
||||||
<TableCell className="text-center">
|
<TableCell className="text-center">
|
||||||
@@ -581,42 +588,61 @@ export default function ActivityDetailDialog({ open, onOpenChange, activity }: P
|
|||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell className="text-sm">
|
<TableCell className="text-sm">
|
||||||
<div className="space-y-1 text-xs">
|
<div className="space-y-1 text-xs">
|
||||||
{item.old?.quantity !== item.new?.quantity && item.old?.quantity !== undefined && (
|
{oldQty !== newQty && oldQty !== 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>
|
<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 && (
|
{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>盤點量: <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>
|
</div>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
)) || null}
|
);
|
||||||
|
}) || null}
|
||||||
|
|
||||||
{/* 新增項目 */}
|
{/* 新增項目 */}
|
||||||
{activity.properties?.items_diff?.added?.map((item: any, idx: number) => (
|
{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">
|
<TableRow key={`add-${idx}`} className="bg-green-50/10 hover:bg-green-50/20">
|
||||||
<TableCell className="font-medium">{item.product_name}</TableCell>
|
<TableCell className="font-medium">{item.product_name}</TableCell>
|
||||||
<TableCell className="text-center">
|
<TableCell className="text-center">
|
||||||
<Badge variant="outline" className="bg-green-50 text-green-700 border-green-200">新增</Badge>
|
<Badge variant="outline" className="bg-green-50 text-green-700 border-green-200">新增</Badge>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell className="text-sm">
|
<TableCell className="text-sm">
|
||||||
數量: {item.new?.quantity ?? item.quantity} {item.unit_name || item.new?.unit_name || ''}
|
<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>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
)) || null}
|
);
|
||||||
|
}) || null}
|
||||||
|
|
||||||
{/* 移除項目 */}
|
{/* 移除項目 */}
|
||||||
{activity.properties?.items_diff?.removed?.map((item: any, idx: number) => (
|
{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">
|
<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="font-medium text-gray-400 line-through">{item.product_name}</TableCell>
|
||||||
<TableCell className="text-center">
|
<TableCell className="text-center">
|
||||||
<Badge variant="outline" className="bg-red-50 text-red-700 border-red-200">移除</Badge>
|
<Badge variant="outline" className="bg-red-50 text-red-700 border-red-200">移除</Badge>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell className="text-sm text-gray-400">
|
<TableCell className="text-sm text-gray-400">
|
||||||
原數量: {item.old?.quantity ?? item.quantity} {item.unit_name || item.old?.unit_name || ''}
|
原數量: {qty} {item.unit_name || item.old?.unit_name || ''}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
)) || null}
|
);
|
||||||
|
}) || null}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|||||||
@@ -106,10 +106,7 @@ export default function CreatePurchaseOrder({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!expectedDate) {
|
|
||||||
toast.error("請選擇預計到貨日期");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items.length === 0) {
|
if (items.length === 0) {
|
||||||
toast.error("請至少新增一項採購商品");
|
toast.error("請至少新增一項採購商品");
|
||||||
@@ -140,7 +137,7 @@ export default function CreatePurchaseOrder({
|
|||||||
vendor_id: supplierId,
|
vendor_id: supplierId,
|
||||||
warehouse_id: warehouseId,
|
warehouse_id: warehouseId,
|
||||||
order_date: orderDate,
|
order_date: orderDate,
|
||||||
expected_delivery_date: expectedDate,
|
expected_delivery_date: expectedDate || null,
|
||||||
remark: notes,
|
remark: notes,
|
||||||
status: status,
|
status: status,
|
||||||
invoice_number: invoiceNumber || null,
|
invoice_number: invoiceNumber || null,
|
||||||
|
|||||||
Reference in New Issue
Block a user