|
|
|
@@ -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<SuggestedPickLot>): List<SuggestedPickLot> { |
|
|
|
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 |
|
|
|
|
|
|
|
|