📄 cogsservices.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.cogs;import java.util.*;import java.sql.Timestamp;import java.math.BigDecimal;import javolution.util.FastMap;import org.ofbiz.base.util.*;import org.ofbiz.entity.*;import org.ofbiz.entity.condition.*;import org.ofbiz.entity.util.*;import org.ofbiz.service.*;import org.ofbiz.accounting.AccountingException;import org.ofbiz.accounting.util.UtilAccounting;import com.opensourcestrategies.financials.util.UtilCOGS;/** * COGSServices - Services for handling Cost of Goods Sold * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> * @version $Rev: 102 $ * @since 2.2 */public class COGSServices { public static String module = COGSServices.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"); public static final BigDecimal ZERO = new BigDecimal("0"); public static Map updateProductAverageCost(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); String productId = (String) context.get("productId"); String organizationPartyId = (String) context.get("organizationPartyId"); try { // get the inventory value for this product - it is important to use this because sometimes we need to update average cost // even when accounting entries haven't been posted yet BigDecimal inventoryValue = UtilCOGS.getInventoryValueFromItems(productId, organizationPartyId, delegator, dispatcher); // get the quantity of this product BigDecimal inventoryQuantity = UtilCOGS.getInventoryQuantityForProduct(productId, organizationPartyId, delegator, dispatcher); // average cost = value / quantity. this should not be rounded. // TODO: put the # of decimal places in fin_arithmetic.properties BigDecimal averageCost = ZERO; if (!(inventoryQuantity.compareTo(ZERO)==0)) { averageCost = inventoryValue.divide(inventoryQuantity, 100, rounding); } else { Debug.logWarning("Inventory quantity is zero for product [" + productId + "], setting average cost to zero", module); } // create a new product average cost record Map input = UtilMisc.toMap("productId", productId, "organizationPartyId", organizationPartyId, "userLogin", userLogin); input.put("averageCost", new Double(averageCost.doubleValue())); Debug.logInfo("value = " + inventoryValue + " quantity = " + inventoryQuantity + " avg = " +averageCost, module); Map serviceResults = dispatcher.runSync("createProductAverageCost", input); if (ServiceUtil.isError(serviceResults)) { return ServiceUtil.returnError("Failed to update product average cost.", null, null, serviceResults); } } catch (GenericEntityException e) { return(ServiceUtil.returnError(e.getMessage())); } catch (GenericServiceException e) { return(ServiceUtil.returnError(e.getMessage())); } return ServiceUtil.returnSuccess(); } /** Service which updates the average cost for every product on a purchase Invoice */ public static Map updateInvoiceAverageCosts(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); String invoiceId = (String) context.get("invoiceId"); // input parameter try { GenericValue invoice = delegator.findByPrimaryKeyCache("Invoice", UtilMisc.toMap("invoiceId", invoiceId)); if (invoice.getString("invoiceTypeId").equals("PURCHASE_INVOICE")) { // Determine the organization party and get the Party GenericValue organizationParty = invoice.getRelatedOne("Party"); // calls updateProductAverageCost on every invoice item which is a product item and has a productId // TODO: Maybe we should not restrict it to product items, but other types of invoice items as well? In that case, build a Set of productIds? List invoiceItems = invoice.getRelatedByAndCache("InvoiceItem", UtilMisc.toMap("invoiceItemTypeId", "PINV_FPROD_ITEM")); for (Iterator iIi = invoiceItems.iterator(); iIi.hasNext(); ) { GenericValue invoiceItem = (GenericValue) iIi.next(); if (invoiceItem.getString("productId") != null) { Debug.logInfo("calling updateProductAverageCost with " + invoiceItem.getString("productId") + " " + organizationParty.getString("partyId"), module); Map result = dispatcher.runSync("updateProductAverageCost", UtilMisc.toMap("organizationPartyId", organizationParty.getString("partyId"), "productId", invoiceItem.getString("productId"), "userLogin", userLogin)); } } } return(ServiceUtil.returnSuccess()); } catch (GenericEntityException ex) { return(ServiceUtil.returnError(ex.getMessage())); } catch (GenericServiceException ex) { return(ServiceUtil.returnError(ex.getMessage())); } } public static Map updateReceiptAverageCost(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); String receiptId = (String) context.get("receiptId"); // input parameter try { GenericValue receipt = delegator.findByPrimaryKeyCache("ShipmentReceipt", UtilMisc.toMap("receiptId", receiptId)); // update average cost for the productId, ownerPartyId from inventory item GenericValue inventoryItem = receipt.getRelatedOne("InventoryItem"); Map result = dispatcher.runSync("updateProductAverageCost", UtilMisc.toMap("organizationPartyId", inventoryItem.getString("ownerPartyId"), "productId", inventoryItem.getString("productId"), "userLogin", userLogin)); return(ServiceUtil.returnSuccess()); } catch (GenericEntityException ex) { return(ServiceUtil.returnError(ex.getMessage())); } catch (GenericServiceException ex) { return(ServiceUtil.returnError(ex.getMessage())); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -