|
|
@@ -1166,23 +1166,42 @@ open fun newBatchSubmit(request: QrPickBatchSubmitRequest): MessageResponse { |
|
|
val actualInventoryLotLineId = line.inventoryLotLineId |
|
|
val actualInventoryLotLineId = line.inventoryLotLineId |
|
|
?: stockOutLine.inventoryLotLine?.id |
|
|
?: stockOutLine.inventoryLotLine?.id |
|
|
|
|
|
|
|
|
if (submitQty > BigDecimal.ZERO && actualInventoryLotLineId != null) { |
|
|
|
|
|
println(" Updating inventory lot line ${actualInventoryLotLineId} with qty $submitQty") |
|
|
|
|
|
inventoryLotLineService.updateInventoryLotLineQuantities( |
|
|
|
|
|
UpdateInventoryLotLineQuantitiesRequest( |
|
|
|
|
|
inventoryLotLineId = actualInventoryLotLineId, |
|
|
|
|
|
qty = submitQty, |
|
|
|
|
|
operation = "pick" |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
if (submitQty > BigDecimal.ZERO) { |
|
|
|
|
|
createStockLedgerForPickDelta(line.stockOutLineId, submitQty) |
|
|
|
|
|
} |
|
|
|
|
|
} else if (submitQty > BigDecimal.ZERO && actualInventoryLotLineId == null) { |
|
|
|
|
|
// ✅ 修复:即使没有 inventoryLotLineId,也应该创建 stock_ledger(用于无批次物品或批次切换后的情况) |
|
|
|
|
|
println(" Warning: No inventoryLotLineId found, but creating stock ledger anyway for stockOutLineId ${line.stockOutLineId}") |
|
|
|
|
|
createStockLedgerForPickDelta(line.stockOutLineId, submitQty) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 在 newBatchSubmit 方法中,修改这部分代码(大约在 1169-1185 行) |
|
|
|
|
|
if (submitQty > BigDecimal.ZERO && actualInventoryLotLineId != null) { |
|
|
|
|
|
println(" Updating inventory lot line ${actualInventoryLotLineId} with qty $submitQty") |
|
|
|
|
|
|
|
|
|
|
|
// ✅ 修复:在更新 inventory_lot_line 之前获取 inventory 的当前 onHandQty |
|
|
|
|
|
val item = stockOutLine.item |
|
|
|
|
|
val inventoryBeforeUpdate = item?.let { |
|
|
|
|
|
inventoryRepository.findByItemId(it.id!!).orElse(null) |
|
|
|
|
|
} |
|
|
|
|
|
val onHandQtyBeforeUpdate = (inventoryBeforeUpdate?.onHandQty ?: BigDecimal.ZERO).toDouble() |
|
|
|
|
|
|
|
|
|
|
|
println(" Inventory before update: onHandQty=$onHandQtyBeforeUpdate") |
|
|
|
|
|
|
|
|
|
|
|
inventoryLotLineService.updateInventoryLotLineQuantities( |
|
|
|
|
|
UpdateInventoryLotLineQuantitiesRequest( |
|
|
|
|
|
inventoryLotLineId = actualInventoryLotLineId, |
|
|
|
|
|
qty = submitQty, |
|
|
|
|
|
operation = "pick" |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if (submitQty > BigDecimal.ZERO) { |
|
|
|
|
|
// ✅ 修复:传入更新前的 onHandQty,让 createStockLedgerForPickDelta 使用它 |
|
|
|
|
|
createStockLedgerForPickDelta(line.stockOutLineId, submitQty, onHandQtyBeforeUpdate) |
|
|
|
|
|
} |
|
|
|
|
|
} else if (submitQty > BigDecimal.ZERO && actualInventoryLotLineId == null) { |
|
|
|
|
|
// ✅ 修复:即使没有 inventoryLotLineId,也应该获取 inventory.onHandQty |
|
|
|
|
|
val item = stockOutLine.item |
|
|
|
|
|
val inventoryBeforeUpdate = item?.let { |
|
|
|
|
|
inventoryRepository.findByItemId(it.id!!).orElse(null) |
|
|
|
|
|
} |
|
|
|
|
|
val onHandQtyBeforeUpdate = (inventoryBeforeUpdate?.onHandQty ?: BigDecimal.ZERO).toDouble() |
|
|
|
|
|
|
|
|
|
|
|
println(" Warning: No inventoryLotLineId found, but creating stock ledger anyway for stockOutLineId ${line.stockOutLineId}") |
|
|
|
|
|
createStockLedgerForPickDelta(line.stockOutLineId, submitQty, onHandQtyBeforeUpdate) |
|
|
|
|
|
} |
|
|
try { |
|
|
try { |
|
|
val stockOutLine = stockOutLines[line.stockOutLineId] |
|
|
val stockOutLine = stockOutLines[line.stockOutLineId] |
|
|
val item = stockOutLine?.item |
|
|
val item = stockOutLine?.item |
|
|
@@ -1369,7 +1388,11 @@ private fun createStockLedgerForStockOut(stockOutLine: StockOutLine) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun createStockLedgerForPickDelta(stockOutLineId: Long, deltaQty: BigDecimal) { |
|
|
|
|
|
|
|
|
private fun createStockLedgerForPickDelta( |
|
|
|
|
|
stockOutLineId: Long, |
|
|
|
|
|
deltaQty: BigDecimal, |
|
|
|
|
|
onHandQtyBeforeUpdate: Double? = null // ✅ 新增参数:更新前的 onHandQty |
|
|
|
|
|
) { |
|
|
if (deltaQty <= BigDecimal.ZERO) return |
|
|
if (deltaQty <= BigDecimal.ZERO) return |
|
|
|
|
|
|
|
|
val sol = stockOutLineRepository.findById(stockOutLineId).orElse(null) ?: return |
|
|
val sol = stockOutLineRepository.findById(stockOutLineId).orElse(null) ?: return |
|
|
@@ -1377,22 +1400,30 @@ private fun createStockLedgerForStockOut(stockOutLine: StockOutLine) { |
|
|
|
|
|
|
|
|
val inventory = inventoryRepository.findAllByItemIdAndDeletedIsFalse(item.id!!) |
|
|
val inventory = inventoryRepository.findAllByItemIdAndDeletedIsFalse(item.id!!) |
|
|
.firstOrNull() ?: return |
|
|
.firstOrNull() ?: return |
|
|
|
|
|
|
|
|
val latestLedger = stockLedgerRepository.findLatestByItemId(item.id!!).firstOrNull() |
|
|
|
|
|
|
|
|
|
|
|
val previousBalance = latestLedger?.balance |
|
|
|
|
|
?: (inventory.onHandQty ?: BigDecimal.ZERO).toDouble() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ 修复:如果传入了 onHandQtyBeforeUpdate,使用它;否则回退到原来的逻辑 |
|
|
|
|
|
val previousBalance = if (onHandQtyBeforeUpdate != null) { |
|
|
|
|
|
// 使用更新前的 onHandQty 作为基础 |
|
|
|
|
|
onHandQtyBeforeUpdate |
|
|
|
|
|
} else { |
|
|
|
|
|
// 回退逻辑:优先使用最新的 ledger balance,如果没有则使用 inventory.onHandQty |
|
|
|
|
|
val latestLedger = stockLedgerRepository.findLatestByItemId(item.id!!).firstOrNull() |
|
|
|
|
|
latestLedger?.balance ?: (inventory.onHandQty ?: BigDecimal.ZERO).toDouble() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
val newBalance = previousBalance - deltaQty.toDouble() |
|
|
val newBalance = previousBalance - deltaQty.toDouble() |
|
|
|
|
|
|
|
|
println(" Creating stock ledger: previousBalance=$previousBalance, deltaQty=$deltaQty, newBalance=$newBalance") |
|
|
println(" Creating stock ledger: previousBalance=$previousBalance, deltaQty=$deltaQty, newBalance=$newBalance") |
|
|
|
|
|
if (onHandQtyBeforeUpdate != null) { |
|
|
|
|
|
println(" Using onHandQtyBeforeUpdate: $onHandQtyBeforeUpdate") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
val ledger = StockLedger().apply { |
|
|
val ledger = StockLedger().apply { |
|
|
this.stockOutLine = sol |
|
|
this.stockOutLine = sol |
|
|
this.inventory = inventory |
|
|
this.inventory = inventory |
|
|
this.inQty = null |
|
|
this.inQty = null |
|
|
this.outQty = deltaQty.toDouble() |
|
|
this.outQty = deltaQty.toDouble() |
|
|
this.balance = newBalance // ✅ 使用计算后的新 balance |
|
|
|
|
|
|
|
|
this.balance = newBalance |
|
|
this.type = "NOR" |
|
|
this.type = "NOR" |
|
|
this.itemId = item.id |
|
|
this.itemId = item.id |
|
|
this.itemCode = item.code |
|
|
this.itemCode = item.code |
|
|
|