diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrders.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrders.java index 3d2f1bf..c8af6e1 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrders.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrders.java @@ -18,13 +18,23 @@ public List getOpenERPOrders() { //Filters orders to keep only the latest action for each product. This is necessary for ensuring consistent and accurate quotation generation, particularly when order objects may not be in chronological order. public List removeDuplicateOrders(List orders) { Map latestOrders = new LinkedHashMap<>(); - for (OpenERPOrder order : orders) { - latestOrders.merge(order.getProductId(), order, (existingOrder, newOrder) -> - (existingOrder.getDateCreated().before(newOrder.getDateCreated())) ? newOrder : existingOrder - ); + if("Drug Order".equals(order.getType())) { + String productId = order.getProductId(); + if (latestOrders.containsKey(productId)) { + OpenERPOrder existingOrder = latestOrders.get(productId); + double updatedQuantity = "NEW".equals(order.getAction()) ? existingOrder.getQuantity() + order.getQuantity() : existingOrder.getQuantity() - order.getQuantity(); + existingOrder.setQuantity(updatedQuantity); + latestOrders.put(productId, existingOrder); + } else { + latestOrders.put(productId, order); + } + } else { + latestOrders.merge(order.getProductId(), order, (existingOrder, newOrder) -> + (existingOrder.getDateCreated().before(newOrder.getDateCreated())) ? newOrder : existingOrder + ); + } } - return new ArrayList<>(latestOrders.values()); }