MRPUpdate:

* organized imports
* fixed indentation
* better exception handling
* BF [ 1874419 ] JDBC Statement not close in a finally block
* refactored
* changed MRequisition to delete lines on delete
This commit is contained in:
teo_sarca 2008-08-25 18:24:14 +00:00
parent c3750a2984
commit 26b5f8ddee
2 changed files with 219 additions and 247 deletions

View File

@ -192,6 +192,13 @@ public class MRequisition extends X_M_Requisition implements DocAction
return true; return true;
} // beforeSave } // beforeSave
@Override
protected boolean beforeDelete() {
for (MRequisitionLine line : getLines()) {
line.deleteEx(true);
}
return true;
}
/************************************************************************** /**************************************************************************
* Process document * Process document

View File

@ -17,13 +17,13 @@
package org.eevolution.process; package org.eevolution.process;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level; import java.util.logging.Level;
import org.compiere.model.MRequisition; import org.compiere.model.MRequisition;
import org.compiere.model.MRequisitionLine;
import org.compiere.model.MWarehouse; import org.compiere.model.MWarehouse;
import org.compiere.model.PO;
import org.compiere.model.POResultSet;
import org.compiere.model.Query;
import org.compiere.process.ProcessInfoParameter; import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess; import org.compiere.process.SvrProcess;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
@ -37,6 +37,7 @@ import org.eevolution.model.MPPOrder;
* MRPUpdate * MRPUpdate
* *
* @author Victor Perez, e-Evolution, S.C. * @author Victor Perez, e-Evolution, S.C.
* @author Teo Sarca, www.arhipac.ro
*/ */
public class MRPUpdate extends SvrProcess public class MRPUpdate extends SvrProcess
{ {
@ -44,7 +45,7 @@ public class MRPUpdate extends SvrProcess
private int p_AD_Org_ID = 0; private int p_AD_Org_ID = 0;
private int p_S_Resource_ID = 0 ; private int p_S_Resource_ID = 0 ;
private int p_M_Warehouse_ID= 0; private int p_M_Warehouse_ID= 0;
private int Planner_ID= 0; // private int Planner_ID= 0;
/** /**
@ -52,8 +53,8 @@ public class MRPUpdate extends SvrProcess
*/ */
protected void prepare() protected void prepare()
{ {
m_AD_Client_ID = Integer.parseInt(Env.getContext(getCtx(), "#AD_Client_ID")); m_AD_Client_ID = Env.getAD_Client_ID(Env.getCtx());
Planner_ID = Integer.parseInt(Env.getContext(getCtx(), "#AD_User_ID")); // Planner_ID = Integer.parseInt(Env.getContext(getCtx(), "#AD_User_ID"));
ProcessInfoParameter[] para = getParameter(); ProcessInfoParameter[] para = getParameter();
for (int i = 0; i < para.length; i++) for (int i = 0; i < para.length; i++)
@ -65,7 +66,6 @@ public class MRPUpdate extends SvrProcess
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("S_Resource_ID")) else if (name.equals("S_Resource_ID"))
{ {
@ -109,7 +109,7 @@ public class MRPUpdate extends SvrProcess
} }
/** /**
* delete Record * Delete MRP records
*/ */
public boolean deleteRecord(int AD_Client_ID,int AD_Org_ID, int S_Resource_ID, int M_Warehouse_ID) public boolean deleteRecord(int AD_Client_ID,int AD_Org_ID, int S_Resource_ID, int M_Warehouse_ID)
{ {
@ -124,92 +124,41 @@ public class MRPUpdate extends SvrProcess
if (S_Resource_ID > 0 ) if (S_Resource_ID > 0 )
resourcewhere = " AND S_Resource_ID=" + S_Resource_ID; resourcewhere = " AND S_Resource_ID=" + S_Resource_ID;
String sql = "DELETE FROM PP_MRP WHERE OrderType = 'MOP' AND AD_Client_ID=" + m_AD_Client_ID + where + resourcewhere; // Delete MRP records (Mfg. Order, Forecast, Material Requisition):
if(DB.executeUpdate(sql, get_TrxName()) < 0) return false; String sql = "DELETE FROM PP_MRP WHERE OrderType IN ('MOP','FCT','POR') AND AD_Client_ID=" + m_AD_Client_ID + where + resourcewhere;
DB.executeUpdateEx(sql, get_TrxName());
sql = "DELETE FROM PP_MRP mrp WHERE mrp.OrderType = 'FCT' AND mrp.AD_Client_ID = " + m_AD_Client_ID+ where + resourcewhere; // Delete notes:
if(DB.executeUpdate(sql,get_TrxName()) < 0) return false;
sql = "DELETE FROM PP_MRP mrp WHERE mrp.OrderType = 'POR' AND mrp.AD_Client_ID = " + m_AD_Client_ID + where + resourcewhere;
if(DB.executeUpdate(sql,get_TrxName()) < 0) return false;
if (AD_Org_ID > 0 )
sql = "DELETE FROM AD_Note n WHERE AD_Table_ID = " + MPPMRP.Table_ID + " AND AD_Client_ID = " + m_AD_Client_ID + " AND AD_Org_ID=" + AD_Org_ID;
else
sql = "DELETE FROM AD_Note n WHERE AD_Table_ID = " + MPPMRP.Table_ID + " AND AD_Client_ID = " + m_AD_Client_ID; sql = "DELETE FROM AD_Note n WHERE AD_Table_ID = " + MPPMRP.Table_ID + " AND AD_Client_ID = " + m_AD_Client_ID;
if (AD_Org_ID > 0)
sql += " AND AD_Org_ID=" + AD_Org_ID;
DB.executeUpdateEx(sql, get_TrxName());
if(DB.executeUpdate(sql, get_TrxName()) < 0) return false; // Delete Mfg. Orders:
String whereClause = "DocStatus = 'DR' AND AD_Client_ID = " + m_AD_Client_ID + where;
if (S_Resource_ID > 0) if (S_Resource_ID > 0)
sql = "SELECT o.PP_Order_ID FROM PP_Order o WHERE o.DocStatus = 'DR' AND o.AD_Client_ID = " + m_AD_Client_ID + " AND S_Resource_ID=" + S_Resource_ID + where; whereClause += " AND S_Resource_ID="+S_Resource_ID;
else deletePO(MPPOrder.Table_Name, whereClause, null);
sql = "SELECT o.PP_Order_ID FROM PP_Order o WHERE o.DocStatus = 'DR' AND o.AD_Client_ID = " + m_AD_Client_ID + where;
PreparedStatement pstmt = null; // Delete Material Requisitions:
ResultSet rs = null; whereClause = "DocStatus = 'DR' AND AD_Client_ID = " + m_AD_Client_ID+ where;
try deletePO(MRequisition.Table_Name, whereClause, null);
{
pstmt = DB.prepareStatement (sql,get_TrxName());
rs = pstmt.executeQuery();
while (rs.next())
{
MPPOrder order = new MPPOrder(getCtx(), rs.getInt(1), get_TrxName());
order.delete(true);
}
}
catch (Exception e)
{
log.log(Level.SEVERE,"doIt - " + sql, e);
return false;
}
finally
{
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
sql = "SELECT r.M_Requisition_ID FROM M_Requisition r WHERE r.DocStatus = 'DR' AND r.AD_Client_ID = " + m_AD_Client_ID+ where;
rs = null;
pstmt = null;
try
{
pstmt = DB.prepareStatement (sql,get_TrxName());
rs = pstmt.executeQuery();
while (rs.next())
{
MRequisition r = new MRequisition(getCtx(), rs.getInt(1),get_TrxName());
MRequisitionLine[] rlines = r. getLines();
for ( int i= 0 ; i < rlines.length; i++ )
{
MRequisitionLine line = rlines[i];
line.delete(true);
}
r.delete(true);
}
}
catch (Exception e)
{
log.log(Level.SEVERE,"doIt - " + sql, e);
return false;
}
finally
{
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
return true; return true;
} }
/** /**
* create Record * Create MRP records
*/ */
public boolean createRecord(int AD_Client_ID,int AD_Org_ID, int S_Resource_ID, int M_Warehouse_ID) public boolean createRecord(int AD_Client_ID,int AD_Org_ID, int S_Resource_ID, int M_Warehouse_ID)
{ {
String sql = "INSERT INTO pp_mrp(" String where = "";
if (AD_Org_ID > 0 )
where = " AND t.AD_Org_ID=" + AD_Org_ID;
if (M_Warehouse_ID > 0 )
where += " AND t.M_Warehouse_ID=" + M_Warehouse_ID;
final String sql = "INSERT INTO PP_MRP ("
+"ad_org_id,created, createdby , dateordered," +"ad_org_id,created, createdby , dateordered,"
+"datepromised, datestart, datestartschedule, description," +"datepromised, datestart, datestartschedule, description,"
+"docstatus, isactive , " +"docstatus, isactive , "
@ -221,6 +170,8 @@ public class MRPUpdate extends SvrProcess
+"pp_mrp_id, planner_id, " +"pp_mrp_id, planner_id, "
+"qty, typemrp, ordertype, updated, updatedby, value, " +"qty, typemrp, ordertype, updated, updatedby, value, "
+"ad_client_id, s_resource_id )"; +"ad_client_id, s_resource_id )";
//
//Insert from M_ForecastLine //Insert from M_ForecastLine
String sql_insert = " SELECT t.ad_org_id," String sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , t.datepromised," +"t.created, t.createdby , t.datepromised,"
@ -233,19 +184,13 @@ public class MRPUpdate extends SvrProcess
+"t.m_product_id, t.m_warehouse_id," +"t.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040,'N') , null," +"nextidfunc(53040,'N') , null,"
+"t.qty, 'D', 'FCT', t.updated, t.updatedby, f.Name," +"t.qty, 'D', 'FCT', t.updated, t.updatedby, f.Name,"
+"t.ad_client_id , " +"t.ad_client_id , "+ S_Resource_ID
+ S_Resource_ID +" FROM M_ForecastLine t "
+" FROM M_ForecastLine t INNER JOIN M_Forecast f ON (f.M_Forecast_ID=t.M_Forecast_ID) WHERE t.Qty > 0 AND t.AD_Client_ID="+ AD_Client_ID; +" INNER JOIN M_Forecast f ON (f.M_Forecast_ID=t.M_Forecast_ID) "
+" WHERE t.Qty > 0 AND t.AD_Client_ID="+ AD_Client_ID;
String where = ""; DB.executeUpdateEx(sql + sql_insert + where , get_TrxName());
if (AD_Org_ID > 0 )
where = " AND t.AD_Org_ID=" + AD_Org_ID;
if (M_Warehouse_ID > 0 )
where += " AND t.M_Warehouse_ID=" + M_Warehouse_ID;
if(DB.executeUpdate(sql + sql_insert + where , get_TrxName()) < 0) return false;
//
//Insert from PP_Order //Insert from PP_Order
sql_insert = " SELECT t.ad_org_id," sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , t.datepromised," +"t.created, t.createdby , t.datepromised,"
@ -258,14 +203,15 @@ public class MRPUpdate extends SvrProcess
+"t.m_product_id, t.m_warehouse_id," +"t.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040,'N') , null," +"nextidfunc(53040,'N') , null,"
+"t.QtyOrdered-t.QtyDelivered, 'S', 'MOP', t.updated, t.updatedby, t.DocumentNo," +"t.QtyOrdered-t.QtyDelivered, 'S', 'MOP', t.updated, t.updatedby, t.DocumentNo,"
+"t.ad_client_id ,t.S_Resource_ID "; +"t.ad_client_id ,t.S_Resource_ID "
+" FROM PP_Order t "
+" WHERE (t.QtyOrdered - t.QtyDelivered) <> 0 AND t.DocStatus IN ('IP','CO')"
+" AND t.AD_Client_ID = " + m_AD_Client_ID;
if(S_Resource_ID > 0) if(S_Resource_ID > 0)
sql_insert += " FROM PP_Order t WHERE (t.QtyOrdered - t.QtyDelivered) <> 0 AND t.DocStatus IN ('IP','CO') AND t.AD_Client_ID = " + m_AD_Client_ID + " AND S_Resource_ID=" + S_Resource_ID ; sql_insert += " AND S_Resource_ID=" + S_Resource_ID ;
else DB.executeUpdateEx(sql + sql_insert + where , get_TrxName());
sql_insert += " FROM PP_Order t WHERE (t.QtyOrdered - t.QtyDelivered) <> 0 AND t.DocStatus IN ('IP','CO') AND t.AD_Client_ID = " + m_AD_Client_ID ;
if(DB.executeUpdate(sql + sql_insert + where , get_TrxName()) < 0) return false;
//
//Insert from PP_Order_BOMLine //Insert from PP_Order_BOMLine
sql_insert = " SELECT t.ad_org_id," sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , o.datepromised," +"t.created, t.createdby , o.datepromised,"
@ -278,15 +224,16 @@ public class MRPUpdate extends SvrProcess
+"t.m_product_id, t.m_warehouse_id," +"t.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040,'N') , null," +"nextidfunc(53040,'N') , null,"
+"t.QtyRequiered-t.QtyDelivered, 'D', 'MOP', t.updated, t.updatedby, o.DocumentNo," +"t.QtyRequiered-t.QtyDelivered, 'D', 'MOP', t.updated, t.updatedby, o.DocumentNo,"
+"t.ad_client_id, o.S_Resource_ID "; +"t.ad_client_id, o.S_Resource_ID "
+" FROM PP_Order_BOMLine t "
+" INNER JOIN PP_Order o ON (o.pp_order_id=t.pp_order_id)"
+" WHERE (t.QtyRequiered-t.QtyDelivered) <> 0 AND o.DocStatus IN ('DR','IP','CO')"
+" AND t.AD_Client_ID = " + m_AD_Client_ID;
if(S_Resource_ID > 0) if(S_Resource_ID > 0)
sql_insert += "FROM PP_Order_BOMLine t INNER JOIN PP_Order o ON (o.pp_order_id=t.pp_order_id) WHERE (t.QtyRequiered-t.QtyDelivered) <> 0 AND o.DocStatus IN ('DR','IP','CO') AND t.AD_Client_ID = " + m_AD_Client_ID + " AND S_Resource_ID=" + S_Resource_ID ; sql_insert += " AND S_Resource_ID=" + S_Resource_ID ;
else DB.executeUpdateEx(sql + sql_insert + where , get_TrxName());
sql_insert += "FROM PP_Order_BOMLine t INNER JOIN PP_Order o ON (o.pp_order_id=t.pp_order_id) WHERE (t.QtyRequiered-t.QtyDelivered) <> 0 AND o.DocStatus IN ('DR','IP','CO') AND t.AD_Client_ID = " + m_AD_Client_ID;
if(DB.executeUpdate(sql + sql_insert + where , get_TrxName()) < 0) return false;
//
// Insert from C_OrderLine // Insert from C_OrderLine
sql_insert = " SELECT t.ad_org_id," sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , t.datepromised," +"t.created, t.createdby , t.datepromised,"
@ -299,12 +246,14 @@ public class MRPUpdate extends SvrProcess
+"t.m_product_id, t.m_warehouse_id," +"t.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040,'N') , null," +"nextidfunc(53040,'N') , null,"
+"t.QtyOrdered-t.QtyDelivered, 'D', 'MOP', t.updated, t.updatedby, o.DocumentNo," +"t.QtyOrdered-t.QtyDelivered, 'D', 'MOP', t.updated, t.updatedby, o.DocumentNo,"
+"t.ad_client_id ," +"t.ad_client_id ,"+S_Resource_ID
+S_Resource_ID +" FROM C_OrderLine t"
+" FROM C_OrderLine t INNER JOIN C_Order o ON (o.c_order_id=t.c_order_id) WHERE (t.QtyOrdered - t.QtyDelivered) <> 0 AND o.DocStatus IN ('IP','CO') AND t.AD_Client_ID = " + m_AD_Client_ID; +" INNER JOIN C_Order o ON (o.c_order_id=t.c_order_id)"
+" WHERE (t.QtyOrdered - t.QtyDelivered) <> 0 AND o.DocStatus IN ('IP','CO')"
if(DB.executeUpdate(sql + sql_insert + where , get_TrxName()) < 0) return false; +" AND t.AD_Client_ID = " + m_AD_Client_ID;
DB.executeUpdateEx(sql + sql_insert + where , get_TrxName());
//
// Insert from M_RequisitionLine // Insert from M_RequisitionLine
sql_insert = " SELECT rl.ad_org_id," sql_insert = " SELECT rl.ad_org_id,"
+"rl.created, rl.createdby , t.daterequired," +"rl.created, rl.createdby , t.daterequired,"
@ -317,13 +266,29 @@ public class MRPUpdate extends SvrProcess
+"rl.m_product_id, t.m_warehouse_id," +"rl.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040, 'N') , null," +"nextidfunc(53040, 'N') , null,"
+"rl.Qty, 'S', 'POR', rl.updated, rl.updatedby, t.DocumentNo," +"rl.Qty, 'S', 'POR', rl.updated, rl.updatedby, t.DocumentNo,"
+"rl.ad_client_id , " +"rl.ad_client_id , "+ S_Resource_ID
+ S_Resource_ID +" FROM M_RequisitionLine rl"
+" FROM M_RequisitionLine rl INNER JOIN M_Requisition t ON (rl.m_requisition_id=t.m_requisition_id) WHERE rl.Qty > 0 AND t.DocStatus IN ('CL') AND t.AD_Client_ID = " + m_AD_Client_ID; +" INNER JOIN M_Requisition t ON (rl.m_requisition_id=t.m_requisition_id)"
+" WHERE rl.Qty > 0 AND t.DocStatus IN ('CL') AND t.AD_Client_ID = " + m_AD_Client_ID;
if(DB.executeUpdate(sql + sql_insert + where , get_TrxName()) < 0) return false; DB.executeUpdateEx(sql + sql_insert + where , get_TrxName());
return true; return true;
} }
private void deletePO(String tableName, String whereClause, Object[] params)
{
// TODO: refactor this method and move it to org.compiere.model.Query class
POResultSet<PO> rs = new Query(getCtx(), tableName, whereClause, get_TrxName())
.setParameters(params)
.scroll();
try {
while(rs.hasNext()) {
rs.next().deleteEx(true);
}
}
finally {
rs.close();
}
}
} }