1 17 package org.openbravo.erpCommon.ad_forms; 18 19 import java.math.*; 20 import javax.servlet.*; 21 import org.apache.log4j.Logger ; 22 import org.openbravo.database.ConnectionProvider; 24 import java.sql.Connection ; 25 26 27 public class ProductInfo{ 28 static Logger log4jProductInfo = Logger.getLogger(ProductInfo.class); 29 32 public ProductInfo (String M_Product_ID, ConnectionProvider conn){ 33 init (M_Product_ID,conn); 34 } public static final BigDecimal ZERO = new BigDecimal("0"); 36 37 public String m_M_Product_ID = ""; 38 public String m_AD_Client_ID = ""; 40 public String m_AD_Org_ID = ""; 41 42 public String m_productType = ""; 43 public String m_ProductCategory = ""; 44 45 public String m_C_RevenueRecognition_ID = ""; 46 47 public String m_C_UOM_ID = ""; 48 public String m_qty = "0"; 49 50 55 private void init (String 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 m_AD_Client_ID = data[0].adClientId; 73 m_AD_Org_ID = data[0].adOrgId; 74 } 75 } 77 84 public Account getAccount(String AcctType, AcctSchema as,ConnectionProvider conn){ 85 if (Integer.parseInt(AcctType) < 1 || Integer.parseInt(AcctType) > 8) 86 return null; 87 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 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 } 123 130 public Account getAccountDefault (String 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 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 } 165 170 public void setQty (String qty, String C_UOM_ID, ConnectionProvider conn){ 171 m_qty = getConvertedQty(qty, C_UOM_ID, m_C_UOM_ID, "Y", conn); if (qty != null && m_qty == null){ log4jProductInfo.warn ("setQty - conversion error - set to " + qty); 174 m_qty = qty; 175 } 176 } 178 187 public static String getConvertedQty (String qty,String C_UOM_From_ID, String C_UOM_To_ID, String StdPrecision, ConnectionProvider conn){ 188 if (qty.equals("") || qty.equals(ZERO) || C_UOM_From_ID.equals(C_UOM_To_ID)) 190 return qty; 191 String 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 } 204 209 public String getProductCosts (AcctSchema as, ConnectionProvider conn, Connection 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 } 226 234 public String getProductItemCost(AcctSchema as, String costType, ConnectionProvider conn, Connection con){ 235 String current = ""; 236 String cost = ""; 237 String 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 if ((costType.equals("") && AcctSchema.COSTING_AVERAGE.equals(cm)) || AcctSchema.COSTING_AVERAGE.equals(costType)) 246 cost = data[0].getField("costaverage"); else if ((costType.equals("") && AcctSchema.COSTING_LASTPO.equals(cm)) || AcctSchema.COSTING_LASTPO.equals(costType)) 252 cost = data[0].getField("pricelastpo"); 253 else cost = data[0].getField("coststandard"); 255 current = data[0].getField("currentcostprice"); 256 257 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 boolean create = (cost.equals("") && current.equals("")); 269 return updateCosts(as, create, conn, con); 270 } 272 281 private String updateCosts (AcctSchema as, boolean create, ConnectionProvider conn, Connection con) 282 { 283 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 String costSource = "PriceList-PO"; 297 String 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 (costs == null || costs.equals("") || costs.equals(ZERO.toString())){ 309 costSource = "Not Found"; 310 costs = "1"; 311 } 312 313 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 } 325 330 private String 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 C_Currency_ID = ""; 338 String PriceList = ""; 339 String PricePO = ""; 340 String 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 if (C_Currency_ID.equals("")) 350 return null; 351 352 String cost = PriceLastPO; if (cost.equals("") || cost.equals(ZERO.toString())) 354 cost = PricePO; 355 if (cost.equals("") || cost.equals(ZERO.toString())) 356 cost = PriceList; 357 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 } 363 369 private String getPriceList (AcctSchema as, boolean onlyPOPriceList, ConnectionProvider conn){ 370 String C_Currency_ID = ""; 371 String PriceList = ""; 372 String PriceStd = ""; 373 String 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 if (C_Currency_ID.equals("")) 388 return ""; 389 390 String price = PriceLimit; if (price.equals("") || price.equals(ZERO.toString())) 392 price = PriceStd; 393 if (price.equals("") || price.equals(ZERO.toString())) 394 price = PriceList; 395 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 } 401 402 403 public static final String ACCTTYPE_P_Revenue = "1"; 404 405 public static final String ACCTTYPE_P_Expense = "2"; 406 407 public static final String ACCTTYPE_P_Asset = "3"; 408 409 public static final String ACCTTYPE_P_Cogs = "4"; 410 411 public static final String ACCTTYPE_P_PPV = "5"; 412 413 public static final String ACCTTYPE_P_IPV = "6"; 414 415 public static final String ACCTTYPE_P_TDiscountRec = "7"; 416 417 public static final String ACCTTYPE_P_TDiscountGrant = "8"; 418 419 420 } | Popular Tags |