KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > financials > cogs > COGSServices


1 /*
2  * Copyright (c) 2006 - 2007 Open Source Strategies, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the Honest Public License.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * Honest Public License for more details.
11  *
12  * You should have received a copy of the Honest Public License
13  * along with this program; if not, write to Funambol,
14  * 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USA
15  */

16
17 package com.opensourcestrategies.financials.cogs;
18
19 import java.util.*;
20 import java.sql.Timestamp JavaDoc;
21 import java.math.BigDecimal JavaDoc;
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 /**
37  * COGSServices - Services for handling Cost of Goods Sold
38  *
39  * @author <a HREF="mailto:leon@opensourcestrategies.com">Leon Torres</a>
40  * @version $Rev: 102 $
41  * @since 2.2
42  */

43
44 public class COGSServices {
45     
46     public static String JavaDoc 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 JavaDoc ZERO = new BigDecimal JavaDoc("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 JavaDoc productId = (String JavaDoc) context.get("productId");
58         String JavaDoc organizationPartyId = (String JavaDoc) context.get("organizationPartyId");
59         try {
60             // get the inventory value for this product - it is important to use this because sometimes we need to update average cost
61
// even when accounting entries haven't been posted yet
62
BigDecimal JavaDoc inventoryValue = UtilCOGS.getInventoryValueFromItems(productId, organizationPartyId, delegator, dispatcher);
63
64             // get the quantity of this product
65
BigDecimal JavaDoc inventoryQuantity = UtilCOGS.getInventoryQuantityForProduct(productId, organizationPartyId, delegator, dispatcher);
66
67             // average cost = value / quantity. this should not be rounded.
68
// TODO: put the # of decimal places in fin_arithmetic.properties
69
BigDecimal JavaDoc 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             // create a new product average cost record
77
Map input = UtilMisc.toMap("productId", productId, "organizationPartyId", organizationPartyId, "userLogin", userLogin);
78             input.put("averageCost", new Double JavaDoc(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     /** Service which updates the average cost for every product on a purchase Invoice */
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 JavaDoc invoiceId = (String JavaDoc) context.get("invoiceId"); // input parameter
101
try {
102             GenericValue invoice = delegator.findByPrimaryKeyCache("Invoice", UtilMisc.toMap("invoiceId", invoiceId));
103             if (invoice.getString("invoiceTypeId").equals("PURCHASE_INVOICE")) {
104                 // Determine the organization party and get the Party
105
GenericValue organizationParty = invoice.getRelatedOne("Party");
106
107                 // calls updateProductAverageCost on every invoice item which is a product item and has a productId
108
// 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?
109
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 JavaDoc receiptId = (String JavaDoc) context.get("receiptId"); // input parameter
134
try {
135             GenericValue receipt = delegator.findByPrimaryKeyCache("ShipmentReceipt", UtilMisc.toMap("receiptId", receiptId));
136             
137             // update average cost for the productId, ownerPartyId from inventory item
138
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