CreateCostElement: bug fixing

MCostElement.getElements: AD_Org_ID parameter is not needed since all M_CostElements are created with AD_Org_ID=0 (see beforeSave)
This commit is contained in:
teo_sarca 2008-09-06 17:55:20 +00:00
parent ccc427a99e
commit 505c7bd4ca
2 changed files with 98 additions and 76 deletions

View File

@ -243,18 +243,20 @@ public class MCostElement extends X_M_CostElement
/** /**
* Get Costs Record for a Cost Type * Get All Cost Elements for current AD_Client_ID
* @param ctx context * @param ctx context
* @param AD_Client_ID client * @param trxName transaction
* @param AD_Org_ID org * @return array cost elements
* @return array costs
**/ **/
public static MCostElement[] getElements (Properties ctx , int AD_Client_ID, int AD_Org_ID, String trxName) public static MCostElement[] getElements (Properties ctx, String trxName)
{ {
int AD_Client_ID = Env.getAD_Client_ID(ctx);
int AD_Org_ID = 0; // Org is always ZERO - see beforeSave
String whereClause = "AD_Client_ID = ? AND AD_Org_ID = ?"; String whereClause = "AD_Client_ID = ? AND AD_Org_ID = ?";
Query query = MTable.get(ctx, MCostElement.Table_ID).createQuery(whereClause, trxName); List<MCostElement> list = new Query(ctx, Table_Name, whereClause, trxName)
query.setParameters(new Object[]{AD_Client_ID, AD_Org_ID}); .setParameters(new Object[]{AD_Client_ID, AD_Org_ID})
List<MCostElement> list = query.list(); .list();
MCostElement[] retValue = new MCostElement[list.size()]; MCostElement[] retValue = new MCostElement[list.size()];
list.toArray(retValue); list.toArray(retValue);
return retValue; return retValue;

View File

@ -16,14 +16,20 @@
package org.eevolution.process; package org.eevolution.process;
import java.util.logging.*; import java.math.BigDecimal;
import java.math.*; import java.sql.PreparedStatement;
import java.sql.*; import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import org.compiere.util.*; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.compiere.model.MCost; import org.compiere.model.MCost;
import org.compiere.model.MCostElement; import org.compiere.model.MCostElement;
import org.compiere.process.*; import org.compiere.model.MProduct;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
@ -35,93 +41,107 @@ import org.compiere.process.*;
*/ */
public class CreateCostElement extends SvrProcess public class CreateCostElement extends SvrProcess
{ {
private int p_AD_Org_ID = 0; private int p_AD_Org_ID = 0;
private int p_C_AcctSchema_ID = 0; private int p_C_AcctSchema_ID = 0;
private int p_M_CostType_ID = 0; private int p_M_CostType_ID = 0;
private int p_M_Product_ID = 0; private int p_M_Product_ID = 0;
/**
/**
* Prepare - e.g., get Parameters. * Prepare - e.g., get Parameters.
*/ */
protected void prepare() protected void prepare()
{ {
ProcessInfoParameter[] para = getParameter(); ProcessInfoParameter[] para = getParameter();
for (int i = 0; i < para.length; i++) for (int i = 0; i < para.length; i++)
{ {
String name = para[i].getParameterName(); String name = para[i].getParameterName();
if (para[i].getParameter() == null) if (para[i].getParameter() == null)
{
; ;
}
else if (name.equals("AD_Org_ID")) else if (name.equals("AD_Org_ID"))
{ {
p_AD_Org_ID = ((BigDecimal)para[i].getParameter()).intValue(); p_AD_Org_ID = ((BigDecimal)para[i].getParameter()).intValue();
}
} else if (name.equals(MCost.COLUMNNAME_C_AcctSchema_ID))
else if (name.equals("C_AcctSchema_ID")) {
{
p_C_AcctSchema_ID = ((BigDecimal)para[i].getParameter()).intValue(); p_C_AcctSchema_ID = ((BigDecimal)para[i].getParameter()).intValue();
}
} else if (name.equals(MCost.COLUMNNAME_M_CostType_ID))
else if (name.equals("M_CostType_ID")) {
{
p_M_CostType_ID = ((BigDecimal)para[i].getParameter()).intValue(); p_M_CostType_ID = ((BigDecimal)para[i].getParameter()).intValue();
}
} else if (name.equals(MCost.COLUMNNAME_M_Product_ID))
else if (name.equals("M_Product_ID")) {
{
p_M_Product_ID = ((BigDecimal)para[i].getParameter()).intValue(); p_M_Product_ID = ((BigDecimal)para[i].getParameter()).intValue();
} }
else else
log.log(Level.SEVERE,"prepare - Unknown Parameter: " + name); log.log(Level.SEVERE,"prepare - Unknown Parameter: " + name);
} }
} // prepare } // prepare
protected String doIt() throws Exception protected String doIt() throws Exception
{ {
int count_costs = 0;
String sql = "SELECT M_Product_ID FROM M_Product p WHERE AD_Client_ID=" +getAD_Client_ID(); ArrayList<Object> params = new ArrayList<Object>();
if (p_M_Product_ID != 0) String sql = "SELECT M_Product_ID FROM M_Product WHERE AD_Client_ID=?";
sql = sql + " and p.M_Product_ID =" +p_M_Product_ID; params.add(getAD_Client_ID());
if (p_M_Product_ID != 0)
{
sql = sql + " AND M_Product_ID=?";
params.add(p_M_Product_ID);
}
MCostElement[] elements = MCostElement.getElements(getCtx(), getAD_Client_ID(), p_AD_Org_ID, get_TrxName()); MCostElement[] elements = MCostElement.getElements(getCtx(), get_TrxName());
if (elements.length == 0)
{
throw new AdempiereException("@NotFound@ @M_CostElement_ID@");
}
PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
PreparedStatement pstmt = DB.prepareStatement (sql, get_TrxName()); pstmt = DB.prepareStatement (sql, get_TrxName());
ResultSet rs = pstmt.executeQuery (); for (int i = 0; i < params.size(); i++)
while (rs.next()) {
{ DB.setParameter(pstmt, i+1, params.get(i));
int m_M_Product_ID = rs.getInt(1); }
rs = pstmt.executeQuery ();
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()); while (rs.next())
{
if (costs == null) int m_M_Product_ID = rs.getInt(MProduct.COLUMNNAME_M_Product_ID);
{ MCost[] costs = MCost.getCosts(getCtx(), getAD_Client_ID(), p_AD_Org_ID,
for(MCostElement element : elements) m_M_Product_ID, p_M_CostType_ID, p_C_AcctSchema_ID,
{ get_TrxName());
MCost cost = new MCost(getCtx(), 0 ,get_TrxName()); if (costs.length == 0)
cost.setM_Product_ID(m_M_Product_ID); {
cost.setAD_Org_ID(p_AD_Org_ID); for(MCostElement element : elements)
cost.setC_AcctSchema_ID(p_C_AcctSchema_ID); {
cost.setM_CostType_ID(p_M_CostType_ID); MCost cost = new MCost(getCtx(), 0, get_TrxName());
cost.setM_CostElement_ID(element.getM_CostElement_ID()); cost.setM_Product_ID(m_M_Product_ID);
cost.save(); 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());
rs.close(); cost.saveEx();
pstmt.close(); count_costs++;
}
}
}
} }
catch (Exception e) catch (Exception e)
{ {
log.log(Level.SEVERE,"doIt - " + sql, e); throw new DBException(e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
return "ok"; return "@Created@ #"+count_costs;
} }
} // Create Cost Element } // Create Cost Element