feat: 整合門市領料日誌、API 文件存取、修改庫存與併發編號問題、供應商商品內聯編輯及日誌 UI 優化
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
This commit is contained in:
@@ -23,24 +23,77 @@ class StoreRequisitionService
|
||||
/**
|
||||
* 建立叫貨單(含明細)
|
||||
*/
|
||||
public function create(array $data, array $items, int $userId): StoreRequisition
|
||||
public function create(array $data, array $items, int $userId, bool $submitImmediately = false): StoreRequisition
|
||||
{
|
||||
return DB::transaction(function () use ($data, $items, $userId) {
|
||||
$requisition = StoreRequisition::create([
|
||||
return DB::transaction(function () use ($data, $items, $userId, $submitImmediately) {
|
||||
$requisition = new StoreRequisition([
|
||||
'store_warehouse_id' => $data['store_warehouse_id'],
|
||||
'status' => 'draft',
|
||||
'status' => $submitImmediately ? 'pending' : 'draft',
|
||||
'submitted_at' => $submitImmediately ? now() : null,
|
||||
'remark' => $data['remark'] ?? null,
|
||||
'created_by' => $userId,
|
||||
]);
|
||||
|
||||
// 手動產生單號,因為 saveQuietly 會繞過模型事件
|
||||
if (empty($requisition->doc_no)) {
|
||||
$today = date('Ymd');
|
||||
$prefix = 'SR-' . $today . '-';
|
||||
$lastDoc = StoreRequisition::where('doc_no', 'like', $prefix . '%')
|
||||
->orderBy('doc_no', 'desc')
|
||||
->first();
|
||||
if ($lastDoc) {
|
||||
$lastNumber = substr($lastDoc->doc_no, -2);
|
||||
$nextNumber = str_pad((int)$lastNumber + 1, 2, '0', STR_PAD_LEFT);
|
||||
} else {
|
||||
$nextNumber = '01';
|
||||
}
|
||||
$requisition->doc_no = $prefix . $nextNumber;
|
||||
}
|
||||
|
||||
// 靜默建立以抑制自動日誌
|
||||
$requisition->saveQuietly();
|
||||
|
||||
$diff = ['added' => [], 'removed' => [], 'updated' => []];
|
||||
foreach ($items as $item) {
|
||||
$requisition->items()->create([
|
||||
'product_id' => $item['product_id'],
|
||||
'requested_qty' => $item['requested_qty'],
|
||||
'remark' => $item['remark'] ?? null,
|
||||
]);
|
||||
|
||||
$product = \App\Modules\Inventory\Models\Product::find($item['product_id']);
|
||||
$diff['added'][] = [
|
||||
'product_name' => $product?->name ?? '未知商品',
|
||||
'new' => [
|
||||
'quantity' => (float)$item['requested_qty'],
|
||||
'remark' => $item['remark'] ?? null,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
// 如果需直接提交,觸發通知
|
||||
if ($submitImmediately) {
|
||||
$this->notifyApprovers($requisition, 'submitted', $userId);
|
||||
}
|
||||
|
||||
// 手動發送高品質日誌
|
||||
activity()
|
||||
->performedOn($requisition)
|
||||
->causedBy($userId)
|
||||
->event('created')
|
||||
->withProperties([
|
||||
'items_diff' => $diff,
|
||||
'attributes' => [
|
||||
'doc_no' => $requisition->doc_no,
|
||||
'store_warehouse_id' => $requisition->store_warehouse_id,
|
||||
'status' => $requisition->status,
|
||||
'remark' => $requisition->remark,
|
||||
'created_by' => $requisition->created_by,
|
||||
'submitted_at' => $requisition->submitted_at,
|
||||
]
|
||||
])
|
||||
->log('created');
|
||||
|
||||
return $requisition->load('items');
|
||||
});
|
||||
}
|
||||
@@ -57,13 +110,74 @@ class StoreRequisitionService
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($requisition, $data, $items) {
|
||||
$requisition->update([
|
||||
'store_warehouse_id' => $data['store_warehouse_id'],
|
||||
'remark' => $data['remark'] ?? null,
|
||||
'reject_reason' => null, // 清除駁回原因
|
||||
]);
|
||||
// 擷取舊狀態供日誌對照
|
||||
$oldAttributes = [
|
||||
'store_warehouse_id' => $requisition->store_warehouse_id,
|
||||
'remark' => $requisition->remark,
|
||||
];
|
||||
|
||||
// 重建明細
|
||||
// 手動更新屬性
|
||||
$requisition->store_warehouse_id = $data['store_warehouse_id'];
|
||||
$requisition->remark = $data['remark'] ?? null;
|
||||
$requisition->reject_reason = null; // 清除駁回原因
|
||||
|
||||
// 品項對比邏輯
|
||||
$oldItems = $requisition->items()->with('product:id,name')->get();
|
||||
$oldItemsMap = $oldItems->keyBy('product_id');
|
||||
$newItemsMap = collect($items)->keyBy('product_id');
|
||||
|
||||
$diff = [
|
||||
'added' => [],
|
||||
'removed' => [],
|
||||
'updated' => [],
|
||||
];
|
||||
|
||||
// 1. 處理更新與新增
|
||||
foreach ($items as $itemData) {
|
||||
$productId = $itemData['product_id'];
|
||||
$newQty = (float)$itemData['requested_qty'];
|
||||
$newRemark = $itemData['remark'] ?? null;
|
||||
|
||||
if ($oldItemsMap->has($productId)) {
|
||||
$oldItem = $oldItemsMap->get($productId);
|
||||
if ((float)$oldItem->requested_qty !== $newQty || $oldItem->remark !== $newRemark) {
|
||||
$diff['updated'][] = [
|
||||
'product_name' => $oldItem->product?->name ?? '未知商品',
|
||||
'old' => [
|
||||
'quantity' => (float)$oldItem->requested_qty,
|
||||
'remark' => $oldItem->remark,
|
||||
],
|
||||
'new' => [
|
||||
'quantity' => $newQty,
|
||||
'remark' => $newRemark,
|
||||
]
|
||||
];
|
||||
}
|
||||
$oldItemsMap->forget($productId);
|
||||
} else {
|
||||
$product = \App\Modules\Inventory\Models\Product::find($productId);
|
||||
$diff['added'][] = [
|
||||
'product_name' => $product?->name ?? '未知商品',
|
||||
'new' => [
|
||||
'quantity' => $newQty,
|
||||
'remark' => $newRemark,
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 處理移除
|
||||
foreach ($oldItemsMap as $productId => $oldItem) {
|
||||
$diff['removed'][] = [
|
||||
'product_name' => $oldItem->product?->name ?? '未知商品',
|
||||
'old' => [
|
||||
'quantity' => (float)$oldItem->requested_qty,
|
||||
'remark' => $oldItem->remark,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
// 儲存實際變動
|
||||
$requisition->items()->delete();
|
||||
foreach ($items as $item) {
|
||||
$requisition->items()->create([
|
||||
@@ -73,6 +187,32 @@ class StoreRequisitionService
|
||||
]);
|
||||
}
|
||||
|
||||
// 檢查是否有任何變動 (主表或明細)
|
||||
$isDirty = $requisition->isDirty();
|
||||
$hasItemsDiff = !empty($diff['added']) || !empty($diff['removed']) || !empty($diff['updated']);
|
||||
|
||||
if ($isDirty || $hasItemsDiff) {
|
||||
// 擷取新狀態
|
||||
$newAttributes = [
|
||||
'store_warehouse_id' => $requisition->store_warehouse_id,
|
||||
'remark' => $requisition->remark,
|
||||
];
|
||||
|
||||
// 靜默更新
|
||||
$requisition->saveQuietly();
|
||||
|
||||
// 手動發送紀錄
|
||||
activity()
|
||||
->performedOn($requisition)
|
||||
->event('updated')
|
||||
->withProperties([
|
||||
'items_diff' => $diff,
|
||||
'attributes' => $newAttributes,
|
||||
'old' => $oldAttributes
|
||||
])
|
||||
->log('updated');
|
||||
}
|
||||
|
||||
return $requisition->load('items');
|
||||
});
|
||||
}
|
||||
@@ -241,6 +381,27 @@ class StoreRequisitionService
|
||||
|
||||
if (!empty($transferItems)) {
|
||||
$this->transferService->updateItems($transferOrder, $transferItems);
|
||||
|
||||
// 手動發送調撥單的「已建立」合併日誌,包含初始明細
|
||||
activity()
|
||||
->performedOn($transferOrder)
|
||||
->causedBy($userId)
|
||||
->event('created')
|
||||
->withProperties(array_merge(
|
||||
['items_diff' => $transferOrder->activityProperties['items_diff'] ?? []],
|
||||
[
|
||||
'attributes' => [
|
||||
'doc_no' => $transferOrder->doc_no,
|
||||
'from_warehouse_id' => $transferOrder->from_warehouse_id,
|
||||
'to_warehouse_id' => $transferOrder->to_warehouse_id,
|
||||
'transit_warehouse_id' => $transferOrder->transit_warehouse_id,
|
||||
'remarks' => $transferOrder->remarks,
|
||||
'status' => $transferOrder->status,
|
||||
'created_by' => $transferOrder->created_by,
|
||||
]
|
||||
]
|
||||
))
|
||||
->log('created');
|
||||
}
|
||||
|
||||
// 更新叫貨單狀態
|
||||
|
||||
Reference in New Issue
Block a user