📄 utilfinancial.java
字号:
/* * Copyright (C) 2006 Open Source Strategies, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */package com.opensourcestrategies.financials.util;import java.util.*;import java.sql.Timestamp;import java.math.BigDecimal;import javolution.util.FastMap;import org.ofbiz.accounting.invoice.InvoiceWorker;import org.ofbiz.accounting.util.UtilAccounting;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilNumber;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntity;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceUtil;/** * UtilFinancial - Utilities for financials. * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> * @version $Rev: 81 $ * @since 2.2 */public class UtilFinancial { public static String module = UtilFinancial.class.getName(); public static int decimals = UtilNumber.getBigDecimalScale("fin_arithmetic.properties", "financial.statements.decimals"); public static int rounding = UtilNumber.getBigDecimalRoundingMode("fin_arithmetic.properties", "financial.statements.rounding"); protected static final BigDecimal ZERO = new BigDecimal("0"); // TODO: this will soon be UtilNumber.BD_ZERO protected static final int MILLISECONDS_PER_DAY = 86400000; public static final String DEFAULT_PRODUCT_ID = "_NA_"; // productId to use in various Maps in case there is no product (ie, bulk items) /** * Return either the field productId from the value or the DEFAULT_PRODUCT_ID (usually "_NA_") * @param value * @return */ public static String getProductIdOrDefault(GenericValue value) { if (value.getString("productId") != null) { return value.getString("productId"); } else { return DEFAULT_PRODUCT_ID; } } public static EntityExpr getAssetExpr(GenericDelegator delegator) throws GenericEntityException { return getGlAccountClassExpr("ASSET", delegator); } public static EntityExpr getLiabilityExpr(GenericDelegator delegator) throws GenericEntityException { return getGlAccountClassExpr("LIABILITY", delegator); } public static EntityExpr getEquityExpr(GenericDelegator delegator) throws GenericEntityException { return getGlAccountClassExpr("EQUITY", delegator); } /** * Gets an entity expression where the field glAccountClassId is a child of the given rootGlAccountClassId. * The intent is to assist with building queries such as 'get all accounts of type ASSET'. * Use one of the shortcut methods, like getAssetExpr() instead. * * Ex. Submitting "DEBIT" results in the expression, * glAccountClassId IN ('DEBIT', 'ASSET', 'DISTRIBUTION', 'EXPENSE', 'INCOME', 'NON_POSTING') * * @param rootGlAccountClassId The ancestor class to check that the field glAccountClassId is a member of * @return A suitable EntityExpr for checking that the glAccountClassId field is a member of this tree */ public static EntityExpr getGlAccountClassExpr(String rootGlAccountClassId, GenericDelegator delegator) throws GenericEntityException { // first get the gl class root value GenericValue glAccountClass = delegator.findByPrimaryKeyCache("GlAccountClass", UtilMisc.toMap("glAccountClassId", rootGlAccountClassId)); if (glAccountClass == null) { throw new GenericEntityException("Cannot find GlAccountClass [" + rootGlAccountClassId + "]"); } // recursively build the list of ids that are of this class List ids = new ArrayList(); recurseGetGlAccountClassIds(glAccountClass, ids); // make a WHERE glAccountId IN (list of ids) expression return new EntityExpr("glAccountClassId", EntityOperator.IN, ids); } /** * Recursively obtains the IDs of all children of a given glAccountClass. * @param ids A List to populate with the children IDs */ public static void recurseGetGlAccountClassIds(GenericValue glAccountClass, List ids) throws GenericEntityException { ids.add(glAccountClass.getString("glAccountClassId")); List children = glAccountClass.getRelatedCache("ChildGlAccountClass"); for (Iterator iter = children.iterator(); iter.hasNext(); ) { GenericValue child = (GenericValue) iter.next(); recurseGetGlAccountClassIds(child, ids); } } /** * Determines conversion factor given a source currencyUomId and the organizationPartyId's accounting preference. * @param delegator * @param dispatcher * @param organizationPartyId * @param currencyUomId * @throws GenericEntityException * @throws GenericServiceException * @author Leon Torres */ public static double determineUomConversionFactor(GenericDelegator delegator, LocalDispatcher dispatcher, String organizationPartyId, String currencyUomId) throws GenericEntityException, GenericServiceException { try { // default conversion factor double conversionFactor = 1.0; // if currencyUomId is null, return default if (currencyUomId == null) { return conversionFactor; } // get our organization's accounting preference GenericValue accountingPreference = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", organizationPartyId)).getRelatedOne("PartyAcctgPreference"); if (accountingPreference == null) { String msg = "Currency conversion failed: No PartyAcctgPreference entity data for organizationPartyId " + organizationPartyId; Debug.logError(msg, module); throw new GenericServiceException(msg); } // if the currencies are equal, return default if (currencyUomId.equals(accountingPreference.getString("baseCurrencyUomId"))) { return conversionFactor; } // this does a currency conversion, based on currencyUomId and the party's accounting preferences. conversionFactor will be used for postings Map tmpResult = dispatcher.runSync("convertUom", UtilMisc.toMap("originalValue", new Double(conversionFactor), "uomId", currencyUomId, "uomIdTo", accountingPreference.getString("baseCurrencyUomId"))); if (((String) tmpResult.get(ModelService.RESPONSE_MESSAGE)).equals(ModelService.RESPOND_SUCCESS)) { conversionFactor = ((Double) tmpResult.get("convertedValue")).doubleValue(); } else { String msg = "Currency conversion failed: No currencyUomId defined in PartyAcctgPreference entity for organizationPartyId " + organizationPartyId; Debug.logError(msg, module); throw new GenericServiceException(msg); } Debug.logInfo("currency conversion factor is = " + conversionFactor, module); return conversionFactor; } catch (GenericEntityException ex) { Debug.logError(ex.getMessage(), module); throw new GenericEntityException(ex); } catch (GenericServiceException ex) { Debug.logError(ex.getMessage(), module); throw new GenericServiceException(ex); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -