Author: sichen
Date: Wed Jul 12 14:36:16 2006 New Revision: 421420 URL: http://svn.apache.org/viewvc?rev=421420&view=rev Log: Added ProductStore.storeCreditValidDays to allow per-store BillingAccount.thruDate to be set. The thruDate is set when BillingAccount is created in processCreditReturn. Modified the BillingAccountWorker service that generates list of billing accounts to obey this thruDate. Resolves OFBIZ-86. Modified: incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/payment/BillingAccountWorker.java incubator/ofbiz/trunk/applications/ecommerce/data/DemoProduct.xml incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java incubator/ofbiz/trunk/applications/product/entitydef/entitymodel.xml 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=421420&r1=421419&r2=421420&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 Wed Jul 12 14:36:16 2006 @@ -36,6 +36,7 @@ import org.ofbiz.accounting.invoice.InvoiceWorker; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; +import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilNumber; import org.ofbiz.entity.GenericDelegator; @@ -97,6 +98,11 @@ while (billingAcctIter.hasNext()) { GenericValue billingAccountRole = (GenericValue) billingAcctIter.next(); GenericValue billingAccountVO = billingAccountRole.getRelatedOne("BillingAccount"); + + // skip accounts that have thruDate < nowTimestamp + java.sql.Timestamp thruDate = billingAccountVO.getTimestamp("thruDate"); + if ((thruDate != null) && UtilDateTime.nowTimestamp().after(thruDate)) continue; + if (currencyUomId.equals(billingAccountVO.getString("accountCurrencyUomId"))) { double accountBalance = (BillingAccountWorker.getBillingAccountBalance(billingAccountVO)).doubleValue(); Modified: incubator/ofbiz/trunk/applications/ecommerce/data/DemoProduct.xml URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/ecommerce/data/DemoProduct.xml?rev=421420&r1=421419&r2=421420&view=diff ============================================================================== --- incubator/ofbiz/trunk/applications/ecommerce/data/DemoProduct.xml (original) +++ incubator/ofbiz/trunk/applications/ecommerce/data/DemoProduct.xml Wed Jul 12 14:36:16 2006 @@ -29,6 +29,7 @@ authDeclinedMessage="There has been a problem with your method of payment. Please try a different method or call customer service." authFraudMessage="Your order has been rejected and your account has been disabled due to fraud." authErrorMessage="Problem connecting to payment processor; we will continue to retry and notify you by email." + storeCreditValidDays="90" styleSheet="/images/ecommain.css" headerLogo="/images/ofbiz_logo.jpg"/> <ProductStorePaymentSetting productStoreId="9000" paymentMethodTypeId="CREDIT_CARD" paymentServiceTypeEnumId="PRDS_PAY_AUTH" paymentService="alwaysApproveCCProcessor"/> <ProductStorePaymentSetting productStoreId="9000" paymentMethodTypeId="CREDIT_CARD" paymentServiceTypeEnumId="PRDS_PAY_RELEASE" paymentService="testCCRelease"/> Modified: incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java?rev=421420&r1=421419&r2=421420&view=diff ============================================================================== --- incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java (original) +++ incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReturnServices.java Wed Jul 12 14:36:16 2006 @@ -542,27 +542,12 @@ } if (billingAccountId == null) { // create new BillingAccount w/ 0 balance - try { - // Note that accountLimit must be 0.0 for store credits, because the available balance of BillingAccounts is calculated as accountLimit + sum of Payments - sum of Invoices - Map newBa = dispatcher.runSync("createBillingAccount", UtilMisc.toMap("accountLimit", new Double(0.00), "description", "Credit Account", "userLogin", userLogin, "accountCurrencyUomId", returnHeader.get("currencyUomId"))); - if (!newBa.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) { - billingAccountId = (String) newBa.get("billingAccountId"); - if (billingAccountId != null) { - // set the role on the account - Map newBaR = dispatcher.runSync("createBillingAccountRole", UtilMisc.toMap("billingAccountId", billingAccountId, "partyId", fromPartyId, "roleTypeId", "BILL_TO_CUSTOMER", "userLogin", userLogin)); - if (newBaR.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) { - Debug.logError("Error with createBillingAccountRole: " + newBaR.get(ModelService.ERROR_MESSAGE), module); - return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderErrorWithCreateBillingAccountRole", locale) + newBaR.get(ModelService.ERROR_MESSAGE)); - } - } - } else { - Debug.logError("Error with createBillingAccount: " + newBa.get(ModelService.ERROR_MESSAGE), module); - return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderErrorWithCreateBillingAccount", locale) + newBa.get(ModelService.ERROR_MESSAGE)); - } - } catch (GenericServiceException e) { - Debug.logError(e, "Problems creating BillingAccount", module); - return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderProblemsCreatingBillingAccount", locale)); + Map results = createBillingAccountFromReturn(returnHeader, returnItems, dctx, context); + if (ServiceUtil.isError(results)) { + Debug.logError("Error creating BillingAccount: " + results.get(ModelService.ERROR_MESSAGE), module); + return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderErrorWithCreateBillingAccount", locale) + results.get(ModelService.ERROR_MESSAGE)); } + billingAccountId = (String) results.get("billingAccountId"); } // double check; make sure we have a billingAccount @@ -684,6 +669,77 @@ } return ServiceUtil.returnSuccess(); + } + + /** + * 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. + * + * The BillingAccount.thruDate will be set to (now + + * ProductStore.storeCreditValidDays + end of day). The product stores + * are obtained via the return orders, and the minimum storeCreditValidDays + * will be used. The default is to set thruDate to null, which implies no + * expiration. + * + * Note that we set BillingAccount.accountLimit to 0.0 for store credits. + * This is because the available balance of BillingAccounts is + * calculated as accountLimit + sum of Payments - sum of Invoices. + */ + private static Map createBillingAccountFromReturn(GenericValue returnHeader, List returnItems, DispatchContext dctx, Map context) { + LocalDispatcher dispatcher = dctx.getDispatcher(); + GenericDelegator delegator = dctx.getDelegator(); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + Locale locale = (Locale) context.get("locale"); + + try { + // get the related product stores via the orders related to this return + List orders = EntityUtil.getRelated("OrderHeader", returnItems); + List productStores = EntityUtil.getRelated("ProductStore", orders); + + // find the minimum storeCreditValidDays of all the ProductStores associated with all the Orders on the Return, skipping null ones + Long storeCreditValidDays = null; + for (Iterator iter = productStores.iterator(); iter.hasNext(); ) { + GenericValue productStore = (GenericValue) iter.next(); + Long thisStoreValidDays = productStore.getLong("storeCreditValidDays"); + if (thisStoreValidDays == null) continue; + + if (storeCreditValidDays == null) { + storeCreditValidDays = thisStoreValidDays; + } else if (thisStoreValidDays.compareTo(storeCreditValidDays) < 0) { + // if this store's days < store credit valid days, use this store's days + storeCreditValidDays = thisStoreValidDays; + } + } + + // if there is a storeCreditValidDays, set the thruDate to (nowTimestamp + storeCreditValidDays + end of day) + Timestamp thruDate = null; + if (storeCreditValidDays != null) thruDate = UtilDateTime.getDayEnd(UtilDateTime.nowTimestamp(), storeCreditValidDays.intValue()); + + // create the billing account + Map input = UtilMisc.toMap("accountLimit", new Double(0.00), "description", "Credit Account for Return #" + returnHeader.get("returnId"), "userLogin", userLogin); + input.put("accountCurrencyUomId", returnHeader.get("currencyUomId")); + input.put("thruDate", thruDate); + Map results = dispatcher.runSync("createBillingAccount", input); + if (ServiceUtil.isError(results)) return results; + String billingAccountId = (String) results.get("billingAccountId"); + + // set the role on the account + input = UtilMisc.toMap("billingAccountId", billingAccountId, "partyId", returnHeader.get("fromPartyId"), "roleTypeId", "BILL_TO_CUSTOMER", "userLogin", userLogin); + Map roleResults = dispatcher.runSync("createBillingAccountRole", input); + if (ServiceUtil.isError(roleResults)) { + Debug.logError("Error with createBillingAccountRole: " + roleResults.get(ModelService.ERROR_MESSAGE), module); + return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderErrorWithCreateBillingAccountRole", locale) + roleResults.get(ModelService.ERROR_MESSAGE)); + } + + return results; + } catch (GenericEntityException e) { + Debug.logError(e, "Entity error when creating BillingAccount: " + e.getMessage(), module); + return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderProblemsCreatingBillingAccount", locale)); + } catch (GenericServiceException e) { + Debug.logError(e, "Service error when creating BillingAccount: " + e.getMessage(), module); + return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderProblemsCreatingBillingAccount", locale)); + } } // refund (cash/charge) return Modified: incubator/ofbiz/trunk/applications/product/entitydef/entitymodel.xml URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/product/entitydef/entitymodel.xml?rev=421420&r1=421419&r2=421420&view=diff ============================================================================== --- incubator/ofbiz/trunk/applications/product/entitydef/entitymodel.xml (original) +++ incubator/ofbiz/trunk/applications/product/entitydef/entitymodel.xml Wed Jul 12 14:36:16 2006 @@ -3081,6 +3081,7 @@ <field name="autoOrderCcTryOtherCards" type="indicator"><description>For auto-orders try other Credit Cards for the customer?</description></field> <field name="autoOrderCcTryLaterNsf" type="indicator"><description>For auto-orders if Credit Cards fails for NSF (Not Sufficient Funds) try again later?</description></field> <field name="autoOrderCcTryLaterMax" type="numeric"><description>For auto-orders if Credit Cards fails for NSF try again how many times?</description></field> + <field name="storeCreditValidDays" type="numeric"><description>How many days that store credit is valid for. Null value implies no expiration.</description></field> <prim-key field="productStoreId"/> <relation type="one" fk-name="PROD_STR_PRSTRGP" title="Primary" rel-entity-name="ProductStoreGroup"> <key-map field-name="primaryStoreGroupId" rel-field-name="productStoreGroupId"/> |
Free forum by Nabble | Edit this page |