[Reprint Invoices] Merge with PI
authorEduardo Becerra <ebec70@yahoo.com>
Tue, 13 Nov 2018 12:08:14 +0100
changeset 34963 e0dd4d4090a1
parent 34958 d973d6edd8ea (current diff)
parent 34962 6323b569fcbb (diff)
child 34969 75a160f452b7
[Reprint Invoices] Merge with PI
--- a/modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml	Fri Nov 09 11:44:53 2018 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml	Tue Nov 13 12:08:14 2018 +0100
@@ -83,6 +83,8 @@
 v_PricePr NUMBER;
 v_isinvoicepaid CHAR(1):= 'N';
 v_IsCashVAT C_Invoice.IsCashVAT%TYPE;
+v_prepaymentamt NUMBER;
+v_hasPrepaymentToReturn BOOLEAN := false;
 
 TYPE RECORD IS REF CURSOR;
 Cur_Params RECORD;
@@ -126,13 +128,13 @@
       C_BPartner_ID, DateInvoiced, totallines, fin_paymentmethod_id,
       fin_payment_priority_id, documentNo, poreference,
       c_project_id, c_campaign_id, c_activity_id, user1_id, user2_id, c_costcenter_id,
-      IsCashVAT
+      IsCashVAT, prepaymentamt
   INTO v_Client_ID, v_Org_ID, v_DocTypeTarget_ID, v_IsSOTrx,
       v_GrandTotal, v_PaymentTerm, v_PaymentRule, v_Currency_ID,
       v_BPartner_ID, v_DateInvoiced, v_totallines, v_PaymentMethod,
       v_PaymentPriority, v_documentno, v_poreference,
       v_project_id, v_campaign_id, v_activity_id, v_user1, v_user2, v_costcenter,
-      v_IsCashVAT
+      v_IsCashVAT, v_prepaymentamt
   FROM c_invoice
   WHERE c_invoice_id=p_record_id;
   
@@ -265,11 +267,18 @@
       INTO v_ConsumedGranTotal
       FROM fin_payment_schedule ps
       WHERE ps.c_invoice_id = p_record_id;
+
+      --When the Total Amount of the Invoice is 0 and nothing has been consumed yet for the Invoice
+      --but there is already a prepayment made for a related Order
+      --Related to Issue https://issues.openbravo.com/view.php?id=39415
+      IF (v_GrandTotal = 0 AND v_ConsumedGranTotal = 0 AND v_prepaymentamt <> 0) THEN
+        v_hasPrepaymentToReturn := true;
+      END IF;
             
       --Insert Payment Schedules for Invoices
       v_pendingAmount := v_GrandTotal - coalesce(v_ConsumedGranTotal, 0);
 
-      IF (v_pendingAmount <> 0) THEN
+      IF (v_pendingAmount <> 0 OR v_hasPrepaymentToReturn) THEN
 
         --get number of payment schedules expected for the invoice
         SELECT count(*) INTO v_paymentcount
@@ -1168,8 +1177,6 @@
     END;
 
   ELSIF (p_docaction = 'RC') THEN
-    DECLARE
-      v_prepaymentamt NUMBER;
     BEGIN
       v_delete_paymentplan := TRUE;
       SELECT c_invoice_id INTO v_reversalinvoice_id
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java	Fri Nov 09 11:44:53 2018 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java	Tue Nov 13 12:08:14 2018 +0100
@@ -1389,14 +1389,16 @@
         firstDueDate = paymentSchedule.getDueDate();
     }
 
-    BigDecimal overdueAmount = calculateOverdueAmount(invoicePaymentSchedule);
-    invoice.setPercentageOverdue(overdueAmount.multiply(new BigDecimal("100"))
-        .divide(invoice.getGrandTotalAmount(), 2, RoundingMode.HALF_UP).longValue());
-
-    if (firstDueDate != null)
+    if (invoice.getGrandTotalAmount().compareTo(BigDecimal.ZERO) != 0) {
+      BigDecimal overdueAmount = calculateOverdueAmount(invoicePaymentSchedule);
+      invoice.setPercentageOverdue(overdueAmount.multiply(new BigDecimal("100"))
+          .divide(invoice.getGrandTotalAmount(), 2, RoundingMode.HALF_UP).longValue());
+    }
+    if (firstDueDate != null) {
       invoice.setDaysTillDue(FIN_Utility.getDaysToDue(firstDueDate));
-    else
+    } else {
       invoice.setDaysTillDue(0L);
+    }
     OBDal.getInstance().save(invoice);
   }
 
