|
|
@@ -718,23 +718,29 @@ open class StockInLineService( |
|
|
private fun createStockLedgerForStockIn(stockInLine: StockInLine) { |
|
|
private fun createStockLedgerForStockIn(stockInLine: StockInLine) { |
|
|
val item = stockInLine.item ?: return |
|
|
val item = stockInLine.item ?: return |
|
|
val inventory = inventoryRepository.findByItemId(item.id!!).orElse(null) ?: return |
|
|
val inventory = inventoryRepository.findByItemId(item.id!!).orElse(null) ?: return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val inQty = stockInLine.acceptedQty?.toDouble() ?: 0.0 |
|
|
val inQty = stockInLine.acceptedQty?.toDouble() ?: 0.0 |
|
|
// 直接使用 inventory.onHandQty 作为 balance(已经是更新后的值) |
|
|
|
|
|
val newBalance = (inventory.onHandQty ?: BigDecimal.ZERO).toDouble() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ 修复:查询最新的 stock_ledger 记录,基于前一笔 balance 计算 |
|
|
|
|
|
val latestLedger = stockLedgerRepository.findLatestByItemId(item.id!!).firstOrNull() |
|
|
|
|
|
|
|
|
|
|
|
val previousBalance = latestLedger?.balance |
|
|
|
|
|
?: (inventory.onHandQty ?: BigDecimal.ZERO).toDouble() |
|
|
|
|
|
|
|
|
|
|
|
val newBalance = previousBalance + inQty |
|
|
|
|
|
|
|
|
val stockLedger = StockLedger().apply { |
|
|
val stockLedger = StockLedger().apply { |
|
|
this.stockInLine = stockInLine |
|
|
this.stockInLine = stockInLine |
|
|
this.inventory = inventory |
|
|
this.inventory = inventory |
|
|
this.inQty = inQty |
|
|
this.inQty = inQty |
|
|
this.outQty = null |
|
|
this.outQty = null |
|
|
this.balance = newBalance |
|
|
|
|
|
|
|
|
this.balance = newBalance |
|
|
this.type = stockInLine.type |
|
|
this.type = stockInLine.type |
|
|
this.itemId = item.id |
|
|
this.itemId = item.id |
|
|
this.itemCode = item.code |
|
|
this.itemCode = item.code |
|
|
this.date = LocalDate.now() |
|
|
this.date = LocalDate.now() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stockLedgerRepository.saveAndFlush(stockLedger) |
|
|
stockLedgerRepository.saveAndFlush(stockLedger) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|