fixes BUG-44457:Proper dates used in Landed Costs
authorinigo_lerga <inigo.lerga@openbravo.com>
Fri, 03 Jul 2020 13:33:48 +0200
changeset 37497 99a12fb6e02e
parent 37496 48ebda2c4c42
child 37498 00803f99592b
fixes BUG 44457:Proper dates used in Landed Costs

- In the case of the Landed Cost accounting. It now uses the
latest date form Receipts accounting date and
Landed Cost Reference date.

- In the case of the Matching Cost accounting:
It now uses the latest date from Matched Invoice accounting date
and Landed Cost Cost accounting date.
src/org/openbravo/erpCommon/ad_forms/DocLCCost.java
src/org/openbravo/erpCommon/ad_forms/DocLandedCost.java
src/org/openbravo/erpCommon/ad_forms/DocLineLCCost_data.xsql
src/org/openbravo/erpCommon/ad_forms/DocLineLandedCost_data.xsql
--- a/src/org/openbravo/erpCommon/ad_forms/DocLCCost.java	Fri Jul 17 09:54:31 2020 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/DocLCCost.java	Fri Jul 03 13:33:48 2020 +0200
@@ -113,7 +113,7 @@
         docLine.m_C_Campaign_ID = data[i].cCampaignId;
         docLine.m_A_Asset_ID = data[i].aAssetId;
 
-        docLine.m_DateAcct = DateDoc;
+        docLine.m_DateAcct = DocLineLCCostData.selectAcctDateOrMaxInvoiceAcctDate(conn, DateDoc, Record_ID);
         docLine.setLandedCostTypeId(data[i].mLcTypeId);
         docLine.setIsMatchingAdjusted(data[i].ismatchingadjusted);
         docLine.setLcCostId(data[i].mLcCostId);
--- a/src/org/openbravo/erpCommon/ad_forms/DocLandedCost.java	Fri Jul 17 09:54:31 2020 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/DocLandedCost.java	Fri Jul 03 13:33:48 2020 +0200
@@ -91,7 +91,7 @@
 
     DocLineLandedCostData[] data = null;
     try {
-      data = DocLineLandedCostData.select(conn, Record_ID);
+      data = DocLineLandedCostData.select(conn, DateDoc, Record_ID);
       for (int i = 0; i < data.length; i++) {
         String Line_ID = data[i].mLcReceiptlineAmtId;
         DocLine_LandedCost docLine = new DocLine_LandedCost(DocumentType, Record_ID, Line_ID);
@@ -100,7 +100,7 @@
         docLine.setWarehouseId(data[i].mWarehouseId);
         docLine.m_C_BPartner_ID = data[i].cBpartnerId;
         docLine.m_M_Product_ID = data[i].mProductId;
-        docLine.m_DateAcct = DateDoc;
+        docLine.m_DateAcct = data[i].dateacct;
         docLine.setLandedCostTypeId(data[i].mLcTypeId);
         docLine.m_C_Project_ID = data[i].cProjectId;
         docLine.m_C_Costcenter_ID = data[i].cCostcenterId;
@@ -179,12 +179,18 @@
     String fact_Acct_Group_ID = SequenceIdData.getUUID();
     String amtDebit = "0";
     String amtCredit = "0";
+    String previousDateAcct = p_lines[0].m_DateAcct;
     // Lines
     for (int i = 0; p_lines != null && i < p_lines.length; i++) {
       DocLine_LandedCost line = (DocLine_LandedCost) p_lines[i];
 
       BigDecimal amount = new BigDecimal(line.getAmount());
       ProductInfo p = new ProductInfo(line.m_M_Product_ID, conn);
+      String currentDateAcct = p_lines[i].m_DateAcct;
+      if (!StringUtils.equals(previousDateAcct, currentDateAcct)) {
+        fact_Acct_Group_ID = SequenceIdData.getUUID();
+      }
+      previousDateAcct = currentDateAcct;
 
       log4jDocLandedCost.debug("previous to creteline, line.getAmount(): " + line.getAmount());
 
--- a/src/org/openbravo/erpCommon/ad_forms/DocLineLCCost_data.xsql	Fri Jul 17 09:54:31 2020 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/DocLineLCCost_data.xsql	Fri Jul 03 13:33:48 2020 +0200
@@ -111,5 +111,21 @@
      </Sql>
      <Parameter name="M_Product_ID"/>
      <Parameter name="C_AcctSchema_ID"/>
-   </SqlMethod>  
+   </SqlMethod>
+      <SqlMethod name="selectAcctDateOrMaxInvoiceAcctDate" type="preparedStatement" return="Date">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql>
+      <![CDATA[
+        SELECT MAX(dateacct)
+        FROM (SELECT GREATEST(i.dateacct, TO_DATE(?)) AS dateacct
+              FROM m_lc_matched lcm
+                JOIN c_invoiceline il ON (lcm.c_invoiceline_id = il.c_invoiceline_id)
+                JOIN c_invoice i ON (il.c_invoice_id = i.c_invoice_id)
+              WHERE lcm.isactive = 'Y'
+                AND lcm.m_lc_cost_id = ?) A
+     ]]>
+     </Sql>
+     <Parameter name="DateAcct"/>
+     <Parameter name="LCCostId"/>
+   </SqlMethod>
  </SqlClass>
--- a/src/org/openbravo/erpCommon/ad_forms/DocLineLandedCost_data.xsql	Fri Jul 17 09:54:31 2020 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/DocLineLandedCost_data.xsql	Fri Jul 03 13:33:48 2020 +0200
@@ -30,7 +30,7 @@
       <![CDATA[
         SELECT LCR.M_LC_RECEIPTLINE_AMT_ID, LCR.AD_CLIENT_ID, LCR.AD_ORG_ID, 
         LCR.M_LC_COST_ID, LCR.AMOUNT  * COALESCE(A.QUANTITY, IOL.MOVEMENTQTY)/IOL.MOVEMENTQTY AS AMOUNT, 
-        LCC.M_LC_TYPE_ID, L.M_WAREHOUSE_ID, IOL.M_PRODUCT_ID,
+        GREATEST(IO.DATEACCT, TO_DATE(?)) AS DATEACCT, LCC.M_LC_TYPE_ID, L.M_WAREHOUSE_ID, IOL.M_PRODUCT_ID,
         LCC.C_CURRENCY_ID, '' AS ACCOUNT_ID, '' AS NAME, '' AS GLITEM_DEBIT_ACCT, '' AS GLITEM_CREDIT_ACCT, 
         A.C_Campaign_ID, COALESCE(A.C_Project_Id, IOL.C_Project_Id) AS C_Project_Id, 
         COALESCE(A.User1_ID, IOL.User1_ID) AS user1Id, COALESCE(A.User2_ID, IOL.User2_ID) AS user2Id, 
@@ -48,6 +48,7 @@
         ORDER BY LCC.LINE, IO.DOCUMENTNO, IO.M_INOUT_ID, IOL.LINE
      ]]>
      </Sql>
+     <Parameter name="LC_Reference_Date"/>
      <Parameter name="LC_Receiptline_Amt"/>
    </SqlMethod>
    <SqlMethod name="selectLCAccount" type="preparedStatement" return="multiple">