diff --git a/org.adempiere.base.process/src/org/adempiere/process/ApplyMigrationScripts.java b/org.adempiere.base.process/src/org/adempiere/process/ApplyMigrationScripts.java index c2d1c7db58..77692c7f1b 100644 --- a/org.adempiere.base.process/src/org/adempiere/process/ApplyMigrationScripts.java +++ b/org.adempiere.base.process/src/org/adempiere/process/ApplyMigrationScripts.java @@ -44,67 +44,73 @@ import org.compiere.util.DB; public class ApplyMigrationScripts extends SvrProcess { /** Logger */ - private static CLogger log = CLogger - .getCLogger(ApplyMigrationScripts.class); + private static CLogger log = CLogger.getCLogger(ApplyMigrationScripts.class); @Override protected String doIt() throws Exception { - // TODO Auto-generated method stub log.info("Applying migrations scripts"); StringBuilder sql = new StringBuilder() .append("select ad_migrationscript_id, script, name from ad_migrationscript where isApply = 'Y' and status = 'IP' order by name, created"); - PreparedStatement pstmt = DB.prepareStatement(sql.toString(), this.get_TrxName()); - ResultSet rs = pstmt.executeQuery(); - while (rs.next()) { - byte[] scriptArray = rs.getBytes(2); - int seqID = rs.getInt(1); - boolean execOk = true; - try { - StringBuilder tmpSql = new StringBuilder(new String(scriptArray)); - - if (tmpSql.length() > 0) { - log.info("Executing script " + rs.getString(3)); - execOk = executeScript(tmpSql.toString(), rs.getString(3)); - System.out.println(); - } - } catch (SQLException e) { - execOk = false; - e.printStackTrace(); - StringBuilder msglog = new StringBuilder("Script: ").append(rs.getString(3)).append(" - ").append(e.getMessage()); - log.saveError("Error", msglog.toString()); - log.severe(e.getMessage()); - } finally { - sql = new StringBuilder("UPDATE ad_migrationscript SET status = ? , isApply = 'N' WHERE ad_migrationscript_id = ? "); - pstmt = DB.prepareStatement(sql.toString(), this.get_TrxName()); - if (execOk) { - pstmt.setString(1, "CO"); - pstmt.setInt(2, seqID); - } else { - pstmt.setString(1, "ER"); - pstmt.setInt(2, seqID); - } + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = DB.prepareStatement(sql.toString(), this.get_TrxName()); + rs = pstmt.executeQuery(); + while (rs.next()) { + byte[] scriptArray = rs.getBytes(2); + int seqID = rs.getInt(1); + boolean execOk = true; try { - pstmt.executeUpdate(); - if (!execOk) { - pstmt.close(); - return null; + StringBuilder tmpSql = new StringBuilder(new String(scriptArray)); + + if (tmpSql.length() > 0) { + log.info("Executing script " + rs.getString(3)); + execOk = executeScript(tmpSql.toString(), rs.getString(3)); + System.out.println(); } } catch (SQLException e) { + execOk = false; e.printStackTrace(); StringBuilder msglog = new StringBuilder("Script: ").append(rs.getString(3)).append(" - ").append(e.getMessage()); log.saveError("Error", msglog.toString()); log.severe(e.getMessage()); + } finally { + sql = new StringBuilder("UPDATE ad_migrationscript SET status = ? , isApply = 'N' WHERE ad_migrationscript_id = ? "); + PreparedStatement pstmtu = DB.prepareStatement(sql.toString(), this.get_TrxName()); + if (execOk) { + pstmtu.setString(1, "CO"); + pstmtu.setInt(2, seqID); + } else { + pstmtu.setString(1, "ER"); + pstmtu.setInt(2, seqID); + } + try { + pstmtu.executeUpdate(); + if (!execOk) { + return null; + } + } catch (SQLException e) { + e.printStackTrace(); + StringBuilder msglog = new StringBuilder("Script: ").append(rs.getString(3)).append(" - ").append(e.getMessage()); + log.saveError("Error", msglog.toString()); + log.severe(e.getMessage()); + } finally { + DB.close(pstmtu); + pstmtu = null; + } } } + } catch (SQLException e) { + throw e; + } finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } - rs.close(); - pstmt.close(); return null; } @Override protected void prepare() { - // TODO Auto-generated method stub } @@ -156,7 +162,8 @@ public class ApplyMigrationScripts extends SvrProcess { log.saveError("Error", msglog.toString()); log.severe(e.getMessage()); } finally { - if (stmt != null)stmt.close(); + DB.close(stmt); + stmt = null; if(execOk) conn.commit(); else diff --git a/org.adempiere.base.process/src/org/compiere/process/M_PriceList_Create.java b/org.adempiere.base.process/src/org/compiere/process/M_PriceList_Create.java index 4f2dfc2491..324e9181e0 100644 --- a/org.adempiere.base.process/src/org/compiere/process/M_PriceList_Create.java +++ b/org.adempiere.base.process/src/org/compiere/process/M_PriceList_Create.java @@ -42,7 +42,7 @@ import org.compiere.util.ValueNamePair; * @version $Id: M_PriceList_Create,v 1.0 2005/10/09 22:19:00 * globalqss Exp $ * @author Carlos Ruiz (globalqss) - * Make T_Selection tables permanent + * Make T_Selection tables permanent */ public class M_PriceList_Create extends SvrProcess { @@ -92,9 +92,9 @@ public class M_PriceList_Create extends SvrProcess { int toti = 0; @SuppressWarnings("unused") int totd = 0; - int V_temp; + int v_temp; int v_NextNo = 0; - StringBuilder Message = new StringBuilder(); + StringBuilder message = new StringBuilder(); // //Checking Prerequisites //PO Prices must exists @@ -171,49 +171,52 @@ public class M_PriceList_Create extends SvrProcess { sql.append(" AND IsCurrentVendor='Y' AND IsActive='Y' "); sql.append(" GROUP BY M_Product_ID ").append(" HAVING COUNT(*) > 1 ) "); - PreparedStatement Cur_Duplicates = null; - Cur_Duplicates = DB.prepareStatement(sql.toString(), get_TrxName()); - ResultSet dupl = Cur_Duplicates.executeQuery(); - while (dupl.next()) { - sql = new StringBuilder("SELECT M_Product_ID ,C_BPartner_ID "); - sql.append(" FROM M_Product_PO WHERE IsCurrentVendor = 'Y' "); - sql.append(" AND IsActive = 'Y' "); - sql.append(" AND M_Product_ID = ").append(dupl.getInt("M_Product_ID")); - sql.append(" ORDER BY PriceList DESC"); + PreparedStatement stmtDupl = null; + ResultSet rsDupl = null; + PreparedStatement stmtVendors = null; + ResultSet rsVend = null; + try { + stmtDupl = DB.prepareStatement(sql.toString(), get_TrxName()); + rsDupl = stmtDupl.executeQuery(); + while (rsDupl.next()) { + sql = new StringBuilder("SELECT M_Product_ID ,C_BPartner_ID "); + sql.append(" FROM M_Product_PO WHERE IsCurrentVendor = 'Y' "); + sql.append(" AND IsActive = 'Y' "); + sql.append(" AND M_Product_ID = ").append(rsDupl.getInt("M_Product_ID")); + sql.append(" ORDER BY PriceList DESC"); - PreparedStatement Cur_Vendors = null; - Cur_Vendors = DB.prepareStatement(sql.toString(), get_TrxName()); - ResultSet Vend = Cur_Vendors.executeQuery(); + stmtVendors = DB.prepareStatement(sql.toString(), get_TrxName()); + rsVend = stmtVendors.executeQuery(); - // - // Leave First - // - Vend.next(); - - while (Vend.next()) { - sqlupd = new StringBuilder("UPDATE M_Product_PO "); - sqlupd.append(" SET IsCurrentVendor = 'N' "); - sqlupd.append(" WHERE M_Product_ID= ").append(Vend.getInt("M_Product_ID")); - sqlupd.append(" AND C_BPartner_ID= "); - sqlupd.append(Vend.getInt("C_BPartner_ID")); + // + // Leave First + // + rsVend.next(); - cntu = DB.executeUpdate(sqlupd.toString(), get_TrxName()); - if (cntu == -1) - raiseError( - "Update IsCurrentVendor to N of M_Product_PO for a M_Product_ID and C_BPartner_ID ingresed", - sqlupd.toString()); - totu += cntu; - log.fine("Updated " + cntu); - + while (rsVend.next()) { + sqlupd = new StringBuilder("UPDATE M_Product_PO "); + sqlupd.append(" SET IsCurrentVendor = 'N' "); + sqlupd.append(" WHERE M_Product_ID= ").append(rsVend.getInt("M_Product_ID")); + sqlupd.append(" AND C_BPartner_ID= "); + sqlupd.append(rsVend.getInt("C_BPartner_ID")); + + cntu = DB.executeUpdate(sqlupd.toString(), get_TrxName()); + if (cntu == -1) + raiseError( + "Update IsCurrentVendor to N of M_Product_PO for a M_Product_ID and C_BPartner_ID ingresed", + sqlupd.toString()); + totu += cntu; + log.fine("Updated " + cntu); + } } - Vend.close(); - Cur_Vendors.close(); - Cur_Vendors = null; - + } catch (SQLException e) { + throw e; + } finally { + DB.close(rsDupl, stmtDupl); + rsDupl = null; stmtDupl = null; + DB.close(rsVend, stmtVendors); + rsVend = null; stmtVendors = null; } - dupl.close(); - Cur_Duplicates.close(); - Cur_Duplicates = null; // DB.commit(true, get_TrxName()); @@ -228,7 +231,7 @@ public class M_PriceList_Create extends SvrProcess { if (cntd == -1) raiseError(" DELETE M_ProductPrice ", sqldel.toString()); totd += cntd; - Message = new StringBuilder("@Deleted@=").append(cntd).append(" - "); + message = new StringBuilder("@Deleted@=").append(cntd).append(" - "); log.fine("Deleted " + cntd); } // @@ -243,476 +246,486 @@ public class M_PriceList_Create extends SvrProcess { sql.append(" AND p.C_Currency_ID = c.C_Currency_ID"); sql.append(" AND v.M_PriceList_Version_ID = ").append(p_PriceList_Version_ID); - PreparedStatement curgen = null; - curgen = DB.prepareStatement(sql.toString(), get_TrxName()); - ResultSet v = curgen.executeQuery(); - while (v.next()) { - // - // For All Discount Lines in Sequence - // - sql = new StringBuilder("SELECT m_discountschemaline_id"); - sql.append(",ad_client_id,ad_org_id,isactive,created,createdby,updated,updatedby"); - sql.append(",m_discountschema_id,seqno,m_product_category_id,c_bpartner_id,m_product_id"); - sql.append(",conversiondate,list_base,list_addamt,list_discount,list_rounding,list_minamt"); - sql.append(",list_maxamt,list_fixed,std_base,std_addamt,std_discount,std_rounding"); - sql.append(",std_minamt,std_maxamt,std_fixed,limit_base,limit_addamt,limit_discount"); - sql.append(",limit_rounding,limit_minamt,limit_maxamt,limit_fixed,group1,group2,c_conversiontype_id"); - sql.append(" FROM M_DiscountSchemaLine"); - sql.append(" WHERE M_DiscountSchema_ID="); - sql.append(v.getInt("M_DiscountSchema_ID")); - sql.append(" AND IsActive='Y' ORDER BY SeqNo"); - - PreparedStatement Cur_DiscountLine = null; - Cur_DiscountLine = DB.prepareStatement(sql.toString(), get_TrxName()); - ResultSet dl = Cur_DiscountLine.executeQuery(); - while (dl.next()) { + PreparedStatement stmtCurgen = null; + ResultSet rsCurgen = null; + PreparedStatement stmtDiscountLine = null; + ResultSet rsDiscountLine = null; + CPreparedStatement stmt = null; + PreparedStatement pstmt = null; + try { + stmtCurgen = DB.prepareStatement(sql.toString(), get_TrxName()); + rsCurgen = stmtCurgen.executeQuery(); + while (rsCurgen.next()) { // - //Clear Temporary Table + // For All Discount Lines in Sequence // - sqldel = new StringBuilder("DELETE FROM T_Selection WHERE AD_PInstance_ID="); - sqldel.append(m_AD_PInstance_ID); - + sql = new StringBuilder("SELECT m_discountschemaline_id"); + sql.append(",ad_client_id,ad_org_id,isactive,created,createdby,updated,updatedby"); + sql.append(",m_discountschema_id,seqno,m_product_category_id,c_bpartner_id,m_product_id"); + sql.append(",conversiondate,list_base,list_addamt,list_discount,list_rounding,list_minamt"); + sql.append(",list_maxamt,list_fixed,std_base,std_addamt,std_discount,std_rounding"); + sql.append(",std_minamt,std_maxamt,std_fixed,limit_base,limit_addamt,limit_discount"); + sql.append(",limit_rounding,limit_minamt,limit_maxamt,limit_fixed,group1,group2,c_conversiontype_id"); + sql.append(" FROM M_DiscountSchemaLine"); + sql.append(" WHERE M_DiscountSchema_ID="); + sql.append(rsCurgen.getInt("M_DiscountSchema_ID")); + sql.append(" AND IsActive='Y' ORDER BY SeqNo"); + + stmtDiscountLine = DB.prepareStatement(sql.toString(), get_TrxName()); + rsDiscountLine = stmtDiscountLine.executeQuery(); + while (rsDiscountLine.next()) { + // + //Clear Temporary Table + // + sqldel = new StringBuilder("DELETE FROM T_Selection WHERE AD_PInstance_ID="); + sqldel.append(m_AD_PInstance_ID); + + cntd = DB.executeUpdate(sqldel.toString(), get_TrxName()); + if (cntd == -1) + raiseError(" DELETE T_Selection ", sqldel.toString()); + totd += cntd; + log.fine("Deleted " + cntd); + // + //Create Selection in temporary table + // + v_temp = rsCurgen.getInt("M_PriceList_Version_Base_ID"); + String dl_Group1 = rsDiscountLine.getString("Group1"); + String dl_Group2 = rsDiscountLine.getString("Group2"); + if (rsCurgen.wasNull()) { + // + //Create Selection from M_Product_PO + // + sqlins.append("INSERT INTO T_Selection (AD_PInstance_ID, T_Selection_ID) "); + sqlins.append( " SELECT DISTINCT ").append(m_AD_PInstance_ID).append(", po.M_Product_ID "); + sqlins.append(" FROM M_Product p, M_Product_PO po"); + sqlins.append(" WHERE p.M_Product_ID=po.M_Product_ID "); + sqlins.append(" AND (p.AD_Client_ID=").append(rsCurgen.getInt("AD_Client_ID")).append(" OR p.AD_Client_ID=0)"); + sqlins.append(" AND p.IsActive='Y' AND po.IsActive='Y' AND po.IsCurrentVendor='Y' "); + // + //Optional Restrictions + // + // globalqss - detected bug, JDBC returns zero for null values + // so we're going to use NULLIF(value, 0) + sqlins.append(" AND (NULLIF(").append(rsDiscountLine.getInt("M_Product_Category_ID")).append(",0) IS NULL"); + sqlins.append(" OR p.M_Product_Category_ID IN (").append(getSubCategoryWhereClause(rsDiscountLine.getInt("M_Product_Category_ID"))) + .append("))"); + if(dl_Group1 != null) + sqlins.append(" AND (p.Group1=?)"); + if (dl_Group2 != null) + sqlins.append(" AND (p.Group2=?)"); + sqlins.append(" AND (NULLIF(").append(rsDiscountLine.getInt("C_BPartner_ID")).append(",0) IS NULL "); + sqlins.append(" OR po.C_BPartner_ID=").append(rsDiscountLine.getInt("C_BPartner_ID")).append(")"); + sqlins.append(" AND (NULLIF(").append(rsDiscountLine.getInt("M_Product_ID")).append(",0) IS NULL "); + sqlins.append(" OR p.M_Product_ID=").append(rsDiscountLine.getInt("M_Product_ID")).append(")"); + + stmt = DB.prepareStatement(sqlins.toString(), get_TrxName()); + + int i = 1; + + if (dl_Group1!=null) + stmt.setString(i++, dl_Group1); + if (dl_Group2!=null) + stmt.setString(i++, dl_Group2); + + cnti = stmt.executeUpdate(); + + if (cnti == -1) + raiseError(" INSERT INTO T_Selection ", sqlins.toString()); + toti += cnti; + log.fine("Inserted " + cnti); + + } else { + // + // Create Selection from existing PriceList + // + sqlins = new StringBuilder("INSERT INTO T_Selection (AD_PInstance_ID, T_Selection_ID)"); + sqlins.append(" SELECT DISTINCT ").append(m_AD_PInstance_ID).append(", p.M_Product_ID"); + sqlins.append(" FROM M_Product p, M_ProductPrice pp"); + sqlins.append(" WHERE p.M_Product_ID=pp.M_Product_ID"); + sqlins.append(" AND pp.M_PriceList_Version_ID = ").append(rsCurgen.getInt("M_PriceList_Version_Base_ID")); + sqlins.append(" AND p.IsActive='Y' AND pp.IsActive='Y'"); + // + //Optional Restrictions + // + sqlins.append(" AND (NULLIF(").append(rsDiscountLine.getInt("M_Product_Category_ID")).append(",0) IS NULL"); + sqlins.append(" OR p.M_Product_Category_ID IN (").append(getSubCategoryWhereClause(rsDiscountLine.getInt("M_Product_Category_ID"))) + .append("))"); + if(dl_Group1 != null) + sqlins.append(" AND (p.Group1=?)"); + if (dl_Group2 != null) + sqlins.append(" AND (p.Group2=?)"); + sqlins.append(" AND (NULLIF(").append(rsDiscountLine.getInt("C_BPartner_ID")).append(",0) IS NULL OR EXISTS "); + sqlins.append("(SELECT m_product_id,c_bpartner_id,ad_client_id,ad_org_id,isactive"); + sqlins.append(",created,createdby,updated,updatedby,iscurrentvendor,c_uom_id"); + sqlins.append(",c_currency_id,pricelist,pricepo,priceeffective,pricelastpo"); + sqlins.append(",pricelastinv,vendorproductno,upc,vendorcategory,discontinued"); + sqlins.append(",discontinuedby,order_min,order_pack,costperorder"); + sqlins.append(",deliverytime_promised,deliverytime_actual,qualityrating"); + sqlins.append(",royaltyamt,group1,group2"); + sqlins.append(",manufacturer FROM M_Product_PO po WHERE po.M_Product_ID=p.M_Product_ID"); + sqlins.append(" AND po.C_BPartner_ID=").append(rsDiscountLine.getInt("C_BPartner_ID")).append("))"); + sqlins.append(" AND (NULLIF(").append(rsDiscountLine.getInt("M_Product_ID")).append(",0) IS NULL "); + sqlins.append(" OR p.M_Product_ID=").append(rsDiscountLine.getInt("M_Product_ID")).append(")"); + + + stmt = DB.prepareStatement(sqlins.toString(), get_TrxName()); + int i = 1; + + if (dl_Group1!=null) + stmt.setString(i++, dl_Group1); + if (dl_Group2!=null) + stmt.setString(i++, dl_Group2); + + cnti = stmt.executeUpdate(); + if (cnti == -1) + raiseError( + " INSERT INTO T_Selection from existing PriceList", + sqlins.toString()); + toti += cnti; + log.fine("Inserted " + cnti); + + } + + message.append("@Selected@=").append(cnti); + + // + //Delete Prices in Selection, so that we can insert + // + v_temp = rsCurgen.getInt("M_PriceList_Version_Base_ID"); + if (rsCurgen.wasNull() || v_temp != p_PriceList_Version_ID) { + + sqldel = new StringBuilder("DELETE M_ProductPrice pp"); + sqldel.append(" WHERE pp.M_PriceList_Version_ID = "); + sqldel.append(p_PriceList_Version_ID); + sqldel.append(" AND EXISTS (SELECT t_selection_id FROM T_Selection s WHERE pp.M_Product_ID=s.T_Selection_ID"); + sqldel.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); + + cntd = DB.executeUpdate(sqldel.toString(), get_TrxName()); + if (cntd == -1) + raiseError(" DELETE M_ProductPrice ", sqldel.toString()); + totd += cntd; + message.append(", @Deleted@=").append(cntd); + log.fine("Deleted " + cntd); + } + + // + // Copy (Insert) Prices + // + v_temp = rsCurgen.getInt("M_PriceList_Version_Base_ID"); + if (v_temp == p_PriceList_Version_ID) + // + // We have Prices already + // + ; + else if (rsCurgen.wasNull()) + // + //Copy and Convert from Product_PO + // + { + sqlins = new StringBuilder("INSERT INTO M_ProductPrice "); + sqlins.append("(M_PriceList_Version_ID"); + sqlins.append(" ,M_Product_ID "); + sqlins.append(" ,AD_Client_ID"); + sqlins.append(" , AD_Org_ID"); + sqlins.append(" , IsActive"); + sqlins.append(" , Created"); + sqlins.append(" , CreatedBy"); + sqlins.append(" , Updated"); + sqlins.append(" , UpdatedBy"); + sqlins.append(" , PriceList"); + sqlins.append(" , PriceStd"); + sqlins.append(" , PriceLimit) "); + sqlins.append("SELECT "); + sqlins.append(p_PriceList_Version_ID); + sqlins.append(" ,po.M_Product_ID "); + sqlins.append(" ,"); + sqlins.append(rsCurgen.getInt("AD_Client_ID")); + sqlins.append(" ,"); + sqlins.append(rsCurgen.getInt("AD_Org_ID")); + sqlins.append(" ,'Y'"); + sqlins.append(" ,SysDate,"); + sqlins.append(rsCurgen.getInt("UpdatedBy")); + sqlins.append(" ,SysDate,"); + sqlins.append(rsCurgen.getInt("UpdatedBy")); + // + //Price List + // + sqlins.append(" ,COALESCE(currencyConvert(po.PriceList, po.C_Currency_ID, "); + sqlins.append(rsCurgen.getInt("C_Currency_ID")); + sqlins.append(", ? , "); + sqlins.append(rsDiscountLine.getInt("C_ConversionType_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Client_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Org_ID")); + sqlins.append("),0)"); + + // Price Std + sqlins.append(" ,COALESCE(currencyConvert(po.PriceList, po.C_Currency_ID, "); + sqlins.append(rsCurgen.getInt("C_Currency_ID")); + sqlins.append(", ? , "); + sqlins.append(rsDiscountLine.getInt("C_ConversionType_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Client_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Org_ID")); + sqlins.append("),0)"); + + // Price Limit + sqlins.append(" ,COALESCE(currencyConvert(po.PricePO ,po.C_Currency_ID, "); + sqlins.append(rsCurgen.getInt("C_Currency_ID")); + sqlins.append(",? , "); + sqlins.append(rsDiscountLine.getInt("C_ConversionType_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Client_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Org_ID")); + sqlins.append("),0)"); + sqlins.append(" FROM M_Product_PO po "); + sqlins.append(" WHERE EXISTS (SELECT * FROM T_Selection s WHERE po.M_Product_ID=s.T_Selection_ID"); + sqlins.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(") "); + sqlins.append(" AND po.IsCurrentVendor='Y' AND po.IsActive='Y'"); + + pstmt = DB.prepareStatement(sqlins.toString(), + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_UPDATABLE, get_TrxName()); + pstmt.setTimestamp(1, rsDiscountLine.getTimestamp("ConversionDate")); + pstmt.setTimestamp(2, rsDiscountLine.getTimestamp("ConversionDate")); + pstmt.setTimestamp(3, rsDiscountLine.getTimestamp("ConversionDate")); + + cnti = pstmt.executeUpdate(); + if (cnti == -1) + raiseError( + " INSERT INTO T_Selection from existing PriceList", + sqlins.toString()); + toti += cnti; + log.fine("Inserted " + cnti); + } else { + // + //Copy and Convert from other PriceList_Version + // + sqlins = new StringBuilder("INSERT INTO M_ProductPrice "); + sqlins.append(" (M_PriceList_Version_ID, M_Product_ID,"); + sqlins.append(" AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,"); + sqlins.append(" PriceList, PriceStd, PriceLimit)"); + sqlins.append(" SELECT "); + sqlins.append(p_PriceList_Version_ID); + sqlins.append(", pp.M_Product_ID,"); + sqlins.append(rsCurgen.getInt("AD_Client_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Org_ID")); + sqlins.append(", 'Y', SysDate, "); + sqlins.append(rsCurgen.getInt("UpdatedBy")); + sqlins.append(", SysDate, "); + sqlins.append(rsCurgen.getInt("UpdatedBy")); + sqlins.append(" ,"); + // Price List + sqlins.append("COALESCE(currencyConvert(pp.PriceList, pl.C_Currency_ID, "); + sqlins.append(rsCurgen.getInt("C_Currency_ID")); + sqlins.append(", ?, "); + sqlins.append(rsDiscountLine.getInt("C_ConversionType_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Client_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Org_ID")); + sqlins.append("),0),"); + // Price Std + sqlins.append("COALESCE(currencyConvert(pp.PriceStd,pl.C_Currency_ID, "); + sqlins.append(rsCurgen.getInt("C_Currency_ID")); + sqlins.append(" , ? , "); + sqlins.append(rsDiscountLine.getInt("C_ConversionType_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Client_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Org_ID")); + sqlins.append("),0),"); + //Price Limit + sqlins.append(" COALESCE(currencyConvert(pp.PriceLimit,pl.C_Currency_ID, "); + sqlins.append(rsCurgen.getInt("C_Currency_ID")); + sqlins.append(" , ? , "); + sqlins.append(rsDiscountLine.getInt("C_ConversionType_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Client_ID")); + sqlins.append(", "); + sqlins.append(rsCurgen.getInt("AD_Org_ID")); + sqlins.append("),0)"); + sqlins.append(" FROM M_ProductPrice pp"); + sqlins.append(" INNER JOIN M_PriceList_Version plv ON (pp.M_PriceList_Version_ID=plv.M_PriceList_Version_ID)"); + sqlins.append(" INNER JOIN M_PriceList pl ON (plv.M_PriceList_ID=pl.M_PriceList_ID)"); + sqlins.append(" WHERE pp.M_PriceList_Version_ID="); + sqlins.append(rsCurgen.getInt("M_PriceList_Version_Base_ID")); + sqlins.append(" AND EXISTS (SELECT * FROM T_Selection s WHERE pp.M_Product_ID=s.T_Selection_ID"); + sqlins.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); + sqlins.append("AND pp.IsActive='Y'"); + + pstmt = DB.prepareStatement(sqlins.toString(), + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_UPDATABLE, get_TrxName()); + pstmt.setTimestamp(1, rsDiscountLine.getTimestamp("ConversionDate")); + pstmt.setTimestamp(2, rsDiscountLine.getTimestamp("ConversionDate")); + pstmt.setTimestamp(3, rsDiscountLine.getTimestamp("ConversionDate")); + + cnti = pstmt.executeUpdate(); + + if (cnti == -1) + raiseError( + " INSERT INTO T_Selection from existing PriceList", + sqlins.toString()); + toti += cnti; + log.fine("Inserted " + cnti); + + } + message.append(", @Inserted@=").append(cnti); + // + // Calculation + // + sqlupd = new StringBuilder("UPDATE M_ProductPrice p "); + sqlupd.append(" SET PriceList = (DECODE( '"); + sqlupd.append(rsDiscountLine.getString("List_Base")); + sqlupd.append("', 'S', PriceStd, 'X', PriceLimit, PriceList)"); + sqlupd.append(" + ?) * (1 - ?/100), PriceStd = (DECODE('"); + sqlupd.append(rsDiscountLine.getString("Std_Base")); + sqlupd.append("', 'L', PriceList, 'X', PriceLimit, PriceStd) "); + sqlupd.append(" + ?) * (1 - ?/100), ").append(" PriceLimit = (DECODE('"); + sqlupd.append(rsDiscountLine.getString("Limit_Base")); + sqlupd.append("', 'L', PriceList, 'S', PriceStd, PriceLimit) "); + sqlupd.append(" + ?) * (1 - ? /100) "); + sqlupd.append(" WHERE M_PriceList_Version_ID = "); + sqlupd.append(p_PriceList_Version_ID); + sqlupd.append(" AND EXISTS (SELECT * FROM T_Selection s "); + sqlupd.append(" WHERE s.T_Selection_ID = p.M_Product_ID"); + sqlupd.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); + + PreparedStatement pstmu = DB.prepareStatement(sqlupd.toString(), + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_UPDATABLE, get_TrxName()); + + pstmu.setDouble(1, rsDiscountLine.getDouble("List_AddAmt")); + pstmu.setDouble(2, rsDiscountLine.getDouble("List_Discount")); + pstmu.setDouble(3, rsDiscountLine.getDouble("Std_AddAmt")); + pstmu.setDouble(4, rsDiscountLine.getDouble("Std_Discount")); + pstmu.setDouble(5, rsDiscountLine.getDouble("Limit_AddAmt")); + pstmu.setDouble(6, rsDiscountLine.getDouble("Limit_Discount")); + + cntu = pstmu.executeUpdate(); + + if (cntu == -1) + raiseError("Update M_ProductPrice ", sqlupd.toString()); + totu += cntu; + log.fine("Updated " + cntu); + + // + //Rounding (AD_Reference_ID=155) + // + sqlupd = new StringBuilder("UPDATE M_ProductPrice p "); + sqlupd.append(" SET PriceList = DECODE('"); + sqlupd.append(rsDiscountLine.getString("List_Rounding")).append("',"); + sqlupd.append(" 'N', PriceList, "); + sqlupd.append(" '0', ROUND(PriceList, 0),"); //Even .00 + sqlupd.append(" 'D', ROUND(PriceList, 1),"); //Dime .10 + sqlupd.append(" 'T', ROUND(PriceList, -1), "); //Ten 10.00 + sqlupd.append(" '5', ROUND(PriceList*20,0)/20,"); //Nickle .05 + sqlupd.append(" 'Q', ROUND(PriceList*4,0)/4,"); //Quarter .25 + sqlupd.append(" '9', CASE"); //Whole 9 or 5 + sqlupd.append(" WHEN MOD(ROUND(PriceList),10)<=5 THEN ROUND(PriceList)+(5-MOD(ROUND(PriceList),10))"); + sqlupd.append(" WHEN MOD(ROUND(PriceList),10)>5 THEN ROUND(PriceList)+(9-MOD(ROUND(PriceList),10)) END,"); + sqlupd.append(" ROUND(PriceList, ").append(rsCurgen.getInt("StdPrecision")); + sqlupd.append(")),");//Currency + sqlupd.append(" PriceStd = DECODE('").append(rsDiscountLine.getString("Std_Rounding")); + sqlupd.append("',").append(" 'N', PriceStd, "); + sqlupd.append(" '0', ROUND(PriceStd, 0), "); //Even .00 + sqlupd.append(" 'D', ROUND(PriceStd, 1), "); //Dime .10 + sqlupd.append("'T', ROUND(PriceStd, -1),"); //Ten 10.00) + sqlupd.append("'5', ROUND(PriceStd*20,0)/20,"); //Nickle .05 + sqlupd.append("'Q', ROUND(PriceStd*4,0)/4,"); //Quarter .25 + sqlupd.append(" '9', CASE"); //Whole 9 or 5 + sqlupd.append(" WHEN MOD(ROUND(PriceStd),10)<=5 THEN ROUND(PriceStd)+(5-MOD(ROUND(PriceStd),10))"); + sqlupd.append(" WHEN MOD(ROUND(PriceStd),10)>5 THEN ROUND(PriceStd)+(9-MOD(ROUND(PriceStd),10)) END,"); + sqlupd.append("ROUND(PriceStd, ").append(rsCurgen.getInt("StdPrecision")).append(")),"); //Currency + sqlupd.append("PriceLimit = DECODE('"); + sqlupd.append(rsDiscountLine.getString("Limit_Rounding")).append("', "); + sqlupd.append(" 'N', PriceLimit, "); + sqlupd.append(" '0', ROUND(PriceLimit, 0), "); // Even .00 + sqlupd.append(" 'D', ROUND(PriceLimit, 1), "); // Dime .10 + sqlupd.append(" 'T', ROUND(PriceLimit, -1), "); // Ten 10.00 + sqlupd.append(" '5', ROUND(PriceLimit*20,0)/20, "); // Nickle .05 + sqlupd.append(" 'Q', ROUND(PriceLimit*4,0)/4, "); //Quarter .25 + sqlupd.append(" '9', CASE"); //Whole 9 or 5 + sqlupd.append(" WHEN MOD(ROUND(PriceLimit),10)<=5 THEN ROUND(PriceLimit)+(5-MOD(ROUND(PriceLimit),10))"); + sqlupd.append(" WHEN MOD(ROUND(PriceLimit),10)>5 THEN ROUND(PriceLimit)+(9-MOD(ROUND(PriceLimit),10)) END,"); + sqlupd.append(" ROUND(PriceLimit, ").append(rsCurgen.getInt("StdPrecision")); + sqlupd.append(")) "); // Currency + sqlupd.append(" WHERE M_PriceList_Version_ID="); + sqlupd.append(p_PriceList_Version_ID); + sqlupd.append(" AND EXISTS (SELECT * FROM T_Selection s "); + sqlupd.append(" WHERE s.T_Selection_ID=p.M_Product_ID"); + sqlupd.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); + + cntu = DB.executeUpdate(sqlupd.toString(), get_TrxName()); + if (cntu == -1) + raiseError("Update M_ProductPrice ", sqlupd.toString()); + totu += cntu; + log.fine("Updated " + cntu); + + message.append(", @Updated@=").append(cntu); + // + //Fixed Price overwrite + // + sqlupd = new StringBuilder("UPDATE M_ProductPrice p "); + sqlupd.append(" SET PriceList = DECODE('"); + sqlupd.append(rsDiscountLine.getString("List_Base")).append("', 'F', "); + sqlupd.append(rsDiscountLine.getDouble("List_Fixed")).append(", PriceList), "); + sqlupd.append(" PriceStd = DECODE('"); + sqlupd.append(rsDiscountLine.getString("Std_Base")).append("', 'F', "); + sqlupd.append(rsDiscountLine.getDouble("Std_Fixed")).append(", PriceStd),"); + sqlupd.append(" PriceLimit = DECODE('"); + sqlupd.append(rsDiscountLine.getString("Limit_Base")).append("', 'F', "); + sqlupd.append(rsDiscountLine.getDouble("Limit_Fixed")).append(", PriceLimit)"); + sqlupd.append(" WHERE M_PriceList_Version_ID="); + sqlupd.append(p_PriceList_Version_ID); + sqlupd.append(" AND EXISTS (SELECT * FROM T_Selection s"); + sqlupd.append(" WHERE s.T_Selection_ID=p.M_Product_ID"); + sqlupd.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); + + cntu = DB.executeUpdate(sqlupd.toString(), get_TrxName()); + if (cntu == -1) + raiseError("Update M_ProductPrice ", sqlupd.toString()); + totu += cntu; + log.fine("Updated " + cntu); + + v_NextNo = v_NextNo + 1; + addLog(0, null, null, message.toString()); + message = new StringBuilder(); + } + // + // Delete Temporary Selection + // + sqldel = new StringBuilder("DELETE FROM T_Selection "); cntd = DB.executeUpdate(sqldel.toString(), get_TrxName()); if (cntd == -1) raiseError(" DELETE T_Selection ", sqldel.toString()); totd += cntd; log.fine("Deleted " + cntd); - // - //Create Selection in temporary table - // - V_temp = v.getInt("M_PriceList_Version_Base_ID"); - String dl_Group1 = dl.getString("Group1"); - String dl_Group2 = dl.getString("Group2"); - if (v.wasNull()) { - // - //Create Selection from M_Product_PO - // - sqlins.append("INSERT INTO T_Selection (AD_PInstance_ID, T_Selection_ID) "); - sqlins.append( " SELECT DISTINCT ").append(m_AD_PInstance_ID).append(", po.M_Product_ID "); - sqlins.append(" FROM M_Product p, M_Product_PO po"); - sqlins.append(" WHERE p.M_Product_ID=po.M_Product_ID "); - sqlins.append(" AND (p.AD_Client_ID=").append(v.getInt("AD_Client_ID")).append(" OR p.AD_Client_ID=0)"); - sqlins.append(" AND p.IsActive='Y' AND po.IsActive='Y' AND po.IsCurrentVendor='Y' "); - // - //Optional Restrictions - // - // globalqss - detected bug, JDBC returns zero for null values - // so we're going to use NULLIF(value, 0) - sqlins.append(" AND (NULLIF(").append(dl.getInt("M_Product_Category_ID")).append(",0) IS NULL"); - sqlins.append(" OR p.M_Product_Category_ID IN (").append(getSubCategoryWhereClause(dl.getInt("M_Product_Category_ID"))) - .append("))"); - if(dl_Group1 != null) - sqlins.append(" AND (p.Group1=?)"); - if (dl_Group2 != null) - sqlins.append(" AND (p.Group2=?)"); - sqlins.append(" AND (NULLIF(").append(dl.getInt("C_BPartner_ID")).append(",0) IS NULL "); - sqlins.append(" OR po.C_BPartner_ID=").append(dl.getInt("C_BPartner_ID")).append(")"); - sqlins.append(" AND (NULLIF(").append(dl.getInt("M_Product_ID")).append(",0) IS NULL "); - sqlins.append(" OR p.M_Product_ID=").append(dl.getInt("M_Product_ID")).append(")"); - - CPreparedStatement stmt = DB.prepareStatement(sqlins.toString(), get_TrxName()); - - int i = 1; - - if (dl_Group1!=null) - stmt.setString(i++, dl_Group1); - if (dl_Group2!=null) - stmt.setString(i++, dl_Group2); - - cnti = stmt.executeUpdate(); - - if (cnti == -1) - raiseError(" INSERT INTO T_Selection ", sqlins.toString()); - toti += cnti; - log.fine("Inserted " + cnti); - - } else { - // - // Create Selection from existing PriceList - // - sqlins = new StringBuilder("INSERT INTO T_Selection (AD_PInstance_ID, T_Selection_ID)"); - sqlins.append(" SELECT DISTINCT ").append(m_AD_PInstance_ID).append(", p.M_Product_ID"); - sqlins.append(" FROM M_Product p, M_ProductPrice pp"); - sqlins.append(" WHERE p.M_Product_ID=pp.M_Product_ID"); - sqlins.append(" AND pp.M_PriceList_Version_ID = ").append(v.getInt("M_PriceList_Version_Base_ID")); - sqlins.append(" AND p.IsActive='Y' AND pp.IsActive='Y'"); - // - //Optional Restrictions - // - sqlins.append(" AND (NULLIF(").append(dl.getInt("M_Product_Category_ID")).append(",0) IS NULL"); - sqlins.append(" OR p.M_Product_Category_ID IN (").append(getSubCategoryWhereClause(dl.getInt("M_Product_Category_ID"))) - .append("))"); - if(dl_Group1 != null) - sqlins.append(" AND (p.Group1=?)"); - if (dl_Group2 != null) - sqlins.append(" AND (p.Group2=?)"); - sqlins.append(" AND (NULLIF(").append(dl.getInt("C_BPartner_ID")).append(",0) IS NULL OR EXISTS "); - sqlins.append("(SELECT m_product_id,c_bpartner_id,ad_client_id,ad_org_id,isactive"); - sqlins.append(",created,createdby,updated,updatedby,iscurrentvendor,c_uom_id"); - sqlins.append(",c_currency_id,pricelist,pricepo,priceeffective,pricelastpo"); - sqlins.append(",pricelastinv,vendorproductno,upc,vendorcategory,discontinued"); - sqlins.append(",discontinuedby,order_min,order_pack,costperorder"); - sqlins.append(",deliverytime_promised,deliverytime_actual,qualityrating"); - sqlins.append(",royaltyamt,group1,group2"); - sqlins.append(",manufacturer FROM M_Product_PO po WHERE po.M_Product_ID=p.M_Product_ID"); - sqlins.append(" AND po.C_BPartner_ID=").append(dl.getInt("C_BPartner_ID")).append("))"); - sqlins.append(" AND (NULLIF(").append(dl.getInt("M_Product_ID")).append(",0) IS NULL "); - sqlins.append(" OR p.M_Product_ID=").append(dl.getInt("M_Product_ID")).append(")"); - - - CPreparedStatement stmt = DB.prepareStatement(sqlins.toString(), get_TrxName()); - int i = 1; - - if (dl_Group1!=null) - stmt.setString(i++, dl_Group1); - if (dl_Group2!=null) - stmt.setString(i++, dl_Group2); - - cnti = stmt.executeUpdate(); - if (cnti == -1) - raiseError( - " INSERT INTO T_Selection from existing PriceList", - sqlins.toString()); - toti += cnti; - log.fine("Inserted " + cnti); - - } - - Message.append("@Selected@=").append(cnti); - - // - //Delete Prices in Selection, so that we can insert - // - V_temp = v.getInt("M_PriceList_Version_Base_ID"); - if (v.wasNull() || V_temp != p_PriceList_Version_ID) { - - sqldel = new StringBuilder("DELETE M_ProductPrice pp"); - sqldel.append(" WHERE pp.M_PriceList_Version_ID = "); - sqldel.append(p_PriceList_Version_ID); - sqldel.append(" AND EXISTS (SELECT t_selection_id FROM T_Selection s WHERE pp.M_Product_ID=s.T_Selection_ID"); - sqldel.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); - - cntd = DB.executeUpdate(sqldel.toString(), get_TrxName()); - if (cntd == -1) - raiseError(" DELETE M_ProductPrice ", sqldel.toString()); - totd += cntd; - Message.append(", @Deleted@=").append(cntd); - log.fine("Deleted " + cntd); - } - - // - // Copy (Insert) Prices - // - V_temp = v.getInt("M_PriceList_Version_Base_ID"); - if (V_temp == p_PriceList_Version_ID) - // - // We have Prices already - // - ; - else if (v.wasNull()) - // - //Copy and Convert from Product_PO - // - { - sqlins = new StringBuilder("INSERT INTO M_ProductPrice "); - sqlins.append("(M_PriceList_Version_ID"); - sqlins.append(" ,M_Product_ID "); - sqlins.append(" ,AD_Client_ID"); - sqlins.append(" , AD_Org_ID"); - sqlins.append(" , IsActive"); - sqlins.append(" , Created"); - sqlins.append(" , CreatedBy"); - sqlins.append(" , Updated"); - sqlins.append(" , UpdatedBy"); - sqlins.append(" , PriceList"); - sqlins.append(" , PriceStd"); - sqlins.append(" , PriceLimit) "); - sqlins.append("SELECT "); - sqlins.append(p_PriceList_Version_ID); - sqlins.append(" ,po.M_Product_ID "); - sqlins.append(" ,"); - sqlins.append(v.getInt("AD_Client_ID")); - sqlins.append(" ,"); - sqlins.append(v.getInt("AD_Org_ID")); - sqlins.append(" ,'Y'"); - sqlins.append(" ,SysDate,"); - sqlins.append(v.getInt("UpdatedBy")); - sqlins.append(" ,SysDate,"); - sqlins.append(v.getInt("UpdatedBy")); - // - //Price List - // - sqlins.append(" ,COALESCE(currencyConvert(po.PriceList, po.C_Currency_ID, "); - sqlins.append(v.getInt("C_Currency_ID")); - sqlins.append(", ? , "); - sqlins.append(dl.getInt("C_ConversionType_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Client_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Org_ID")); - sqlins.append("),0)"); - - // Price Std - sqlins.append(" ,COALESCE(currencyConvert(po.PriceList, po.C_Currency_ID, "); - sqlins.append(v.getInt("C_Currency_ID")); - sqlins.append(", ? , "); - sqlins.append(dl.getInt("C_ConversionType_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Client_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Org_ID")); - sqlins.append("),0)"); - - // Price Limit - sqlins.append(" ,COALESCE(currencyConvert(po.PricePO ,po.C_Currency_ID, "); - sqlins.append(v.getInt("C_Currency_ID")); - sqlins.append(",? , "); - sqlins.append(dl.getInt("C_ConversionType_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Client_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Org_ID")); - sqlins.append("),0)"); - sqlins.append(" FROM M_Product_PO po "); - sqlins.append(" WHERE EXISTS (SELECT * FROM T_Selection s WHERE po.M_Product_ID=s.T_Selection_ID"); - sqlins.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(") "); - sqlins.append(" AND po.IsCurrentVendor='Y' AND po.IsActive='Y'"); - - PreparedStatement pstmt = DB.prepareStatement(sqlins.toString(), - ResultSet.TYPE_SCROLL_INSENSITIVE, - ResultSet.CONCUR_UPDATABLE, get_TrxName()); - pstmt.setTimestamp(1, dl.getTimestamp("ConversionDate")); - pstmt.setTimestamp(2, dl.getTimestamp("ConversionDate")); - pstmt.setTimestamp(3, dl.getTimestamp("ConversionDate")); - - cnti = pstmt.executeUpdate(); - if (cnti == -1) - raiseError( - " INSERT INTO T_Selection from existing PriceList", - sqlins.toString()); - toti += cnti; - log.fine("Inserted " + cnti); - } else { - // - //Copy and Convert from other PriceList_Version - // - sqlins = new StringBuilder("INSERT INTO M_ProductPrice "); - sqlins.append(" (M_PriceList_Version_ID, M_Product_ID,"); - sqlins.append(" AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,"); - sqlins.append(" PriceList, PriceStd, PriceLimit)"); - sqlins.append(" SELECT "); - sqlins.append(p_PriceList_Version_ID); - sqlins.append(", pp.M_Product_ID,"); - sqlins.append(v.getInt("AD_Client_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Org_ID")); - sqlins.append(", 'Y', SysDate, "); - sqlins.append(v.getInt("UpdatedBy")); - sqlins.append(", SysDate, "); - sqlins.append(v.getInt("UpdatedBy")); - sqlins.append(" ,"); - // Price List - sqlins.append("COALESCE(currencyConvert(pp.PriceList, pl.C_Currency_ID, "); - sqlins.append(v.getInt("C_Currency_ID")); - sqlins.append(", ?, "); - sqlins.append(dl.getInt("C_ConversionType_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Client_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Org_ID")); - sqlins.append("),0),"); - // Price Std - sqlins.append("COALESCE(currencyConvert(pp.PriceStd,pl.C_Currency_ID, "); - sqlins.append(v.getInt("C_Currency_ID")); - sqlins.append(" , ? , "); - sqlins.append(dl.getInt("C_ConversionType_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Client_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Org_ID")); - sqlins.append("),0),"); - //Price Limit - sqlins.append(" COALESCE(currencyConvert(pp.PriceLimit,pl.C_Currency_ID, "); - sqlins.append(v.getInt("C_Currency_ID")); - sqlins.append(" , ? , "); - sqlins.append(dl.getInt("C_ConversionType_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Client_ID")); - sqlins.append(", "); - sqlins.append(v.getInt("AD_Org_ID")); - sqlins.append("),0)"); - sqlins.append(" FROM M_ProductPrice pp"); - sqlins.append(" INNER JOIN M_PriceList_Version plv ON (pp.M_PriceList_Version_ID=plv.M_PriceList_Version_ID)"); - sqlins.append(" INNER JOIN M_PriceList pl ON (plv.M_PriceList_ID=pl.M_PriceList_ID)"); - sqlins.append(" WHERE pp.M_PriceList_Version_ID="); - sqlins.append(v.getInt("M_PriceList_Version_Base_ID")); - sqlins.append(" AND EXISTS (SELECT * FROM T_Selection s WHERE pp.M_Product_ID=s.T_Selection_ID"); - sqlins.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); - sqlins.append("AND pp.IsActive='Y'"); - - PreparedStatement pstmt = DB.prepareStatement(sqlins.toString(), - ResultSet.TYPE_SCROLL_INSENSITIVE, - ResultSet.CONCUR_UPDATABLE, get_TrxName()); - pstmt.setTimestamp(1, dl.getTimestamp("ConversionDate")); - pstmt.setTimestamp(2, dl.getTimestamp("ConversionDate")); - pstmt.setTimestamp(3, dl.getTimestamp("ConversionDate")); - - cnti = pstmt.executeUpdate(); - - if (cnti == -1) - raiseError( - " INSERT INTO T_Selection from existing PriceList", - sqlins.toString()); - toti += cnti; - log.fine("Inserted " + cnti); - - } - Message.append(", @Inserted@=").append(cnti); - // - // Calculation - // - sqlupd = new StringBuilder("UPDATE M_ProductPrice p "); - sqlupd.append(" SET PriceList = (DECODE( '"); - sqlupd.append(dl.getString("List_Base")); - sqlupd.append("', 'S', PriceStd, 'X', PriceLimit, PriceList)"); - sqlupd.append(" + ?) * (1 - ?/100), PriceStd = (DECODE('"); - sqlupd.append(dl.getString("Std_Base")); - sqlupd.append("', 'L', PriceList, 'X', PriceLimit, PriceStd) "); - sqlupd.append(" + ?) * (1 - ?/100), ").append(" PriceLimit = (DECODE('"); - sqlupd.append(dl.getString("Limit_Base")); - sqlupd.append("', 'L', PriceList, 'S', PriceStd, PriceLimit) "); - sqlupd.append(" + ?) * (1 - ? /100) "); - sqlupd.append(" WHERE M_PriceList_Version_ID = "); - sqlupd.append(p_PriceList_Version_ID); - sqlupd.append(" AND EXISTS (SELECT * FROM T_Selection s "); - sqlupd.append(" WHERE s.T_Selection_ID = p.M_Product_ID"); - sqlupd.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); - PreparedStatement pstmu = DB.prepareStatement(sqlupd.toString(), - ResultSet.TYPE_SCROLL_INSENSITIVE, - ResultSet.CONCUR_UPDATABLE, get_TrxName()); - - pstmu.setDouble(1, dl.getDouble("List_AddAmt")); - pstmu.setDouble(2, dl.getDouble("List_Discount")); - pstmu.setDouble(3, dl.getDouble("Std_AddAmt")); - pstmu.setDouble(4, dl.getDouble("Std_Discount")); - pstmu.setDouble(5, dl.getDouble("Limit_AddAmt")); - pstmu.setDouble(6, dl.getDouble("Limit_Discount")); - - cntu = pstmu.executeUpdate(); - - if (cntu == -1) - raiseError("Update M_ProductPrice ", sqlupd.toString()); - totu += cntu; - log.fine("Updated " + cntu); - // - //Rounding (AD_Reference_ID=155) + //commit; // - sqlupd = new StringBuilder("UPDATE M_ProductPrice p "); - sqlupd.append(" SET PriceList = DECODE('"); - sqlupd.append(dl.getString("List_Rounding")).append("',"); - sqlupd.append(" 'N', PriceList, "); - sqlupd.append(" '0', ROUND(PriceList, 0),"); //Even .00 - sqlupd.append(" 'D', ROUND(PriceList, 1),"); //Dime .10 - sqlupd.append(" 'T', ROUND(PriceList, -1), "); //Ten 10.00 - sqlupd.append(" '5', ROUND(PriceList*20,0)/20,"); //Nickle .05 - sqlupd.append(" 'Q', ROUND(PriceList*4,0)/4,"); //Quarter .25 - sqlupd.append(" '9', CASE"); //Whole 9 or 5 - sqlupd.append(" WHEN MOD(ROUND(PriceList),10)<=5 THEN ROUND(PriceList)+(5-MOD(ROUND(PriceList),10))"); - sqlupd.append(" WHEN MOD(ROUND(PriceList),10)>5 THEN ROUND(PriceList)+(9-MOD(ROUND(PriceList),10)) END,"); - sqlupd.append(" ROUND(PriceList, ").append(v.getInt("StdPrecision")); - sqlupd.append(")),");//Currency - sqlupd.append(" PriceStd = DECODE('").append(dl.getString("Std_Rounding")); - sqlupd.append("',").append(" 'N', PriceStd, "); - sqlupd.append(" '0', ROUND(PriceStd, 0), "); //Even .00 - sqlupd.append(" 'D', ROUND(PriceStd, 1), "); //Dime .10 - sqlupd.append("'T', ROUND(PriceStd, -1),"); //Ten 10.00) - sqlupd.append("'5', ROUND(PriceStd*20,0)/20,"); //Nickle .05 - sqlupd.append("'Q', ROUND(PriceStd*4,0)/4,"); //Quarter .25 - sqlupd.append(" '9', CASE"); //Whole 9 or 5 - sqlupd.append(" WHEN MOD(ROUND(PriceStd),10)<=5 THEN ROUND(PriceStd)+(5-MOD(ROUND(PriceStd),10))"); - sqlupd.append(" WHEN MOD(ROUND(PriceStd),10)>5 THEN ROUND(PriceStd)+(9-MOD(ROUND(PriceStd),10)) END,"); - sqlupd.append("ROUND(PriceStd, ").append(v.getInt("StdPrecision")).append(")),"); //Currency - sqlupd.append("PriceLimit = DECODE('"); - sqlupd.append(dl.getString("Limit_Rounding")).append("', "); - sqlupd.append(" 'N', PriceLimit, "); - sqlupd.append(" '0', ROUND(PriceLimit, 0), "); // Even .00 - sqlupd.append(" 'D', ROUND(PriceLimit, 1), "); // Dime .10 - sqlupd.append(" 'T', ROUND(PriceLimit, -1), "); // Ten 10.00 - sqlupd.append(" '5', ROUND(PriceLimit*20,0)/20, "); // Nickle .05 - sqlupd.append(" 'Q', ROUND(PriceLimit*4,0)/4, "); //Quarter .25 - sqlupd.append(" '9', CASE"); //Whole 9 or 5 - sqlupd.append(" WHEN MOD(ROUND(PriceLimit),10)<=5 THEN ROUND(PriceLimit)+(5-MOD(ROUND(PriceLimit),10))"); - sqlupd.append(" WHEN MOD(ROUND(PriceLimit),10)>5 THEN ROUND(PriceLimit)+(9-MOD(ROUND(PriceLimit),10)) END,"); - sqlupd.append(" ROUND(PriceLimit, ").append(v.getInt("StdPrecision")); - sqlupd.append(")) "); // Currency - sqlupd.append(" WHERE M_PriceList_Version_ID="); - sqlupd.append(p_PriceList_Version_ID); - sqlupd.append(" AND EXISTS (SELECT * FROM T_Selection s "); - sqlupd.append(" WHERE s.T_Selection_ID=p.M_Product_ID"); - sqlupd.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); - - cntu = DB.executeUpdate(sqlupd.toString(), get_TrxName()); - if (cntu == -1) - raiseError("Update M_ProductPrice ", sqlupd.toString()); - totu += cntu; - log.fine("Updated " + cntu); + // log.fine("Committing ..."); + // DB.commit(true, get_TrxName()); - Message.append(", @Updated@=").append(cntu); - // - //Fixed Price overwrite - // - sqlupd = new StringBuilder("UPDATE M_ProductPrice p "); - sqlupd.append(" SET PriceList = DECODE('"); - sqlupd.append(dl.getString("List_Base")).append("', 'F', "); - sqlupd.append(dl.getDouble("List_Fixed")).append(", PriceList), "); - sqlupd.append(" PriceStd = DECODE('"); - sqlupd.append(dl.getString("Std_Base")).append("', 'F', "); - sqlupd.append(dl.getDouble("Std_Fixed")).append(", PriceStd),"); - sqlupd.append(" PriceLimit = DECODE('"); - sqlupd.append(dl.getString("Limit_Base")).append("', 'F', "); - sqlupd.append(dl.getDouble("Limit_Fixed")).append(", PriceLimit)"); - sqlupd.append(" WHERE M_PriceList_Version_ID="); - sqlupd.append(p_PriceList_Version_ID); - sqlupd.append(" AND EXISTS (SELECT * FROM T_Selection s"); - sqlupd.append(" WHERE s.T_Selection_ID=p.M_Product_ID"); - sqlupd.append(" AND s.AD_PInstance_ID=").append(m_AD_PInstance_ID).append(")"); - - cntu = DB.executeUpdate(sqlupd.toString(), get_TrxName()); - if (cntu == -1) - raiseError("Update M_ProductPrice ", sqlupd.toString()); - totu += cntu; - log.fine("Updated " + cntu); - - v_NextNo = v_NextNo + 1; - addLog(0, null, null, Message.toString()); - Message = new StringBuilder(); } - dl.close(); - Cur_DiscountLine.close(); - Cur_DiscountLine = null; - - // - // Delete Temporary Selection - // - sqldel = new StringBuilder("DELETE FROM T_Selection "); - cntd = DB.executeUpdate(sqldel.toString(), get_TrxName()); - if (cntd == -1) - raiseError(" DELETE T_Selection ", sqldel.toString()); - totd += cntd; - log.fine("Deleted " + cntd); - - // - //commit; - // - // log.fine("Committing ..."); - // DB.commit(true, get_TrxName()); - + } catch (SQLException e) { + throw e; + } finally { + DB.close(rsCurgen, stmtCurgen); + rsCurgen = null; stmtCurgen = null; + DB.close(rsDiscountLine, stmtDiscountLine); + rsDiscountLine = null; stmtDiscountLine = null; + DB.close(stmt); + stmt = null; + DB.close(pstmt); + pstmt = null; } - v.close(); - curgen.close(); - curgen = null; return "OK"; @@ -734,24 +747,32 @@ public class M_PriceList_Create extends SvrProcess { * It is used as restriction in MQuery. * @param productCategoryId * @return + * @throws */ - private String getSubCategoryWhereClause(int productCategoryId) throws SQLException, AdempiereSystemError{ + private String getSubCategoryWhereClause(int productCategoryId) throws SQLException, AdempiereSystemError { //if a node with this id is found later in the search we have a loop in the tree int subTreeRootParentId = 0; StringBuilder retString = new StringBuilder(); String sql = " SELECT M_Product_Category_ID, M_Product_Category_Parent_ID FROM M_Product_Category"; final Vector categories = new Vector(100); - Statement stmt = DB.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - while (rs.next()) { - if(rs.getInt(1)==productCategoryId) { - subTreeRootParentId = rs.getInt(2); + Statement stmt = null; + ResultSet rs = null; + try { + stmt = DB.createStatement(); + rs = stmt.executeQuery(sql); + while (rs.next()) { + if(rs.getInt(1)==productCategoryId) { + subTreeRootParentId = rs.getInt(2); + } + categories.add(new SimpleTreeNode(rs.getInt(1), rs.getInt(2))); } - categories.add(new SimpleTreeNode(rs.getInt(1), rs.getInt(2))); + retString.append(getSubCategoriesString(productCategoryId, categories, subTreeRootParentId)); + } catch (SQLException e) { + throw e; + } finally { + DB.close(rs, stmt); + rs = null; stmt = null; } - retString.append(getSubCategoriesString(productCategoryId, categories, subTreeRootParentId)); - rs.close(); - stmt.close(); return retString.toString(); } @@ -760,7 +781,7 @@ public class M_PriceList_Create extends SvrProcess { * @param productCategoryId * @param categories * @param loopIndicatorId - * @return comma seperated list of category ids + * @return comma separated list of category ids * @throws AdempiereSystemError if a loop is detected */ private String getSubCategoriesString(int productCategoryId, Vector categories, int loopIndicatorId) throws AdempiereSystemError {