1 16 17 package com.opensourcestrategies.financials.util; 18 19 20 import java.util.*; 21 import java.sql.Timestamp ; 22 import java.math.BigDecimal ; 23 24 import javolution.util.FastMap; 25 26 import org.ofbiz.accounting.invoice.InvoiceWorker; 27 import org.ofbiz.accounting.util.UtilAccounting; 28 import org.ofbiz.base.util.Debug; 29 import org.ofbiz.base.util.UtilDateTime; 30 import org.ofbiz.base.util.UtilMisc; 31 import org.ofbiz.base.util.UtilNumber; 32 import org.ofbiz.entity.GenericDelegator; 33 import org.ofbiz.entity.GenericEntity; 34 import org.ofbiz.entity.GenericEntityException; 35 import org.ofbiz.entity.GenericValue; 36 import org.ofbiz.entity.condition.EntityConditionList; 37 import org.ofbiz.entity.condition.EntityExpr; 38 import org.ofbiz.entity.condition.EntityOperator; 39 import org.ofbiz.entity.util.EntityUtil; 40 import org.ofbiz.service.DispatchContext; 41 import org.ofbiz.service.GenericServiceException; 42 import org.ofbiz.service.LocalDispatcher; 43 import org.ofbiz.service.ModelService; 44 import org.ofbiz.service.ServiceUtil; 45 52 53 public class UtilFinancial { 54 55 public static String module = UtilFinancial.class.getName(); 56 57 public static int decimals = UtilNumber.getBigDecimalScale("fin_arithmetic.properties", "financial.statements.decimals"); 58 public static int rounding = UtilNumber.getBigDecimalRoundingMode("fin_arithmetic.properties", "financial.statements.rounding"); 59 protected static final BigDecimal ZERO = new BigDecimal ("0"); protected static final int MILLISECONDS_PER_DAY = 86400000; 61 public static final String DEFAULT_PRODUCT_ID = "_NA_"; 63 68 public static String getProductIdOrDefault(GenericValue value) { 69 if (value.getString("productId") != null) { 70 return value.getString("productId"); 71 } else { 72 return DEFAULT_PRODUCT_ID; 73 } 74 } 75 76 public static EntityExpr getAssetExpr(GenericDelegator delegator) throws GenericEntityException { 77 return getGlAccountClassExpr("ASSET", delegator); 78 } 79 80 public static EntityExpr getLiabilityExpr(GenericDelegator delegator) throws GenericEntityException { 81 return getGlAccountClassExpr("LIABILITY", delegator); 82 } 83 84 public static EntityExpr getEquityExpr(GenericDelegator delegator) throws GenericEntityException { 85 return getGlAccountClassExpr("EQUITY", delegator); 86 } 87 88 100 public static EntityExpr getGlAccountClassExpr(String rootGlAccountClassId, GenericDelegator delegator) throws GenericEntityException { 101 102 GenericValue glAccountClass = delegator.findByPrimaryKeyCache("GlAccountClass", UtilMisc.toMap("glAccountClassId", rootGlAccountClassId)); 104 if (glAccountClass == null) { 105 Debug.logWarning("Cannot find GlAccountClass [" + rootGlAccountClassId + "]", module); 106 return new EntityExpr(new Integer (1), EntityOperator.EQUALS, new Integer (1)); 107 } 108 109 List ids = new ArrayList(); 111 recurseGetGlAccountClassIds(glAccountClass, ids); 112 113 return new EntityExpr("glAccountClassId", EntityOperator.IN, ids); 115 } 116 117 121 public static void recurseGetGlAccountClassIds(GenericValue glAccountClass, List ids) throws GenericEntityException { 122 ids.add(glAccountClass.getString("glAccountClassId")); 123 List children = glAccountClass.getRelatedCache("ChildGlAccountClass"); 124 for (Iterator iter = children.iterator(); iter.hasNext(); ) { 125 GenericValue child = (GenericValue) iter.next(); 126 recurseGetGlAccountClassIds(child, ids); 127 } 128 } 129 130 141 public static EntityExpr getGlAccountTypeExpr(String rootGlAccountTypeId, GenericDelegator delegator) throws GenericEntityException { 142 143 GenericValue glAccountType = delegator.findByPrimaryKeyCache("GlAccountType", UtilMisc.toMap("glAccountTypeId", rootGlAccountTypeId)); 145 if (glAccountType == null) { 146 Debug.logWarning("Cannot find GlAccountType [" + rootGlAccountTypeId + "]", module); 147 return new EntityExpr("1", EntityOperator.EQUALS, "1"); 148 } 149 150 List ids = new ArrayList(); 152 recurseGetGlAccountTypeIds(glAccountType, ids); 153 154 return new EntityExpr("glAccountTypeId", EntityOperator.IN, ids); 156 } 157 158 162 public static void recurseGetGlAccountTypeIds(GenericValue glAccountType, List ids) throws GenericEntityException { 163 ids.add(glAccountType.getString("glAccountTypeId")); 164 List children = glAccountType.getRelatedCache("ChildGlAccountType"); 165 for (Iterator iter = children.iterator(); iter.hasNext(); ) { 166 GenericValue child = (GenericValue) iter.next(); 167 recurseGetGlAccountTypeIds(child, ids); 168 } 169 } 170 171 181 public static double determineUomConversionFactor(GenericDelegator delegator, LocalDispatcher dispatcher, 182 String organizationPartyId, String currencyUomId) throws GenericEntityException, GenericServiceException { 183 return determineUomConversionFactor(delegator, dispatcher, organizationPartyId, currencyUomId, UtilDateTime.nowTimestamp() ); 184 } 185 186 189 public static double determineUomConversionFactor(GenericDelegator delegator, LocalDispatcher dispatcher, 190 String organizationPartyId, String currencyUomId, Timestamp asOfDate) throws GenericEntityException, GenericServiceException { 191 try { 192 double conversionFactor = 1.0; 194 if (currencyUomId == null) { 196 return conversionFactor; 197 } 198 199 GenericValue accountingPreference = delegator.findByPrimaryKey("Party", 201 UtilMisc.toMap("partyId", organizationPartyId)).getRelatedOne("PartyAcctgPreference"); 202 if (accountingPreference == null) { 203 String msg = "Currency conversion failed: No PartyAcctgPreference entity data for organizationPartyId " + organizationPartyId; 204 Debug.logError(msg, module); 205 throw new GenericServiceException(msg); 206 } 207 208 if (currencyUomId.equals(accountingPreference.getString("baseCurrencyUomId"))) { 210 return conversionFactor; 211 } 212 213 Map tmpResult = dispatcher.runSync("convertUom", UtilMisc.toMap("originalValue", new Double (conversionFactor), 215 "uomId", currencyUomId, "uomIdTo", accountingPreference.getString("baseCurrencyUomId"), "asOfDate", asOfDate )); 216 217 if (((String ) tmpResult.get(ModelService.RESPONSE_MESSAGE)).equals(ModelService.RESPOND_SUCCESS)) { 218 conversionFactor = ((Double ) tmpResult.get("convertedValue")).doubleValue(); 219 } else { 220 String msg = "Currency conversion failed: No currencyUomId defined in PartyAcctgPreference entity for organizationPartyId " + organizationPartyId; 221 Debug.logError(msg, module); 222 throw new GenericServiceException(msg); 223 } 224 225 Debug.logInfo("currency conversion factor is = " + conversionFactor, module); 226 return conversionFactor; 227 228 } catch (GenericEntityException ex) { 229 Debug.logError(ex.getMessage(), module); 230 throw new GenericEntityException(ex); 231 } catch (GenericServiceException ex) { 232 Debug.logError(ex.getMessage(), module); 233 throw new GenericServiceException(ex); 234 } 235 } 236 237 } 238 | Popular Tags |