--- a/src-db/database/model/functions/C_INVOICE_POST.xml	Fri Nov 09 11:44:53 2018 +0100
+++ b/src-db/database/model/functions/C_INVOICE_POST.xml	Tue Nov 13 12:08:14 2018 +0100
@@ -1767,8 +1767,8 @@
     UPDATE C_Invoice i
     SET prepaymentamt = (
       SELECT COALESCE(SUM(fps.paidamt), 0)
-      FROM FIN_Payment_Schedule fps
-      WHERE fps.C_Order_ID = i.C_Order_ID
+      FROM FIN_Payment_Schedule fps 
+      WHERE EXISTS ( select 1  from c_invoiceline il , c_orderline ol where il.c_invoice_id=v_Record_ID and il.c_orderline_id=ol.c_orderline_id and fps.C_Order_ID =ol.c_order_id)  
     )
     WHERE i.C_Invoice_ID = v_Record_ID;
   END IF;--FINISH_PROCESS
--- a/src/org/openbravo/erpCommon/ad_callouts/SL_RequisitionLine_Product.java	Fri Nov 09 11:44:53 2018 +0100
+++ b/src/org/openbravo/erpCommon/ad_callouts/SL_RequisitionLine_Product.java	Tue Nov 13 12:08:14 2018 +0100
@@ -79,7 +79,7 @@
         PriceList priceListObj = OBDal.getInstance().get(PriceList.class, strPriceListId);
 
         // Set Auxiliary Input parameter Gross Price
-        info.addResult("inpgrossprice", priceListObj.isPriceIncludesTax());
+        info.addResult("inpgrossprice", priceListObj.isPriceIncludesTax() ? "Y" : "N");
 
         // Discount, Price List, Gross Unit Price, Price Actual
         String strMessage = "";
@@ -118,6 +118,8 @@
           info.showMessage(FormatUtilities.replaceJS(Utility.messageBD(this, strMessage,
               info.vars.getLanguage())));
         }
+      } else {
+        info.addResult("inpgrossprice", "");
       }
     }
 
