diff --git a/src/main/java/com/ffii/fpsms/modules/pickOrder/service/PickOrderService.kt b/src/main/java/com/ffii/fpsms/modules/pickOrder/service/PickOrderService.kt index 03cd3e0..c4f1d1b 100644 --- a/src/main/java/com/ffii/fpsms/modules/pickOrder/service/PickOrderService.kt +++ b/src/main/java/com/ffii/fpsms/modules/pickOrder/service/PickOrderService.kt @@ -871,6 +871,7 @@ open class PickOrderService( LEFT JOIN fpsmsdb.stock_out_line sol ON sol.pickOrderLineId = :pickOrderLineId AND sol.inventoryLotLineId = ill.id AND sol.deleted = false -- ✅ FIXED: Only include lots that are either suggested OR have stock out lines for this pick order WHERE (spl.pickOrderLineId = :pickOrderLineId OR sol.pickOrderLineId = :pickOrderLineId) + AND(sol.status IS NULL OR sol.status != 'completed') AND ill.deleted = false AND il.deleted = false ORDER BY @@ -1037,11 +1038,21 @@ open class PickOrderService( val saveSuggestedPickLots = suggestedPickLotService.saveAll(suggestions.suggestedList) pickOrderRepository.saveAll(pickOrders) - + val inventoryLotLines = inventoryLotLineRepository.findAllByIdIn( saveSuggestedPickLots.mapNotNull { it.suggestedLotLine?.id } ) - + saveSuggestedPickLots.forEach { lot -> + val lotLineId = lot.suggestedLotLine?.id + if (lotLineId != null) { + val lineIndex = inventoryLotLines.indexOf(lot.suggestedLotLine) + if (lineIndex >= 0) { + val currentHold = inventoryLotLines[lineIndex].holdQty ?: zero + val addHold = lot.qty ?: zero + inventoryLotLines[lineIndex].holdQty = currentHold.plus(addHold) + } + } + } logger.info("saveSuggestedPickLots: $saveSuggestedPickLots") inventoryLotLineRepository.saveAll(inventoryLotLines) var precreated = 0 diff --git a/src/main/java/com/ffii/fpsms/modules/stock/service/SuggestedPickLotService.kt b/src/main/java/com/ffii/fpsms/modules/stock/service/SuggestedPickLotService.kt index 3cb69a5..0edcd99 100644 --- a/src/main/java/com/ffii/fpsms/modules/stock/service/SuggestedPickLotService.kt +++ b/src/main/java/com/ffii/fpsms/modules/stock/service/SuggestedPickLotService.kt @@ -26,6 +26,9 @@ import com.ffii.fpsms.modules.stock.web.model.StockOutLineStatus import com.ffii.fpsms.modules.stock.web.model.PickAnotherLotRequest import com.ffii.fpsms.modules.stock.service.InventoryLotLineService import com.ffii.fpsms.modules.stock.entity.FailInventoryLotLineRepository +import com.ffii.fpsms.modules.stock.entity.StockOutRepository +import com.ffii.fpsms.modules.master.entity.ItemsRepository +import com.ffii.fpsms.modules.pickOrder.enums.PickOrderLineStatus @Service open class SuggestedPickLotService( val suggestedPickLotRepository: SuggestPickLotRepository, @@ -36,7 +39,9 @@ open class SuggestedPickLotService( val itemUomService: ItemUomService, val pickOrderRepository: PickOrderRepository, val inventoryRepository: InventoryRepository, - val failInventoryLotLineRepository: FailInventoryLotLineRepository + val failInventoryLotLineRepository: FailInventoryLotLineRepository, + val stockOutRepository: StockOutRepository, + val itemRepository: ItemsRepository ) { // Calculation Available Qty / Remaining Qty open fun calculateRemainingQtyForInfo(inventoryLotLine: InventoryLotLineInfo?): BigDecimal { @@ -217,7 +222,72 @@ open class SuggestedPickLotService( open fun saveAll(request: List): List { return suggestedPickLotRepository.saveAllAndFlush(request) } - + private fun createStockOutLineForSuggestion( + suggestion: SuggestedPickLot, + pickOrder: PickOrder + ): StockOutLine? { + try { + val suggestedLotLine = suggestion.suggestedLotLine + val pickOrderLine = suggestion.pickOrderLine + + if (suggestedLotLine == null || pickOrderLine == null) { + println("Cannot create stock out line: missing suggestedLotLine or pickOrderLine") + return null + } + + // Check if stock out line already exists + val existingStockOutLine = stockOutLIneRepository.findByPickOrderLineIdAndInventoryLotLineIdAndDeletedFalse( + pickOrderLine.id!!, + suggestedLotLine.id!! + ) + + if (existingStockOutLine.isNotEmpty()) { + println("Stock out line already exists for pickOrderLineId: ${pickOrderLine.id}, inventoryLotLineId: ${suggestedLotLine.id}") + return existingStockOutLine.first() + } + + // Get or create StockOut + val stockOut = stockOutRepository.findByConsoPickOrderCode(pickOrder.consoCode ?: "") + .orElseGet { + // Create new StockOut if it doesn't exist + val newStockOut = StockOut().apply { + this.consoPickOrderCode = pickOrder.consoCode ?: "" + + } + stockOutRepository.save(newStockOut) + } + + // Update pick order line status to PICKING + val updatedPickOrderLine = pickOrderLineRepository.saveAndFlush( + pickOrderLine.apply { + this.status = PickOrderLineStatus.PICKING + } + ) + + // Get item + val item = itemRepository.findById(updatedPickOrderLine.item!!.id!!).orElseThrow() + + // Create stock out line + val stockOutLine = StockOutLine().apply { + this.item = item + this.qty = 0.0 + this.stockOut = stockOut + this.inventoryLotLine = suggestedLotLine + this.pickOrderLine = updatedPickOrderLine + this.status = StockOutLineStatus.PENDING.status + } + + val savedStockOutLine = stockOutLIneRepository.saveAndFlush(stockOutLine) + println("✅ Created stock out line ID: ${savedStockOutLine.id} for suggestion ID: ${suggestion.id}") + + return savedStockOutLine + + } catch (e: Exception) { + println("❌ Error creating stock out line for suggestion: ${e.message}") + e.printStackTrace() + return null + } + } @Transactional(rollbackFor = [Exception::class]) open fun resuggestPickOrder(pickOrderId: Long): MessageResponse { try { @@ -409,12 +479,21 @@ allPickOrdersToResuggest.forEach { pickOrderToResuggest -> if (response.suggestedList.isNotEmpty()) { val savedSuggestions = suggestedPickLotRepository.saveAllAndFlush(response.suggestedList) println("Saved ${savedSuggestions.size} new suggestions for pick order: ${pickOrderToResuggest.code}") - + savedSuggestions.forEach { suggestion -> + if (suggestion.suggestedLotLine != null) { + val stockOutLine = createStockOutLineForSuggestion(suggestion, pickOrderToResuggest) + if (stockOutLine != null) { + println("✅ Created stock out line ${stockOutLine.id} for suggestion ${suggestion.id}") + } else { + println("❌ Failed to create stock out line for suggestion ${suggestion.id}") + } + } + } // ✅ FIX: Update holdQty for the lots that were suggested - CUMULATIVE response.holdQtyMap.forEach { (lotId, newHoldQty) -> if (lotId != null && newHoldQty != null && newHoldQty > BigDecimal.ZERO) { - val lot = inventoryLotLineRepository.findById(lotId).orElse(null) - lot?.let { + val lot = inventoryLotLineRepository.findById(lotId).orElse(null) + lot?.let { val currentHoldQty = it.holdQty ?: BigDecimal.ZERO val existingHoldQty = existingHoldQtyMap[lotId] ?: BigDecimal.ZERO