diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/MapERPOrders.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/MapERPOrders.java index cca8ceb4..11ab77d3 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/MapERPOrders.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/MapERPOrders.java @@ -64,6 +64,7 @@ private String mapOpenERPOrders() throws IOException { openERPOrder.setProductId(drugOrder.getDrugUuid()); openERPOrder.setProductName(drugOrder.getDrugName()); openERPOrder.setAction(drugOrder.getAction()); + openERPOrder.setDateCreated(drugOrder.getDateActivated()); openERPOrder.setQuantity(drugOrder.getQuantity()); openERPOrder.setQuantityUnits(drugOrder.getQuantityUnits()); openERPOrder.setVoided(drugOrder.isVoided()); @@ -87,6 +88,7 @@ private String mapOpenERPOrders() throws IOException { openERPOrder.setProductId(order.getConceptUuid()); openERPOrder.setProductName(order.getConceptName()); openERPOrder.setAction(order.getAction()); + openERPOrder.setDateCreated(order.getDateCreated()); openERPOrder.setQuantity((double) 1); openERPOrder.setQuantityUnits("Unit(s)"); openERPOrder.setVoided(order.isVoided()); diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrder.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrder.java index ca05239b..d64e1dec 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrder.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrder.java @@ -1,5 +1,7 @@ package org.bahmni.feed.openerp.domain.encounter; +import java.util.Date; + public class OpenERPOrder { private String orderId; private String previousOrderId; @@ -18,6 +20,7 @@ public class OpenERPOrder { private String providerName; private String dispensed; private String conceptName; + private Date dateCreated; public String getEncounterId() { @@ -52,8 +55,6 @@ public void setType(String type) { this.type = type; } - - public String getDescription() { return description; } @@ -119,6 +120,14 @@ public void setAction(String action) { this.action = action; } + public Date getDateCreated() { + return dateCreated; + } + + public void setDateCreated(Date dateCreated) { + this.dateCreated = dateCreated; + } + public String getPreviousOrderId() { return previousOrderId; } 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 8eb0a15b..3d2f1bfc 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 @@ -1,7 +1,6 @@ package org.bahmni.feed.openerp.domain.encounter; -import java.util.ArrayList; -import java.util.List; +import java.util.*; public class OpenERPOrders { private String id; @@ -13,7 +12,20 @@ public OpenERPOrders(String id){ } public List getOpenERPOrders() { - return openERPOrders; + return removeDuplicateOrders(openERPOrders); + } + + //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 + ); + } + + return new ArrayList<>(latestOrders.values()); } public void add(OpenERPOrder order) { @@ -34,4 +46,4 @@ public String getId() { public void setId(String id) { this.id = id; } -} +} \ No newline at end of file diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenMRSDrugOrder.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenMRSDrugOrder.java index 50fa59ea..d9317885 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenMRSDrugOrder.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenMRSDrugOrder.java @@ -3,6 +3,8 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import java.util.Date; + @JsonIgnoreProperties(ignoreUnknown = true) public class OpenMRSDrugOrder { private OpenMRSConcept concept; @@ -13,6 +15,7 @@ public class OpenMRSDrugOrder { private String uuid; private String previousOrderUuid; private String orderType; + private Date dateActivated; private boolean voided; public OpenMRSConcept getConcept() { @@ -43,6 +46,10 @@ public String getAction() { return action; } + public Date getDateActivated() { + return dateActivated; + } + public String getUuid() { return uuid; } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenMRSOrder.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenMRSOrder.java index 6e0bc9ad..b19896c1 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenMRSOrder.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/encounter/OpenMRSOrder.java @@ -1,11 +1,14 @@ package org.bahmni.feed.openerp.domain.encounter; +import java.util.Date; + import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class OpenMRSOrder { private OpenMRSConcept concept; private String action; + private Date dateCreated; private String uuid; private String orderType; private String orderNumber; @@ -21,6 +24,10 @@ public String getAction() { return action; } + public Date getDateCreated() { + return dateCreated; + } + public String getUuid() { return uuid; } diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/labOrderType/encounter/MapERPOrdersTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/encounter/MapERPOrdersTest.java similarity index 98% rename from openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/labOrderType/encounter/MapERPOrdersTest.java rename to openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/encounter/MapERPOrdersTest.java index 9ec4fbfe..3fa8d4a4 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/labOrderType/encounter/MapERPOrdersTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/encounter/MapERPOrdersTest.java @@ -1,4 +1,4 @@ -package org.bahmni.feed.openerp.domain.labOrderType.encounter; +package org.bahmni.feed.openerp.domain.encounter; import org.bahmni.feed.openerp.ObjectMapperRepository; import org.bahmni.feed.openerp.domain.encounter.MapERPOrders; diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrdersTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrdersTest.java new file mode 100644 index 00000000..4a4ae8d0 --- /dev/null +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/domain/encounter/OpenERPOrdersTest.java @@ -0,0 +1,42 @@ +package org.bahmni.feed.openerp.domain.encounter; + +import org.junit.Test; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import static org.junit.Assert.assertEquals; + +public class OpenERPOrdersTest { + + @Test + public void shouldRemoveDuplicateOrders() { + OpenERPOrder order1 = new OpenERPOrder(); + order1.setProductId("1"); + order1.setDateCreated(new Date(1704718128000L)); + + OpenERPOrder order2 = new OpenERPOrder(); + order2.setProductId("2"); + order2.setDateCreated(new Date(17047181283000L)); + + OpenERPOrder order3 = new OpenERPOrder(); + order3.setProductId("1"); + order3.setDateCreated(new Date(1704718125000L)); + + OpenERPOrder order4 = new OpenERPOrder(); + order4.setProductId("3"); + order4.setDateCreated(new Date(1704718131000L)); + + OpenERPOrders openerpOrders = new OpenERPOrders("123"); + openerpOrders.add(order1); + openerpOrders.add(order2); + openerpOrders.add(order3); + openerpOrders.add(order4); + + List result = openerpOrders.removeDuplicateOrders(openerpOrders.getOpenERPOrders()); + + List expected = Arrays.asList(order1, order2, order4); + + assertEquals(expected, result); + } +} +