You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by mb...@apache.org on 2017/12/12 06:03:57 UTC

svn commit: r1817880 [2/3] - in /ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice: InvoiceServices.java InvoiceWorker.java

Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java?rev=1817880&r1=1817879&r2=1817880&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java Tue Dec 12 06:03:56 2017
@@ -33,6 +33,8 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
+
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVRecord;
@@ -139,7 +141,12 @@ public class InvoiceServices {
             result.remove("invoiceTypeId");  //remove extra parameter
             return result;
         }
-        catch (GenericServiceException | GenericEntityException e) {
+        catch (GenericServiceException e) {
+            Debug.logError (e, "Entity/data problem creating invoice from order items: " + e.toString(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource,
+                    "AccountingEntityDataProblemCreatingInvoiceFromOrderItems",
+                    UtilMisc.toMap("reason", e.toString()), locale));
+        } catch (GenericEntityException e) {
             Debug.logError (e, "Entity/data problem creating invoice from order items: " + e.toString(), module);
             return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                     "AccountingEntityDataProblemCreatingInvoiceFromOrderItems",
@@ -165,14 +172,14 @@ public class InvoiceServices {
 
         if (UtilValidate.isEmpty(billItems)) {
             Debug.logVerbose("No order items to invoice; not creating invoice; returning success", module);
-            return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource,
+            return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, 
                     "AccountingNoOrderItemsToInvoice", locale));
         }
 
         try {
             GenericValue orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
             if (orderHeader == null) {
-                return ServiceUtil.returnError(UtilProperties.getMessage(resource,
+                return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
                         "AccountingNoOrderHeader", locale));
             }
 
@@ -188,9 +195,7 @@ public class InvoiceServices {
 
             // Set the precision depending on the type of invoice
             int invoiceTypeDecimals = UtilNumber.getBigDecimalScale("invoice." + invoiceType + ".decimals");
-            if (invoiceTypeDecimals == -1) {
-                invoiceTypeDecimals = DECIMALS;
-            }
+            if (invoiceTypeDecimals == -1) invoiceTypeDecimals = DECIMALS;
 
             // Make an order read helper from the order
             OrderReadHelper orh = new OrderReadHelper(orderHeader);
@@ -237,7 +242,7 @@ public class InvoiceServices {
 
             // create the invoice record
             if (UtilValidate.isEmpty(invoiceId)) {
-                Map<String, Object> createInvoiceContext = new HashMap<>();
+                Map<String, Object> createInvoiceContext = new HashMap<String, Object>();
                 createInvoiceContext.put("partyId", billToCustomerPartyId);
                 createInvoiceContext.put("partyIdFrom", billFromVendorPartyId);
                 createInvoiceContext.put("billingAccountId", billingAccountId);
@@ -252,7 +257,7 @@ public class InvoiceServices {
                 // store the invoice first
                 Map<String, Object> createInvoiceResult = dispatcher.runSync("createInvoice", createInvoiceContext);
                 if (ServiceUtil.isError(createInvoiceResult)) {
-                    return ServiceUtil.returnError(UtilProperties.getMessage(resource,
+                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
                             "AccountingErrorCreatingInvoiceFromOrder", locale), null, null, createInvoiceResult);
                 }
 
@@ -262,7 +267,7 @@ public class InvoiceServices {
 
             // order roles to invoice roles
             List<GenericValue> orderRoles = orderHeader.getRelated("OrderRole", null, null, false);
-            Map<String, Object> createInvoiceRoleContext = new HashMap<>();
+            Map<String, Object> createInvoiceRoleContext = new HashMap<String, Object>();
             createInvoiceRoleContext.put("invoiceId", invoiceId);
             createInvoiceRoleContext.put("userLogin", userLogin);
             for (GenericValue orderRole : orderRoles) {
@@ -385,14 +390,14 @@ public class InvoiceServices {
                     orderItem = itemIssuance.getRelatedOne("OrderItem", false);
                 } else if ((orderItem == null) && (shipmentReceipt != null)) {
                     orderItem = shipmentReceipt.getRelatedOne("OrderItem", false);
-                }
-
+                } 
+                
                 if (orderItem == null) {
                     Debug.logError("Cannot create invoice when orderItem, itemIssuance, and shipmentReceipt are all null", module);
                     return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                             "AccountingIllegalValuesPassedToCreateInvoiceService", locale));
                 }
-
+                
                 GenericValue product = null;
                 if (orderItem.get("productId") != null) {
                     product = orderItem.getRelatedOne("Product", false);
@@ -417,9 +422,7 @@ public class InvoiceServices {
                         billingQuantity = ZERO;
                     }
                 }
-                if (billingQuantity == null) {
-                    billingQuantity = ZERO;
-                }
+                if (billingQuantity == null) billingQuantity = ZERO;
 
                 // check if shipping applies to this item.  Shipping is calculated for sales invoices, not purchase invoices.
                 boolean shippingApplies = false;
@@ -440,7 +443,7 @@ public class InvoiceServices {
                     billingAmount = orderItem.getBigDecimal("unitPrice").setScale(invoiceTypeDecimals, ROUNDING);
                 }
 
-                Map<String, Object> createInvoiceItemContext = new HashMap<>();
+                Map<String, Object> createInvoiceItemContext = new HashMap<String, Object>();
                 createInvoiceItemContext.put("invoiceId", invoiceId);
                 createInvoiceItemContext.put("invoiceItemSeqId", invoiceItemSeqId);
                 createInvoiceItemContext.put("invoiceItemTypeId", getInvoiceItemType(delegator, (orderItem.getString("orderItemTypeId")), (product == null ? null : product.getString("productTypeId")), invoiceType, "INV_FPROD_ITEM"));
@@ -484,7 +487,7 @@ public class InvoiceServices {
                 invoiceQuantity = invoiceQuantity.add(billingQuantity).setScale(invoiceTypeDecimals, ROUNDING);
 
                 // create the OrderItemBilling record
-                Map<String, Object> createOrderItemBillingContext = new HashMap<>();
+                Map<String, Object> createOrderItemBillingContext = new HashMap<String, Object>();
                 createOrderItemBillingContext.put("invoiceId", invoiceId);
                 createOrderItemBillingContext.put("invoiceItemSeqId", invoiceItemSeqId);
                 createOrderItemBillingContext.put("orderId", orderItem.get("orderId"));
@@ -514,7 +517,7 @@ public class InvoiceServices {
                     if (UtilValidate.isEmpty(shipmentItemBillings)) {
 
                         // create the ShipmentItemBilling record
-                        Map<String, Object> shipmentItemBillingCtx = new HashMap<>();
+                        Map<String, Object> shipmentItemBillingCtx = new HashMap<String, Object>();
                         shipmentItemBillingCtx.put("invoiceId", invoiceId);
                         shipmentItemBillingCtx.put("invoiceItemSeqId", invoiceItemSeqId);
                         shipmentItemBillingCtx.put("shipmentId", currentValue.get("shipmentId"));
@@ -555,6 +558,9 @@ public class InvoiceServices {
                                 "AccountingTroubleCallingCalculateInvoicedAdjustmentTotalService", locale));
                     }
 
+//                    if (adj.get("amount") == null) { TODO check usage with webPos. Was: fix a bug coming from POS in case of use of a discount (on item(s) or sale, item(s) here) and a cash amount higher than total (hence issuing change)
+//                        continue;
+//                    }
                     // Set adjustment amount as amountAlreadyIncluded to continue invoice item creation process
                     Boolean isTaxIncludedInPrice = "VAT_TAX".equals(adj.getString("orderAdjustmentTypeId")) && UtilValidate.isNotEmpty(adj.getBigDecimal("amountAlreadyIncluded")) && adj.getBigDecimal("amountAlreadyIncluded").signum() != 0;
                     if ((adj.getBigDecimal("amount").signum() == 0) && isTaxIncludedInPrice) {
@@ -594,12 +600,12 @@ public class InvoiceServices {
                                         amount = amount.multiply(billingQuantity);
                                         // get adjustment amount
                                         /* Get tax amount of other invoice and calculate remaining amount need to store in invoice item(Handle case of of partial shipment and promotional item)
-                                                                              to adjust tax amount in invoice item.
+                                                                              to adjust tax amount in invoice item. 
                                          */
                                         BigDecimal otherInvoiceTaxAmount = BigDecimal.ZERO;
                                         GenericValue orderAdjBilling = EntityUtil.getFirst(delegator.findByAnd("OrderAdjustmentBilling", UtilMisc.toMap("orderAdjustmentId", adj.getString("orderAdjustmentId")), null, false));
                                         if (UtilValidate.isNotEmpty(orderAdjBilling)) {
-                                            List<GenericValue> invoiceItems = delegator.findByAnd("InvoiceItem",
+                                            List<GenericValue> invoiceItems = delegator.findByAnd("InvoiceItem", 
                                                     UtilMisc.toMap("invoiceId", orderAdjBilling.getString("invoiceId"), "invoiceItemTypeId", "ITM_SALES_TAX", "productId", originalOrderItem.getString("productId")), null, isTaxIncludedInPrice);
                                             for (GenericValue invoiceItem : invoiceItems) {
                                                 otherInvoiceTaxAmount = otherInvoiceTaxAmount.add(invoiceItem.getBigDecimal("amount"));
@@ -633,7 +639,7 @@ public class InvoiceServices {
                         }
                     }
                     if (amount.signum() != 0) {
-                        Map<String, Object> createInvoiceItemAdjContext = new HashMap<>();
+                        Map<String, Object> createInvoiceItemAdjContext = new HashMap<String, Object>();
                         createInvoiceItemAdjContext.put("invoiceId", invoiceId);
                         createInvoiceItemAdjContext.put("invoiceItemSeqId", invoiceItemSeqId);
                         createInvoiceItemAdjContext.put("invoiceItemTypeId", getInvoiceItemType(delegator, adj.getString("orderAdjustmentTypeId"), null, invoiceType, "INVOICE_ITM_ADJ"));
@@ -681,7 +687,7 @@ public class InvoiceServices {
                         }
 
                         // Create the OrderAdjustmentBilling record
-                        Map<String, Object> createOrderAdjustmentBillingContext = new HashMap<>();
+                        Map<String, Object> createOrderAdjustmentBillingContext = new HashMap<String, Object>();
                         createOrderAdjustmentBillingContext.put("orderAdjustmentId", adj.getString("orderAdjustmentId"));
                         createOrderAdjustmentBillingContext.put("invoiceId", invoiceId);
                         createOrderAdjustmentBillingContext.put("invoiceItemSeqId", invoiceItemSeqId);
@@ -717,8 +723,8 @@ public class InvoiceServices {
             }
 
             // create header adjustments as line items -- always to tax/shipping last
-            Map<GenericValue, BigDecimal> shipAdjustments = new HashMap<>();
-            Map<GenericValue, BigDecimal> taxAdjustments = new HashMap<>();
+            Map<GenericValue, BigDecimal> shipAdjustments = new HashMap<GenericValue, BigDecimal>();
+            Map<GenericValue, BigDecimal> taxAdjustments = new HashMap<GenericValue, BigDecimal>();
 
             List<GenericValue> headerAdjustments = orh.getOrderHeaderAdjustments();
             for (GenericValue adj : headerAdjustments) {
@@ -738,6 +744,9 @@ public class InvoiceServices {
                             "AccountingTroubleCallingCalculateInvoicedAdjustmentTotalService", locale));
                 }
 
+//                if (null == adj.get("amount")) { TODO check usage with webPos. Was: fix a bug coming from POS in case of use of a discount (on item(s) or sale, sale here) and a cash amount higher than total (hence issuing change)
+//                    continue;
+//                }
                 // If the absolute invoiced amount >= the abs of the adjustment amount, the full amount has already been invoiced, so skip this adjustment
                 if (adjAlreadyInvoicedAmount.abs().compareTo(adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING).abs()) >= 0) {
                     continue;
@@ -776,7 +785,7 @@ public class InvoiceServices {
             for (Map.Entry<GenericValue, BigDecimal> set : shipAdjustments.entrySet()) {
                 BigDecimal adjAlreadyInvoicedAmount = set.getValue();
                 GenericValue adj = set.getKey();
-
+                
                 if ("N".equalsIgnoreCase(prorateShipping)) {
 
                     // Set the divisor and multiplier to 1 to avoid prorating
@@ -786,7 +795,7 @@ public class InvoiceServices {
                     // The base amount in this case is the adjustment amount minus the total already invoiced for that adjustment, since
                     //  it won't be prorated
                     BigDecimal baseAmount = adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING).subtract(adjAlreadyInvoicedAmount);
-                    calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier, baseAmount,
+                    calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier, baseAmount, 
                             invoiceTypeDecimals, ROUNDING, userLogin, dispatcher, locale);
                 } else {
 
@@ -802,7 +811,7 @@ public class InvoiceServices {
 
                     // The base amount in this case is the adjustment amount, since we want to prorate based on the full amount
                     BigDecimal baseAmount = adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING);
-                    calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier,
+                    calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier, 
                             baseAmount, invoiceTypeDecimals, ROUNDING, userLogin, dispatcher, locale);
                 }
 
@@ -855,7 +864,7 @@ public class InvoiceServices {
                     .where(EntityCondition.makeCondition("orderId", EntityOperator.EQUALS, orderId),
                             EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_CANCELLED")
                     ).queryList();
-            List<GenericValue> currentPayments = new LinkedList<>();
+            List<GenericValue> currentPayments = new LinkedList<GenericValue>();
             for (GenericValue paymentPref : orderPaymentPrefs) {
                 List<GenericValue> payments = paymentPref.getRelated("Payment", null, null, false);
                 currentPayments.addAll(payments);
@@ -867,7 +876,7 @@ public class InvoiceServices {
                 }
                 BigDecimal notApplied = PaymentWorker.getPaymentNotApplied(payment);
                 if (notApplied.signum() > 0) {
-                    Map<String, Object> appl = new HashMap<>();
+                    Map<String, Object> appl = new HashMap<String, Object>();
                     appl.put("paymentId", payment.get("paymentId"));
                     appl.put("invoiceId", invoiceId);
                     appl.put("billingAccountId", billingAccountId);
@@ -916,8 +925,8 @@ public class InvoiceServices {
         GenericValue userLogin = (GenericValue) context.get("userLogin");
         Locale locale = (Locale) context.get("locale");
         List<String> salesInvoiceIds = UtilGenerics.checkList(context.get("invoiceIds"));
-        List<Map<String, String>> invoicesCreated = new LinkedList<>();
-        Map<String, List<Map<String, Object>>> commissionParties = new HashMap<>();
+        List<Map<String, String>> invoicesCreated = new LinkedList<Map<String,String>>();
+        Map<String, List<Map<String, Object>>> commissionParties = new HashMap<String, List<Map<String,Object>>>();
         for (String salesInvoiceId : salesInvoiceIds) {
             List<String> salesRepPartyIds = UtilGenerics.checkList(context.get("partyIds"));
             BigDecimal amountTotal =  InvoiceWorker.getInvoiceTotal(delegator, salesInvoiceId);
@@ -1019,7 +1028,7 @@ public class InvoiceServices {
         Timestamp now = UtilDateTime.nowTimestamp();
         // Create invoice for each commission receiving party
         for (Map.Entry<String, List<Map<String, Object>>> commissionParty : commissionParties.entrySet()) {
-            List<GenericValue> toStore = new LinkedList<>();
+            List<GenericValue> toStore = new LinkedList<GenericValue>();
             List<Map<String, Object>> commList = commissionParty.getValue();
             // get the billing parties
             if (UtilValidate.isEmpty(commList)) {
@@ -1033,7 +1042,7 @@ public class InvoiceServices {
             Long days = (Long) (commList.get(0)).get("days");
             // create the invoice record
             // To and From are in commission's sense, opposite for invoice
-            Map<String, Object> createInvoiceMap = new HashMap<>();
+            Map<String, Object> createInvoiceMap = new HashMap<String, Object>();
             createInvoiceMap.put("partyId", partyIdBillTo);
             createInvoiceMap.put("partyIdFrom", partyIdBillFrom);
             createInvoiceMap.put("invoiceDate", now);
@@ -1135,10 +1144,10 @@ public class InvoiceServices {
             invoicesCreated.add(UtilMisc.<String, String>toMap("commissionInvoiceId",invoiceId, "salesRepresentative ",partyIdBillFrom));
         }
         String invCreated = Integer.toString(invoicesCreated.size());
-        Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource,
-                "AccountingCommissionInvoicesCreated",
+        Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, 
+                "AccountingCommissionInvoicesCreated", 
                 UtilMisc.toMap("invoicesCreated", invCreated), locale));
-        Debug.logInfo("Created Commission invoices for each commission receiving parties " +
+        Debug.logInfo("Created Commission invoices for each commission receiving parties " + 
                 invCreated, module);
         result.put("invoicesCreated", invoicesCreated);
         return result;
@@ -1193,8 +1202,8 @@ public class InvoiceServices {
             } catch (GenericEntityException e) {
                 return ServiceUtil.returnError(e.getMessage());
             }
-        }
-
+        } 
+        
         // Either no orderShipment exists, or there's a null invoicePerShipment in the OrderHeader.
         // In either case, use the default value from the properties
         if (invoicePerShipment == null) {
@@ -1263,11 +1272,11 @@ public class InvoiceServices {
         // The orders can now be placed in separate groups, each for
         // 1. The group of orders for which payment is already captured. No grouping and action required.
         // 2. The group of orders for which invoice is IN-Process status.
-        Map<String, GenericValue> ordersWithInProcessInvoice = new HashMap<>();
+        Map<String, GenericValue> ordersWithInProcessInvoice = new HashMap<String, GenericValue>();
 
         for (GenericValue itemIssuance : itemIssuances) {
             String orderId = itemIssuance.getString("orderId");
-            Map<String, Object> billFields = new HashMap<>();
+            Map<String, Object> billFields = new HashMap<String, Object>();
             billFields.put("orderId", orderId);
 
             GenericValue orderItemBilling = null;
@@ -1346,15 +1355,13 @@ public class InvoiceServices {
         List<String> shipmentIds = UtilGenerics.checkList(context.get("shipmentIds"));
         Locale locale = (Locale) context.get("locale");
         Boolean createSalesInvoicesForDropShipments = (Boolean) context.get("createSalesInvoicesForDropShipments");
-        if (UtilValidate.isEmpty(createSalesInvoicesForDropShipments)) {
-            createSalesInvoicesForDropShipments = Boolean.FALSE;
-        }
+        if (UtilValidate.isEmpty(createSalesInvoicesForDropShipments)) createSalesInvoicesForDropShipments = Boolean.FALSE;
 
         boolean salesShipmentFound = false;
         boolean purchaseShipmentFound = false;
         boolean dropShipmentFound = false;
 
-        List<String> invoicesCreated = new LinkedList<>();
+        List<String> invoicesCreated = new LinkedList<String>();
 
         //DEJ20060520: not used? planned to be used? List shipmentIdList = new LinkedList();
         for (String tmpShipmentId : shipmentIds) {
@@ -1429,7 +1436,7 @@ public class InvoiceServices {
         } catch (GenericEntityException e) {
             Debug.logError(e, "Problem getting issued items from shipments", module);
             return ServiceUtil.returnError(UtilProperties.getMessage(resource,
-                    "AccountingProblemGettingItemsFromShipments", locale));
+                    "AccountingProblemGettingItemsFromShipments", locale));    
         }
         if (items.size() == 0) {
             Debug.logInfo("No items issued for shipments", module);
@@ -1437,17 +1444,17 @@ public class InvoiceServices {
         }
 
         // group items by order
-        Map<String, List<GenericValue>> shippedOrderItems = new HashMap<>();
+        Map<String, List<GenericValue>> shippedOrderItems = new HashMap<String, List<GenericValue>>();
         for (GenericValue item : items) {
             String orderId = item.getString("orderId");
             String orderItemSeqId = item.getString("orderItemSeqId");
             List<GenericValue> itemsByOrder = shippedOrderItems.get(orderId);
             if (itemsByOrder == null) {
-                itemsByOrder = new LinkedList<>();
+                itemsByOrder = new LinkedList<GenericValue>();
             }
 
             // check and make sure we haven't already billed for this issuance or shipment receipt
-            List<EntityCondition> billFields = new LinkedList<>();
+            List<EntityCondition> billFields = new LinkedList<EntityCondition>();
             billFields.add(EntityCondition.makeCondition("orderId", orderId));
             billFields.add(EntityCondition.makeCondition("orderItemSeqId", orderItemSeqId));
             billFields.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "INVOICE_CANCELLED"));
@@ -1489,10 +1496,10 @@ public class InvoiceServices {
             List<GenericValue> billItems = order.getValue();
 
             // a new list to be used to pass to the create invoice service
-            List<GenericValue> toBillItems = new LinkedList<>();
+            List<GenericValue> toBillItems = new LinkedList<GenericValue>();
 
             // map of available quantities so we only have to calc once
-            Map<String, BigDecimal> itemQtyAvail = new HashMap<>();
+            Map<String, BigDecimal> itemQtyAvail = new HashMap<String, BigDecimal>();
 
             // now we will check each issuance and make sure it hasn't already been billed
             for (GenericValue issue : billItems) {
@@ -1506,7 +1513,7 @@ public class InvoiceServices {
 
                 BigDecimal billAvail = itemQtyAvail.get(issue.getString("orderItemSeqId"));
                 if (billAvail == null) {
-                    List<EntityCondition> lookup = new LinkedList<>();
+                    List<EntityCondition> lookup = new LinkedList<EntityCondition>();
                     lookup.add(EntityCondition.makeCondition("orderId", orderId));
                     lookup.add(EntityCondition.makeCondition("orderItemSeqId", issue.get("orderItemSeqId")));
                     lookup.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "INVOICE_CANCELLED"));
@@ -1632,13 +1639,11 @@ public class InvoiceServices {
                 }
 
                 // Total the additional shipping charges for the shipments
-                Map<GenericValue, BigDecimal> additionalShippingCharges = new HashMap<>();
+                Map<GenericValue, BigDecimal> additionalShippingCharges = new HashMap<GenericValue, BigDecimal>();
                 BigDecimal totalAdditionalShippingCharges = ZERO;
                 if (UtilValidate.isNotEmpty(invoiceableShipments)) {
                     for (GenericValue shipment : invoiceableShipments) {
-                        if (shipment.get("additionalShippingCharge") == null) {
-                            continue;
-                        }
+                        if (shipment.get("additionalShippingCharge") == null) continue;
                         BigDecimal shipmentAdditionalShippingCharges = shipment.getBigDecimal("additionalShippingCharge").setScale(DECIMALS, ROUNDING);
                         additionalShippingCharges.put(shipment, shipmentAdditionalShippingCharges);
                         totalAdditionalShippingCharges = totalAdditionalShippingCharges.add(shipmentAdditionalShippingCharges);
@@ -1653,7 +1658,7 @@ public class InvoiceServices {
                         GenericValue shipment = entry.getKey();
                         BigDecimal additionalShippingCharge = entry.getValue();
                         String shipmentId = shipment.getString("shipmentId");
-                        Map<String, Object> createOrderAdjustmentContext = new HashMap<>();
+                        Map<String, Object> createOrderAdjustmentContext = new HashMap<String, Object>();
                         createOrderAdjustmentContext.put("orderId", orderId);
                         createOrderAdjustmentContext.put("orderAdjustmentTypeId", "SHIPPING_CHARGES");
                         String addtlChargeDescription = shipment.getString("addtlShippingChargeDesc");
@@ -1689,8 +1694,8 @@ public class InvoiceServices {
                                     "AccountingTroubleCallingCreateInvoicesFromShipmentService", locale));
                         }
 
-                        List<Object> emptyList = new LinkedList<>();
-                        Map<String, Object> calcTaxContext = new HashMap<>();
+                        List<Object> emptyList = new LinkedList<Object>();
+                        Map<String, Object> calcTaxContext = new HashMap<String, Object>();
                         calcTaxContext.put("productStoreId", orh.getProductStoreId());
                         calcTaxContext.put("payToPartyId", payToParty.getString("partyId"));
                         calcTaxContext.put("billToPartyId", billToParty.getString("partyId"));
@@ -1884,8 +1889,9 @@ public class InvoiceServices {
         }
         if (itemMap != null) {
             return itemMap.getString("invoiceItemTypeId");
+        } else {
+            return defaultValue;
         }
-        return defaultValue;
     }
 
     public static Map<String, Object> createInvoicesFromReturnShipment(DispatchContext dctx, Map<String, Object> context) {
@@ -1899,7 +1905,7 @@ public class InvoiceServices {
         boolean salesReturnFound = false;
         boolean purchaseReturnFound = false;
 
-        List<String> invoicesCreated = new LinkedList<>();
+        List<String> invoicesCreated = new LinkedList<String>();
         try {
 
             // get the shipment and validate that it is a sales return
@@ -1914,7 +1920,7 @@ public class InvoiceServices {
                 purchaseReturnFound = true;
             }
             if (!(salesReturnFound || purchaseReturnFound)) {
-                 return ServiceUtil.returnError(errorMsg + UtilProperties.getMessage(resource,
+                 return ServiceUtil.returnError(errorMsg + UtilProperties.getMessage(resource, 
                          "AccountingShipmentNotSalesReturnAndPurchaseReturn", locale));
             }
             // get the items of the shipment. They can come from ItemIssuance if the shipment were from a purchase return, ShipmentReceipt if it were from a sales return
@@ -1930,7 +1936,7 @@ public class InvoiceServices {
             }
 
             // group the shipments by returnId (because we want a seperate itemized invoice for each return)
-            Map<String, List<GenericValue>> itemsShippedGroupedByReturn = new HashMap<>();
+            Map<String, List<GenericValue>> itemsShippedGroupedByReturn = new HashMap<String, List<GenericValue>>();
 
             for (GenericValue item : shippedItems) {
                 String returnId = null;
@@ -1957,14 +1963,12 @@ public class InvoiceServices {
                     billingCount = EntityQuery.use(delegator).from("ReturnItemBilling").where("returnId", returnId, "returnItemSeqId", returnItemSeqId).queryCount();
                 }
                 // if there are billings, we have already billed the item, so skip it
-                if (billingCount > 0) {
-                    continue;
-                }
+                if (billingCount > 0) continue;
 
                 // get the List of items shipped to/from this returnId
                 List<GenericValue> billItems = itemsShippedGroupedByReturn.get(returnId);
                 if (billItems == null) {
-                    billItems = new LinkedList<>();
+                    billItems = new LinkedList<GenericValue>();
                 }
 
                 // add our item to the group and put it back in the map
@@ -1976,8 +1980,9 @@ public class InvoiceServices {
             for (Map.Entry<String, List<GenericValue>> entry : itemsShippedGroupedByReturn.entrySet()) {
                 String returnId = entry.getKey();
                 List<GenericValue> billItems = entry.getValue();
-                Debug.logVerbose("Creating invoice for return [" + returnId + "] with items: " + billItems.toString(), module);
-
+                if (Debug.verboseOn()) {
+                    Debug.logVerbose("Creating invoice for return [" + returnId + "] with items: " + billItems.toString(), module);
+                }
                 Map<String, Object> input = UtilMisc.toMap("returnId", returnId, "billItems", billItems, "userLogin", context.get("userLogin"));
                 Map<String, Object> serviceResults = dispatcher.runSync("createInvoiceFromReturn", input);
                 if (ServiceUtil.isError(serviceResults)) {
@@ -1987,7 +1992,10 @@ public class InvoiceServices {
                 // put the resulting invoiceId in the return list
                 invoicesCreated.add((String) serviceResults.get("invoiceId"));
             }
-        } catch (GenericServiceException | GenericEntityException e) {
+        } catch (GenericServiceException e) {
+            Debug.logError(e, errorMsg + e.getMessage(), module);
+            return ServiceUtil.returnError(errorMsg + e.getMessage());
+        } catch (GenericEntityException e) {
             Debug.logError(e, errorMsg + e.getMessage(), module);
             return ServiceUtil.returnError(errorMsg + e.getMessage());
         }
@@ -2013,9 +2021,9 @@ public class InvoiceServices {
             // get the return header
             GenericValue returnHeader = EntityQuery.use(delegator).from("ReturnHeader").where("returnId", returnId).queryOne();
             if (returnHeader == null || returnHeader.get("returnHeaderTypeId") == null) {
-            	 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingReturnTypeCannotBeNull", locale));
+            	 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingReturnTypeCannotBeNull", locale));                                                      
             }
-
+            
             if (returnHeader.getString("returnHeaderTypeId").startsWith("CUSTOMER_")) {
                 invoiceTypeId = "CUST_RTN_INVOICE";
                 description = "Return Invoice for Customer Return #" + returnId;
@@ -2044,7 +2052,7 @@ public class InvoiceServices {
 
                             if (isNonPhysicalItemToReturn) {
                                 if (UtilValidate.isEmpty(billItems)) {
-                                    billItems = new ArrayList<>();
+                                    billItems = new ArrayList<GenericValue>();
                                 }
 
                                 billItems.add(returnItem);
@@ -2105,10 +2113,7 @@ public class InvoiceServices {
                     GenericValue returnItemShipment = EntityUtil.getFirst(shipmentItem.getRelated("ReturnItemShipment", null, null, false));
                     returnItem = returnItemShipment.getRelatedOne("ReturnItem", true);
                 }
-                if (returnItem == null)
-                 {
-                    continue; // Just to prevent NPE
-                }
+                if (returnItem == null) continue; // Just to prevent NPE
                 GenericValue product = returnItem.getRelatedOne("Product", true);
 
                 // extract the return price as a big decimal for convenience
@@ -2155,8 +2160,10 @@ public class InvoiceServices {
                 if (ServiceUtil.isError(serviceResults)) {
                     return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResults));
                 }
-                Debug.logVerbose("Creating Invoice Item with amount " + returnPrice + " and quantity " + quantity
+                if (Debug.verboseOn()) {
+                    Debug.logVerbose("Creating Invoice Item with amount " + returnPrice + " and quantity " + quantity
                             + " for shipment [" + item.getString("shipmentId") + ":" + item.getString("shipmentItemSeqId") + "]", module);
+                }
 
                 String parentInvoiceItemSeqId = invoiceItemSeqId;
                 // increment the seqId counter after creating the invoice item and return item billing
@@ -2170,9 +2177,7 @@ public class InvoiceServices {
                 } else if (itemIssuanceFound) {
                     cancelQuantity = item.getBigDecimal("cancelQuantity");
                 }
-                if (cancelQuantity == null) {
-                    cancelQuantity = ZERO;
-                }
+                if (cancelQuantity == null) cancelQuantity = ZERO;
                 BigDecimal actualAmount = returnPrice.multiply(quantity).setScale(DECIMALS, ROUNDING);
                 BigDecimal promisedAmount = returnPrice.multiply(quantity.add(cancelQuantity)).setScale(DECIMALS, ROUNDING);
                 invoiceTotal = invoiceTotal.add(actualAmount).setScale(DECIMALS, ROUNDING);
@@ -2190,7 +2195,7 @@ public class InvoiceServices {
                     // determine invoice item type from the return item type
                     invoiceItemTypeId = getInvoiceItemType(delegator, adjustment.getString("returnAdjustmentTypeId"), null, invoiceTypeId, null);
                     if (invoiceItemTypeId == null) {
-                        return ServiceUtil.returnError(errorMsg + UtilProperties.getMessage(resource,
+                        return ServiceUtil.returnError(errorMsg + UtilProperties.getMessage(resource, 
                                 "AccountingNoKnownInvoiceItemTypeReturnAdjustmentType",
                                 UtilMisc.toMap("returnAdjustmentTypeId", adjustment.getString("returnAdjustmentTypeId")), locale));
                     }
@@ -2199,8 +2204,10 @@ public class InvoiceServices {
                     BigDecimal ratio = quantity.divide(returnItem.getBigDecimal("returnQuantity"), 100, ROUNDING);
                     BigDecimal amount = adjustment.getBigDecimal("amount");
                     amount = amount.multiply(ratio).setScale(DECIMALS, ROUNDING);
-                    Debug.logVerbose("Creating Invoice Item with amount " + adjustment.getBigDecimal("amount") + " prorated to " + amount
+                    if (Debug.verboseOn()) {
+                        Debug.logVerbose("Creating Invoice Item with amount " + adjustment.getBigDecimal("amount") + " prorated to " + amount
                                 + " for return adjustment [" + adjustment.getString("returnAdjustmentId") + "]", module);
+                    }
 
                     // prepare invoice item data for this adjustment
                     input = UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemTypeId", invoiceItemTypeId, "quantity", BigDecimal.ONE);
@@ -2250,15 +2257,17 @@ public class InvoiceServices {
                 // determine invoice item type from the return item type
                 String invoiceItemTypeId = getInvoiceItemType(delegator, adjustment.getString("returnAdjustmentTypeId"), null, invoiceTypeId, null);
                 if (invoiceItemTypeId == null) {
-                    return ServiceUtil.returnError(errorMsg + UtilProperties.getMessage(resource,
+                    return ServiceUtil.returnError(errorMsg + UtilProperties.getMessage(resource, 
                             "AccountingNoKnownInvoiceItemTypeReturnAdjustmentType",
                             UtilMisc.toMap("returnAdjustmentTypeId", adjustment.getString("returnAdjustmentTypeId")), locale));
                 }
 
                 // prorate the adjustment amount by the actual to promised ratio
                 BigDecimal amount = adjustment.getBigDecimal("amount").multiply(actualToPromisedRatio).setScale(DECIMALS, ROUNDING);
-                Debug.logVerbose("Creating Invoice Item with amount " + adjustment.getBigDecimal("amount") + " prorated to " + amount
+                if (Debug.verboseOn()) {
+                    Debug.logVerbose("Creating Invoice Item with amount " + adjustment.getBigDecimal("amount") + " prorated to " + amount
                             + " for return adjustment [" + adjustment.getString("returnAdjustmentId") + "]", module);
+                }
 
                 // prepare the invoice item for the return-wide adjustment
                 input = UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemTypeId", invoiceItemTypeId, "quantity", BigDecimal.ONE);
@@ -2294,7 +2303,10 @@ public class InvoiceServices {
             results.put("invoiceId", invoiceId);
             }
             return results;
-        } catch (GenericServiceException | GenericEntityException e) {
+        } catch (GenericServiceException e) {
+            Debug.logError(e, errorMsg + e.getMessage(), module);
+            return ServiceUtil.returnError(errorMsg + e.getMessage());
+        } catch (GenericEntityException e) {
             Debug.logError(e, errorMsg + e.getMessage(), module);
             return ServiceUtil.returnError(errorMsg + e.getMessage());
         }
@@ -2344,12 +2356,12 @@ public class InvoiceServices {
             }
         } catch (GenericEntityException e) {
             Debug.logError(e, "Problem getting PaymentApplication(s) for Invoice ID " + invoiceId, module);
-            return ServiceUtil.returnError(UtilProperties.getMessage(resource,
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
                     "AccountingProblemGettingPaymentApplication",
                     UtilMisc.toMap("invoiceId", invoiceId), locale));
         }
 
-        Map<String, BigDecimal> payments = new HashMap<>();
+        Map<String, BigDecimal> payments = new HashMap<String, BigDecimal>();
         Timestamp paidDate = null;
         for (GenericValue payAppl : paymentAppl) {
             payments.put(payAppl.getString("paymentId"), payAppl.getBigDecimal("amountApplied"));
@@ -2365,17 +2377,16 @@ public class InvoiceServices {
 
         BigDecimal totalPayments = ZERO;
         for (BigDecimal amount : payments.values()) {
-            if (amount == null) {
-                amount = ZERO;
-            }
+            if (amount == null) amount = ZERO;
             totalPayments = totalPayments.add(amount).setScale(DECIMALS, ROUNDING);
         }
 
         if (totalPayments.signum() == 1) {
             BigDecimal invoiceTotal = InvoiceWorker.getInvoiceTotal(delegator, invoiceId);
-            Debug.logVerbose("Invoice #" + invoiceId + " total: " + invoiceTotal, module);
-            Debug.logVerbose("Total payments : " + totalPayments, module);
-
+            if (Debug.verboseOn()) {
+                Debug.logVerbose("Invoice #" + invoiceId + " total: " + invoiceTotal, module);
+                Debug.logVerbose("Total payments : " + totalPayments, module);
+            }
             if (totalPayments.compareTo(invoiceTotal) >= 0) { // this checks that totalPayments is greater than or equal to invoiceTotal
                 // this invoice is paid
                 Map<String, Object> svcCtx = UtilMisc.toMap("statusId", "INVOICE_PAID", "invoiceId", invoiceId,
@@ -2415,7 +2426,7 @@ public class InvoiceServices {
                 amount = baseAmount.multiply(multiplier).divide(divisor, decimals, rounding);
             }
             if (amount.signum() != 0) {
-                Map<String, Object> createInvoiceItemContext = new HashMap<>();
+                Map<String, Object> createInvoiceItemContext = new HashMap<String, Object>();
                 createInvoiceItemContext.put("invoiceId", invoiceId);
                 createInvoiceItemContext.put("invoiceItemSeqId", invoiceItemSeqId);
                 createInvoiceItemContext.put("invoiceItemTypeId", getInvoiceItemType(delegator, adj.getString("orderAdjustmentTypeId"), null, invoiceTypeId, "INVOICE_ADJ"));
@@ -2440,7 +2451,7 @@ public class InvoiceServices {
                 }
 
                 // Create the OrderAdjustmentBilling record
-                Map<String, Object> createOrderAdjustmentBillingContext = new HashMap<>();
+                Map<String, Object> createOrderAdjustmentBillingContext = new HashMap<String, Object>();
                 createOrderAdjustmentBillingContext.put("orderAdjustmentId", adj.getString("orderAdjustmentId"));
                 createOrderAdjustmentBillingContext.put("invoiceId", invoiceId);
                 createOrderAdjustmentBillingContext.put("invoiceItemSeqId", invoiceItemSeqId);
@@ -2470,7 +2481,7 @@ public class InvoiceServices {
                 amount = percent.multiply(divisor);
             }
             if (amount.signum() != 0) {
-                Map<String, Object> createInvoiceItemContext = new HashMap<>();
+                Map<String, Object> createInvoiceItemContext = new HashMap<String, Object>();
                 createInvoiceItemContext.put("invoiceId", invoiceId);
                 createInvoiceItemContext.put("invoiceItemSeqId", invoiceItemSeqId);
                 createInvoiceItemContext.put("invoiceItemTypeId", getInvoiceItemType(delegator, adj.getString("orderAdjustmentTypeId"), null, invoiceTypeId, "INVOICE_ADJ"));
@@ -2495,7 +2506,7 @@ public class InvoiceServices {
                 }
 
                 // Create the OrderAdjustmentBilling record
-                Map<String, Object> createOrderAdjustmentBillingContext = new HashMap<>();
+                Map<String, Object> createOrderAdjustmentBillingContext = new HashMap<String, Object>();
                 createOrderAdjustmentBillingContext.put("orderAdjustmentId", adj.getString("orderAdjustmentId"));
                 createOrderAdjustmentBillingContext.put("invoiceId", invoiceId);
                 createOrderAdjustmentBillingContext.put("invoiceItemSeqId", invoiceItemSeqId);
@@ -2527,7 +2538,7 @@ public class InvoiceServices {
         if (terms != null) {
             for (GenericValue term : terms) {
 
-                Map<String, Object> createInvoiceTermContext = new HashMap<>();
+                Map<String, Object> createInvoiceTermContext = new HashMap<String, Object>();
                 createInvoiceTermContext.put("invoiceId", invoiceId);
                 createInvoiceTermContext.put("invoiceItemSeqId", "_NA_");
                 createInvoiceTermContext.put("termTypeId", term.get("termTypeId"));
@@ -2603,6 +2614,8 @@ public class InvoiceServices {
 
         String defaultInvoiceProcessing = EntityUtilProperties.getPropertyValue("accounting","invoiceProcessing", delegator);
 
+        boolean debug = true; // show processing messages in the log..or not....
+
         // a 'y' in invoiceProssesing will reverse the default processing
         String changeProcessing = (String) context.get("invoiceProcessing");
         String invoiceId = (String) context.get("invoiceId");
@@ -2615,9 +2628,9 @@ public class InvoiceServices {
         String taxAuthGeoId = (String) context.get("taxAuthGeoId");
         String useHighestAmount = (String) context.get("useHighestAmount");
 
-        List<String> errorMessageList = new LinkedList<>();
+        List<String> errorMessageList = new LinkedList<String>();
 
-        Debug.logInfo("updatePaymentApplicationDefBd input parameters..." +
+        if (debug) Debug.logInfo("updatePaymentApplicationDefBd input parameters..." +
                 " defaultInvoiceProcessing: " + defaultInvoiceProcessing +
                 " changeDefaultInvoiceProcessing: " + changeProcessing +
                 " useHighestAmount: " + useHighestAmount +
@@ -2650,34 +2663,20 @@ public class InvoiceServices {
         // Billing Account to pay for an Invoice
         if (paymentApplicationId == null) {
             int count = 0;
-            if (invoiceId != null) {
-                count++;
-            }
-            if (toPaymentId != null) {
-                count++;
-            }
-            if (billingAccountId != null) {
-                count++;
-            }
-            if (taxAuthGeoId != null) {
-                count++;
-            }
-            if ((billingAccountId != null) && (invoiceId != null)) {
-                count--;
-            }
+            if (invoiceId != null) count++;
+            if (toPaymentId != null) count++;
+            if (billingAccountId != null) count++;
+            if (taxAuthGeoId != null) count++;
+            if ((billingAccountId != null) && (invoiceId != null)) count--;
             if (count != 1) {
                 errorMessageList.add(UtilProperties.getMessage(resource, "AccountingSpecifyInvoiceToPaymentBillingAccountTaxGeoId", locale));
             }
         }
 
         // avoid null pointer exceptions.
-        if (amountApplied == null) {
-            amountApplied = ZERO;
-        }
+        if (amountApplied == null) amountApplied = ZERO;
         // makes no sense to have an item numer without an invoice number
-        if (invoiceId == null) {
-            invoiceItemSeqId = null;
-        }
+        if (invoiceId == null) invoiceItemSeqId = null;
 
         // retrieve all information and perform checking on the retrieved info.....
 
@@ -2760,11 +2759,11 @@ public class InvoiceServices {
             // check if at least one send is the same as one receiver on the other payment
             if (!payment.getString("partyIdFrom").equals(toPayment.getString("partyIdTo")) &&
                     !payment.getString("partyIdTo").equals(toPayment.getString("partyIdFrom")))    {
-                errorMessageList.add(UtilProperties.getMessage(resource,
+                errorMessageList.add(UtilProperties.getMessage(resource, 
                         "AccountingFromPartySameToParty", locale));
             }
 
-            Debug.logInfo("toPayment info retrieved and checked...", module);
+            if (debug) Debug.logInfo("toPayment info retrieved and checked...", module);
         }
 
         // assign payment to billing account if the invoice is assigned to this billing account
@@ -2777,7 +2776,7 @@ public class InvoiceServices {
             }
 
             if (invoice == null) {
-                errorMessageList.add(UtilProperties.getMessage(resource,
+                errorMessageList.add(UtilProperties.getMessage(resource, 
                         "AccountingInvoiceNotFound", UtilMisc.toMap("invoiceId", invoiceId), locale));
             } else {
                 if (invoice.getString("billingAccountId") != null) {
@@ -2795,7 +2794,7 @@ public class InvoiceServices {
                 return ServiceUtil.returnError(e.getMessage());
             }
             if (billingAccount == null) {
-                errorMessageList.add(UtilProperties.getMessage(resource,
+                errorMessageList.add(UtilProperties.getMessage(resource, 
                         "AccountingBillingAccountNotFound", UtilMisc.toMap("billingAccountId", billingAccountId), locale));
                 return ServiceUtil.returnError(errorMessageList);
             }
@@ -2808,7 +2807,7 @@ public class InvoiceServices {
                                 "paymentId", paymentId, "paymentCurrencyUomId", currencyUomId), locale));
             }
 
-            Debug.logInfo("Billing Account info retrieved and checked...", module);
+            if (debug) Debug.logInfo("Billing Account info retrieved and checked...", module);
         }
 
         // get the invoice (item) information
@@ -2826,7 +2825,7 @@ public class InvoiceServices {
             }
 
             if (invoice == null) {
-                errorMessageList.add(UtilProperties.getMessage(resource,
+                errorMessageList.add(UtilProperties.getMessage(resource, 
                         "AccountingInvoiceNotFound", UtilMisc.toMap("invoiceId", invoiceId), locale));
             } else { // check the invoice and when supplied the invoice item...
 
@@ -2872,7 +2871,7 @@ public class InvoiceServices {
                         errorMessageList.add(UtilProperties.getMessage(resource, "AccountingInvoiceLessRequested",
                                 UtilMisc.<String, Object>toMap("invoiceId", invoiceId,
                                             "invoiceApplyAvailable", invoiceApplyAvailable,
-                                            "amountApplied", amountApplied,
+                                            "amountApplied", amountApplied, 
                                             "isoCode", invoice.getString("currencyUomId")), locale));
                     }
                 }
@@ -2884,7 +2883,7 @@ public class InvoiceServices {
                             "AccountingFromPartySameToParty", locale));
                 }
 
-                Debug.logInfo("Invoice info retrieved and checked ...", module);
+                if (debug) Debug.logInfo("Invoice info retrieved and checked ...", module);
             }
 
             // if provided check the invoice item.
@@ -2898,13 +2897,13 @@ public class InvoiceServices {
 
                 if (invoiceItem == null) {
                     errorMessageList.add(UtilProperties.getMessage(resource,
-                            "AccountingInvoiceItemNotFound",
+                            "AccountingInvoiceItemNotFound", 
                             UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", invoiceItemSeqId), locale));
                 } else {
                     if (invoice.get("currencyUomId") != null && currencyUomId != null && !invoice.getString("currencyUomId").equals(currencyUomId)) {
                         errorMessageList.add(UtilProperties.getMessage(resource,
                                 "AccountingInvoicePaymentCurrencyProblem",
-                                UtilMisc.toMap("paymentCurrencyId", currencyUomId,
+                                UtilMisc.toMap("paymentCurrencyId", currencyUomId, 
                                         "itemCurrency", invoice.getString("currencyUomId")), locale));
                     }
 
@@ -2927,7 +2926,7 @@ public class InvoiceServices {
                                             "amountApplied", amountApplied, "isoCode", uomId), locale));
                     }
                 }
-                Debug.logInfo("InvoiceItem info retrieved and checked against the Invoice (currency and amounts) ...", module);
+                if (debug) Debug.logInfo("InvoiceItem info retrieved and checked against the Invoice (currency and amounts) ...", module);
             }
         }
 
@@ -2935,7 +2934,7 @@ public class InvoiceServices {
         if (paymentApplicationId == null) {
             // only check for new application records, update on existing records is checked in the paymentApplication section
             if (paymentApplyAvailable.signum() == 0) {
-                errorMessageList.add(UtilProperties.getMessage(resource,
+                errorMessageList.add(UtilProperties.getMessage(resource, 
                         "AccountingPaymentAlreadyApplied", UtilMisc.toMap("paymentId", paymentId), locale));
             } else {
                 // check here for too much application if a new record is
@@ -2970,8 +2969,8 @@ public class InvoiceServices {
             }
 
             if (paymentApplication == null) {
-                errorMessageList.add(UtilProperties.getMessage(resource,
-                        "AccountingPaymentApplicationNotFound",
+                errorMessageList.add(UtilProperties.getMessage(resource, 
+                        "AccountingPaymentApplicationNotFound", 
                         UtilMisc.toMap("paymentApplicationId", paymentApplicationId), locale));
                 paymentApplicationId = null;
             } else {
@@ -3008,8 +3007,8 @@ public class InvoiceServices {
                     newPaymentApplyAvailable = paymentApplyAvailable.add(paymentApplyAvailable).subtract(amountApplied).setScale(DECIMALS, ROUNDING);
                 }
                 if (newPaymentApplyAvailable.compareTo(ZERO) < 0) {
-                    errorMessageList.add(UtilProperties.getMessage(resource,
-                            "AccountingPaymentNotEnough",
+                    errorMessageList.add(UtilProperties.getMessage(resource, 
+                            "AccountingPaymentNotEnough", 
                             UtilMisc.<String, Object>toMap("paymentId", paymentId,
                                     "paymentApplyAvailable", paymentApplyAvailable.add(paymentApplication.getBigDecimal("amountApplied")),
                                     "amountApplied", amountApplied), locale));
@@ -3024,7 +3023,7 @@ public class InvoiceServices {
                         if (invoiceItemSeqId == null && paymentApplication.get("invoiceItemSeqId") == null) {
                             newInvoiceApplyAvailable = invoiceApplyAvailable.add(paymentApplication.getBigDecimal("amountApplied")).subtract(amountApplied).setScale(DECIMALS, ROUNDING);
                             if (invoiceApplyAvailable.compareTo(ZERO) < 0) {
-                                errorMessageList.add(UtilProperties.getMessage(resource,
+                                errorMessageList.add(UtilProperties.getMessage(resource, 
                                         "AccountingInvoiceNotEnough",
                                         UtilMisc.<String, Object>toMap("tooMuch", newInvoiceApplyAvailable.negate(),
                                                 "invoiceId", invoiceId), locale));
@@ -3034,7 +3033,7 @@ public class InvoiceServices {
                             newInvoiceApplyAvailable = invoiceApplyAvailable.add(paymentApplication.getBigDecimal("amountApplied")).subtract(amountApplied).setScale(DECIMALS, ROUNDING);
                             if (invoiceApplyAvailable.compareTo(ZERO) < 0) {
                                 errorMessageList.add(UtilProperties.getMessage(resource,
-                                        "AccountingInvoiceNotEnough",
+                                        "AccountingInvoiceNotEnough", 
                                         UtilMisc.<String, Object>toMap("tooMuch", newInvoiceApplyAvailable.negate(),
                                                 "invoiceId", invoiceId), locale));
                             }
@@ -3043,7 +3042,7 @@ public class InvoiceServices {
                             // a real Item number
                             newInvoiceItemApplyAvailable = invoiceItemApplyAvailable.subtract(amountApplied).setScale(DECIMALS, ROUNDING);
                             if (newInvoiceItemApplyAvailable.compareTo(ZERO) < 0) {
-                                errorMessageList.add(UtilProperties.getMessage(resource,
+                                errorMessageList.add(UtilProperties.getMessage(resource, 
                                         "AccountingItemInvoiceNotEnough",
                                         UtilMisc.<String, Object>toMap("tooMuch", newInvoiceItemApplyAvailable.negate(),
                                                 "invoiceId", invoiceId,
@@ -3054,7 +3053,7 @@ public class InvoiceServices {
                             // item number the same numeric value
                             newInvoiceItemApplyAvailable = invoiceItemApplyAvailable.add(paymentApplication.getBigDecimal("amountApplied")).subtract(amountApplied).setScale(DECIMALS, ROUNDING);
                             if (newInvoiceItemApplyAvailable.compareTo(ZERO) < 0) {
-                                errorMessageList.add(UtilProperties.getMessage(resource,
+                                errorMessageList.add(UtilProperties.getMessage(resource, 
                                         "AccountingItemInvoiceNotEnough",
                                         UtilMisc.<String, Object>toMap("tooMuch", newInvoiceItemApplyAvailable.negate(),
                                                 "invoiceId", invoiceId,
@@ -3100,8 +3099,8 @@ public class InvoiceServices {
                 if (toPaymentId != null && toPaymentId.equals(paymentApplication.getString("toPaymentId"))) {
                     newToPaymentApplyAvailable = toPaymentApplyAvailable.subtract(paymentApplication.getBigDecimal("amountApplied")).add(amountApplied).setScale(DECIMALS, ROUNDING);
                     if (newToPaymentApplyAvailable.compareTo(ZERO) < 0) {
-                        errorMessageList.add(UtilProperties.getMessage(resource,
-                                "AccountingPaymentNotEnough",
+                        errorMessageList.add(UtilProperties.getMessage(resource, 
+                                "AccountingPaymentNotEnough", 
                                 UtilMisc.<String, Object>toMap("paymentId", toPaymentId,
                                         "paymentApplyAvailable", newToPaymentApplyAvailable,
                                         "amountApplied", amountApplied), locale));
@@ -3111,8 +3110,8 @@ public class InvoiceServices {
                     // check the new billing account number.
                     newToPaymentApplyAvailable = toPaymentApplyAvailable.add(amountApplied).setScale(DECIMALS, ROUNDING);
                     if (newToPaymentApplyAvailable.compareTo(ZERO) < 0) {
-                        errorMessageList.add(UtilProperties.getMessage(resource,
-                                "AccountingPaymentNotEnough",
+                        errorMessageList.add(UtilProperties.getMessage(resource, 
+                                "AccountingPaymentNotEnough", 
                                 UtilMisc.<String, Object>toMap("paymentId", toPaymentId,
                                         "paymentApplyAvailable", newToPaymentApplyAvailable,
                                         "amountApplied", amountApplied), locale));
@@ -3120,53 +3119,51 @@ public class InvoiceServices {
 
                 }
             }
-            Debug.logInfo("paymentApplication record info retrieved and checked...", module);
+            if (debug) Debug.logInfo("paymentApplication record info retrieved and checked...", module);
         }
 
         // show the maximumus what can be added in the payment application file.
         String toMessage = null;  // prepare for success message
-
-        String extra = "";
-        if (invoiceItemSeqId != null) {
-            extra = " Invoice item(" + invoiceItemSeqId + ") amount not yet applied: " + newInvoiceItemApplyAvailable;
-        }
-        Debug.logInfo("checking finished, start processing with the following data... ", module);
-        if (invoiceId != null) {
-            Debug.logInfo(" Invoice(" + invoiceId + ") amount not yet applied: " + newInvoiceApplyAvailable + extra + " Payment(" + paymentId + ") amount not yet applied: " + newPaymentApplyAvailable +  " Requested amount to apply:" + amountApplied, module);
-            toMessage = UtilProperties.getMessage(resource,
-                    "AccountingApplicationToInvoice",
-                    UtilMisc.toMap("invoiceId", invoiceId), locale);
-            if (extra.length() > 0) {
-                toMessage = UtilProperties.getMessage(resource,
+        if (debug) {
+            String extra = "";
+            if (invoiceItemSeqId != null) {
+                extra = " Invoice item(" + invoiceItemSeqId + ") amount not yet applied: " + newInvoiceItemApplyAvailable;
+            }
+            Debug.logInfo("checking finished, start processing with the following data... ", module);
+            if (invoiceId != null) {
+                Debug.logInfo(" Invoice(" + invoiceId + ") amount not yet applied: " + newInvoiceApplyAvailable + extra + " Payment(" + paymentId + ") amount not yet applied: " + newPaymentApplyAvailable +  " Requested amount to apply:" + amountApplied, module);
+                toMessage = UtilProperties.getMessage(resource, 
+                        "AccountingApplicationToInvoice",
+                        UtilMisc.toMap("invoiceId", invoiceId), locale);
+                if (extra.length() > 0) toMessage = UtilProperties.getMessage(resource,
                         "AccountingApplicationToInvoiceItem",
                         UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", invoiceItemSeqId), locale);
             }
+            if (toPaymentId != null) {
+                Debug.logInfo(" toPayment(" + toPaymentId + ") amount not yet applied: " + newToPaymentApplyAvailable + " Payment(" + paymentId + ") amount not yet applied: " + newPaymentApplyAvailable + " Requested amount to apply:" + amountApplied, module);
+                toMessage = UtilProperties.getMessage(resource, 
+                        "AccountingApplicationToPayment",
+                        UtilMisc.toMap("paymentId", toPaymentId), locale);
+            }
+            if (taxAuthGeoId != null) {
+                Debug.logInfo(" taxAuthGeoId(" + taxAuthGeoId + ")  Payment(" + paymentId + ") amount not yet applied: " + newPaymentApplyAvailable + " Requested amount to apply:" + amountApplied, module);
+                toMessage = UtilProperties.getMessage(resource, 
+                        "AccountingApplicationToTax",
+                        UtilMisc.toMap("taxAuthGeoId", taxAuthGeoId), locale);
+            }
         }
-        if (toPaymentId != null) {
-            Debug.logInfo(" toPayment(" + toPaymentId + ") amount not yet applied: " + newToPaymentApplyAvailable + " Payment(" + paymentId + ") amount not yet applied: " + newPaymentApplyAvailable + " Requested amount to apply:" + amountApplied, module);
-            toMessage = UtilProperties.getMessage(resource,
-                    "AccountingApplicationToPayment",
-                    UtilMisc.toMap("paymentId", toPaymentId), locale);
-        }
-        if (taxAuthGeoId != null) {
-            Debug.logInfo(" taxAuthGeoId(" + taxAuthGeoId + ")  Payment(" + paymentId + ") amount not yet applied: " + newPaymentApplyAvailable + " Requested amount to apply:" + amountApplied, module);
-            toMessage = UtilProperties.getMessage(resource,
-                    "AccountingApplicationToTax",
-                    UtilMisc.toMap("taxAuthGeoId", taxAuthGeoId), locale);
-        }
-
         // if the amount to apply was not provided or was zero fill it with the maximum possible and provide information to the user
         if (amountApplied.signum() == 0 &&  "Y".equals(useHighestAmount)) {
             amountApplied = newPaymentApplyAvailable;
             if (invoiceId != null && newInvoiceApplyAvailable.compareTo(amountApplied) < 0) {
                 amountApplied = newInvoiceApplyAvailable;
-                toMessage = UtilProperties.getMessage(resource,
+                toMessage = UtilProperties.getMessage(resource, 
                         "AccountingApplicationToInvoice",
                         UtilMisc.toMap("invoiceId", invoiceId), locale);
             }
             if (toPaymentId != null && newToPaymentApplyAvailable.compareTo(amountApplied) < 0) {
                 amountApplied = newToPaymentApplyAvailable;
-                toMessage = UtilProperties.getMessage(resource,
+                toMessage = UtilProperties.getMessage(resource, 
                         "AccountingApplicationToPayment",
                         UtilMisc.toMap("paymentId", toPaymentId), locale);
             }
@@ -3180,7 +3177,7 @@ public class InvoiceServices {
                     "AccountingApplicationSuccess",
                     UtilMisc.<String, Object>toMap("amountApplied", amountApplied,
                             "paymentId", paymentId,
-                            "isoCode", currencyUomId,
+                            "isoCode", currencyUomId, 
                             "toMessage", toMessage), locale);
         }
         // report error messages if any
@@ -3192,7 +3189,7 @@ public class InvoiceServices {
            // if the application is specified it is easy, update the existing record only
         if (paymentApplicationId != null) {
             // record is already retrieved previously
-            Debug.logInfo("Process an existing paymentApplication record: " + paymentApplicationId, module);
+            if (debug) Debug.logInfo("Process an existing paymentApplication record: " + paymentApplicationId, module);
             // update the current record
             paymentApplication.set("invoiceId", invoiceId);
             paymentApplication.set("invoiceItemSeqId", invoiceItemSeqId);
@@ -3210,8 +3207,7 @@ public class InvoiceServices {
         if (invoiceId != null && paymentId != null && (invoiceItemSeqId == null)) {
             if (invoiceProcessing) {
                 // create only a single record with a null seqId
-                Debug.logInfo("Try to allocate the payment to the invoice as a whole", module);
-
+                if (debug) Debug.logInfo("Try to allocate the payment to the invoice as a whole", module);
                 paymentApplication.set("paymentId", paymentId);
                 paymentApplication.set("toPaymentId",null);
                 paymentApplication.set("invoiceId", invoiceId);
@@ -3220,103 +3216,105 @@ public class InvoiceServices {
                 paymentApplication.set("amountApplied", amountApplied);
                 paymentApplication.set("billingAccountId", billingAccountId);
                 paymentApplication.set("taxAuthGeoId", null);
-
-                Debug.logInfo("creating new paymentapplication", module);
+                if (debug) Debug.logInfo("creating new paymentapplication", module);
                 return storePaymentApplication(delegator, paymentApplication,locale);
-            }
-            Debug.logInfo("Try to allocate the payment to the itemnumbers of the invoice", module);
-
-            // get the invoice items
-            List<GenericValue> invoiceItems = null;
-            try {
-                invoiceItems = EntityQuery.use(delegator).from("InvoiceItem").where("invoiceId", invoiceId).queryList();
-            } catch (GenericEntityException e) {
-                return ServiceUtil.returnError(e.getMessage());
-            }
-            if (invoiceItems.size() == 0) {
-                errorMessageList.add(UtilProperties.getMessage(resource, "AccountingNoInvoiceItemsFoundForInvoice", UtilMisc.toMap("invoiceId", invoiceId), locale));
-                return ServiceUtil.returnError(errorMessageList);
-            }
-            // check if the user want to apply a smaller amount than the maximum possible on the payment
-            if (amountApplied.signum() != 0 && amountApplied.compareTo(paymentApplyAvailable) < 0)    {
-                paymentApplyAvailable = amountApplied;
-            }
-            for (GenericValue currentInvoiceItem : invoiceItems) {
-                if (paymentApplyAvailable.compareTo(ZERO) > 0) {
-                    break;
-                }
-                Debug.logInfo("Start processing item: " + currentInvoiceItem.getString("invoiceItemSeqId"), module);
-
-                BigDecimal itemQuantity = BigDecimal.ONE;
-                if (currentInvoiceItem.get("quantity") != null && currentInvoiceItem.getBigDecimal("quantity").signum() != 0) {
-                    itemQuantity = new BigDecimal(currentInvoiceItem.getString("quantity")).setScale(DECIMALS,ROUNDING);
-                }
-                BigDecimal itemAmount = currentInvoiceItem.getBigDecimal("amount").setScale(DECIMALS,ROUNDING);
-                BigDecimal itemTotal = itemAmount.multiply(itemQuantity).setScale(DECIMALS,ROUNDING);
-
-                // get the application(s) already allocated to this
-                // item, if available
-                List<GenericValue> paymentApplications = null;
+            } else { // spread the amount over every single item number
+                if (debug) Debug.logInfo("Try to allocate the payment to the itemnumbers of the invoice", module);
+                // get the invoice items
+                List<GenericValue> invoiceItems = null;
                 try {
-                    paymentApplications = currentInvoiceItem.getRelated("PaymentApplication", null, null, false);
+                    invoiceItems = EntityQuery.use(delegator).from("InvoiceItem").where("invoiceId", invoiceId).queryList();
                 } catch (GenericEntityException e) {
                     return ServiceUtil.returnError(e.getMessage());
                 }
-                BigDecimal tobeApplied = ZERO;
-                // item total amount - already applied (if any)
-                BigDecimal alreadyApplied = ZERO;
-                if (UtilValidate.isNotEmpty(paymentApplications)) {
-                    // application(s) found, add them all together
-                    Iterator<GenericValue> p = paymentApplications.iterator();
-                    while (p.hasNext()) {
-                        paymentApplication = p.next();
-                        alreadyApplied = alreadyApplied.add(paymentApplication.getBigDecimal("amountApplied").setScale(DECIMALS,ROUNDING));
-                    }
-                    tobeApplied = itemTotal.subtract(alreadyApplied).setScale(DECIMALS,ROUNDING);
-                } else {
-                    // no application connected yet
-                    tobeApplied = itemTotal;
-                }
-                Debug.logInfo("tobeApplied:(" + tobeApplied + ") = " + "itemTotal(" + itemTotal + ") - alreadyApplied(" + alreadyApplied + ") but not more then (nonapplied) paymentAmount(" + paymentApplyAvailable + ")", module);
+                if (invoiceItems.size() == 0) {
+                    errorMessageList.add(UtilProperties.getMessage(resource, "AccountingNoInvoiceItemsFoundForInvoice", UtilMisc.toMap("invoiceId", invoiceId), locale));
+                    return ServiceUtil.returnError(errorMessageList);
+                } else { // we found some invoice items, start processing....
+                    // check if the user want to apply a smaller amount than the maximum possible on the payment
+                    if (amountApplied.signum() != 0 && amountApplied.compareTo(paymentApplyAvailable) < 0)    {
+                        paymentApplyAvailable = amountApplied;
+                    }
+                    for (GenericValue currentInvoiceItem : invoiceItems) {
+                        if (paymentApplyAvailable.compareTo(ZERO) > 0) {
+                            break;
+                        }
+                        if (debug) Debug.logInfo("Start processing item: " + currentInvoiceItem.getString("invoiceItemSeqId"), module);
+                        BigDecimal itemQuantity = BigDecimal.ONE;
+                        if (currentInvoiceItem.get("quantity") != null && currentInvoiceItem.getBigDecimal("quantity").signum() != 0) {
+                            itemQuantity = new BigDecimal(currentInvoiceItem.getString("quantity")).setScale(DECIMALS,ROUNDING);
+                        }
+                        BigDecimal itemAmount = currentInvoiceItem.getBigDecimal("amount").setScale(DECIMALS,ROUNDING);
+                        BigDecimal itemTotal = itemAmount.multiply(itemQuantity).setScale(DECIMALS,ROUNDING);
+
+                        // get the application(s) already allocated to this
+                        // item, if available
+                        List<GenericValue> paymentApplications = null;
+                        try {
+                            paymentApplications = currentInvoiceItem.getRelated("PaymentApplication", null, null, false);
+                        } catch (GenericEntityException e) {
+                            return ServiceUtil.returnError(e.getMessage());
+                        }
+                        BigDecimal tobeApplied = ZERO;
+                        // item total amount - already applied (if any)
+                        BigDecimal alreadyApplied = ZERO;
+                        if (UtilValidate.isNotEmpty(paymentApplications)) {
+                            // application(s) found, add them all together
+                            Iterator<GenericValue> p = paymentApplications.iterator();
+                            while (p.hasNext()) {
+                                paymentApplication = p.next();
+                                alreadyApplied = alreadyApplied.add(paymentApplication.getBigDecimal("amountApplied").setScale(DECIMALS,ROUNDING));
+                            }
+                            tobeApplied = itemTotal.subtract(alreadyApplied).setScale(DECIMALS,ROUNDING);
+                        } else {
+                            // no application connected yet
+                            tobeApplied = itemTotal;
+                        }
+                        if (debug) Debug.logInfo("tobeApplied:(" + tobeApplied + ") = " + "itemTotal(" + itemTotal + ") - alreadyApplied(" + alreadyApplied + ") but not more then (nonapplied) paymentAmount(" + paymentApplyAvailable + ")", module);
 
-                if (tobeApplied.signum() == 0) {
-                    // invoiceItem already fully applied so look at the next one....
-                    continue;
-                }
+                        if (tobeApplied.signum() == 0) {
+                            // invoiceItem already fully applied so look at the next one....
+                            continue;
+                        }
 
-                if (paymentApplyAvailable.compareTo(tobeApplied) > 0) {
-                    paymentApplyAvailable = paymentApplyAvailable.subtract(tobeApplied);
-                } else {
-                    tobeApplied = paymentApplyAvailable;
-                    paymentApplyAvailable = ZERO;
-                }
+                        if (paymentApplyAvailable.compareTo(tobeApplied) > 0) {
+                            paymentApplyAvailable = paymentApplyAvailable.subtract(tobeApplied);
+                        } else {
+                            tobeApplied = paymentApplyAvailable;
+                            paymentApplyAvailable = ZERO;
+                        }
 
-                // create application payment record but check currency
-                // first if supplied
-                if (invoice.get("currencyUomId") != null && currencyUomId != null && !invoice.getString("currencyUomId").equals(currencyUomId)) {
-                    errorMessageList.add("Payment currency (" + currencyUomId + ") and invoice currency(" + invoice.getString("currencyUomId") + ") not the same\n");
-                } else {
-                    paymentApplication.set("paymentApplicationId", null);
-                    // make sure we get a new record
-                    paymentApplication.set("invoiceId", invoiceId);
-                    paymentApplication.set("invoiceItemSeqId", currentInvoiceItem.getString("invoiceItemSeqId"));
-                    paymentApplication.set("paymentId", paymentId);
-                    paymentApplication.set("toPaymentId", toPaymentId);
-                    paymentApplication.set("amountApplied", tobeApplied);
-                    paymentApplication.set("billingAccountId", billingAccountId);
-                    paymentApplication.set("taxAuthGeoId", taxAuthGeoId);
-                    storePaymentApplication(delegator, paymentApplication,locale);
-                }
+                        // create application payment record but check currency
+                        // first if supplied
+                        if (invoice.get("currencyUomId") != null && currencyUomId != null && !invoice.getString("currencyUomId").equals(currencyUomId)) {
+                            errorMessageList.add("Payment currency (" + currencyUomId + ") and invoice currency(" + invoice.getString("currencyUomId") + ") not the same\n");
+                        } else {
+                            paymentApplication.set("paymentApplicationId", null);
+                            // make sure we get a new record
+                            paymentApplication.set("invoiceId", invoiceId);
+                            paymentApplication.set("invoiceItemSeqId", currentInvoiceItem.getString("invoiceItemSeqId"));
+                            paymentApplication.set("paymentId", paymentId);
+                            paymentApplication.set("toPaymentId", toPaymentId);
+                            paymentApplication.set("amountApplied", tobeApplied);
+                            paymentApplication.set("billingAccountId", billingAccountId);
+                            paymentApplication.set("taxAuthGeoId", taxAuthGeoId);
+                            storePaymentApplication(delegator, paymentApplication,locale);
+                        }
 
-            }
+                    }
 
-            if (errorMessageList.size() > 0) {
-                return ServiceUtil.returnError(errorMessageList);
-            }
-            if (successMessage != null) {
-                return ServiceUtil.returnSuccess(successMessage);
+                    if (errorMessageList.size() > 0) {
+                        return ServiceUtil.returnError(errorMessageList);
+                    } else {
+                        if (successMessage != null) {
+                            return ServiceUtil.returnSuccess(successMessage);
+                        }
+                        else {
+                            return ServiceUtil.returnSuccess();
+                        }
+                    }
+                }
             }
-            return ServiceUtil.returnSuccess();
         }
 
         // if no paymentApplicationId supplied create a new record with the data
@@ -3345,7 +3343,7 @@ public class InvoiceServices {
             invoicedAdjustments = EntityQuery.use(delegator).from("OrderAdjustmentBilling").where("orderAdjustmentId", orderAdjustment.get("orderAdjustmentId")).queryList();
         } catch (GenericEntityException e) {
             Debug.logError(e, "Accounting trouble calling calculateInvoicedAdjustmentTotal service", module);
-            return ServiceUtil.returnError(UtilProperties.getMessage(resource,
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
                     "AccountingTroubleCallingCalculateInvoicedAdjustmentTotalService" + ": " + e.getMessage(), locale));
         }
         for (GenericValue invoicedAdjustment : invoicedAdjustments) {
@@ -3364,13 +3362,13 @@ public class InvoiceServices {
      * @return map results
      */
     private static Map<String, Object> storePaymentApplication(Delegator delegator, GenericValue paymentApplication,Locale locale) {
-        Map<String, Object> results = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource,
+        Map<String, Object> results = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, 
                 "AccountingSuccessful", locale));
-
-        Debug.logInfo("Start updating the paymentApplication table ", module);
+        boolean debug = true;
+        if (debug) Debug.logInfo("Start updating the paymentApplication table ", module);
 
         if (DECIMALS == -1 || ROUNDING == -1) {
-            return ServiceUtil.returnError(UtilProperties.getMessage(resource,
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
                     "AccountingAritmeticPropertiesNotConfigured", locale));
         }
 
@@ -3389,8 +3387,7 @@ public class InvoiceServices {
             return ServiceUtil.returnError(e.getMessage());
         }
         if (checkAppls.size() > 0) {
-            Debug.logInfo(checkAppls.size() + " records already exist", module);
-
+            if (debug) Debug.logInfo(checkAppls.size() + " records already exist", module);
             // 1 record exists just update and if different ID delete other record and add together.
             GenericValue checkAppl = checkAppls.get(0);
             // if new record  add to the already existing one.
@@ -3398,7 +3395,7 @@ public class InvoiceServices {
                 // add 2 amounts together
                 checkAppl.set("amountApplied", paymentApplication.getBigDecimal("amountApplied").
                         add(checkAppl.getBigDecimal("amountApplied")).setScale(DECIMALS,ROUNDING));
-                Debug.logInfo("Update paymentApplication record: " + checkAppl.getString("paymentApplicationId") + " with appliedAmount:" + checkAppl.getBigDecimal("amountApplied"), module);
+                if (debug)     Debug.logInfo("Update paymentApplication record: " + checkAppl.getString("paymentApplicationId") + " with appliedAmount:" + checkAppl.getBigDecimal("amountApplied"), module);
                 try {
                     checkAppl.store();
                 } catch (GenericEntityException e) {
@@ -3407,7 +3404,7 @@ public class InvoiceServices {
             } else if (paymentApplication.getString("paymentApplicationId").equals(checkAppl.getString("paymentApplicationId"))) {
                 // update existing record in-place
                 checkAppl.set("amountApplied", paymentApplication.getBigDecimal("amountApplied"));
-                Debug.logInfo("Update paymentApplication record: " + checkAppl.getString("paymentApplicationId") + " with appliedAmount:" + checkAppl.getBigDecimal("amountApplied"), module);
+                if (debug)     Debug.logInfo("Update paymentApplication record: " + checkAppl.getString("paymentApplicationId") + " with appliedAmount:" + checkAppl.getBigDecimal("amountApplied"), module);
                 try {
                     checkAppl.store();
                 } catch (GenericEntityException e) {
@@ -3418,14 +3415,14 @@ public class InvoiceServices {
                 checkAppl.set("amountApplied", paymentApplication.getBigDecimal("amountApplied").
                         add(checkAppl.getBigDecimal("amountApplied")).setScale(DECIMALS,ROUNDING));
                 // delete paymentApplication record and update the checkAppls one.
-                Debug.logInfo("Delete paymentApplication record: " + paymentApplication.getString("paymentApplicationId") + " with appliedAmount:" + paymentApplication.getBigDecimal("amountApplied"), module);
+                if (debug) Debug.logInfo("Delete paymentApplication record: " + paymentApplication.getString("paymentApplicationId") + " with appliedAmount:" + paymentApplication.getBigDecimal("amountApplied"), module);
                 try {
                     paymentApplication.remove();
                 } catch (GenericEntityException e) {
                     return ServiceUtil.returnError(e.getMessage());
                 }
                 // update amount existing record
-                Debug.logInfo("Update paymentApplication record: " + checkAppl.getString("paymentApplicationId") + " with appliedAmount:" + checkAppl.getBigDecimal("amountApplied"), module);
+                if (debug)     Debug.logInfo("Update paymentApplication record: " + checkAppl.getString("paymentApplicationId") + " with appliedAmount:" + checkAppl.getBigDecimal("amountApplied"), module);
                 try {
                     checkAppl.store();
                 } catch (GenericEntityException e) {
@@ -3433,11 +3430,11 @@ public class InvoiceServices {
                 }
             }
         } else {
-            Debug.logInfo("No records found with paymentId,invoiceid..etc probaly changed one of them...", module);
+            if (debug) Debug.logInfo("No records found with paymentId,invoiceid..etc probaly changed one of them...", module);
             // create record if ID null;
             if (paymentApplication.get("paymentApplicationId") == null) {
                 paymentApplication.set("paymentApplicationId", delegator.getNextSeqId("PaymentApplication"));
-                    Debug.logInfo("Create new paymentAppication record: " + paymentApplication.getString("paymentApplicationId") + " with appliedAmount:" + paymentApplication.getBigDecimal("amountApplied"), module);
+                    if (debug) Debug.logInfo("Create new paymentAppication record: " + paymentApplication.getString("paymentApplicationId") + " with appliedAmount:" + paymentApplication.getBigDecimal("amountApplied"), module);
                 try {
                     paymentApplication.create();
                 } catch (GenericEntityException e) {
@@ -3445,7 +3442,7 @@ public class InvoiceServices {
                 }
             } else {
                 // update existing record (could not be found because a non existing combination of paymentId/invoiceId/invoiceSeqId/ etc... was provided
-                Debug.logInfo("Update existing paymentApplication record: " + paymentApplication.getString("paymentApplicationId") + " with appliedAmount:" + paymentApplication.getBigDecimal("amountApplied"), module);
+                if (debug) Debug.logInfo("Update existing paymentApplication record: " + paymentApplication.getString("paymentApplicationId") + " with appliedAmount:" + paymentApplication.getBigDecimal("amountApplied"), module);
                 try {
                     paymentApplication.store();
                 } catch (GenericEntityException e) {
@@ -3464,29 +3461,29 @@ public class InvoiceServices {
         String paymentId = (String) context.get("paymentId");
         try {
             GenericValue payment = EntityQuery.use(delegator).from("Payment").where("paymentId", paymentId).queryOne();
-            if (payment == null) {
-                throw new GenericServiceException("Payment with ID [" + paymentId  + "] not found!");
-            }
+            if (payment == null) throw new GenericServiceException("Payment with ID [" + paymentId  + "] not found!");
 
             List<GenericValue> paymentApplications = payment.getRelated("PaymentApplication", null, null, false);
-            if (UtilValidate.isEmpty(paymentApplications)) {
-                return ServiceUtil.returnSuccess();
-            }
+            if (UtilValidate.isEmpty(paymentApplications)) return ServiceUtil.returnSuccess();
 
             // TODO: this is inefficient -- instead use HashSet to construct a distinct Set of invoiceIds, then iterate over it and call checkInvoicePaymentAppls
+            Map<String, Object> serviceResult = null;
             for (GenericValue paymentApplication : paymentApplications) {
                 String invoiceId = paymentApplication.getString("invoiceId");
                 if (invoiceId != null) {
-                    Map<String, Object> serviceResult = dispatcher.runSync("checkInvoicePaymentApplications", UtilMisc.<String, Object>toMap("invoiceId", invoiceId, "userLogin", userLogin));
+                    serviceResult = dispatcher.runSync("checkInvoicePaymentApplications", UtilMisc.<String, Object>toMap("invoiceId", invoiceId, "userLogin", userLogin));
                     if (ServiceUtil.isError(serviceResult)) {
-                        return serviceResult;
+                       return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
                     }
                 }
             }

[... 57 lines stripped ...]