|
|
|
@@ -12,8 +12,6 @@ import com.ffii.fpsms.modules.stock.web.model.StockInRequest |
|
|
|
import com.ffii.fpsms.modules.stock.web.model.StockOutRequest |
|
|
|
import org.springframework.stereotype.Service |
|
|
|
import org.springframework.transaction.annotation.Transactional |
|
|
|
import java.math.BigDecimal |
|
|
|
import java.time.LocalDate |
|
|
|
import com.ffii.fpsms.modules.stock.entity.InventoryLotLineRepository |
|
|
|
import com.ffii.fpsms.modules.stock.entity.enum.InventoryLotLineStatus |
|
|
|
|
|
|
|
@@ -29,6 +27,10 @@ open class StockTransferRecordService( |
|
|
|
jdbcDao, |
|
|
|
stockTransferRecordRepository |
|
|
|
) { |
|
|
|
private enum class TransferStockInMode { |
|
|
|
MERGED_EXISTING_LOT, |
|
|
|
CREATED_NEW_LOT |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
open fun createStockTransfer(request: CreateStockTransferRequest): MessageResponse { |
|
|
|
@@ -36,7 +38,7 @@ open class StockTransferRecordService( |
|
|
|
val stockOutLine = createStockOut(request) |
|
|
|
|
|
|
|
// Step 2: Stock In - using generic function |
|
|
|
val stockInLine = createStockIn(request) |
|
|
|
val (stockInLine, stockInMode) = createStockIn(request) |
|
|
|
|
|
|
|
// Step 3: Create Stock Transfer Record |
|
|
|
val stockTransferRecord = createStockTransferRecord(request, stockOutLine, stockInLine) |
|
|
|
@@ -44,9 +46,15 @@ open class StockTransferRecordService( |
|
|
|
return MessageResponse( |
|
|
|
id = stockTransferRecord.id, |
|
|
|
name = "Stock Transfer Record", |
|
|
|
code = "STOCK_TRANSFER_CREATED", |
|
|
|
code = when (stockInMode) { |
|
|
|
TransferStockInMode.MERGED_EXISTING_LOT -> "MERGED_EXISTING_LOT" |
|
|
|
TransferStockInMode.CREATED_NEW_LOT -> "CREATED_NEW_LOT" |
|
|
|
}, |
|
|
|
type = "success", |
|
|
|
message = "Stock transfer completed successfully", |
|
|
|
message = when (stockInMode) { |
|
|
|
TransferStockInMode.MERGED_EXISTING_LOT -> "Stock transfer completed successfully (merged into existing target lot)" |
|
|
|
TransferStockInMode.CREATED_NEW_LOT -> "Stock transfer completed successfully (created new target lot)" |
|
|
|
}, |
|
|
|
errorPosition = null |
|
|
|
) |
|
|
|
} |
|
|
|
@@ -62,7 +70,7 @@ open class StockTransferRecordService( |
|
|
|
return stockOutLineService.createStockOut(stockOutRequest) |
|
|
|
} |
|
|
|
|
|
|
|
private fun createStockIn(request: CreateStockTransferRequest): StockInLine { |
|
|
|
private fun createStockIn(request: CreateStockTransferRequest): Pair<StockInLine, TransferStockInMode> { |
|
|
|
// Step 1: Get inventoryLotLine to extract item information |
|
|
|
val inventoryLotLine = inventoryLotLineRepository.findById(request.inventoryLotLineId) |
|
|
|
.orElseThrow { IllegalArgumentException("InventoryLotLine not found with id: ${request.inventoryLotLineId}") } |
|
|
|
@@ -107,7 +115,22 @@ open class StockTransferRecordService( |
|
|
|
warehouseId = request.warehouseId |
|
|
|
) |
|
|
|
|
|
|
|
return stockInLineService.createStockIn(stockInRequest) |
|
|
|
val existingTargetLotLine = inventoryLotLineRepository.findByLotNoAndItemIdAndWarehouseId( |
|
|
|
lotNo = lotNo, |
|
|
|
itemId = itemId, |
|
|
|
warehouseId = request.warehouseId |
|
|
|
) |
|
|
|
|
|
|
|
return if ( |
|
|
|
existingTargetLotLine != null && |
|
|
|
existingTargetLotLine.status == InventoryLotLineStatus.AVAILABLE && |
|
|
|
existingTargetLotLine.id != inventoryLotLine.id |
|
|
|
) { |
|
|
|
stockInLineService.createStockInForExistingInventoryLotLine(stockInRequest, existingTargetLotLine) to |
|
|
|
TransferStockInMode.MERGED_EXISTING_LOT |
|
|
|
} else { |
|
|
|
stockInLineService.createStockIn(stockInRequest) to TransferStockInMode.CREATED_NEW_LOT |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private fun createStockTransferRecord( |
|
|
|
|