Author: jaz
Date: Mon Mar 26 21:38:07 2007 New Revision: 522742 URL: http://svn.apache.org/viewvc?view=rev&rev=522742 Log: generic financial account creation from a product purchase (fulfillment services) Added: ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/finaccount/FinAccountProductServices.java (with props) Added: ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/finaccount/FinAccountProductServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/finaccount/FinAccountProductServices.java?view=auto&rev=522742 ============================================================================== --- ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/finaccount/FinAccountProductServices.java (added) +++ ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/finaccount/FinAccountProductServices.java Mon Mar 26 21:38:07 2007 @@ -0,0 +1,203 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +package org.ofbiz.accounting.finaccount; + +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.service.ServiceUtil; +import org.ofbiz.service.GenericServiceException; +import org.ofbiz.entity.GenericDelegator; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.util.EntityUtil; +import org.ofbiz.base.util.*; +import org.ofbiz.order.order.OrderReadHelper; +import org.ofbiz.order.finaccount.FinAccountHelper; + +import java.util.*; +import java.math.BigDecimal; + +import javolution.util.FastMap; + +/** + * FinAccountProductServices - Financial Accounts created from product purchases (i.e. gift certificates) + */ +public class FinAccountProductServices { + + public static final String module = FinAccountProductServices.class.getName(); + + public static Map createPartyFinAccountFromPurchase(DispatchContext dctx, Map context) { + // this service should always be called via FULFILLMENT_EXTASYNC + LocalDispatcher dispatcher = dctx.getDispatcher(); + GenericDelegator delegator = dctx.getDelegator(); + + GenericValue orderItem = (GenericValue) context.get("orderItem"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + + // order ID for tracking + String orderId = orderItem.getString("orderId"); + + // the order header for store info + GenericValue orderHeader; + try { + orderHeader = orderItem.getRelatedOne("OrderHeader"); + } catch (GenericEntityException e) { + Debug.logError(e, "Unable to get OrderHeader from OrderItem",module); + return ServiceUtil.returnError("Unable to get OrderHeader from OrderItem"); + } + + String productId = orderItem.getString("productId"); + GenericValue featureAndAppl; + try { + List featureAndAppls = delegator.findByAnd("ProductFeatureAndAppl", UtilMisc.toMap("productId", productId, + "productFeatureTypeId", "TYPE", "productFeatureApplTypeId", "STANDARD_FEATURE")); + featureAndAppls = EntityUtil.filterByDate(featureAndAppls); + featureAndAppl = EntityUtil.getFirst(featureAndAppls); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + + // financial account data; pulled from the TYPE feature of the product + String finAccountTypeId = "BALANCE_ACCOUNT"; // default + String finAccountName = "Customer Financial Account"; + if (featureAndAppl != null) { + if (UtilValidate.isNotEmpty(featureAndAppl.getString("idCode"))) { + finAccountTypeId = featureAndAppl.getString("idCode"); + } + if (UtilValidate.isNotEmpty(featureAndAppl.getString("description"))) { + finAccountName = featureAndAppl.getString("description"); + } + } + + // locate the financial account type + GenericValue finAccountType; + try { + finAccountType = delegator.findByPrimaryKey("FinAccountType", UtilMisc.toMap("finAccountTypeId", finAccountTypeId)); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + String replenishEnumId = finAccountType.getString("replenishEnumId"); + + // get the order read helper + OrderReadHelper orh = new OrderReadHelper(orderHeader); + + // get the currency + String currency = orh.getCurrency(); + + // make sure we have a currency + if (currency == null) { + currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); + } + + // get the product store + String productStoreId = null; + if (orderHeader != null) { + productStoreId = orh.getProductStoreId(); + } + if (productStoreId == null) { + return ServiceUtil.returnError("Unable to create financial account; no productStoreId on OrderHeader : " + orderId); + } + + // party ID (owner) + GenericValue billToParty = orh.getBillToParty(); + String partyId = null; + if (billToParty != null) { + partyId = billToParty.getString("partyId"); + } + + // price/amount/quantity to create initial deposit amount + BigDecimal quantity = orderItem.getBigDecimal("quantity"); + BigDecimal price = orderItem.getBigDecimal("unitPrice"); + BigDecimal deposit = price.multiply(quantity).setScale(FinAccountHelper.decimals, FinAccountHelper.rounding); + + // create the financial account + Map createCtx = FastMap.newInstance(); + String finAccountId; + + createCtx.put("finAccountTypeId", finAccountTypeId); + createCtx.put("finAccountName", finAccountName); + createCtx.put("productStoreId", productStoreId); + createCtx.put("ownerPartyId", partyId); + createCtx.put("currencyUomId", currency); + createCtx.put("userLogin", userLogin); + + // if we auto-replenish this type; set the level to the initial deposit + if (replenishEnumId != null && "FARP_AUTOMATIC".equals(replenishEnumId)) { + createCtx.put("replenishLevel", deposit); + } + + Map createResp; + try { + createResp = dispatcher.runSync("createFinAccountForStore", createCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + if (ServiceUtil.isError(createResp)) { + return createResp; + } else { + finAccountId = (String) createResp.get("finAccountId"); + } + + // create the owner role + Map roleCtx = FastMap.newInstance(); + roleCtx.put("partyId", partyId); + roleCtx.put("roleTypeId", "OWNER"); + roleCtx.put("finAccountId", finAccountId); + roleCtx.put("userLogin", userLogin); + roleCtx.put("fromDate", UtilDateTime.nowTimestamp()); + Map roleResp; + try { + roleResp = dispatcher.runSync("createFinAccountRole", roleCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + if (ServiceUtil.isError(roleResp)) { + return roleResp; + } + + // create the initial deposit + Map depositCtx = FastMap.newInstance(); + depositCtx.put("finAccountId", finAccountId); + depositCtx.put("productStoreId", productStoreId); + depositCtx.put("currency", currency); + depositCtx.put("partyId", partyId); + depositCtx.put("amount", new Double(deposit.doubleValue())); + depositCtx.put("userLogin", userLogin); + + Map depositResp; + try { + depositResp = dispatcher.runSync("finAccountDeposit", depositCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + if (ServiceUtil.isError(depositResp)) { + return depositResp; + } + + Map result = ServiceUtil.returnSuccess(); + result.put("finAccountId", finAccountId); + return result; + } +} Propchange: ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/finaccount/FinAccountProductServices.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/finaccount/FinAccountProductServices.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/finaccount/FinAccountProductServices.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
Free forum by Nabble | Edit this page |