Author: jacopoc
Date: Fri Mar 28 15:05:23 2014
New Revision: 1582774

URL: http://svn.apache.org/r1582774
Log:
Moved two util methods for billing accounts from the "accounting" to the 
"order" component and removed duplicated code from the order component that was 
there to avoid a cyclic dependency between the two components.

Modified:
    
ofbiz/trunk/applications/accounting/script/org/ofbiz/accounting/test/PaymentApplicationTests.xml
    
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java
    ofbiz/trunk/applications/accounting/widget/BillingAccountForms.xml
    
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/accounting/script/org/ofbiz/accounting/test/PaymentApplicationTests.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/accounting/script/org/ofbiz/accounting/test/PaymentApplicationTests.xml?rev=1582774&r1=1582773&r2=1582774&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/accounting/script/org/ofbiz/accounting/test/PaymentApplicationTests.xml
 (original)
+++ 
ofbiz/trunk/applications/accounting/script/org/ofbiz/accounting/test/PaymentApplicationTests.xml
 Fri Mar 28 15:05:23 2014
@@ -106,9 +106,11 @@ under the License.
             <field field="delegator" type="org.ofbiz.entity.Delegator"/>
             <field field="serviceInMap.paymentId"/>
         </call-class-method>
-        <call-class-method method-name="getBillingAccountBalance" 
class-name="org.ofbiz.accounting.payment.BillingAccountWorker" 
ret-field="appliedBillling">
-            <field field="delegator" type="org.ofbiz.entity.Delegator"/>
-            <field field="serviceInMap.billingAccountId"/>
+        <entity-one entity-name="BillingAccount" value-field="billingAccount">
+            <field-map field-name="billingAccountId" 
from-field="serviceInMap.billingAccountId"/>
+        </entity-one>
+        <call-class-method method-name="getBillingAccountBalance" 
class-name="org.ofbiz.order.order.OrderReadHelper" ret-field="appliedBillling">
+            <field field="billingAccount" type="GenericValue"/>
         </call-class-method>
         <set field="zero" value="0" type="BigDecimal"/>
         <assert>

Modified: 
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java?rev=1582774&r1=1582773&r2=1582774&view=diff
==============================================================================
--- 
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java
 (original)
+++ 
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java
 Fri Mar 28 15:05:23 2014
@@ -44,6 +44,7 @@ import org.ofbiz.entity.condition.Entity
 import org.ofbiz.entity.condition.EntityExpr;
 import org.ofbiz.entity.condition.EntityOperator;
 import org.ofbiz.entity.util.EntityUtil;
+import org.ofbiz.order.order.OrderReadHelper;
 import org.ofbiz.service.DispatchContext;
 import org.ofbiz.service.LocalDispatcher;
 import org.ofbiz.service.ServiceUtil;
