This change let lock more short, the reason is when you have big volume of record is best execute sort deletes and insert, this case you create the record for resource, organization , warehouse

This commit is contained in:
vpj-cd 2008-09-11 04:08:23 +00:00
parent 42b66d7d2d
commit f0e1413491
1 changed files with 163 additions and 125 deletions

View File

@ -24,10 +24,12 @@ import java.util.logging.Level;
import org.compiere.model.MNote;
import org.compiere.model.MOrderLine;
import org.compiere.model.MOrg;
import org.compiere.model.MRequisition;
import org.compiere.model.MRequisitionLine;
import org.compiere.model.MResource;
import org.compiere.model.MTable;
import org.compiere.model.MWarehouse;
import org.compiere.model.PO;
import org.compiere.model.POResultSet;
import org.compiere.model.Query;
@ -36,6 +38,7 @@ import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.eevolution.model.MDDOrder;
import org.eevolution.model.MForecastLine;
import org.eevolution.model.MPPMRP;
import org.eevolution.model.MPPOrder;
@ -95,49 +98,120 @@ public class MRPUpdate extends SvrProcess
*/
protected String doIt() throws Exception
{
deleteRecords();
createRecords();
return Msg.getMsg(getCtx(), "ProcessOK");
String result = null;
ArrayList <Object> parameters = new ArrayList<Object>();
StringBuffer whereClause = new StringBuffer(MResource.COLUMNNAME_ManufacturingResourceType+"=? AND AD_Client_ID=?");
parameters.add(MResource.MANUFACTURINGRESOURCETYPE_Plant);
parameters.add(m_AD_Client_ID);
if (p_S_Resource_ID > 0)
{
whereClause.append(" AND S_Resource_ID=?");
parameters.add(p_S_Resource_ID);
}
List <MResource> plants = new Query(getCtx(), MResource.Table_Name, whereClause.toString(), get_TrxName())
.setParameters(parameters)
.list();
for(MResource plant : plants)
{
log.info("Run MRP to Plant: " + plant.getName());
parameters = new ArrayList<Object>();
whereClause = new StringBuffer("AD_Client_ID=?");
parameters.add(m_AD_Client_ID);
if (p_AD_Org_ID > 0)
{
whereClause.append(" AND AD_Org_ID=?");
parameters.add(p_AD_Org_ID);
}
List <MOrg> organizations = new Query(getCtx(),MOrg.Table_Name, whereClause.toString(), get_TrxName())
.setParameters(parameters)
.list();
for (MOrg organization : organizations)
{
log.info("Run MRP to Organization: " + organization.getName());
if(p_M_Warehouse_ID==0)
{
MWarehouse[] ws = MWarehouse.getForOrg(getCtx(), organization.getAD_Org_ID());
for(MWarehouse w : ws)
{
log.info("Run MRP to Wharehouse: " + w.getName());
deleteRecords(m_AD_Client_ID,organization.getAD_Org_ID(),plant.getS_Resource_ID(),w.getM_Warehouse_ID());
createRecords(m_AD_Client_ID,organization.getAD_Org_ID(),plant.getS_Resource_ID(),w.getM_Warehouse_ID());
result = result + "<br>finish MRP to Warehouse " +w.getName();
}
}
else
{
log.info("Run MRP to Wharehouse: " + p_M_Warehouse_ID);
deleteRecords(m_AD_Client_ID,organization.getAD_Org_ID(),plant.getS_Resource_ID(),p_M_Warehouse_ID);
createRecords(m_AD_Client_ID,organization.getAD_Org_ID(),plant.getS_Resource_ID(),p_M_Warehouse_ID);
}
result = result + "<br>finish MRP to Organization " +organization.getName();
}
result = result + "<br>finish MRP to Plant " +plant.getName();
}
return result + Msg.getMsg(getCtx(), "ProcessOK");
}
/**
* Delete MRP records
*/
private void deleteRecords()
private void deleteRecords(int AD_Client_ID, int AD_Org_ID,int S_Resource_ID, int M_Warehouse_ID)
{
// Delete MRP records (Orders, Forecasts, Material Requisitions):
// Delete MRP records (Orders (SO,PO), Forecasts, Material Requisitions):
{
List<Object> params = new ArrayList<Object>();
String whereClause = "OrderType IN ('MOP','FCT','POR', 'SOO', 'POO') AND "+getWhereClause(params, MPPMRP.Table_Name, null);
params.add(AD_Client_ID);
params.add(AD_Org_ID);
params.add(M_Warehouse_ID);
String whereClause = "OrderType IN ('FCT','POR', 'SOO', 'POO') AND AD_Client_ID=? AND AD_Org_ID=? AND M_Warehouse_ID=?";
executeUpdate("DELETE FROM PP_MRP WHERE "+whereClause, params);
//Delete Material Requisitions Document
whereClause = "DocStatus='DR' AND AD_Client_ID=? AND AD_Org_ID=? AND M_Warehouse_ID=?";
deletePO(MRequisition.Table_Name, whereClause, params);
// Delete Distribution Orders:
deletePO(MDDOrder.Table_Name, whereClause, params);
}
{
List<Object> params = new ArrayList<Object>();
params.add(AD_Client_ID);
params.add(AD_Org_ID);
params.add(S_Resource_ID);
params.add(M_Warehouse_ID);
String whereClause = "OrderType IN ('MOP','DOO') AND AD_Client_ID=? AND AD_Org_ID=? AND S_Resource_ID= ? AND M_Warehouse_ID=?";
executeUpdate("DELETE FROM PP_MRP WHERE "+whereClause, params);
// Delete Mfg. Orders:
whereClause = "DocStatus='DR' AND AD_Client_ID=? AND AD_Org_ID=? AND S_Resource_ID= ? AND M_Warehouse_ID=?";
deletePO(MPPOrder.Table_Name, whereClause, params);
}
// Delete notes:
{
List<Object> params = new ArrayList<Object>();
String whereClause = "AD_Table_ID="+MPPMRP.Table_ID+" AND "+getWhereClause(params, MNote.Table_Name, null);
params.add(AD_Client_ID);
params.add(AD_Org_ID);
String whereClause = "AD_Table_ID="+MPPMRP.Table_ID+" AND AD_Client_ID=? AND AD_Org_ID=?";
executeUpdate("DELETE FROM AD_Note WHERE "+whereClause, params);
}
// Delete Mfg. Orders:
{
List<Object> params = new ArrayList<Object>();
String whereClause = "DocStatus='DR' AND "+getWhereClause(params, MPPOrder.Table_Name, null);
deletePO(MPPOrder.Table_Name, whereClause, params);
}
// Delete Material Requisitions:
{
List<Object> params = new ArrayList<Object>();
String whereClause = "DocStatus='DR' AND "+getWhereClause(params, MRequisition.Table_Name, null);
deletePO(MRequisition.Table_Name, whereClause, params);
}
}
/**
* Create MRP records
*/
private void createRecords ()
private void createRecords (int AD_Client_ID, int AD_Org_ID,int S_Resource_ID, int M_Warehouse_ID)
{
final String sql = "INSERT INTO PP_MRP ("
+"ad_org_id, created, createdby , dateordered,"
@ -155,6 +229,11 @@ public class MRPUpdate extends SvrProcess
//
//Insert from M_ForecastLine
List<Object> params = new ArrayList<Object>();
params.add(AD_Client_ID);
params.add(AD_Org_ID);
params.add(M_Warehouse_ID);
String sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , t.datepromised,"
+"t.datepromised, t.datepromised, t.datepromised, f.Name,"
@ -163,59 +242,17 @@ public class MRPUpdate extends SvrProcess
+ "null, null,"
+ "null, null,"
+ "null, null,"
+"t.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040,'N') , null,"
+"t.m_product_id, t.m_warehouse_id,"
+ "nextidfunc(53040,'Y'), null ,"
+"t.qty, 'D', 'FCT', t.updated, t.updatedby, f.Name,"
+"t.ad_client_id , null as S_Resource_ID, null as C_BPartner_ID "
+" FROM M_ForecastLine t "
+" INNER JOIN M_Forecast f ON (f.M_Forecast_ID=t.M_Forecast_ID) "
+" WHERE t.Qty > 0 AND "+getWhereClause(params, MForecastLine.Table_Name, "t");
executeUpdate(sql + sql_insert, params);
//
//Insert from PP_Order
params = new ArrayList<Object>();
sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , t.datepromised,"
+"t.datepromised, t.datepromised, t.datepromised, t.DocumentNo,"
+"t.DocStatus, t.isactive , "
+" null, null, "
+"t.pp_order_id, null,"
+" null, null, "
+" null, null, "
+"t.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040,'N') , null,"
+"t.QtyOrdered-t.QtyDelivered, 'S', 'MOP', t.updated, t.updatedby, t.DocumentNo,"
+"t.ad_client_id, t.S_Resource_ID, null as C_BPartner_ID "
+" FROM PP_Order t "
+" WHERE (t.QtyOrdered - t.QtyDelivered) <> 0 AND t.DocStatus IN ('IP','CO')"
+" AND "+getWhereClause(params, MPPOrder.Table_Name, "t");
executeUpdate(sql + sql_insert, params);
//
//Insert from PP_Order_BOMLine
params = new ArrayList<Object>();
sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , o.datepromised,"
+"o.datepromised, o.datepromised, o.datepromised, o.DocumentNo,"
+"o.DocStatus, o.isactive , "
+" null, null, "
+"t.pp_order_id, t.pp_order_bomline_id,"
+" null, null, "
+" null, null, "
+"t.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040,'N') , null,"
+"t.QtyRequiered-t.QtyDelivered, 'D', 'MOP', t.updated, t.updatedby, o.DocumentNo,"
+"t.ad_client_id, o.S_Resource_ID, null as C_BPartner_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 "+getWhereClause(params, MPPOrderBOMLine.Table_Name, "t");
executeUpdate(sql + sql_insert , params);
//
+" WHERE t.Qty > 0 AND "
+"t.AD_Client_ID=? AND t.AD_Org_ID=? AND t.M_Warehouse_ID= ?";
executeUpdate(sql + sql_insert, params);
// Insert from C_OrderLine
params = new ArrayList<Object>();
sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , t.datepromised,"
+"t.datepromised, t.datepromised, t.datepromised, o.DocumentNo,"
@ -225,18 +262,17 @@ public class MRPUpdate extends SvrProcess
+" t.c_order_id, t.c_orderline_id, "
+" null, null, "
+"t.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040,'N') , null,"
+ "nextidfunc(53040,'Y'), null ,"
+"t.QtyOrdered-t.QtyDelivered, 'D', (case when o.IsSOTrx='Y' then 'SOO' else 'POO' end), t.updated, t.updatedby, o.DocumentNo,"
+"t.ad_client_id , null as S_Resource_ID, o.C_BPartner_ID"
+" 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 "+getWhereClause(params, MOrderLine.Table_Name, "t");
executeUpdate(sql + sql_insert, params);
//
+" WHERE (t.QtyOrdered - t.QtyDelivered) <> 0 AND o.DocStatus IN ('IP','CO') AND "
+"t.AD_Client_ID=? AND t.AD_Org_ID=? AND t.M_Warehouse_ID= ?";
executeUpdate(sql + sql_insert, params);
// Insert from M_RequisitionLine
params = new ArrayList<Object>();
sql_insert = " SELECT rl.ad_org_id,"
+"rl.created, rl.createdby , t.daterequired,"
+" t.daterequired, t.daterequired, t.daterequired, t.DocumentNo,"
@ -246,58 +282,59 @@ public class MRPUpdate extends SvrProcess
+" null, null, "
+"rl.m_requisition_id, rl.m_requisitionline_id, "
+"rl.m_product_id, t.m_warehouse_id,"
+"nextidfunc(53040, 'N') , null,"
+ "nextidfunc(53040,'Y'), null ,"
+"rl.Qty, 'S', 'POR', rl.updated, rl.updatedby, t.DocumentNo,"
+"rl.ad_client_id , null as S_Resource_ID, null as C_BPartner_ID "
+" 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 "+getWhereClause(params, MRequisitionLine.Table_Name, "rl");
executeUpdate(sql + sql_insert, params);
+" WHERE rl.Qty > 0 AND t.DocStatus IN ('DR','IN') AND "
+"t.AD_Client_ID=? AND t.AD_Org_ID=? AND t.M_Warehouse_ID= ?";
executeUpdate(sql + sql_insert, params);
//Insert from PP_Order
params = new ArrayList<Object>();
params.add(AD_Client_ID);
params.add(AD_Org_ID);
params.add(S_Resource_ID);
params.add(M_Warehouse_ID);
sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , t.datepromised,"
+"t.datepromised, t.datepromised, t.datepromised, t.DocumentNo,"
+"t.DocStatus, t.isactive , "
+" null, null, "
+"t.pp_order_id, null,"
+" null, null, "
+" null, null, "
+"t.m_product_id, t.m_warehouse_id,"
+ "nextidfunc(53040,'Y'), null ,"
+"t.QtyOrdered-t.QtyDelivered, 'S', 'MOP', t.updated, t.updatedby, t.DocumentNo,"
+"t.ad_client_id, t.S_Resource_ID, null as C_BPartner_ID "
+" FROM PP_Order t "
+" WHERE (t.QtyOrdered - t.QtyDelivered) <> 0 AND t.DocStatus IN ('IP','CO') AND "
+"t.AD_Client_ID=? AND t.AD_Org_ID=? AND t.S_Resource_ID=? AND t.M_Warehouse_ID= ?";
executeUpdate(sql + sql_insert, params);
//
//Insert from PP_Order_BOMLine
sql_insert = " SELECT t.ad_org_id,"
+"t.created, t.createdby , o.datepromised,"
+"o.datepromised, o.datepromised, o.datepromised, o.DocumentNo,"
+"o.DocStatus, o.isactive , "
+" null, null, "
+"t.pp_order_id, t.pp_order_bomline_id,"
+" null, null, "
+" null, null, "
+"t.m_product_id, t.m_warehouse_id,"
+ "nextidfunc(53040,'Y'), null ,"
+"t.QtyRequiered-t.QtyDelivered, 'D', 'MOP', t.updated, t.updatedby, o.DocumentNo,"
+"t.ad_client_id, o.S_Resource_ID, null as C_BPartner_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=? AND t.AD_Org_ID=? AND o.S_Resource_ID=? AND t.M_Warehouse_ID= ?";
executeUpdate(sql + sql_insert , params);
}
private String getWhereClause(List<Object> params, String tableName, String tableAlias)
{
boolean filterWarehouse = MTable.get(getCtx(), tableName).getColumn("M_Warehouse_ID") != null;
boolean filterResource = MTable.get(getCtx(), tableName).getColumn("S_Resource_ID") != null;
String alias = (tableAlias != null ? tableAlias : tableName) + ".";
StringBuffer sb = new StringBuffer();
//
// AD_Client
sb.append(alias).append("AD_Client_ID=?");
params.add(m_AD_Client_ID);
//
// AD_Org
if (p_AD_Org_ID > 0) {
sb.append(" AND ").append(alias).append("AD_Org_ID=? ");
params.add(p_AD_Org_ID);
}
//
// M_Warehouse
if (filterWarehouse) {
if (p_M_Warehouse_ID > 0) {
sb.append(" AND ").append(alias).append("M_Warehouse_ID=?");
params.add(p_M_Warehouse_ID);
}
}
//
// Resource
if (filterResource) {
if (p_S_Resource_ID > 0) {
sb.append(" AND ").append(alias).append("S_Resource_ID=? ");
params.add(p_S_Resource_ID);
}
else {
sb.append(" AND EXISTS (SELECT 1 FROM S_Resource zz WHERE ")
.append("zz."+MResource.COLUMNNAME_ManufacturingResourceType+"=?")
.append(" AND zz.AD_Client_ID="+alias+"AD_Client_ID")
.append(") ");
params.add(MResource.MANUFACTURINGRESOURCETYPE_Plant);
}
}
//
return sb.toString();
}
private void executeUpdate(String sql, List<Object> params)
{
@ -309,11 +346,12 @@ public class MRPUpdate extends SvrProcess
else
{
pa = new Object[]{};
}
}
int no = DB.executeUpdateEx(sql, pa, get_TrxName());
commit();
log.fine("#"+no+" -- "+sql);
}