| @@ -1329,29 +1329,31 @@ if (submitQty > BigDecimal.ZERO && actualInventoryLotLineId != null) { | |||||
| ) | ) | ||||
| } | } | ||||
| } | } | ||||
| @Transactional | |||||
| private fun createStockLedgerForStockOut(stockOutLine: StockOutLine) { | |||||
| val item = stockOutLine.item ?: return | |||||
| val inventory = inventoryRepository.findByItemId(item.id!!).orElse(null) ?: return | |||||
| val outQty = stockOutLine.qty?.toDouble() ?: 0.0 | |||||
| // 直接使用 inventory.onHandQty 作为 balance(已经是更新后的值) | |||||
| val newBalance = (inventory.onHandQty ?: BigDecimal.ZERO).toDouble() | |||||
| val stockLedger = StockLedger().apply { | |||||
| this.stockOutLine = stockOutLine | |||||
| this.inventory = inventory | |||||
| this.inQty = null | |||||
| this.outQty = outQty | |||||
| this.balance = newBalance | |||||
| this.type = stockOutLine.type | |||||
| this.itemId = item.id | |||||
| this.itemCode = item.code | |||||
| this.date = LocalDate.now() | |||||
| @Transactional | |||||
| private fun createStockLedgerForStockOut(stockOutLine: StockOutLine) { | |||||
| val item = stockOutLine.item ?: return | |||||
| val inventory = inventoryRepository.findByItemId(item.id!!).orElse(null) ?: return | |||||
| val outQty = stockOutLine.qty?.toDouble() ?: 0.0 | |||||
| // Use latest ledger balance (same pattern as createStockLedgerForStockIn) so balance is correct when multiple actions run in one transaction | |||||
| val latestLedger = stockLedgerRepository.findLatestByItemId(item.id!!).firstOrNull() | |||||
| val previousBalance = latestLedger?.balance ?: (inventory.onHandQty ?: BigDecimal.ZERO).toDouble() | |||||
| val newBalance = previousBalance - outQty | |||||
| val stockLedger = StockLedger().apply { | |||||
| this.stockOutLine = stockOutLine | |||||
| this.inventory = inventory | |||||
| this.inQty = null | |||||
| this.outQty = outQty | |||||
| this.balance = newBalance | |||||
| this.type = stockOutLine.type | |||||
| this.itemId = item.id | |||||
| this.itemCode = item.code | |||||
| this.date = LocalDate.now() | |||||
| } | |||||
| stockLedgerRepository.saveAndFlush(stockLedger) | |||||
| } | } | ||||
| stockLedgerRepository.saveAndFlush(stockLedger) | |||||
| } | |||||
| open fun createStockOut(request: StockOutRequest): StockOutLine { | open fun createStockOut(request: StockOutRequest): StockOutLine { | ||||