From 058b1151465458bb05026940fd3cf829c12e043e Mon Sep 17 00:00:00 2001 From: kelvinsuen Date: Mon, 27 Oct 2025 19:09:21 +0800 Subject: [PATCH] update po --- .../stock/service/StockInLineService.kt | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) 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 63f18c1..226aff1 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 @@ -126,10 +126,7 @@ open class StockInLineService( // update po status to receiving if (po != null) { - po.apply { - status = PurchaseOrderStatus.RECEIVING - } - val savedPo = poRepository.save(po) + val savedPo = updatePurchaseOrderStatus(po) stockInLine.apply { this.purchaseOrder = savedPo } @@ -323,19 +320,21 @@ open class StockInLineService( @Throws(IOException::class) @Transactional - open fun updatePurchaseOrderStatus(po : PurchaseOrder) { - val unfinishedPol = polRepository - .findAllByPurchaseOrderIdAndStatusNotAndDeletedIsFalse(po.id!!, - PurchaseOrderLineStatus.COMPLETED) - // If all POL is completed - if (unfinishedPol.isEmpty()) { - po.apply { - status = PurchaseOrderStatus.COMPLETED - } - poRepository.saveAndFlush(po) - } else { + open fun updatePurchaseOrderStatus(po : PurchaseOrder) : PurchaseOrder { + val pols = polRepository.findAllByPurchaseOrderIdAndDeletedIsFalse(po.id!!) + var newStatus = PurchaseOrderStatus.PENDING + if (pols.isNotEmpty()) { + newStatus = if (pols.any { pol-> pol.status != PurchaseOrderLineStatus.COMPLETED }) { + PurchaseOrderStatus.RECEIVING + } else { + PurchaseOrderStatus.COMPLETED + } + } + po.apply { + status = newStatus } + return poRepository.saveAndFlush(po) } @Throws(IOException::class) @Transactional