CreateCostElement:

* fix M_Cost creation
* improved performances
This commit is contained in:
teo_sarca 2008-09-17 10:38:59 +00:00
parent ec95527738
commit 8937778bab
1 changed files with 57 additions and 41 deletions

View File

@ -12,17 +12,19 @@
* For the text or an alternative of this public license, you may reach us *
* Copyright (C) 2003-2007 e-Evolution,SC. All Rights Reserved. *
* Contributor(s): Victor Perez www.e-evolution.com *
* Teo Sarca, www.arhipac.ro *
*****************************************************************************/
package org.eevolution.process;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.compiere.model.MAcctSchema;
import org.compiere.model.MCost;
import org.compiere.model.MCostElement;
import org.compiere.model.MProduct;
@ -33,10 +35,12 @@ import org.compiere.util.DB;
/**
* Create Cost Element
*
* @author victor.perez@e-evolution.com, e-Evolution, S.C.
* @version $Id: CreateCostElement.java,v 1.1 2004/06/22 05:24:03 vpj-cd Exp $
* Create Cost Element
*
* @author victor.perez@e-evolution.com, e-Evolution, S.C.
* @version $Id: CreateCostElement.java,v 1.1 2004/06/22 05:24:03 vpj-cd Exp $
*
* @author Teo Sarca, www.arhipac.ro
*/
public class CreateCostElement extends SvrProcess
{
@ -91,30 +95,54 @@ public class CreateCostElement extends SvrProcess
protected String doIt() throws Exception
{
MAcctSchema as = MAcctSchema.get(getCtx(), p_C_AcctSchema_ID);
int count_costs = 0;
ArrayList<Object> params = new ArrayList<Object>();
String sql = "SELECT M_Product_ID FROM M_Product WHERE AD_Client_ID=?";
String sql = "SELECT org.AD_Org_ID, p.M_Product_ID, ce.M_CostElement_ID"
+" FROM AD_Org org, M_Product p, M_CostElement ce"
+" WHERE ";
sql += "org.AD_Client_ID=? AND p.AD_Client_ID=org.AD_Client_ID AND ce.AD_Client_ID=p.AD_Client_ID";
params.add(getAD_Client_ID());
if (p_M_Product_Category_ID != 0)
if (p_AD_Org_ID > 0)
{
sql = sql + " AND M_Product_Category_ID=?";
sql += " AND org.AD_Org_ID=?";
params.add(p_AD_Org_ID);
}
if (p_M_Product_Category_ID > 0)
{
sql += " AND p."+MProduct.COLUMNNAME_M_Product_Category_ID+"=?";
params.add(p_M_Product_Category_ID);
}
if (p_M_Product_ID != 0)
if (p_M_Product_ID > 0)
{
sql = sql + " AND M_Product_ID=?";
sql += " AND p."+MProduct.COLUMNNAME_M_Product_ID+"=?";
params.add(p_M_Product_ID);
}
MCostElement[] elements = MCostElement.getElements(getCtx(), get_TrxName());
if (elements.length == 0)
{
throw new AdempiereException("@NotFound@ @M_CostElement_ID@");
}
if (p_M_CostElement_ID > 0)
{
sql += " AND ce."+MCostElement.COLUMNNAME_M_CostElement_ID+"=?";
params.add(p_M_CostElement_ID);
}
// M_Cost not already created:
sql += " AND NOT EXISTS (SELECT 1 FROM M_Cost c WHERE"
+" c.AD_Client_ID=ce.AD_Client_ID"
+" AND c.AD_Org_ID=org.AD_Org_ID"
+" AND c.M_Product_ID=p.M_Product_ID"
+" AND c.C_AcctSchema_ID=?"
+" AND c.M_CostType_ID=?"
+" AND c.M_CostElement_ID=ce.M_CostElement_ID)";
params.add(p_C_AcctSchema_ID);
params.add(p_M_CostType_ID);
// ORDER BY
sql += " ORDER BY p."+MProduct.COLUMNNAME_M_Product_ID;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
@ -124,32 +152,21 @@ public class CreateCostElement extends SvrProcess
rs = pstmt.executeQuery ();
while (rs.next())
{
int m_M_Product_ID = rs.getInt(MProduct.COLUMNNAME_M_Product_ID);
MCost[] costs = MCost.getCosts(getCtx(), getAD_Client_ID(), p_AD_Org_ID,
m_M_Product_ID, p_M_CostType_ID, p_C_AcctSchema_ID,
get_TrxName());
if (costs.length == 0)
{
for(MCostElement element : elements)
{
if(p_M_CostElement_ID > 0 && element.get_ID() != p_M_CostElement_ID)
continue;
MCost cost = new MCost(getCtx(), 0, get_TrxName());
cost.setM_Product_ID(m_M_Product_ID);
cost.setAD_Org_ID(p_AD_Org_ID);
cost.setC_AcctSchema_ID(p_C_AcctSchema_ID);
cost.setM_CostType_ID(p_M_CostType_ID);
cost.setM_CostElement_ID(element.get_ID());
cost.saveEx();
count_costs++;
}
}
int AD_Org_ID = rs.getInt("AD_Org_ID");
int M_Product_ID = rs.getInt(MProduct.COLUMNNAME_M_Product_ID);
int M_CostElement_ID = rs.getInt(MCostElement.COLUMNNAME_M_CostElement_ID);
int M_ASI_ID = 0;
MProduct product = MProduct.get(getCtx(), M_Product_ID);
//
MCost cost = new MCost(product, M_ASI_ID, as, AD_Org_ID, M_CostElement_ID);
cost.setM_CostType_ID(p_M_CostType_ID);
cost.saveEx(get_TrxName());
count_costs++;
}
}
catch (Exception e)
catch (SQLException e)
{
throw new DBException(e);
throw new DBException(e, sql);
}
finally
{
@ -159,5 +176,4 @@ public class CreateCostElement extends SvrProcess
return "@Created@ #"+count_costs;
}
} // Create Cost Element