# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1548862494 -19800
#      Wed Jan 30 21:04:54 2019 +0530
# Node ID 814e2c1990ea261a9a7e62914bdd0c2e3ab2bfc6
# Parent  22b071f5c3ce0a7f3ea684bee0d72d4c13429bd9
Fixed issue 40061 : PaymentMethodCashup should be updated when Cashup is Done

* PaymentMethodCashup should be created/updated for the following cases
  1) when cashup is closed
  2) when ticket is closed with respective payment

diff -r 22b071f5c3ce -r 814e2c1990ea src/org/openbravo/retail/posterminal/UpdateCashup.java
--- a/src/org/openbravo/retail/posterminal/UpdateCashup.java	Wed Jan 30 20:48:07 2019 +0530
+++ b/src/org/openbravo/retail/posterminal/UpdateCashup.java	Wed Jan 30 21:04:54 2019 +0530
@@ -1,6 +1,6 @@
 /*
  ************************************************************************************
- * Copyright (C) 2016-2018 Openbravo S.L.U.
+ * Copyright (C) 2016-2019 Openbravo S.L.U.
  * Licensed under the Openbravo Commercial License version 1.0
  * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
  * or in the legal folder of this module distribution.
@@ -28,12 +28,11 @@
 import org.openbravo.dal.service.OBQuery;
 import org.openbravo.mobile.core.process.JSONPropertyToEntity;
 import org.openbravo.mobile.core.process.PropertyByType;
-import org.openbravo.service.importprocess.ImportEntryManager;
 import org.openbravo.service.json.JsonToDataConverter;
 
 public class UpdateCashup {
 
-  private static final Logger log = Logger.getLogger(ImportEntryManager.class);
+  private static final Logger log = Logger.getLogger(UpdateCashup.class);
 
   /**
    * Get and update a cashup. If cashup not exist it's created, otherwise update the cashup data
@@ -217,39 +216,44 @@
     if (jsonCashup.has("cashPaymentMethodInfo")) {
       JSONArray paymentCashupInfo = jsonCashup.getJSONArray("cashPaymentMethodInfo");
       for (int i = 0; i < paymentCashupInfo.length(); ++i) {
+        boolean createPaymentMethods = false;
         JSONObject payment = paymentCashupInfo.getJSONObject(i);
         if ((payment.has("newPaymentMethod")
             && payment.getString("newPaymentMethod").equals("true"))
             || (payment.has("usedInCurrentTrx")
                 && payment.getString("usedInCurrentTrx").equals("true"))) {
+          createPaymentMethods = true;
+        }
 
-          // Set Amount To Keep
-          if (jsonCashup.has("cashCloseInfo")) {
-            // Get the paymentMethod id
-            JSONArray cashCloseInfo = jsonCashup.getJSONArray("cashCloseInfo");
-            for (int j = 0; j < cashCloseInfo.length(); ++j) {
-              JSONObject paymentMethod = cashCloseInfo.getJSONObject(j);
-              if (paymentMethod.getString("paymentTypeId")
-                  .equals(payment.getString("paymentMethodId"))) {
-                payment.put("amountToKeep",
-                    paymentMethod.getJSONObject("paymentMethod").getString("amountToKeep"));
-                BigDecimal expected = BigDecimal.ZERO;
-                BigDecimal difference = BigDecimal.ZERO;
-                BigDecimal rate = new BigDecimal(payment.getString("rate"));
-                if (rate.compareTo(BigDecimal.ONE) == 0) {
-                  expected = new BigDecimal(paymentMethod.getString("expected"));
-                  difference = new BigDecimal(paymentMethod.getString("difference"));
-                } else if (paymentMethod.has("foreignExpected")) {
-                  expected = new BigDecimal(paymentMethod.getString("foreignExpected"));
-                  difference = new BigDecimal(paymentMethod.has("foreignDifference")
-                      ? paymentMethod.getString("foreignDifference")
-                      : paymentMethod.getString("difference"));
-                }
-                payment.put("totalCounted",
-                    expected.add(difference).setScale(2, RoundingMode.HALF_UP).toString());
+        // Set Amount To Keep
+        if (jsonCashup.has("cashCloseInfo")) {
+          // Get the paymentMethod id
+          JSONArray cashCloseInfo = jsonCashup.getJSONArray("cashCloseInfo");
+          for (int j = 0; j < cashCloseInfo.length(); ++j) {
+            JSONObject paymentMethod = cashCloseInfo.getJSONObject(j);
+            if (paymentMethod.getString("paymentTypeId")
+                .equals(payment.getString("paymentMethodId"))) {
+              payment.put("amountToKeep",
+                  paymentMethod.getJSONObject("paymentMethod").getString("amountToKeep"));
+              BigDecimal expected = BigDecimal.ZERO;
+              BigDecimal difference = BigDecimal.ZERO;
+              BigDecimal rate = new BigDecimal(payment.getString("rate"));
+              if (rate.compareTo(BigDecimal.ONE) == 0) {
+                expected = new BigDecimal(paymentMethod.getString("expected"));
+                difference = new BigDecimal(paymentMethod.getString("difference"));
+              } else if (paymentMethod.has("foreignExpected")) {
+                expected = new BigDecimal(paymentMethod.getString("foreignExpected"));
+                difference = new BigDecimal(paymentMethod.has("foreignDifference")
+                    ? paymentMethod.getString("foreignDifference")
+                    : paymentMethod.getString("difference"));
               }
+              payment.put("totalCounted",
+                  expected.add(difference).setScale(2, RoundingMode.HALF_UP).toString());
+              createPaymentMethods = true;
             }
           }
+        }
+        if (createPaymentMethods) {
           createPaymentMethodCashUp(cashup, payment);
         }
       }
