Author: sichen
Date: Thu Jul 27 10:58:03 2006 New Revision: 426168 URL: http://svn.apache.org/viewvc?rev=426168&view=rev Log: Use the correct available to capture amount to determine how much to capture on billing account versus other payment methods Modified: incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java Modified: incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java?rev=426168&r1=426167&r2=426168&view=diff ============================================================================== --- incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java (original) +++ incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java Thu Jul 27 10:58:03 2006 @@ -205,32 +205,42 @@ return balance; } + /** + * Returns the amount of the billing account which could be captured, which is BillingAccount.accountLimit - net balance + * @param billingAccount + * @return + * @throws GenericEntityException + */ + public static BigDecimal availableToCapture(GenericValue billingAccount) throws GenericEntityException { + BigDecimal netBalance = getBillingAccountNetBalance(billingAccount.getDelegator(), billingAccount.getString("billingAccountId")); + BigDecimal accountLimit = new BigDecimal(billingAccount.getDouble("accountLimit").doubleValue()); + + return accountLimit.subtract(netBalance).setScale(decimals, rounding); + } + public static Map calcBillingAccountBalance(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); String billingAccountId = (String) context.get("billingAccountId"); - GenericValue billingAccount = null; - Double accountBalance = null; - Double netAccountBalance = null; - Double availableBalance = null; + Map result = ServiceUtil.returnSuccess(); + try { - billingAccount = delegator.findByPrimaryKey("BillingAccount", UtilMisc.toMap("billingAccountId", billingAccountId)); - accountBalance = new Double((getBillingAccountBalance(delegator, billingAccountId)).doubleValue()); - netAccountBalance = new Double((getBillingAccountNetBalance(delegator, billingAccountId)).doubleValue()); - availableBalance = new Double(getBillingAccountAvailableBalance(billingAccount).doubleValue()); + GenericValue billingAccount = delegator.findByPrimaryKey("BillingAccount", UtilMisc.toMap("billingAccountId", billingAccountId)); + if (billingAccount == null) { + return ServiceUtil.returnError("Unable to locate billing account #" + billingAccountId); + } + + result.put("billingAccount", billingAccount); + result.put("accountBalance", new Double((getBillingAccountBalance(delegator, billingAccountId)).doubleValue())); + result.put("netAccountBalance", new Double((getBillingAccountNetBalance(delegator, billingAccountId)).doubleValue())); + result.put("availableBalance", new Double(getBillingAccountAvailableBalance(billingAccount).doubleValue())); + result.put("availableToCapture", new Double(availableToCapture(billingAccount).doubleValue())); + + return result; } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError("Error getting billing account or calculating balance for billing account #" + billingAccountId); } - if (billingAccount == null) { - return ServiceUtil.returnError("Unable to locate billing account #" + billingAccountId); - } - Map result = ServiceUtil.returnSuccess(); - result.put("accountBalance", accountBalance); - result.put("netAccountBalance", netAccountBalance); - result.put("availableBalance", availableBalance); - result.put("billingAccount", billingAccount); - return result; } } Modified: incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java?rev=426168&r1=426167&r2=426168&view=diff ============================================================================== --- incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java (original) +++ incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/PaymentGatewayServices.java Thu Jul 27 10:58:03 2006 @@ -832,7 +832,6 @@ // or at least charge up the full amount of the billing account's net balance and then try other payment methods // Note that this needs to run here first because orders charged to billing account alone will have no OrderPaymentPreference GenericValue billingAccount = null; - BigDecimal billingAccountBalance = null; BigDecimal billingAccountAvail = null; BigDecimal billingAccountCaptureAmount = ZERO; Map billingAccountInfo = null; @@ -846,18 +845,16 @@ if (billingAccountInfo != null) { billingAccount = (GenericValue) billingAccountInfo.get("billingAccount"); // use net account balance because we want to know how much we can charge to a billing account - Double billingAccountBalanceD = (Double) billingAccountInfo.get("netAccountBalance"); - billingAccountBalance = new BigDecimal(billingAccountBalanceD.doubleValue()); + Double availableToCapture = (Double) billingAccountInfo.get("availableToCapture"); + billingAccountAvail = new BigDecimal(availableToCapture.doubleValue()); } // if a billing account is used to pay for an order, then charge as much as we can to it before proceeding to other payment methods. - if (billingAccount != null && billingAccountBalance != null) { + if (billingAccount != null && billingAccountAvail != null) { try { - billingAccountAvail = BillingAccountWorker.getBillingAccountAvailableBalance(billingAccount); - - // the amount to be "charged" to the billing account, which is the minimum of billing account remaining amount and amount to capture + // the amount to be "charged" to the billing account, which is the minimum of billing account available amount and amount to capture BigDecimal captureAmountBd = new BigDecimal(captureAmount.doubleValue()); - billingAccountCaptureAmount = billingAccountAvail.min(captureAmountBd); + billingAccountCaptureAmount = captureAmountBd.min(billingAccountAvail); Debug.logInfo("billing account avail = [" + billingAccountAvail + "] capture amount = [" + billingAccountCaptureAmount + "]", module); // capturing to a billing account is a matter of a creating a payment and then applying it to the invoice @@ -878,8 +875,6 @@ result.put("processResult", "COMPLETE"); return result; } - } catch (GenericEntityException ex) { - return ServiceUtil.returnError(ex.getMessage()); } catch (GenericServiceException ex) { return ServiceUtil.returnError(ex.getMessage()); } |
Free forum by Nabble | Edit this page |