1 16 17 package com.opensourcestrategies.financials.cogs; 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.base.util.*; 26 import org.ofbiz.entity.*; 27 import org.ofbiz.entity.condition.*; 28 import org.ofbiz.entity.util.*; 29 import org.ofbiz.service.*; 30 31 import org.ofbiz.accounting.AccountingException; 32 import org.ofbiz.accounting.util.UtilAccounting; 33 34 import com.opensourcestrategies.financials.util.UtilCOGS; 35 36 43 44 public class COGSServices { 45 46 public static String module = COGSServices.class.getName(); 47 48 public static int decimals = UtilNumber.getBigDecimalScale("fin_arithmetic.properties", "financial.statements.decimals"); 49 public static int rounding = UtilNumber.getBigDecimalRoundingMode("fin_arithmetic.properties", "financial.statements.rounding"); 50 public static final BigDecimal ZERO = new BigDecimal ("0"); 51 52 public static Map updateProductAverageCost(DispatchContext dctx, Map context) { 53 LocalDispatcher dispatcher = dctx.getDispatcher(); 54 GenericDelegator delegator = dctx.getDelegator(); 55 GenericValue userLogin = (GenericValue) context.get("userLogin"); 56 57 String productId = (String ) context.get("productId"); 58 String organizationPartyId = (String ) context.get("organizationPartyId"); 59 try { 60 BigDecimal inventoryValue = UtilCOGS.getInventoryValueFromItems(productId, organizationPartyId, delegator, dispatcher); 63 64 BigDecimal inventoryQuantity = UtilCOGS.getInventoryQuantityForProduct(productId, organizationPartyId, delegator, dispatcher); 66 67 BigDecimal averageCost = ZERO; 70 if (!(inventoryQuantity.compareTo(ZERO)==0)) { 71 averageCost = inventoryValue.divide(inventoryQuantity, 100, rounding); 72 } else { 73 Debug.logWarning("Inventory quantity is zero for product [" + productId + "], setting average cost to zero", module); 74 } 75 76 Map input = UtilMisc.toMap("productId", productId, "organizationPartyId", organizationPartyId, "userLogin", userLogin); 78 input.put("averageCost", new Double (averageCost.doubleValue())); 79 80 Debug.logInfo("value = " + inventoryValue + " quantity = " + inventoryQuantity + " avg = " +averageCost, module); 81 82 Map serviceResults = dispatcher.runSync("createProductAverageCost", input); 83 if (ServiceUtil.isError(serviceResults)) { 84 return ServiceUtil.returnError("Failed to update product average cost.", null, null, serviceResults); 85 } 86 } catch (GenericEntityException e) { 87 return(ServiceUtil.returnError(e.getMessage())); 88 } catch (GenericServiceException e) { 89 return(ServiceUtil.returnError(e.getMessage())); 90 } 91 return ServiceUtil.returnSuccess(); 92 } 93 94 95 public static Map updateInvoiceAverageCosts(DispatchContext dctx, Map context) { 96 GenericDelegator delegator = dctx.getDelegator(); 97 LocalDispatcher dispatcher = dctx.getDispatcher(); 98 GenericValue userLogin = (GenericValue) context.get("userLogin"); 99 100 String invoiceId = (String ) context.get("invoiceId"); try { 102 GenericValue invoice = delegator.findByPrimaryKeyCache("Invoice", UtilMisc.toMap("invoiceId", invoiceId)); 103 if (invoice.getString("invoiceTypeId").equals("PURCHASE_INVOICE")) { 104 GenericValue organizationParty = invoice.getRelatedOne("Party"); 106 107 List invoiceItems = invoice.getRelatedByAndCache("InvoiceItem", UtilMisc.toMap("invoiceItemTypeId", "PINV_FPROD_ITEM")); 110 for (Iterator iIi = invoiceItems.iterator(); iIi.hasNext(); ) { 111 GenericValue invoiceItem = (GenericValue) iIi.next(); 112 if (invoiceItem.getString("productId") != null) { 113 Debug.logInfo("calling updateProductAverageCost with " + invoiceItem.getString("productId") + " " + organizationParty.getString("partyId"), module); 114 Map result = dispatcher.runSync("updateProductAverageCost", UtilMisc.toMap("organizationPartyId", organizationParty.getString("partyId"), 115 "productId", invoiceItem.getString("productId"), "userLogin", userLogin)); 116 } 117 } 118 } 119 120 return(ServiceUtil.returnSuccess()); 121 } catch (GenericEntityException ex) { 122 return(ServiceUtil.returnError(ex.getMessage())); 123 } catch (GenericServiceException ex) { 124 return(ServiceUtil.returnError(ex.getMessage())); 125 } 126 } 127 128 public static Map updateReceiptAverageCost(DispatchContext dctx, Map context) { 129 GenericDelegator delegator = dctx.getDelegator(); 130 LocalDispatcher dispatcher = dctx.getDispatcher(); 131 GenericValue userLogin = (GenericValue) context.get("userLogin"); 132 133 String receiptId = (String ) context.get("receiptId"); try { 135 GenericValue receipt = delegator.findByPrimaryKeyCache("ShipmentReceipt", UtilMisc.toMap("receiptId", receiptId)); 136 137 GenericValue inventoryItem = receipt.getRelatedOne("InventoryItem"); 139 Map result = dispatcher.runSync("updateProductAverageCost", UtilMisc.toMap("organizationPartyId", inventoryItem.getString("ownerPartyId"), 140 "productId", inventoryItem.getString("productId"), "userLogin", userLogin)); 141 142 return(ServiceUtil.returnSuccess()); 143 } catch (GenericEntityException ex) { 144 return(ServiceUtil.returnError(ex.getMessage())); 145 } catch (GenericServiceException ex) { 146 return(ServiceUtil.returnError(ex.getMessage())); 147 } 148 } 149 150 } 151 | Popular Tags |