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