--- a/src/org/openbravo/erpCommon/utility/ErrorTextParser.java	Fri Nov 09 11:44:53 2018 +0100
+++ b/src/org/openbravo/erpCommon/utility/ErrorTextParser.java	Tue Nov 13 12:08:14 2018 +0100
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2001-2010 Openbravo SLU 
+ * All portions are Copyright (C) 2001-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -156,7 +156,7 @@
   protected String getTableName(String tableName) {
     try {
       String pkColumnName = tableName + "_ID";
-      return ErrorTextParserData.selectColumnName(conn, language, pkColumnName);
+      return ErrorTextParserData.selectColumnName(conn, language, tableName, pkColumnName);
     } catch (ServletException e) {
       log4j.error(
           "Error while trying to name for table via ad_element for tablename: " + tableName, e);
@@ -168,14 +168,17 @@
    * Helper method to get a (slightly better) human-readable name for a database column based on its
    * name. Method uses AD_ELEMENT.name and AD_ELEMENT_TRL.NAME for this purpose
    * 
+   * @param tableName
+   *          table where the column is in
+   * 
    * @param columnName
-   *          name of a database column
+   *          name of the column to get its human readable name
    * @return translated, human-readable name
    */
-  protected String getColumnName(String columnName) {
+  protected String getColumnName(String tableName, String columnName) {
     String res;
     try {
-      res = ErrorTextParserData.selectColumnName(conn, language, columnName);
+      res = ErrorTextParserData.selectColumnName(conn, language, tableName, columnName);
       return res;
     } catch (ServletException e) {
       log4j.error("Error while trying to get name for ad_element.columnname: " + columnName, e);
@@ -236,7 +239,7 @@
           if (columns.length() > 0) {
             columns.append(", ");
           }
-          columns.append(getColumnName(column));
+          columns.append(getColumnName(constraintData[0].tableName, column));
         }
         String columnName;
         if (columnList.length > 1) {
@@ -300,7 +303,7 @@
           if (msgText != null) {
             String msgTemplate = msgText.getField("msgText");
             String tableName = getTableName(constraintData[0].tableName);
-            columnName = getColumnName(columnName);
+            columnName = getColumnName(constraintData[0].tableName, columnName);
             Map<String, String> replaceMap = new HashMap<String, String>();
             replaceMap.put("TABLE_NAME", tableName);
             replaceMap.put("COLUMN_NAME", columnName);
@@ -312,7 +315,7 @@
         } else if (searchCond.endsWith(" IN ('Y','N')") || searchCond.endsWith(" IN ('Y', 'N')")
             || searchCond.endsWith(" IN ('N','Y')") || searchCond.endsWith(" IN ('N', 'Y')")) {
           String columnName = searchCond.substring(0, searchCond.lastIndexOf(" IN (")).trim();
-          columnName = getColumnName(columnName);
+          columnName = getColumnName(constraintData[0].tableName, columnName);
 
           FieldProvider msgText = Utility.locateMessage(getConnection(), "NotYNError",
               getLanguage());
--- a/src/org/openbravo/erpCommon/utility/ErrorTextParserORACLE.java	Fri Nov 09 11:44:53 2018 +0100
+++ b/src/org/openbravo/erpCommon/utility/ErrorTextParserORACLE.java	Tue Nov 13 12:08:14 2018 +0100
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2001-2010 Openbravo SLU 
+ * All portions are Copyright (C) 2001-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -184,8 +184,8 @@
               getLanguage());
           if (msgText != null) {
             String msgTemplate = msgText.getField("msgText");
+            columnName = getColumnName(tableName, columnName);
             tableName = getTableName(tableName);
-            columnName = getColumnName(columnName);
             Map<String, String> replaceMap = new HashMap<String, String>();
             replaceMap.put("TABLE_NAME", tableName);
             replaceMap.put("COLUMN_NAME", columnName);
--- a/src/org/openbravo/erpCommon/utility/ErrorTextParser_data.xsql	Fri Nov 09 11:44:53 2018 +0100
+++ b/src/org/openbravo/erpCommon/utility/ErrorTextParser_data.xsql	Tue Nov 13 12:08:14 2018 +0100
@@ -12,7 +12,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2001-2010 Openbravo SLU
+ * All portions are Copyright (C) 2001-2018 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -38,12 +38,21 @@
   <SqlMethod name="selectColumnName" type="preparedStatement" return="String">
     <SqlMethodComment></SqlMethodComment>
     <Sql>
-      select coalesce(ad_element_trl.name, ad_element.name) as column_name
-      from ad_element left join ad_element_trl on ad_element.ad_element_id = ad_element_trl.ad_element_id and ad_element_trl.ad_language = ?
-      where upper(ad_element.columnname) = upper(?)
+        select coalesce(et.name, e.name, c.name) as column_name
+          from ad_table t,
+               ad_column c
+                left join ad_element e
+                       on c.ad_element_id = e.ad_element_id
+                left join ad_element_trl et
+                       on e.ad_element_id = et.ad_element_id
+                      and et.ad_language = ?
+         where t.ad_table_id = c.ad_table_id
+           and upper(t.tableName) = upper(?)
+           and upper(c.columnName) = upper(?)
     </Sql>
     <Parameter name="language"/>
-    <Parameter name="columnname"/>
+    <Parameter name="tableName"/>
+    <Parameter name="columnName"/>
   </SqlMethod>
 
   <SqlMethod name="selectColumnNamesForConstraint" type="preparedStatement" return="multiple">