Fixed bug 31699: Payments after manual cash vat settlement
authorVíctor Martínez Romanos <victor.martinez@openbravo.com>
Mon, 14 Dec 2015 11:14:22 +0100
changeset 28128 45c7d09e8fd0
parent 28127 67bf31e6746a
child 28129 7c6686c84928
Fixed bug 31699: Payments after manual cash vat settlement

When an invoice has been already included in a Manual Cash VAT Settlement, the system won't create new Cash VAT management records when partially/fully paying the invoice.
src/org/openbravo/erpCommon/utility/CashVATUtil.java
--- a/src/org/openbravo/erpCommon/utility/CashVATUtil.java	Mon Dec 14 14:11:12 2015 +0100
+++ b/src/org/openbravo/erpCommon/utility/CashVATUtil.java	Mon Dec 14 11:14:22 2015 +0100
@@ -229,7 +229,10 @@
 
   /**
    * Creates the records into the Cash VAT management table (InvoiceTaxCashVAT), calculating the
-   * percentage paid/collected tax amount and taxable amount. Only for cash vat tax rates
+   * percentage paid/collected tax amount and taxable amount. Only for cash vat tax rates.
+   * 
+   * If the invoice has been already settled in a Manual Cash VAT Settlement, we don't create a new
+   * Cash VAT management record
    * 
    */
   public static void createInvoiceTaxCashVAT(final FIN_PaymentDetail paymentDetail,
@@ -245,7 +248,7 @@
           for (InvoiceTaxCashVAT previousITCV : previousITCashVATs) {
             OBDal.getInstance().remove(previousITCV);
           }
-        } else {
+        } else if (!hasManualCashVATSettlement(invoice)) {
           final boolean calculateAmountsBasedOnPercentage;
           BigDecimal percentage = null; /* Calculate it later on */
           final BigDecimal outstandingAmt = invoice.getOutstandingAmount();
@@ -302,6 +305,31 @@
   }
 
   /**
+   * Returns true if the invoice has a manual cash vat settlement associated
+   */
+  public static boolean hasManualCashVATSettlement(final Invoice invoice) {
+    try {
+      OBContext.setAdminMode(true);
+
+      final StringBuffer hql = new StringBuffer();
+      hql.append(" select itcv.id ");
+      hql.append(" from InvoiceTaxCashVAT itcv ");
+      hql.append(" inner join itcv.invoiceTax it ");
+      hql.append(" where it.invoice.id = :invoiceId ");
+      hql.append(" and itcv.isManualSettlement = true ");
+
+      final Session session = OBDal.getInstance().getSession();
+      final Query query = session.createQuery(hql.toString());
+      query.setParameter("invoiceId", invoice.getId());
+      query.setMaxResults(1);
+
+      return !query.list().isEmpty();
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+  }
+
+  /**
    * Gets the InvoiceTaxCashVAT records linked to the payment detail
    */
   public static List<InvoiceTaxCashVAT> getInvoiceTaxCashVAT(final FIN_PaymentDetail paymentDetail) {