Author: jleroux
Date: Tue Apr 17 11:30:15 2007
New Revision: 529701

URL: http://svn.apache.org/viewvc?view=rev&rev=529701
Log:
This fix "Problem in the pos module related to item discount and sales 
discount" (https://issues.apache.org/jira/browse/OFBIZ-828)
Fix a double bug in POS when using a discount (on item(s) or sale) and a cash 
amount higher than total (hence issuing change).
Though I'm sure that it corrects this double bug (in InvoiceServices.java) I 
commit it as CTR because I'm still not sure it corrects all the problems in 
accounting.
I also applied Si'advice from 
https://issues.apache.org/jira/browse/OFBIZ-828#action_12483045 in 
OrderServices.java even if I guess it was not needed (because I agree that  "A 
payment of a negative amount is highly suspicious")
This is enough from a POS POV, but I'm not sure that the corrects payments are 
generated (it generates paiements of zero value). 
Anyway all the informations are there and it's usable as is. Let me know if you 
find the payments an issue, thanks.

BTW I noticed that there are still a *lot of* Doubles where BigDecimals should 
be used... (even after recent Scott's effort).
I hope to do an exhaustive work on this begining from POS (where there are only 
Doubles for now) ... one day... (before 2007 end, hopefully)

Modified:
    
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java
    ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java

Modified: 
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java?view=diff&rev=529701&r1=529700&r2=529701
==============================================================================
--- 
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java
 (original)
+++ 
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java
 Tue Apr 17 11:30:15 2007
@@ -496,6 +496,9 @@
         
                         // If the absolute invoiced amount >= the abs of the 
adjustment amount, the full amount has already been invoiced,
                         //  so skip this adjustment
+                        if (null == adj.get("amount")) { // JLR 17/4/7 : 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;
+                        }                        
                         if 
(adjAlreadyInvoicedAmount.abs().compareTo(adj.getBigDecimal("amount").setScale(decimals,
 rounding).abs()) > 0) {
                             continue;
                         }
@@ -519,34 +522,34 @@
                             amount = amount.setScale(decimals, rounding);
                         }
                         if (amount.signum() != 0) {                      
-                               Map createInvoiceItemAdjContext = 
FastMap.newInstance();
-                               createInvoiceItemAdjContext.put("invoiceId", 
invoiceId);
-                               
createInvoiceItemAdjContext.put("invoiceItemSeqId", invoiceItemSeqId);
-                               
createInvoiceItemAdjContext.put("invoiceItemTypeId", 
getInvoiceItemType(delegator, adj.getString("orderAdjustmentTypeId"), null, 
invoiceType, "INVOICE_ITM_ADJ"));
-                               createInvoiceItemAdjContext.put("description", 
adj.get("description"));
-                               createInvoiceItemAdjContext.put("quantity", new 
Double(1));
-                               createInvoiceItemAdjContext.put("amount", new 
Double(amount.doubleValue()));
-                               createInvoiceItemAdjContext.put("productId", 
orderItem.get("productId"));
-                               
createInvoiceItemAdjContext.put("productFeatureId", 
orderItem.get("productFeatureId"));
-                               
createInvoiceItemAdjContext.put("overrideGlAccountId", 
adj.get("overrideGlAccountId"));
-                               
createInvoiceItemAdjContext.put("parentInvoiceId", invoiceId);
-                               
createInvoiceItemAdjContext.put("parentInvoiceItemSeqId", 
parentInvoiceItemSeqId);
-                               //createInvoiceItemAdjContext.put("uomId", "");
-                               createInvoiceItemAdjContext.put("userLogin", 
userLogin);
-                               
createInvoiceItemAdjContext.put("taxAuthPartyId", adj.get("taxAuthPartyId"));
-                               createInvoiceItemAdjContext.put("taxAuthGeoId", 
adj.get("taxAuthGeoId"));
-                               
createInvoiceItemAdjContext.put("taxAuthorityRateSeqId", 
adj.get("taxAuthorityRateSeqId"));
-           
-                               // invoice items for sales tax are not taxable 
themselves
-                               // TODO: This is not an ideal solution. 
Instead, we need to use OrderAdjustment.includeInTax when it is implemented
-                               if 
(!(adj.getString("orderAdjustmentTypeId").equals("SALES_TAX"))) {
-                                   
createInvoiceItemAdjContext.put("taxableFlag", product.get("taxable"));    
-                               }
-           
-                               Map createInvoiceItemAdjResult = 
dispatcher.runSync("createInvoiceItem", createInvoiceItemAdjContext);
-                               if 
(ServiceUtil.isError(createInvoiceItemAdjResult)) {
-                                   return 
ServiceUtil.returnError(UtilProperties.getMessage(resource,"AccountingErrorCreatingInvoiceItemFromOrder",locale),
 null, null, createInvoiceItemAdjResult);
-                               }
+                            Map createInvoiceItemAdjContext = 
FastMap.newInstance();
+                            createInvoiceItemAdjContext.put("invoiceId", 
invoiceId);
+                            
createInvoiceItemAdjContext.put("invoiceItemSeqId", invoiceItemSeqId);
+                            
createInvoiceItemAdjContext.put("invoiceItemTypeId", 
getInvoiceItemType(delegator, adj.getString("orderAdjustmentTypeId"), null, 
invoiceType, "INVOICE_ITM_ADJ"));
+                            createInvoiceItemAdjContext.put("description", 
adj.get("description"));
+                            createInvoiceItemAdjContext.put("quantity", new 
Double(1));
+                            createInvoiceItemAdjContext.put("amount", new 
Double(amount.doubleValue()));
+                            createInvoiceItemAdjContext.put("productId", 
orderItem.get("productId"));
+                            
createInvoiceItemAdjContext.put("productFeatureId", 
orderItem.get("productFeatureId"));
+                            
createInvoiceItemAdjContext.put("overrideGlAccountId", 
adj.get("overrideGlAccountId"));
+                            createInvoiceItemAdjContext.put("parentInvoiceId", 
invoiceId);
+                            
createInvoiceItemAdjContext.put("parentInvoiceItemSeqId", 
parentInvoiceItemSeqId);
+                            //createInvoiceItemAdjContext.put("uomId", "");
+                            createInvoiceItemAdjContext.put("userLogin", 
userLogin);
+                            createInvoiceItemAdjContext.put("taxAuthPartyId", 
adj.get("taxAuthPartyId"));
+                            createInvoiceItemAdjContext.put("taxAuthGeoId", 
adj.get("taxAuthGeoId"));
+                            
createInvoiceItemAdjContext.put("taxAuthorityRateSeqId", 
adj.get("taxAuthorityRateSeqId"));
+        
+                            // invoice items for sales tax are not taxable 
themselves
+                            // TODO: This is not an ideal solution. Instead, 
we need to use OrderAdjustment.includeInTax when it is implemented
+                            if 
(!(adj.getString("orderAdjustmentTypeId").equals("SALES_TAX"))) {
+                                createInvoiceItemAdjContext.put("taxableFlag", 
product.get("taxable"));    
+                            }
+        
+                            Map createInvoiceItemAdjResult = 
dispatcher.runSync("createInvoiceItem", createInvoiceItemAdjContext);
+                            if 
(ServiceUtil.isError(createInvoiceItemAdjResult)) {
+                                return 
ServiceUtil.returnError(UtilProperties.getMessage(resource,"AccountingErrorCreatingInvoiceItemFromOrder",locale),
 null, null, createInvoiceItemAdjResult);
+                            }
 
                             // Create the OrderAdjustmentBilling record
                             Map createOrderAdjustmentBillingContext = 
FastMap.newInstance();
@@ -561,24 +564,24 @@
                                 return 
ServiceUtil.returnError(UtilProperties.getMessage(resource,"AccountingErrorCreatingOrderAdjustmentBillingFromOrder",locale),
 null, null, createOrderAdjustmentBillingContext);
                             }
 
-                               // this adjustment amount
-                               BigDecimal thisAdjAmount = new 
BigDecimal(amount.doubleValue()).setScale(decimals, rounding);
-       
-                               // adjustments only apply to totals when they 
are not tax or shipping adjustments
-                               if 
(!"SALES_TAX".equals(adj.getString("orderAdjustmentTypeId")) &&
-                                       
!"SHIPPING_ADJUSTMENT".equals(adj.getString("orderAdjustmentTypeId"))) {
-                                   // increment the invoice subtotal
-                                   invoiceSubTotal = 
invoiceSubTotal.add(thisAdjAmount).setScale(100, rounding);
-       
-                                   // add to the ship amount only if it 
applies to this item
-                                   if (shippingApplies) {
-                                       invoiceShipProRateAmount = 
invoiceShipProRateAmount.add(thisAdjAmount).setScale(decimals, rounding);
-                                   }
-                               }
-       
-                               // increment the counter
-                               invoiceItemSeqNum++;
-                               invoiceItemSeqId = 
UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum, 2);
+                            // this adjustment amount
+                            BigDecimal thisAdjAmount = new 
BigDecimal(amount.doubleValue()).setScale(decimals, rounding);
+    
+                            // adjustments only apply to totals when they are 
not tax or shipping adjustments
+                            if 
(!"SALES_TAX".equals(adj.getString("orderAdjustmentTypeId")) &&
+                                    
!"SHIPPING_ADJUSTMENT".equals(adj.getString("orderAdjustmentTypeId"))) {
+                                // increment the invoice subtotal
+                                invoiceSubTotal = 
invoiceSubTotal.add(thisAdjAmount).setScale(100, rounding);
+    
+                                // add to the ship amount only if it applies 
to this item
+                                if (shippingApplies) {
+                                    invoiceShipProRateAmount = 
invoiceShipProRateAmount.add(thisAdjAmount).setScale(decimals, rounding);
+                                }
+                            }
+    
+                            // increment the counter
+                            invoiceItemSeqNum++;
+                            invoiceItemSeqId = 
UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum, 2);
                         }
                     }
                 }
@@ -606,6 +609,9 @@
 
                 // If the absolute invoiced amount >= the abs of the 
adjustment amount, the full amount has already been invoiced,
                 //  so skip this adjustment
+                if (null == adj.get("amount")) { // JLR 17/4/7 : 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 
(adjAlreadyInvoicedAmount.abs().compareTo(adj.getBigDecimal("amount").setScale(decimals,
 rounding).abs()) > 0) {
                     continue;
                 }
@@ -1967,7 +1973,7 @@
         else if (adj.get("sourcePercentage") != null) {
             // pro-rate the amount
             BigDecimal percent = adj.getBigDecimal("sourcePercentage");
-            percent = percent.divide(new BigDecimal(100), 100, rounding);      
        
+            percent = percent.divide(new BigDecimal(100), 100, rounding);      
      
             BigDecimal amount = ZERO;
             // make sure the divisor is not 0 to avoid NaN problems; just 
leave the amount as 0 and skip it in essense
             if (divisor.signum() != 0) {

Modified: 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java?view=diff&rev=529701&r1=529700&r2=529701
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java 
(original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java 
Tue Apr 17 11:30:15 2007
@@ -19,6 +19,7 @@
 package org.ofbiz.order.order;
 
 import java.math.BigDecimal;
+import java.lang.Math;
 import java.sql.Timestamp;
 import java.text.NumberFormat;
 import java.text.ParseException;
@@ -3520,15 +3521,29 @@
 
             // create the payment
             Map paymentParams = new HashMap();
-            paymentParams.put("paymentTypeId", "CUSTOMER_PAYMENT");
-            paymentParams.put("paymentMethodTypeId", 
orderPaymentPreference.getString("paymentMethodTypeId"));
-            paymentParams.put("paymentPreferenceId", 
orderPaymentPreference.getString("orderPaymentPreferenceId"));
-            paymentParams.put("amount", 
orderPaymentPreference.getDouble("maxAmount"));
-            paymentParams.put("statusId", "PMNT_RECEIVED");
-            paymentParams.put("effectiveDate", UtilDateTime.nowTimestamp());
-            paymentParams.put("partyIdFrom", billToParty.getString("partyId"));
-            paymentParams.put("currencyUomId", 
productStore.getString("defaultCurrencyUomId"));
-            paymentParams.put("partyIdTo", payToPartyId);
+            double maxAmount = 
orderPaymentPreference.getDouble("maxAmount").doubleValue();
+            if (maxAmount > 0.0) {            
+                paymentParams.put("paymentTypeId", "CUSTOMER_PAYMENT");
+                paymentParams.put("paymentMethodTypeId", 
orderPaymentPreference.getString("paymentMethodTypeId"));
+                paymentParams.put("paymentPreferenceId", 
orderPaymentPreference.getString("orderPaymentPreferenceId"));
+                paymentParams.put("amount", new Double(maxAmount));
+                paymentParams.put("statusId", "PMNT_RECEIVED");
+                paymentParams.put("effectiveDate", 
UtilDateTime.nowTimestamp());
+                paymentParams.put("partyIdFrom", 
billToParty.getString("partyId"));
+                paymentParams.put("currencyUomId", 
productStore.getString("defaultCurrencyUomId"));
+                paymentParams.put("partyIdTo", payToPartyId);
+            }
+            else {
+                paymentParams.put("paymentTypeId", "CUSTOMER_REFUND"); // JLR 
17/7/4 from a suggestion of Si cf. 
https://issues.apache.org/jira/browse/OFBIZ-828#action_12483045
+                paymentParams.put("paymentMethodTypeId", 
orderPaymentPreference.getString("paymentMethodTypeId"));
+                paymentParams.put("paymentPreferenceId", 
orderPaymentPreference.getString("orderPaymentPreferenceId"));
+                paymentParams.put("amount", new Double(Math.abs(maxAmount)));
+                paymentParams.put("statusId", "PMNT_RECEIVED");
+                paymentParams.put("effectiveDate", 
UtilDateTime.nowTimestamp());
+                paymentParams.put("partyIdFrom", payToPartyId);
+                paymentParams.put("currencyUomId", 
productStore.getString("defaultCurrencyUomId"));
+                paymentParams.put("partyIdTo", 
billToParty.getString("partyId"));
+            }
             if (paymentRefNum != null) {
                 paymentParams.put("paymentRefNum", paymentRefNum);
             }


Reply via email to