diff --git a/src/main/java/com/ffii/fpsms/modules/master/entity/ItemUomRespository.kt b/src/main/java/com/ffii/fpsms/modules/master/entity/ItemUomRespository.kt index 74ea098..a60c41a 100644 --- a/src/main/java/com/ffii/fpsms/modules/master/entity/ItemUomRespository.kt +++ b/src/main/java/com/ffii/fpsms/modules/master/entity/ItemUomRespository.kt @@ -27,4 +27,6 @@ interface ItemUomRespository : AbstractRepository { fun findBaseUnitByItemIdAndStockUnitIsTrueAndDeletedIsFalse(itemId: Long): ItemUom? fun findFirstByItemIdAndUomIdAndDeletedIsFalse(itemId: Long, uomId: Long): ItemUom? fun findByItemIdAndUomIdAndStockUnitAndDeletedIsFalse(itemId: Long, uomId: Long, stockUnit: Boolean): ItemUom? + + fun findFirstByItemIdAndStockUnitIsTrueAndDeletedIsFalseOrderByIdAsc(itemId: Long): ItemUom? } \ No newline at end of file diff --git a/src/main/java/com/ffii/fpsms/modules/stock/entity/InventoryRepository.kt b/src/main/java/com/ffii/fpsms/modules/stock/entity/InventoryRepository.kt index 98cf7fb..16598df 100644 --- a/src/main/java/com/ffii/fpsms/modules/stock/entity/InventoryRepository.kt +++ b/src/main/java/com/ffii/fpsms/modules/stock/entity/InventoryRepository.kt @@ -29,4 +29,6 @@ interface InventoryRepository: AbstractRepository { @Query("SELECT i FROM Inventory i WHERE i.item.id = :itemId AND i.deleted = false") fun findAllByItemIdAndDeletedIsFalse(itemId: Long): List + + fun findFirstByItemIdAndDeletedIsFalseOrderByIdAsc(itemId: Long): Inventory? } \ No newline at end of file diff --git a/src/main/java/com/ffii/fpsms/modules/stock/service/StockInLineService.kt b/src/main/java/com/ffii/fpsms/modules/stock/service/StockInLineService.kt index f018853..db204e2 100644 --- a/src/main/java/com/ffii/fpsms/modules/stock/service/StockInLineService.kt +++ b/src/main/java/com/ffii/fpsms/modules/stock/service/StockInLineService.kt @@ -740,11 +740,17 @@ open class StockInLineService( ) } @Transactional +<<<<<<< Updated upstream 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 +======= + private fun createStockLedgerForStockIn(stockInLine: StockInLine) { + val item = stockInLine.item ?: return + val inventory = inventoryRepository.findFirstByItemIdAndDeletedIsFalseOrderByIdAsc(item.id!!) ?: return +>>>>>>> Stashed changes // ✅ 修复:查询最新的 stock_ledger 记录,基于前一笔 balance 计算 val latestLedger = stockLedgerRepository.findLatestByItemId(item.id!!).firstOrNull() @@ -822,9 +828,12 @@ private fun createStockLedgerForStockIn(stockInLine: StockInLine) { savedStockInLine.inventoryLot = savedInventoryLot // Step 4: Create a row in inventory_lot_line table - val stockItemUom = itemUomRepository.findBaseUnitByItemIdAndStockUnitIsTrueAndDeletedIsFalse( - itemId = request.itemId - ) ?: throw IllegalArgumentException("Stock UOM not found for item ${request.itemId}") + val stockItemUom = itemUomRepository.findFirstByItemIdAndStockUnitIsTrueAndDeletedIsFalseOrderByIdAsc( + request.itemId + ) ?: throw IllegalArgumentException( + "Stock UOM not found for item: id=${request.itemId}, itemNo=${request.itemNo}. " + + "Ensure at least one item_uom row has stockUnit=1, deleted=0 for this item." + ) val warehouse = if (request.warehouseId != null) { warehouseRepository.findById(request.warehouseId).orElseThrow()