1 16 17 package com.opensourcestrategies.financials.util; 18 19 import java.util.*; 20 import java.sql.Timestamp ; 21 import java.math.BigDecimal ; 22 23 import javolution.util.FastMap; 24 25 import org.ofbiz.accounting.AccountingException; 26 import org.ofbiz.base.util.*; 27 import org.ofbiz.entity.*; 28 import org.ofbiz.entity.condition.*; 29 import org.ofbiz.entity.util.*; 30 import org.ofbiz.service.*; 31 32 import com.opensourcestrategies.financials.util.UtilFinancial; 33 34 41 42 public class UtilCOGS { 43 44 public static String module = UtilCOGS.class.getName(); 45 46 public static int decimals = UtilNumber.getBigDecimalScale("fin_arithmetic.properties", "financial.statements.decimals"); 47 public static int rounding = UtilNumber.getBigDecimalRoundingMode("fin_arithmetic.properties", "financial.statements.rounding"); 48 public static final BigDecimal ZERO = new BigDecimal ("0"); 49 50 55 public static BigDecimal getProductAverageCost(String productId, String organizationPartyId, GenericValue userLogin, GenericDelegator delegator, LocalDispatcher dispatcher) { 56 BigDecimal cost = null; 57 58 try { 59 EntityConditionList conditions = new EntityConditionList(UtilMisc.toList( 61 EntityUtil.getFilterByDateExpr(), 62 new EntityExpr("productId", EntityOperator.EQUALS, productId), 63 new EntityExpr("organizationPartyId", EntityOperator.EQUALS, organizationPartyId) 64 ), EntityOperator.AND); 65 GenericValue entry = EntityUtil.getFirst( delegator.findByCondition("ProductAverageCost", conditions, UtilMisc.toList("averageCost"), null) ); 66 if (entry != null) { 67 cost = entry.getBigDecimal("averageCost"); 68 } 69 70 if ((cost != null) && (cost.signum() != 0)) return cost; 72 73 GenericValue acctgPref = delegator.findByPrimaryKeyCache("PartyAcctgPreference", UtilMisc.toMap("partyId", organizationPartyId)); 75 Map results = dispatcher.runSync("calculateProductCosts", UtilMisc.toMap("productId", productId, "currencyUomId", acctgPref.get("baseCurrencyUomId"), "costComponentTypePrefix", "EST_STD", "userLogin", userLogin)); 76 if (!ServiceUtil.isError(results)) { 77 Double totalCost = (Double ) results.get("totalCost"); 78 if ((totalCost != null) && (totalCost.doubleValue() != 0.0)) { 79 cost = new BigDecimal (totalCost.doubleValue()); 80 } 81 } 82 83 return cost; 85 86 } catch (GenericEntityException e) { 87 Debug.logError("Failed to get product average cost for productId [" + productId + "] and organization [" + organizationPartyId + "] due to entity error.", module); 88 return null; 89 } catch (GenericServiceException e) { 90 Debug.logError("Failed to get product average cost for productId [" + productId + "] and organization [" + organizationPartyId + "] due to service error.", module); 91 return null; 92 } 93 } 94 95 96 106 public static BigDecimal getInventoryValueForProduct(String productId, String organizationPartyId, Timestamp transactionDate, GenericDelegator delegator) 107 throws GenericEntityException { 108 Map results = getNetInventoryValueHelper(productId, null, organizationPartyId, transactionDate, delegator); 109 return (BigDecimal ) results.get(productId); 110 } 111 112 122 public static Map getInventoryValueForAllProducts(String organizationPartyId, Timestamp transactionDate, GenericDelegator delegator) 123 throws GenericEntityException { 124 return getNetInventoryValueHelper(null, null, organizationPartyId, transactionDate, delegator); 125 } 126 127 137 public static Map getInventoryValueForProductsByCondition(EntityCondition condition, String organizationPartyId, Timestamp transactionDate, GenericDelegator delegator) 138 throws GenericEntityException { 139 return getNetInventoryValueHelper(null, condition, organizationPartyId, transactionDate, delegator); 140 } 141 142 152 private static Map getNetInventoryValueHelper(String productId, EntityCondition condition, String organizationPartyId, Timestamp transactionDate, GenericDelegator delegator) 153 throws GenericEntityException { 154 155 if (organizationPartyId == null) { 156 throw new GenericEntityException("No organizationPartyId specified for getting product inventory value(s)."); 157 } 158 159 List selectFields = UtilMisc.toList("productId", "amount", "glAccountTypeId"); 161 162 List commonConditions = UtilMisc.toList( 164 new EntityExpr("isPosted", EntityOperator.EQUALS, "Y"), 165 new EntityExpr("organizationPartyId", EntityOperator.EQUALS, organizationPartyId) 166 ); 167 168 commonConditions.add(new EntityConditionList(UtilMisc.toList( 170 new EntityExpr("glAccountTypeId", EntityOperator.EQUALS, "INVENTORY_ACCOUNT"), 171 new EntityExpr("glAccountTypeId", EntityOperator.EQUALS, "INV_ADJ_AVG_COST"), 172 new EntityExpr("glAccountTypeId", EntityOperator.EQUALS, "RAWMAT_INVENTORY"), 173 new EntityExpr("glAccountTypeId", EntityOperator.EQUALS, "WIP_INVENTORY") 174 ), EntityOperator.OR)); 175 176 if (productId != null) commonConditions.add(new EntityExpr("productId", EntityOperator.EQUALS, productId)); 178 if (condition != null) commonConditions.add(condition); 179 if (transactionDate != null) commonConditions.add(new EntityExpr("transactionDate", EntityOperator.LESS_THAN_EQUAL_TO, transactionDate)); 180 181 List debitConditions = new ArrayList(commonConditions); 183 debitConditions.add(new EntityExpr("debitCreditFlag", EntityOperator.EQUALS, "D")); 184 EntityConditionList debitConditionList = new EntityConditionList(debitConditions, EntityOperator.AND); 185 186 EntityListIterator listIt = delegator.findListIteratorByCondition("AcctgTransEntryProdSums", debitConditionList, null, selectFields, 188 UtilMisc.toList("productId"), new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true)); 191 192 List debitSums = listIt.getCompleteList(); 194 195 List creditConditions = new ArrayList(commonConditions); 197 creditConditions.add(new EntityExpr("debitCreditFlag", EntityOperator.EQUALS, "C")); 198 EntityConditionList creditConditionList = new EntityConditionList(creditConditions, EntityOperator.AND); 199 200 listIt = delegator.findListIteratorByCondition("AcctgTransEntryProdSums", creditConditionList, null, selectFields, 202 UtilMisc.toList("productId"), new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true)); 205 206 List creditSums = listIt.getCompleteList(); 208 209 Map inventoryValueByProduct = FastMap.newInstance(); 211 212 for (Iterator iter = creditSums.iterator(); iter.hasNext(); ) { 214 GenericValue value = (GenericValue) iter.next(); 215 BigDecimal creditAmount = (BigDecimal ) inventoryValueByProduct.get(UtilFinancial.getProductIdOrDefault(value)); 216 if (creditAmount == null) creditAmount = ZERO; 217 inventoryValueByProduct.put(UtilFinancial.getProductIdOrDefault(value), creditAmount.subtract(value.getBigDecimal("amount")).setScale(decimals, rounding)); 218 } 219 220 for (Iterator iter = debitSums.iterator(); iter.hasNext(); ) { 222 GenericValue value = (GenericValue) iter.next(); 223 BigDecimal debitAmount = (BigDecimal ) inventoryValueByProduct.get(UtilFinancial.getProductIdOrDefault(value)); 224 if (debitAmount == null) debitAmount = ZERO; BigDecimal difference = value.getBigDecimal("amount").add(debitAmount).setScale(decimals, rounding); 226 inventoryValueByProduct.put(UtilFinancial.getProductIdOrDefault(value), difference); 227 } 228 229 return inventoryValueByProduct; 230 } 231 232 235 public static BigDecimal getInventoryQuantityForProduct(String productId, String organizationPartyId, GenericDelegator delegator, LocalDispatcher dispatcher) 236 throws GenericServiceException, GenericEntityException { 237 BigDecimal quantity = ZERO; 238 239 List facilities = delegator.findByAnd("Facility", UtilMisc.toMap("ownerPartyId", organizationPartyId)); 241 for (Iterator iter = facilities.iterator(); iter.hasNext(); ) { 242 String facilityId = ((GenericValue) iter.next()).getString("facilityId"); 243 Map serviceResults = dispatcher.runSync("getInventoryAvailableByFacility", UtilMisc.toMap("productId", productId, "facilityId", facilityId)); 244 if (ServiceUtil.isError(serviceResults)) { 245 throw new GenericServiceException(ServiceUtil.getErrorMessage(serviceResults)); 246 } 247 BigDecimal facilityQuantity = new BigDecimal (((Double ) serviceResults.get("quantityOnHandTotal")).doubleValue()); 248 quantity = quantity.add(facilityQuantity).setScale(decimals, rounding); } 250 return quantity; 251 } 252 253 264 public static BigDecimal getInventoryValueFromItems(String productId, String organizationPartyId, GenericDelegator delegator, LocalDispatcher dispatcher) 265 throws GenericEntityException, GenericServiceException { 266 List items = delegator.findByAnd("InventoryItem", UtilMisc.toMap("productId", productId, "ownerPartyId", organizationPartyId)); 267 BigDecimal total = ZERO; 268 269 for (Iterator it = items.iterator(); it.hasNext(); ) { 270 GenericValue item = (GenericValue) it.next(); 271 if ((item.get("unitCost") != null) && (item.get("quantityOnHandTotal") != null)) { 272 BigDecimal conversionFactor = new BigDecimal (UtilFinancial.determineUomConversionFactor(delegator, dispatcher, organizationPartyId, item.getString("currencyUomId"))); 273 total = total.add(item.getBigDecimal("unitCost").multiply(item.getBigDecimal("quantityOnHandTotal")).multiply(conversionFactor)).setScale(decimals, rounding); 275 } 276 } 277 278 return total; 279 } 280 } 281 | Popular Tags |