Author: lektran
Date: Mon Dec 17 02:02:48 2007
New Revision: 604811

URL: http://svn.apache.org/viewvc?rev=604811&view=rev
Log:
A few improvements to return refunds (mainly focused on handling offline 
payment types):
1. When calculating payments received for an order, now looking at Payment 
instead of PaymentGatewayResponse.
2. When pulling the PaymentPreferences to refund against, now considering the 
received status as well as settled.
3. If the PaymentPreference is not electronic or a billing account, now 
creating a payment with the correct details set instead of creating a "filler" 
payment (whatever that was)

Modified:
    
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java
    
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java

Modified: 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java?rev=604811&r1=604810&r2=604811&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java 
(original)
+++ 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java 
Mon Dec 17 02:02:48 2007
@@ -212,19 +212,22 @@
         Iterator ppit = paymentPrefs.iterator();
         while (ppit.hasNext()) {
             GenericValue paymentPref = (GenericValue) ppit.next();
-            List paymentGatewayResponses = new ArrayList();
+            List payments = FastList.newInstance();
             try {
-                paymentGatewayResponses = 
paymentPref.getRelatedByAnd("PaymentGatewayResponse", 
UtilMisc.toMap("paymentServiceTypeEnumId","PRDS_PAY_CAPTURE"));
+                List exprs = UtilMisc.toList(new EntityExpr("statusId", 
EntityOperator.EQUALS, "PMNT_RECEIVED"),
+                                            new EntityExpr("statusId", 
EntityOperator.EQUALS, "PMNT_CONFIRMED"));
+                payments = paymentPref.getRelated("Payment");
+                payments = EntityUtil.filterByOr(payments, exprs);
             } catch (GenericEntityException e) {
                 Debug.logError(e, module);
             }
 
             BigDecimal chargedToPaymentPref = ZERO;
-            Iterator pgrit = paymentGatewayResponses.iterator();
-            while(pgrit.hasNext()) {
-                GenericValue paymentGatewayResponse = (GenericValue) 
pgrit.next();
-                if (paymentGatewayResponse.get("amount") != null) {
-                    chargedToPaymentPref = 
chargedToPaymentPref.add(paymentGatewayResponse.getBigDecimal("amount")).setScale(scale+1,
 rounding);
+            Iterator payit = payments.iterator();
+            while(payit.hasNext()) {
+                GenericValue payment = (GenericValue) payit.next();
+                if (payment.get("amount") != null) {
+                    chargedToPaymentPref = 
chargedToPaymentPref.add(payment.getBigDecimal("amount")).setScale(scale+1, 
rounding);
                 }
             }
 

Modified: 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java?rev=604811&r1=604810&r2=604811&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java
 (original)
+++ 
ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java
 Mon Dec 17 02:02:48 2007
@@ -904,7 +904,9 @@
                 try {
                     orderHeader = delegator.findByPrimaryKey("OrderHeader", 
UtilMisc.toMap("orderId", orderId));
                     // sort these desending by maxAmount
-                    orderPayPrefs = 
orderHeader.getRelated("OrderPaymentPreference", UtilMisc.toMap("statusId", 
"PAYMENT_SETTLED"), UtilMisc.toList("-maxAmount"));
+                    orderPayPrefs = 
orderHeader.getRelated("OrderPaymentPreference", UtilMisc.toList("-maxAmount"));
+                    List exprs = UtilMisc.toList(new EntityExpr("statusId", 
EntityOperator.EQUALS, "PAYMENT_SETTLED"), new EntityExpr("statusId", 
EntityOperator.EQUALS, "PAYMENT_RECEIVED"));
+                    orderPayPrefs = EntityUtil.filterByOr(orderPayPrefs, 
exprs);
                 } catch (GenericEntityException e) {
                     Debug.logError(e, "Cannot get Order details for #" + 
orderId, module);
                     continue;
@@ -1050,7 +1052,26 @@
                                     return 
ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderProblemsWithTheRefundSeeLogs",
 locale));
                                 }
                             } else {
-                                // TODO: handle manual refunds (accounts 
payable)
+                                // handle manual refunds
+                                try {
+                                    Map input = UtilMisc.toMap("userLogin", 
userLogin, "amount", new Double(amountLeftToRefund.doubleValue()), "statusId", 
"PMNT_NOT_PAID");
+                                    input.put("partyIdTo", 
returnHeader.get("fromPartyId"));
+                                    input.put("partyIdFrom", 
returnHeader.get("toPartyId"));
+                                    input.put("paymentTypeId", 
"CUSTOMER_REFUND");
+                                    input.put("paymentMethodId", 
orderPaymentPreference.get("paymentMethodId"));
+                                    input.put("paymentMethodTypeId", 
orderPaymentPreference.get("paymentMethodTypeId"));
+                                    input.put("paymentPreferenceId", 
orderPaymentPreference.get("orderPaymentPreferenceId"));
+                                    
+                                    serviceResult = 
dispatcher.runSync("createPayment", input);
+
+                                    if (ServiceUtil.isError(serviceResult) || 
ServiceUtil.isFailure(serviceResult)) {
+                                        Debug.logError("Error in refund 
payment: " + ServiceUtil.getErrorMessage(serviceResult), module);
+                                        continue;
+                                    }
+                                    paymentId = (String) 
serviceResult.get("paymentId");
+                                } catch (GenericServiceException e) {
+                                    return 
ServiceUtil.returnError(e.getMessage());
+                                }
                             }
 
                             // Fill out the data for the new ReturnItemResponse
@@ -1059,10 +1080,7 @@
                             response.put("responseAmount", new 
Double(amountToRefund.setScale(decimals, rounding).doubleValue()));
                             response.put("responseDate", now);
                             response.put("userLogin", userLogin);
-                            if (paymentId != null) {
-                                // A null payment ID means no electronic 
refund was available; manual refund needed
-                                response.put("paymentId", paymentId);
-                            }
+                            response.put("paymentId", paymentId);
                             if (paymentMethodTypeId.equals("EXT_BILLACT")) {
                                 response.put("billingAccountId", 
orderReadHelper.getBillingAccount().getString("billingAccountId"));
                             }
@@ -1122,31 +1140,6 @@
                         }
                     }                    
                 }
-
-                // OFBIZ-459:  Create a "filler" payment and return item 
response by hand for the remaining amount, note that this won't be applied to 
the invoice
-                if (amountLeftToRefund.compareTo(ZERO) == 1) {
-                    try {
-                        Map input = UtilMisc.toMap("userLogin", userLogin, 
"amount", new Double(amountLeftToRefund.doubleValue()), "statusId", 
"PMNT_NOT_PAID");
-                        input.put("partyIdTo", 
returnHeader.get("fromPartyId"));
-                        input.put("partyIdFrom", 
returnHeader.get("toPartyId"));
-                        input.put("paymentTypeId", "CUSTOMER_REFUND");
-                        if (UtilValidate.isNotEmpty(refundPaymentMethod)) {
-                            input.put("paymentMethodId", 
refundPaymentMethod.get("paymentMethodId"));
-                            input.put("paymentMethodTypeId", 
refundPaymentMethod.get("paymentMethodTypeId"));
-                        } else {
-                            Debug.logInfo("refundPaymentMethodId not 
configured in PartyAcctgPreference, not setting for remaining refund amount", 
module);
-                        }
-                        
-                        Map results = dispatcher.runSync("createPayment", 
input);
-                        if (ServiceUtil.isError(results)) return results;
-
-                        input = UtilMisc.toMap("userLogin", userLogin, 
"paymentId", results.get("paymentId"), "responseAmount", new 
Double(amountLeftToRefund.doubleValue()));
-                        results = 
dispatcher.runSync("createReturnItemResponse", input);
-                        if (ServiceUtil.isError(results)) return results;
-                    } catch (GenericServiceException e) {
-                        return ServiceUtil.returnError(e.getMessage());
-                    }
-                }
             }
         }
 
@@ -1989,7 +1982,7 @@
                     Debug.logError("Order [" + orderId + "] refund amount[ " + 
returnAmount + "] less than zero", module);
                     return 
ServiceUtil.returnError(UtilProperties.getMessage(resource_error, 
"OrderReturnTotalCannotLessThanZero", locale));
                 }
-                OrderReadHelper helper = new 
OrderReadHelper(OrderReadHelper.getOrderHeader(delegator, orderId));
+                OrderReadHelper helper = new OrderReadHelper(delegator, 
orderId);
                 BigDecimal grandTotal = helper.getOrderGrandTotal();
                 if (returnAmount == null) {
                     Debug.logInfo("No returnAmount found for order:" + 
orderId, module);


Reply via email to