KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_forms > ProductInfo


1 /*
2  ******************************************************************************
3  * The contents of this file are subject to the Compiere License Version 1.1
4  * ("License"); You may not use this file except in compliance with the License
5  * You may obtain a copy of the License at http://www.compiere.org/license.html
6  * Software distributed under the License is distributed on an "AS IS" basis,
7  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8  * the specific language governing rights and limitations under the License.
9  * The Original Code is Compiere ERP & CRM Business Solution
10  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
11  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
12  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
13  * Contributor(s): Openbravo SL
14  * Contributions are Copyright (C) 2001-2006 Openbravo S.L.
15  ******************************************************************************
16 */

17 package org.openbravo.erpCommon.ad_forms;
18
19 import java.math.*;
20 import javax.servlet.*;
21 import org.apache.log4j.Logger ;
22 // imports for transactions
23
import org.openbravo.database.ConnectionProvider;
24 import java.sql.Connection JavaDoc;
25
26
27 public class ProductInfo{
28   static Logger log4jProductInfo = Logger.getLogger(ProductInfo.class);
29   /**
30    * Constructor
31    */

32   public ProductInfo (String JavaDoc M_Product_ID, ConnectionProvider conn){
33     init (M_Product_ID,conn);
34   } // ProductInfo
35
public static final BigDecimal ZERO = new BigDecimal("0");
36   /** The Product Key */
37   public String JavaDoc m_M_Product_ID = "";
38   // Product Info
39
public String JavaDoc m_AD_Client_ID = "";
40   public String JavaDoc m_AD_Org_ID = "";
41
42   public String JavaDoc m_productType = "";
43   public String JavaDoc m_ProductCategory = "";
44
45   public String JavaDoc m_C_RevenueRecognition_ID = "";
46
47   public String JavaDoc m_C_UOM_ID = "";
48   public String JavaDoc m_qty = "0";
49
50   /**
51    * Get Product Info (Service, Revenue Recognition).
52    * automatically called by constructor
53    * @param M_Product_ID Product
54    */

55   private void init (String JavaDoc M_Product_ID, ConnectionProvider conn){
56     m_M_Product_ID = M_Product_ID;
57     if (m_M_Product_ID == "")
58       return;
59
60     ProductInfoData[] data = null;
61     try{
62       data = ProductInfoData.select(conn, m_M_Product_ID);
63     }catch(ServletException e){
64       log4jProductInfo.warn(e);
65     }
66     if (data.length==1){
67       m_productType = data[0].producttype;
68       m_ProductCategory = data[0].value;
69       m_C_RevenueRecognition_ID = data[0].cRevenuerecognitionId;
70       m_C_UOM_ID = data[0].cUomId;
71       // reference
72
m_AD_Client_ID = data[0].adClientId;
73       m_AD_Org_ID = data[0].adOrgId;
74     }
75   } // init
76

77   /**
78    * Line Account from Product
79    *
80    * @param AcctType see ACCTTYPE_* (1..8)
81    * @param as Accounting Schema
82    * @return Requested Product Account
83    */

84   public Account getAccount(String JavaDoc AcctType, AcctSchema as,ConnectionProvider conn){
85     if (Integer.parseInt(AcctType) < 1 || Integer.parseInt(AcctType) > 8)
86       return null;
87     // No Product - get Default from Product Category
88
if (m_M_Product_ID.equals(""))
89       return getAccountDefault(AcctType, as, conn);
90     ProductInfoData [] data = null;
91     Account acc =null;
92     try{
93       data = ProductInfoData.selectProductAcct(conn, m_M_Product_ID, as.getC_AcctSchema_ID());
94       if(data==null || data.length == 0) return null;
95       String JavaDoc validCombination_ID = "";
96       switch (Integer.parseInt(AcctType)){
97         case 1: validCombination_ID = data[0].revenue;
98             break;
99         case 2: validCombination_ID = data[0].expense;
100             break;
101         case 3: validCombination_ID = data[0].asset;
102             break;
103         case 4: validCombination_ID = data[0].cogs;
104             break;
105         case 5: validCombination_ID = data[0].purchasepricevariance;
106             break;
107         case 6: validCombination_ID = data[0].invoicepricevariance;
108             break;
109         case 7: validCombination_ID = data[0].discountrec;
110             break;
111         case 8: validCombination_ID = data[0].discountgrant;
112             break;
113       }
114       if (validCombination_ID.equals(""))
115         return null;
116       acc = Account.getAccount(conn,validCombination_ID);
117     }catch(ServletException e){
118       log4jProductInfo.warn(e);
119     }
120     return acc;
121   } // getAccount
122

123   /**
124    * Account from Default Product Category
125    *
126    * @param AcctType see ACCTTYPE_* (1..8)
127    * @param as accounting schema
128    * @return Requested Product Account
129    */

130   public Account getAccountDefault (String JavaDoc AcctType, AcctSchema as,ConnectionProvider conn){
131     if (Integer.parseInt(AcctType) < 1 || Integer.parseInt(AcctType) > 8)
132       return null;
133     ProductInfoData [] data = null;
134     Account acct = null;
135     try{
136       data = ProductInfoData.selectDefaultAcct(conn, as.getC_AcctSchema_ID());
137       String JavaDoc validCombination_ID = "";
138       switch (Integer.parseInt(AcctType)){
139         case 1: validCombination_ID = data[0].revenue;
140             break;
141         case 2: validCombination_ID = data[0].expense;
142             break;
143         case 3: validCombination_ID = data[0].asset;
144             break;
145         case 4: validCombination_ID = data[0].cogs;
146             break;
147         case 5: validCombination_ID = data[0].purchasepricevariance;
148             break;
149         case 6: validCombination_ID = data[0].invoicepricevariance;
150             break;
151         case 7: validCombination_ID = data[0].discountrec;
152             break;
153         case 8: validCombination_ID = data[0].discountgrant;
154             break;
155       }
156       if (validCombination_ID.equals(""))
157         return null;
158       acct = Account.getAccount(conn,validCombination_ID);
159   }catch(ServletException e){
160     log4jProductInfo.warn(e);
161   }
162     return acct;
163   } // getAccountDefault
164

165   /**
166    * Set Quantity in UOM
167    * @param qty quantity
168    * @param C_UOM_ID UOM
169    */

170   public void setQty (String JavaDoc qty, String JavaDoc C_UOM_ID, ConnectionProvider conn){
171     m_qty = getConvertedQty(qty, C_UOM_ID, m_C_UOM_ID, "Y", conn); // StdPrecision
172
if (qty != null && m_qty == null){ // conversion error
173
log4jProductInfo.warn ("setQty - conversion error - set to " + qty);
174       m_qty = qty;
175     }
176   } // setQty
177

178 /**
179  * Get Converted Qty
180  * @param qty The quantity to be converted
181  * @param C_UOM_From_ID The C_UOM_ID of the qty
182  * @param C_UOM_To_ID The targeted UOM
183  * @param StdPrecision if true, standard precision, if false costing precision
184  * @return amount
185  * @depreciated should not be used
186  */

187 public static String JavaDoc getConvertedQty (String JavaDoc qty,String JavaDoc C_UOM_From_ID, String JavaDoc C_UOM_To_ID, String JavaDoc StdPrecision, ConnectionProvider conn){
188   // Nothing to do
189
if (qty.equals("") || qty.equals(ZERO) || C_UOM_From_ID.equals(C_UOM_To_ID))
190     return qty;
191   //
192
String JavaDoc retValue = "";
193   ProductInfoData [] data = null;
194   try{
195     data = ProductInfoData.UOMConvert(conn, qty, C_UOM_From_ID, C_UOM_To_ID, StdPrecision);
196   }catch (ServletException e){
197     log4jProductInfo.warn(e);
198     return null;
199   }
200   retValue = data[0].converted;
201   return retValue;
202 } // getConvertedQty
203

204 /**
205  * Get Total Costs in Accounting Schema Currency
206  * @param as accounting schema
207  * @return cost or null, if qty or costs cannot be determined
208  */

209 public String JavaDoc getProductCosts (AcctSchema as, ConnectionProvider conn, Connection JavaDoc con){
210   if (m_qty == null || m_qty.equals("")){
211     log4jProductInfo.debug("getProductCosts - No Qty");
212     return null;
213   }
214   BigDecimal cost = new BigDecimal(getProductItemCost(as, "", conn, con));
215   if (cost == null)
216   {
217     log4jProductInfo.debug("getProductCosts - No Costs");
218     return null;
219   }
220   log4jProductInfo.debug("getProductCosts - qty = " + m_qty);
221   BigDecimal qty = new BigDecimal(m_qty);
222   log4jProductInfo.debug("getProductCosts - Qty(" + m_qty + ") * Cost(" + cost + ") = " + qty.multiply(cost));
223   return qty.multiply(cost).toString();
224 } // getProductCosts
225

226 /**
227  * Get Product Costs per UOM for Accounting Schema in Accounting Schema Currency.
228  * - if costType defined - cost
229  * - else CurrentCosts
230  * @param as accounting schema
231  * @param costType - if null uses Accounting Schema Costs - see AcctSchema.COSTING_*
232  * @return product costs
233  */

234 public String JavaDoc getProductItemCost(AcctSchema as, String JavaDoc costType, ConnectionProvider conn, Connection JavaDoc con){
235   String JavaDoc current = "";
236   String JavaDoc cost = "";
237   String JavaDoc cm = as.m_CostingMethod;
238   ProductInfoData [] data = null;
239   try{
240     data = ProductInfoData.selectProductCost(conn, m_M_Product_ID, as.getC_AcctSchema_ID());
241   }catch(ServletException e){
242     log4jProductInfo.warn(e);
243   }
244   //
245
if ((costType.equals("") && AcctSchema.COSTING_AVERAGE.equals(cm)) || AcctSchema.COSTING_AVERAGE.equals(costType))
246     cost = data[0].getField("costaverage"); // 2
247
// else if (AcctSchema.COSTING_FIFO.equals(cm))
248
// cost = data[0].getField("costfifo");
249
// else if (AcctSchema.COSTING_LIFO.equals(cm))
250
// cost = data[0].getField("costlifo");
251
else if ((costType.equals("") && AcctSchema.COSTING_LASTPO.equals(cm)) || AcctSchema.COSTING_LASTPO.equals(costType))
252     cost = data[0].getField("pricelastpo");
253   else // AcctSchema.COSTING_STANDARD
254
cost = data[0].getField("coststandard");
255   current = data[0].getField("currentcostprice");
256
257   // Return Costs
258
if (costType.equals("") && !cost.equals("") && !cost.equals(ZERO.toString())){
259     log4jProductInfo.debug("getProductItemCosts = " + cost);
260     return cost;
261   }
262   else if (!current.equals("") && !current.equals(ZERO.toString())){
263     log4jProductInfo.debug("getProductItemCosts - Current=" + current);
264     return current;
265   }
266
267   // Create/Update Cost Record
268
boolean create = (cost.equals("") && current.equals(""));
269   return updateCosts(as, create, conn, con);
270 } // getProductCost
271

272 /**
273  * Update/Create initial Cost Record.
274  * Check first for Purchase Price List,
275  * then Product Purchase Costs
276  * and then Price List
277  * @param as accounting schema
278  * @param create create record
279  * @return costs
280  */

281 private String JavaDoc updateCosts (AcctSchema as, boolean create, ConnectionProvider conn, Connection JavaDoc con)
282 {
283   // Create Zero Record
284
if (create){
285     int no = 0;
286     try{
287       no = ProductInfoData.insertProductCosting(con,conn,m_M_Product_ID,as.getC_AcctSchema_ID(),m_AD_Client_ID,m_AD_Org_ID);
288     }catch(ServletException e){
289       log4jProductInfo.warn(e);
290     }
291     if (no == 1)
292       log4jProductInfo.debug("updateCosts - CostingCreated");
293   }
294
295   // Try to find non ZERO Price
296
String JavaDoc costSource = "PriceList-PO";
297   String JavaDoc costs = getPriceList (as, true, conn);
298   if (costs.equals("") || costs.equals(ZERO.toString())){
299     costSource = "PO Cost";
300     costs = getPOCost(as, conn);
301   }
302   if (costs == null || costs.equals("") || costs.equals(ZERO.toString())){
303     costSource = "PriceList";
304     costs = getPriceList(as, false,conn);
305   }
306
307   // if not found use $1 (to be able to do material transactions)
308
if (costs == null || costs.equals("") || costs.equals(ZERO.toString())){
309     costSource = "Not Found";
310     costs = "1";
311   }
312
313   // update current costs
314
int no = 0;
315   try{
316     no = ProductInfoData.updateProductCosting(con, conn, costs, m_M_Product_ID, as.getC_AcctSchema_ID());
317   }catch(ServletException e){
318     log4jProductInfo.debug(e);
319   }
320   if (no == 1)
321     log4jProductInfo.debug("updateCosts - " + costSource + " - " + costs);
322   return costs;
323 } // createCosts
324

325 /**
326  * Get PO Cost from Purchase Info - and convert it to AcctSchema Currency
327  * @param as accounting schema
328  * @return po cost
329  */

330 private String JavaDoc getPOCost (AcctSchema as, ConnectionProvider conn){
331   ProductInfoData [] data = null;
332   try{
333     data = ProductInfoData.selectPOCost(conn, m_M_Product_ID);
334   }catch(ServletException e){
335     log4jProductInfo.warn(e);
336   }
337     String JavaDoc C_Currency_ID = "";
338     String JavaDoc PriceList = "";
339     String JavaDoc PricePO = "";
340     String JavaDoc PriceLastPO = "";
341     if (data.length!=0){
342       C_Currency_ID = data[0].cCurrencyId;
343       PriceList = data[0].pricelist;
344       PricePO = data[0].pricepo;
345       PriceLastPO = data[0].pricelastpo;
346     }else return null;
347     log4jProductInfo.debug("getPOCost - data[0].cCurrencyId: - " + data[0].cCurrencyId);
348   // nothing found
349
if (C_Currency_ID.equals(""))
350     return null;
351
352   String JavaDoc cost = PriceLastPO; // best bet
353
if (cost.equals("") || cost.equals(ZERO.toString()))
354     cost = PricePO;
355   if (cost.equals("") || cost.equals(ZERO.toString()))
356     cost = PriceList;
357   // Convert - standard precision!! - should be costing precision
358
if (!cost.equals("") && !cost.equals(ZERO.toString()))
359     cost = AcctServer.getConvertedAmt(cost, C_Currency_ID, as.getC_Currency_ID(), m_AD_Client_ID, m_AD_Org_ID, conn);
360   return cost;
361 } // getPOCost
362

363 /**
364  * Get PO Price from PriceList - and convert it to AcctSchema Currency
365  * @param as accounting schema
366  * @param onlyPOPriceList use only PO price list
367  * @return po price
368  */

369 private String JavaDoc getPriceList (AcctSchema as, boolean onlyPOPriceList, ConnectionProvider conn){
370   String JavaDoc C_Currency_ID = "";
371   String JavaDoc PriceList = "";
372   String JavaDoc PriceStd = "";
373   String JavaDoc PriceLimit = "";
374   ProductInfoData [] data = null;
375   try{
376     data = ProductInfoData.selectPriceList(conn, m_M_Product_ID, onlyPOPriceList?"onlyPOPriceList":"");
377   }catch(ServletException e){
378     log4jProductInfo.warn(e);
379   }
380   if (data.length==1){
381     C_Currency_ID = data[0].getField("cCurrencyId");
382     PriceList = data[0].getField("pricelist");
383     PriceStd = data[0].getField("pricestd");
384     PriceLimit = data[0].getField("pricelimit");
385   }
386   // nothing found
387
if (C_Currency_ID.equals(""))
388     return "";
389
390   String JavaDoc price = PriceLimit; // best bet
391
if (price.equals("") || price.equals(ZERO.toString()))
392     price = PriceStd;
393   if (price.equals("") || price.equals(ZERO.toString()))
394     price = PriceList;
395   // Convert
396
if (!price.equals("") && !price.equals(ZERO.toString()))
397     price = AcctServer.getConvertedAmt(price, C_Currency_ID, as.m_C_Currency_ID, as.m_AD_Client_ID, "", conn);
398   return price;
399 } // getPOPrice
400

401
402   /** Product Revenue Acct */
403   public static final String JavaDoc ACCTTYPE_P_Revenue = "1";
404   /** Product Expense Acct */
405   public static final String JavaDoc ACCTTYPE_P_Expense = "2";
406   /** Product Asset Acct */
407   public static final String JavaDoc ACCTTYPE_P_Asset = "3";
408   /** Product COGS Acct */
409   public static final String JavaDoc ACCTTYPE_P_Cogs = "4";
410   /** Purchase Price Variance */
411   public static final String JavaDoc ACCTTYPE_P_PPV = "5";
412   /** Invoice Price Variance */
413   public static final String JavaDoc ACCTTYPE_P_IPV = "6";
414   /** Trade Discount Revenue */
415   public static final String JavaDoc ACCTTYPE_P_TDiscountRec = "7";
416   /** Trade Discount Costs */
417   public static final String JavaDoc ACCTTYPE_P_TDiscountGrant = "8";
418
419
420 } // ProductInfo
421
Popular Tags