@@ -92,10 +93,10 @@ public class BillingAccountWorker {
                 if ((thruDate != null) && 
UtilDateTime.nowTimestamp().after(thruDate)) continue;
 
                 if 
(currencyUomId.equals(billingAccountVO.getString("accountCurrencyUomId"))) {
-                    BigDecimal accountBalance = 
BillingAccountWorker.getBillingAccountBalance(billingAccountVO);
+                    BigDecimal accountBalance = 
OrderReadHelper.getBillingAccountBalance(billingAccountVO);
 
                     Map<String, Object> billingAccount = new HashMap<String, 
Object>(billingAccountVO);
-                    BigDecimal accountLimit = 
getAccountLimit(billingAccountVO);
+                    BigDecimal accountLimit = 
OrderReadHelper.getAccountLimit(billingAccountVO);
 
                     billingAccount.put("accountBalance", accountBalance);
                     BigDecimal accountAvailable = 
accountLimit.subtract(accountBalance);
@@ -109,101 +110,6 @@ public class BillingAccountWorker {
     }
 
     /**
-     * Returns the accountLimit of the BillingAccount or BigDecimal ZERO if it 
is null
-     * @param billingAccount
-     * @throws GenericEntityException
-     */
-    public static BigDecimal getAccountLimit(GenericValue billingAccount) 
throws GenericEntityException {
-        if (billingAccount.getBigDecimal("accountLimit") != null) {
-            return billingAccount.getBigDecimal("accountLimit");
-        } else {
-            Debug.logWarning("Billing Account [" + 
billingAccount.getString("billingAccountId") + "] does not have an account 
limit defined, assuming zero.", module);
-            return ZERO;
-        }
-    }
-
-    /**
-     * Calculates the "available" balance of a billing account, which is the
-     * net balance minus amount of pending (not cancelled, rejected, or 
received) order payments.
-     * When looking at using a billing account for a new order, you should use 
this method.
-     * @param billingAccountId the billing account id
-     * @param delegator the delegato
-     * @return return the "available" balance of a billing account
-     * @throws GenericEntityException
-     */
-    public static BigDecimal getBillingAccountBalance(Delegator delegator, 
String billingAccountId) throws GenericEntityException {
-        GenericValue billingAccount = delegator.findOne("BillingAccount", 
UtilMisc.toMap("billingAccountId", billingAccountId), false);
-        return getBillingAccountBalance(billingAccount);
-    }
-
-    public static BigDecimal getBillingAccountBalance(GenericValue 
billingAccount) throws GenericEntityException {
-
-        Delegator delegator = billingAccount.getDelegator();
-        String billingAccountId = billingAccount.getString("billingAccountId");
-
-        BigDecimal balance = ZERO;
-        BigDecimal accountLimit = getAccountLimit(billingAccount);
-        balance = balance.add(accountLimit);
-        // pending (not cancelled, rejected, or received) order payments
-        EntityConditionList<EntityExpr> whereConditions = 
EntityCondition.makeCondition(UtilMisc.toList(
-                EntityCondition.makeCondition("billingAccountId", 
EntityOperator.EQUALS, billingAccountId),
-                EntityCondition.makeCondition("paymentMethodTypeId", 
EntityOperator.EQUALS, "EXT_BILLACT"),
-                EntityCondition.makeCondition("statusId", 
EntityOperator.NOT_IN, UtilMisc.toList("ORDER_CANCELLED", "ORDER_REJECTED")),
-                EntityCondition.makeCondition("preferenceStatusId", 
EntityOperator.NOT_IN, UtilMisc.toList("PAYMENT_SETTLED", "PAYMENT_RECEIVED", 
"PAYMENT_DECLINED", "PAYMENT_CANCELLED")) // PAYMENT_NOT_AUTH
-           ), EntityOperator.AND);
-
-        List<GenericValue> orderPaymentPreferenceSums = 
delegator.findList("OrderPurchasePaymentSummary", whereConditions, 
UtilMisc.toSet("maxAmount"), null, null, false);
-        for (Iterator<GenericValue> oppsi = 
orderPaymentPreferenceSums.iterator(); oppsi.hasNext();) {
-            GenericValue orderPaymentPreferenceSum = oppsi.next();
-            BigDecimal maxAmount = 
orderPaymentPreferenceSum.getBigDecimal("maxAmount");
-            balance = maxAmount != null ? balance.subtract(maxAmount) : 
balance;
-        }
-
-        List<GenericValue> paymentAppls = 
delegator.findByAnd("PaymentApplication", UtilMisc.toMap("billingAccountId", 
billingAccountId), null, false);
-        // TODO: cancelled payments?
-        for (Iterator<GenericValue> pAi = paymentAppls.iterator(); 
pAi.hasNext();) {
-            GenericValue paymentAppl = pAi.next();
-            if (paymentAppl.getString("invoiceId") == null) {
-                BigDecimal amountApplied = 
paymentAppl.getBigDecimal("amountApplied");
-                balance = balance.add(amountApplied);
-            }
-        }
-
-        balance = balance.setScale(decimals, rounding);
-        return balance;
-        /*
-        Delegator delegator = billingAccount.getDelegator();
-        String billingAccountId = billingAccount.getString("billingAccountId");
-
-        // first get the net balance of invoices - payments
-        BigDecimal balance = getBillingAccountNetBalance(delegator, 
billingAccountId);
-
-        // now the amounts of all the pending orders (not cancelled, rejected 
or completed)
-        List orderHeaders = getBillingAccountOpenOrders(delegator, 
billingAccountId);
-
-        if (orderHeaders != null) {
-            Iterator ohi = orderHeaders.iterator();
-            while (ohi.hasNext()) {
-                GenericValue orderHeader = (GenericValue) ohi.next();
-                OrderReadHelper orh = new OrderReadHelper(orderHeader);
-                balance = balance.add(orh.getOrderGrandTotal());
-            }
-        }
-
-        // set the balance to BillingAccount.accountLimit if it is greater.  
This is necessary because nowhere do we track the amount of BillingAccount
-        // to be charged to an order, such as FinAccountAuth entity does for 
FinAccount.  As a result, we must assume that the system is doing things 
correctly
-        // and use the accountLimit
-        BigDecimal accountLimit = billingAccount.getBigDecimal("accountLimit");
-        if (balance.compareTo(accountLimit) > 0) {
-            balance = accountLimit;
-        } else {
-            balance = balance.setScale(decimals, rounding);
-        }
-        return balance;
-         */
-    }
-
-    /**
      * Returns list of orders which are currently open against a billing 
account
      */
     public static List<GenericValue> getBillingAccountOpenOrders(Delegator 
delegator, String billingAccountId) throws GenericEntityException {
@@ -226,7 +132,7 @@ public class BillingAccountWorker {
     public static BigDecimal getBillingAccountAvailableBalance(GenericValue 
billingAccount) throws GenericEntityException {
         if ((billingAccount != null) && (billingAccount.get("accountLimit") != 
null)) {
             BigDecimal accountLimit = 
billingAccount.getBigDecimal("accountLimit");
-            BigDecimal availableBalance = 
accountLimit.subtract(getBillingAccountBalance(billingAccount)).setScale(decimals,
 rounding);
+            BigDecimal availableBalance = 
accountLimit.subtract(OrderReadHelper.getBillingAccountBalance(billingAccount)).setScale(decimals,
 rounding);
             return availableBalance;
         } else {
             Debug.logWarning("Available balance requested for null billing 
account, returning zero", module);
@@ -298,7 +204,7 @@ public class BillingAccountWorker {
             }
 
             result.put("billingAccount", billingAccount);
-            result.put("accountBalance",  getBillingAccountBalance(delegator, 
billingAccountId));
+            result.put("accountBalance", 
OrderReadHelper.getBillingAccountBalance(billingAccount));
             result.put("netAccountBalance", 
getBillingAccountNetBalance(delegator, billingAccountId));
             result.put("availableBalance", 
getBillingAccountAvailableBalance(billingAccount));
             result.put("availableToCapture", 
availableToCapture(billingAccount));

Modified: ofbiz/trunk/applications/accounting/widget/BillingAccountForms.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/applications/accounting/widget/BillingAccountForms.xml?rev=1582774&r1=1582773&r2=1582774&view=diff
==============================================================================
--- ofbiz/trunk/applications/accounting/widget/BillingAccountForms.xml 
(original)
+++ ofbiz/trunk/applications/accounting/widget/BillingAccountForms.xml Fri Mar 
28 15:05:23 2014
@@ -119,7 +119,7 @@ under the License.
     <form name="EditBillingAccount" type="single" 
target="updateBillingAccount" title=""
         header-row-style="header-row" default-table-style="basic-table">
         <actions>
-            <set field="availableBalance" value="${bsh:billingAccount != null 
? 
org.ofbiz.accounting.payment.BillingAccountWorker.getBillingAccountBalance(billingAccount)
 : 0}" type="BigDecimal"/>
+            <set field="availableBalance" value="${bsh:billingAccount != null 
? 
org.ofbiz.order.order.OrderReadHelper.getBillingAccountBalance(billingAccount) 
: 0}" type="BigDecimal"/>
         </actions>
         <alt-target use-when="billingAccount==null" 
target="createBillingAccount"/>
         <auto-fields-service service-name="updateBillingAccount" 
map-name="billingAccount"/>

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=1582774&r1=1582773&r2=1582774&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 
Fri Mar 28 15:05:23 2014
@@ -2912,4 +2912,62 @@ public class OrderReadHelper {
         result.put("taxGrandTotal", taxGrandTotal);
         return result;
     }
+
+    /**
+     * Calculates the "available" balance of a billing account, which is the
+     * net balance minus amount of pending (not cancelled, rejected, or 
received) order payments.
+     * When looking at using a billing account for a new order, you should use 
this method.
+     * @param billingAccount the billing account record
+     * @return return the "available" balance of a billing account
+     * @throws GenericEntityException
+     */
+    public static BigDecimal getBillingAccountBalance(GenericValue 
billingAccount) throws GenericEntityException {
+
+        Delegator delegator = billingAccount.getDelegator();
+        String billingAccountId = billingAccount.getString("billingAccountId");
+
+        BigDecimal balance = ZERO;
+        BigDecimal accountLimit = getAccountLimit(billingAccount);
+        balance = balance.add(accountLimit);
+        // pending (not cancelled, rejected, or received) order payments
+        EntityConditionList<EntityExpr> whereConditions = 
EntityCondition.makeCondition(UtilMisc.toList(
+                EntityCondition.makeCondition("billingAccountId", 
EntityOperator.EQUALS, billingAccountId),
+                EntityCondition.makeCondition("paymentMethodTypeId", 
EntityOperator.EQUALS, "EXT_BILLACT"),
+                EntityCondition.makeCondition("statusId", 
EntityOperator.NOT_IN, UtilMisc.toList("ORDER_CANCELLED", "ORDER_REJECTED")),
+                EntityCondition.makeCondition("preferenceStatusId", 
EntityOperator.NOT_IN, UtilMisc.toList("PAYMENT_SETTLED", "PAYMENT_RECEIVED", 
"PAYMENT_DECLINED", "PAYMENT_CANCELLED")) // PAYMENT_NOT_AUTH
+           ), EntityOperator.AND);
+
+        List<GenericValue> orderPaymentPreferenceSums = 
delegator.findList("OrderPurchasePaymentSummary", whereConditions, 
UtilMisc.toSet("maxAmount"), null, null, false);
+        for (GenericValue orderPaymentPreferenceSum : 
orderPaymentPreferenceSums) {
+            BigDecimal maxAmount = 
orderPaymentPreferenceSum.getBigDecimal("maxAmount");
+            balance = maxAmount != null ? balance.subtract(maxAmount) : 
balance;
+        }
+
+        List<GenericValue> paymentAppls = 
delegator.findByAnd("PaymentApplication", UtilMisc.toMap("billingAccountId", 
billingAccountId), null, false);
+        // TODO: cancelled payments?
+        for (GenericValue paymentAppl : paymentAppls) {
+            if (paymentAppl.getString("invoiceId") == null) {
+                BigDecimal amountApplied = 
paymentAppl.getBigDecimal("amountApplied");
+                balance = balance.add(amountApplied);
+            }
+        }
+
+        balance = balance.setScale(scale, rounding);
+        return balance;
+    }
+
+    /**
+     * Returns the accountLimit of the BillingAccount or BigDecimal ZERO if it 
is null
+     * @param billingAccount
+     * @throws GenericEntityException
+     */
+    public static BigDecimal getAccountLimit(GenericValue billingAccount) 
throws GenericEntityException {
+        if (billingAccount.getBigDecimal("accountLimit") != null) {
+            return billingAccount.getBigDecimal("accountLimit");
+        } else {
+            Debug.logWarning("Billing Account [" + 
billingAccount.getString("billingAccountId") + "] does not have an account 
limit defined, assuming zero.", module);
+            return ZERO;
+        }
+    }
+
 }

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=1582774&r1=1582773&r2=1582774&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
 Fri Mar 28 15:05:23 2014
@@ -780,7 +780,8 @@ public class OrderReturnServices {
                         String thisBillingAccountId = 
billingAccountItr.next().getString("billingAccountId");
                         BigDecimal billingAccountBalance = ZERO;
                         try {
-                            billingAccountBalance = 
getBillingAccountBalance(thisBillingAccountId, dctx);
+                            GenericValue billingAccount = 
delegator.findOne("BillingAccount", UtilMisc.toMap("billingAccountId", 
thisBillingAccountId), false);
+                            billingAccountBalance = 
OrderReadHelper.getBillingAccountBalance(billingAccount);
                         } catch (GenericEntityException e) {
                             return ServiceUtil.returnError(e.getMessage());
                         }
@@ -1005,51 +1006,6 @@ public class OrderReturnServices {
     }
 
     /**
-     * Helper method to get billing account balance, cannot use 
BillingAccountWorker.getBillingAccountBalance()
-     * due to circular build dependency.
-     * @param billingAccountId the billing account id
-     * @param dctx the dispatch context
-     * @return returns the billing account balance
-     * @throws GenericEntityException
-     */
-    public static BigDecimal getBillingAccountBalance(String billingAccountId, 
DispatchContext dctx) throws GenericEntityException {
-        Delegator delegator = dctx.getDelegator();
-        GenericValue billingAccount = delegator.findOne("BillingAccount", 
UtilMisc.toMap("billingAccountId", billingAccountId), false);
-
-        BigDecimal balance = ZERO;
-        BigDecimal accountLimit = ZERO;
-        if (billingAccount.getBigDecimal("accountLimit") != null) {
-            accountLimit = billingAccount.getBigDecimal("accountLimit");
-        }
-        balance = balance.add(accountLimit);
-        // pending (not cancelled, rejected, or received) order payments
-        EntityConditionList<EntityExpr> whereConditions = 
EntityCondition.makeCondition(UtilMisc.toList(
-                EntityCondition.makeCondition("billingAccountId", 
EntityOperator.EQUALS, billingAccountId),
-                EntityCondition.makeCondition("paymentMethodTypeId", 
EntityOperator.EQUALS, "EXT_BILLACT"),
-                EntityCondition.makeCondition("statusId", 
EntityOperator.NOT_IN, UtilMisc.toList("ORDER_CANCELLED", "ORDER_REJECTED")),
-                EntityCondition.makeCondition("preferenceStatusId", 
EntityOperator.NOT_IN, UtilMisc.toList("PAYMENT_SETTLED", "PAYMENT_RECEIVED", 
"PAYMENT_DECLINED", "PAYMENT_CANCELLED")) // PAYMENT_NOT_AUTH
-           ), EntityOperator.AND);
-
-        List<GenericValue> orderPaymentPreferenceSums = 
delegator.findList("OrderPurchasePaymentSummary", whereConditions, 
UtilMisc.toSet("maxAmount"), null, null, false);
-        for (GenericValue orderPaymentPreferenceSum : 
orderPaymentPreferenceSums) {
-            BigDecimal maxAmount = 
orderPaymentPreferenceSum.getBigDecimal("maxAmount");
-            balance = maxAmount != null ? balance.subtract(maxAmount) : 
balance;
-        }
-
-        List<GenericValue> paymentAppls = 
delegator.findByAnd("PaymentApplication", UtilMisc.toMap("billingAccountId", 
billingAccountId), null, false);
-        // TODO: cancelled payments?
-        for (GenericValue paymentAppl : paymentAppls) {
-            if (paymentAppl.getString("invoiceId") == null) {
-                BigDecimal amountApplied = 
paymentAppl.getBigDecimal("amountApplied");
-                balance = balance.add(amountApplied);
-            }
-        }
-
-        balance = balance.setScale(decimals, rounding);
-        return balance;
-    }
-
-    /**
      * Helper method to generate a BillingAccount (store credit) from a return
      * header.  This method takes care of all business logic relating to
      * the initialization of a Billing Account from the Return data.


Reply via email to