From fc7149381217ed2e0624c2340f1b72da8ddb10c2 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:40:10 +0000 Subject: [PATCH] bug: skip expired lots in replenishment autopick --- .../warehouse/picklist/PicklistService.groovy | 4 +- .../warehouse/api/StockMovementItem.groovy | 5 + .../PicklistServiceAutopickSpec.groovy | 100 ++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/test/groovy/org/pih/warehouse/picklist/PicklistServiceAutopickSpec.groovy diff --git a/grails-app/services/org/pih/warehouse/picklist/PicklistService.groovy b/grails-app/services/org/pih/warehouse/picklist/PicklistService.groovy index ea3a36116dd..22098477e7c 100644 --- a/grails-app/services/org/pih/warehouse/picklist/PicklistService.groovy +++ b/grails-app/services/org/pih/warehouse/picklist/PicklistService.groovy @@ -210,7 +210,9 @@ class PicklistService { List getSuggestedItems(List availableItems, Integer quantityRequested, Location warehouse) { List suggestedItems = [] - List autoPickableItems = availableItems?.findAll { it.quantityAvailable > 0 && it.pickable } + List autoPickableItems = availableItems?.findAll { + it.quantityAvailable > 0 && it.pickable && !it.expired + } // As long as quantity requested is less than the total available we can iterate through available items // and pick until quantity requested is 0. Otherwise, we don't suggest anything because the user must diff --git a/src/main/groovy/org/pih/warehouse/api/StockMovementItem.groovy b/src/main/groovy/org/pih/warehouse/api/StockMovementItem.groovy index b2274aefcb1..335644648ae 100644 --- a/src/main/groovy/org/pih/warehouse/api/StockMovementItem.groovy +++ b/src/main/groovy/org/pih/warehouse/api/StockMovementItem.groovy @@ -400,6 +400,11 @@ class AvailableItem { return binLocation?.onHold } + Boolean isExpired() { + Date expirationDate = inventoryItem?.expirationDate + return expirationDate ? expirationDate.before(new Date().clearTime() + 1) : false + } + Boolean isRecalled() { return inventoryItem?.recalled } diff --git a/src/test/groovy/org/pih/warehouse/picklist/PicklistServiceAutopickSpec.groovy b/src/test/groovy/org/pih/warehouse/picklist/PicklistServiceAutopickSpec.groovy new file mode 100644 index 00000000000..15511c03f25 --- /dev/null +++ b/src/test/groovy/org/pih/warehouse/picklist/PicklistServiceAutopickSpec.groovy @@ -0,0 +1,100 @@ +package org.pih.warehouse.picklist + +import grails.testing.gorm.DataTest +import spock.lang.Specification + +import org.pih.warehouse.api.AvailableItem +import org.pih.warehouse.api.SuggestedItem +import org.pih.warehouse.core.Location +import org.pih.warehouse.inventory.InventoryItem +import org.pih.warehouse.inventory.InventoryLevel +import org.pih.warehouse.inventory.ProductAvailabilityService +import org.pih.warehouse.order.Order +import org.pih.warehouse.order.OrderItem +import org.pih.warehouse.product.Product + +class PicklistServiceAutopickSpec extends Specification implements DataTest { + + PicklistService picklistService + ProductAvailabilityService productAvailabilityServiceStub + + Location depot = new Location(name: 'Main Warehouse') + Location ward = new Location(name: 'Ward A') + Location bin = new Location(name: 'RM1-RACK1-SHELF1') + Product product = new Product(productCode: '10001', name: 'Lamivudine 150mg tablet') + + void setupSpec() { + mockDomains(InventoryLevel) + } + + void setup() { + picklistService = new PicklistService() + productAvailabilityServiceStub = Stub(ProductAvailabilityService) { + sortAvailableItems(_) >> { args -> new ProductAvailabilityService().sortAvailableItems(args[0]) } + } + picklistService.productAvailabilityService = productAvailabilityServiceStub + } + + private AvailableItem availableItem(String lotNumber, Date expirationDate, Integer quantity) { + InventoryItem inventoryItem = new InventoryItem(product: product, lotNumber: lotNumber, expirationDate: expirationDate) + return new AvailableItem(inventoryItem: inventoryItem, binLocation: bin, + quantityAvailable: quantity, quantityOnHand: quantity) + } + + private OrderItem replenishmentOrderItem(Integer quantity) { + Order order = new Order(origin: depot, destination: ward) + return new OrderItem(order: order, product: product, quantity: quantity) + } + + void 'replenishment autopick skips an expired lot when an unexpired lot is in stock'() { + given: 'an expired lot (earliest expiry, so first in FEFO order) and an unexpired lot at the same depot' + AvailableItem expiredLot = availableItem('4041283', new Date() - 365, 100) + AvailableItem unexpiredLot = availableItem('WALK50117', new Date() + 365 * 4, 100) + productAvailabilityServiceStub.getAvailableBinLocations(depot, product.id) >> [unexpiredLot, expiredLot] + OrderItem orderItem = replenishmentOrderItem(5) + + when: + List availableItems = picklistService.getAvailableItems(depot, orderItem) + List suggestedItems = picklistService.getSuggestedItems(availableItems, 5, ward) + + then: 'the expired lot is still listed for manual picking' + availableItems*.inventoryItem*.lotNumber == ['4041283', 'WALK50117'] + + and: 'autopick suggests only the unexpired lot' + suggestedItems.size() == 1 + suggestedItems[0].inventoryItem.lotNumber == 'WALK50117' + suggestedItems[0].quantityPicked == 5 + } + + void 'replenishment autopick does not cover a shortfall with an expired lot'() { + given: 'not enough unexpired stock on its own' + AvailableItem expiredLot = availableItem('4041283', new Date() - 30, 100) + AvailableItem unexpiredLot = availableItem('WALK50117', new Date() + 365, 3) + + expect: 'nothing is suggested, as when the depot is short, so the user picks manually' + picklistService.getSuggestedItems([expiredLot, unexpiredLot], 5, ward).isEmpty() + } + + void 'replenishment autopick suggests nothing when only expired lots are in stock'() { + expect: + picklistService.getSuggestedItems([availableItem('4041283', new Date() - 1, 100)], 5, ward).isEmpty() + } + + void 'a lot expiring today is skipped, while later and undated lots are autopicked in FEFO order'() { + given: + AvailableItem noExpiry = availableItem('NOEXP', null, 2) + AvailableItem expiresToday = availableItem('TODAY', new Date().clearTime(), 10) + AvailableItem expiresTomorrow = availableItem('TOMORROW', new Date().clearTime() + 1, 2) + AvailableItem expiresLater = availableItem('LATER', new Date() + 30, 2) + productAvailabilityServiceStub.getAvailableBinLocations(depot, product.id) >> [noExpiry, expiresLater, expiresToday, expiresTomorrow] + OrderItem orderItem = replenishmentOrderItem(6) + + when: + List availableItems = picklistService.getAvailableItems(depot, orderItem) + List suggestedItems = picklistService.getSuggestedItems(availableItems, 6, ward) + + then: + suggestedItems*.inventoryItem*.lotNumber == ['TOMORROW', 'LATER', 'NOEXP'] + suggestedItems*.quantityPicked == [2, 2, 2] + } +}