Related to ISSUE-44457. Fixes Costing Automated Tests
authordavid.miguelez <david.miguelez@openbravo.com>
Fri, 17 Jul 2020 13:01:27 +0200
changeset 37498 00803f99592b
parent 37497 99a12fb6e02e
child 37499 762a3b290f85
Related to ISSUE 44457. Fixes Costing Automated Tests
src-test/src/org/openbravo/test/costing/utils/TestCostingUtils.java
--- a/src-test/src/org/openbravo/test/costing/utils/TestCostingUtils.java	Fri Jul 03 13:33:48 2020 +0200
+++ b/src-test/src/org/openbravo/test/costing/utils/TestCostingUtils.java	Fri Jul 17 13:01:27 2020 +0200
@@ -5236,12 +5236,18 @@
       criteria2.addOrderBy(AccountingFact.PROPERTY_SEQUENCENUMBER, true);
       List<AccountingFact> accountingFactList = criteria2.list();
       String groupId = accountingFactList.get(0).getGroupID();
+      Date previousDate = accountingFactList.get(0).getAccountingDate();
 
       assertEquals(accountingFactList.size(), documentPostAssertList.size());
 
       int i = 0;
       for (AccountingFact accountingFact : accountingFactList) {
 
+        if (previousDate != accountingFact.getAccountingDate()) {
+          groupId = accountingFact.getGroupID();
+          previousDate = accountingFact.getAccountingDate();
+        }
+
         String lineListProperty = Character.toLowerCase(document.getEntityName().charAt(0))
             + document.getEntityName().substring(1) + "LineList";
 
@@ -5547,8 +5553,15 @@
         } else if (document.getEntityName().equals(LandedCost.ENTITY_NAME)) {
           assertEquals(formatDate(accountingFact.getTransactionDate()),
               formatDate(((LandedCost) document).getReferenceDate()));
-          assertEquals(formatDate(accountingFact.getAccountingDate()),
-              formatDate(((LandedCost) document).getReferenceDate()));
+          final Date shipmentAcctDate = (OBDal.getInstance()
+              .get(ShipmentInOutLine.class,
+                  ((LCReceiptLineAmt) line).getGoodsShipmentLine().getId())).getShipmentReceipt()
+                      .getAccountingDate();
+          Date accountingDate = ((LandedCost) document).getReferenceDate();
+          if (shipmentAcctDate.after(accountingDate)) {
+            accountingDate = shipmentAcctDate;
+          }
+          assertEquals(formatDate(accountingFact.getAccountingDate()), formatDate(accountingDate));
           if (i % 2 == 0) {
             assertEquals(accountingFact.getBusinessPartner(),
                 OBDal.getInstance()
@@ -5561,8 +5574,12 @@
         } else if (document.getEntityName().equals(LandedCostCost.ENTITY_NAME)) {
           assertEquals(formatDate(accountingFact.getTransactionDate()),
               formatDate(((LandedCostCost) document).getAccountingDate()));
-          assertEquals(formatDate(accountingFact.getAccountingDate()),
-              formatDate(((LandedCostCost) document).getAccountingDate()));
+          final Date maxInvoiceAcctDate = getMaxInvoiceAccountingDate((LandedCostCost) document);
+          Date accountingDate = ((LandedCostCost) document).getAccountingDate();
+          if (maxInvoiceAcctDate.after(accountingDate)) {
+            accountingDate = maxInvoiceAcctDate;
+          }
+          assertEquals(formatDate(accountingFact.getAccountingDate()), formatDate(accountingDate));
           if (i == 0 || (documentPostAssert.getProductId() != null
               && OBDal.getInstance()
                   .get(InvoiceLine.class,
@@ -5786,6 +5803,23 @@
     }
   }
 
+  private static Date getMaxInvoiceAccountingDate(final LandedCostCost lcCost) {
+    //@formatter:off
+    String hql = ""
+        + " select max(i.accountingDate)"
+        + " from LandedCostMatched lcm "
+        + "   join lcm.invoiceLine il"
+        + "   join il.invoice i"
+        + " where lcm.active = true"
+        + "   and lcm.landedCostCost.id = :lcCostId";
+    //@formatter:on
+    return (Date) OBDal.getInstance()
+        .getSession()
+        .createQuery(hql)
+        .setParameter("lcCostId", lcCost.getId())
+        .uniqueResult();
+  }
+
   // Format date
   public static String formatDate(Date date) {
     return new SimpleDateFormat("yyyy-MM-dd").format(date);