|
|
|
@@ -740,35 +740,36 @@ open class StockInLineService( |
|
|
|
) |
|
|
|
} |
|
|
|
@Transactional |
|
|
|
private fun createStockLedgerForStockIn(stockInLine: StockInLine) { |
|
|
|
val item = stockInLine.item ?: return |
|
|
|
val inventory = inventoryRepository.findByItemId(item.id!!).orElse(null) ?: return |
|
|
|
private fun createStockLedgerForStockIn(stockInLine: StockInLine) { |
|
|
|
val item = stockInLine.item ?: return |
|
|
|
val inventory = inventoryRepository.findByItemId(item.id!!).orElse(null) ?: return |
|
|
|
|
|
|
|
val inQty = stockInLine.acceptedQty?.toDouble() ?: 0.0 |
|
|
|
|
|
|
|
val inQty = stockInLine.acceptedQty?.toDouble() ?: 0.0 |
|
|
|
|
|
|
|
// ✅ 修复:查询最新的 stock_ledger 记录,基于前一笔 balance 计算 |
|
|
|
val latestLedger = stockLedgerRepository.findLatestByItemId(item.id!!).firstOrNull() |
|
|
|
|
|
|
|
val previousBalance = latestLedger?.balance |
|
|
|
?: (inventory.onHandQty ?: BigDecimal.ZERO).toDouble() |
|
|
|
|
|
|
|
val newBalance = previousBalance + inQty |
|
|
|
// ✅ 修复:查询最新的 stock_ledger 记录,基于前一笔 balance 计算 |
|
|
|
val latestLedger = stockLedgerRepository.findLatestByItemId(item.id!!).firstOrNull() |
|
|
|
|
|
|
|
val stockLedger = StockLedger().apply { |
|
|
|
this.stockInLine = stockInLine |
|
|
|
this.inventory = inventory |
|
|
|
this.inQty = inQty |
|
|
|
this.outQty = null |
|
|
|
this.balance = newBalance |
|
|
|
this.type = stockInLine.type |
|
|
|
this.itemId = item.id |
|
|
|
this.itemCode = item.code |
|
|
|
this.date = LocalDate.now() |
|
|
|
} |
|
|
|
// ✅ 修复:如果 latestLedger 为 null(第一次 stock in),previousBalance 应该是 0 |
|
|
|
// 因为 inventory.onHandQty 已经被触发器更新为包含本次 stock in 的数量 |
|
|
|
val previousBalance = latestLedger?.balance ?: 0.0 |
|
|
|
|
|
|
|
stockLedgerRepository.saveAndFlush(stockLedger) |
|
|
|
val newBalance = previousBalance + inQty |
|
|
|
|
|
|
|
val stockLedger = StockLedger().apply { |
|
|
|
this.stockInLine = stockInLine |
|
|
|
this.inventory = inventory |
|
|
|
this.inQty = inQty |
|
|
|
this.outQty = null |
|
|
|
this.balance = newBalance |
|
|
|
this.type = stockInLine.type |
|
|
|
this.itemId = item.id |
|
|
|
this.itemCode = item.code |
|
|
|
this.date = LocalDate.now() |
|
|
|
} |
|
|
|
|
|
|
|
stockLedgerRepository.saveAndFlush(stockLedger) |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
open fun createStockIn(request: StockInRequest): StockInLine { |
|
|
|
// Step 1: Create a row of stock_in |
|
|
|
|