use of org.compiere.model.MReplenishPlan in project adempiere by adempiere.
the class GenerateReplenishPlanReport method doIt.
@Override
protected String doIt() throws Exception {
MReplenishPlan run = new MReplenishPlan(getCtx(), getRecord_ID(), get_TrxName());
dateFrom = run.getDateStart();
dateTo = run.getDateFinish();
if (dateFrom == null)
throw new IllegalArgumentException(Msg.translate(getCtx(), "FillMandatory") + Msg.translate(getCtx(), "DatePosted - From"));
if (dateTo == null)
throw new IllegalArgumentException(Msg.translate(getCtx(), "FillMandatory") + Msg.translate(getCtx(), "DatePosted - To"));
int isAfterDate = dateTo.compareTo(dateFrom);
if (isAfterDate > 0) {
START_WEEK = DB.getSQLValue(get_TrxName(), "SELECT EXTRACT( WEEK FROM ?::Timestamp )", dateFrom) - 2;
END_WEEK = DB.getSQLValue(get_TrxName(), CalculateReplenishPlan.SQL_GET_ISO_WEEKNO, dateFrom, dateTo, dateTo, dateTo) + 2;
if (START_WEEK == 0) {
START_WEEK = DB.getSQLValue(get_TrxName(), "SELECT EXTRACT(WEEK FROM (DATE_TRUNC('YEAR', ?::DATE) + interval '-1 day')) ", dateFrom);
END_WEEK += START_WEEK;
}
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(dateFrom);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.WEEK_OF_YEAR, -2);
dateFrom.setTime(cal.getTimeInMillis());
int weekDifference = END_WEEK - START_WEEK;
cal.setTime(dateFrom);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.WEEK_OF_YEAR, weekDifference);
dateTo.setTime(cal.getTimeInMillis());
if (weekDifference > 24) {
throw new IllegalArgumentException(Msg.translate(getCtx(), "Week difference should not be greater than 20 for selected horizon.") + Msg.translate(getCtx(), ""));
}
} else {
throw new IllegalArgumentException(Msg.translate(getCtx(), "ToDate must me greater than selected FromDate") + Msg.translate(getCtx(), ""));
}
addWeekDateInfo();
if (Ini.isClient())
getProcessInfo().setTransientObject(getPrintFormat());
else
getProcessInfo().setSerializableObject(getPrintFormat());
return null;
}
use of org.compiere.model.MReplenishPlan in project adempiere by adempiere.
the class CalculateReplenishPlan method doIt.
@Override
protected String doIt() throws Exception {
Timestamp t1 = new Timestamp(System.currentTimeMillis());
log.config("Start DoIt: " + sdf.format(t1));
MReplenishPlan run = new MReplenishPlan(ctx, mrpRunID, trx);
StringBuilder error = new StringBuilder();
dateFrom = run.getDateStart();
dateTo = run.getDateFinish();
M_PriceList_ID = run.getM_PriceList_ID();
docType_PlannedOrder = run.getC_DocType_PlannedOrder();
docType_ConfirmedOrder = run.getC_DocType_ConfirmedOrder();
docType_PurchaseOrder = run.getC_DocType_PO();
docType_MRPRequisition = run.getC_DocType_Requisition();
if (docType_PlannedOrder <= 0)
error.append("No Mfg Planned Order Document set. \n");
if (docType_ConfirmedOrder <= 0)
error.append("No Confirmed Mfg Order Document set. \n");
if (docType_PurchaseOrder <= 0)
error.append("No Purchase Order Document set. \n");
if (docType_MRPRequisition <= 0)
error.append("No MRP Requisition Document set. \n");
if (error.length() > 0) {
throw new Exception(error.toString());
}
String sql = "DELETE FROM M_ReplenishPlanLine WHERE M_ReplenishPlan_ID=? AND AD_Client_ID=?";
int noOfLinesDeleted = DB.executeUpdateEx(sql, new Object[] { mrpRunID, Env.getAD_Client_ID(ctx) }, trx);
log.fine("No. of MRP lines deleted : " + noOfLinesDeleted);
if (dateFrom == null)
throw new IllegalArgumentException(Msg.translate(ctx, "FillMandatory") + Msg.translate(ctx, "DatePosted - From"));
if (dateTo == null)
throw new IllegalArgumentException(Msg.translate(ctx, "FillMandatory") + Msg.translate(ctx, "DatePosted - To"));
if (M_PriceList_ID == 0)
throw new IllegalArgumentException(Msg.translate(ctx, "FillMandatory") + Msg.translate(ctx, "M_PriceList_ID"));
int isAfterDate = dateTo.compareTo(dateFrom);
if (isAfterDate > 0) {
START_WEEK = DB.getSQLValue(trx, "SELECT EXTRACT( WEEK FROM ?::Timestamp )", dateFrom) - 2;
END_WEEK = DB.getSQLValue(trx, SQL_GET_ISO_WEEKNO, dateFrom, dateTo, dateTo, dateTo) + 2;
if (START_WEEK == 0) {
START_WEEK = DB.getSQLValue(trx, "SELECT EXTRACT(WEEK FROM (DATE_TRUNC('YEAR', ?::DATE) + interval '-1 day')) ", dateFrom);
END_WEEK += START_WEEK;
}
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(dateFrom);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.WEEK_OF_YEAR, -2);
dateFrom.setTime(cal.getTimeInMillis());
int weekDifference = END_WEEK - START_WEEK;
cal.setTime(dateFrom);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.WEEK_OF_YEAR, weekDifference);
dateTo.setTime(cal.getTimeInMillis());
if (weekDifference > 24) {
throw new IllegalArgumentException(Msg.translate(ctx, "Week difference should not be greater than 20 for selected horizon."));
}
} else {
throw new IllegalArgumentException(Msg.translate(ctx, "ToDate must me greater than selected FromDate"));
}
if (run.isDeleteUnconfirmedProduction()) {
sql = "DELETE FROM M_ProductionLine WHERE M_Production_ID IN (SELECT M_Production_ID FROM M_Production WHERE Processed='N' AND C_DocType_ID = ?)";
noOfLinesDeleted = DB.executeUpdate(sql, docType_PlannedOrder, trx);
log.fine("No. of planned production line deleted : " + noOfLinesDeleted);
sql = "DELETE FROM M_Production WHERE Processed='N' AND C_DocType_ID = ? ";
noOfLinesDeleted = DB.executeUpdate(sql, docType_PlannedOrder, trx);
log.fine("No. of planned production deleted : " + noOfLinesDeleted);
sql = "DELETE FROM m_productionbatch b " + "WHERE b.c_doctype_id = ? " + "AND NOT EXISTS (SELECT * " + " FROM m_production " + " WHERE m_productionbatch_id = b.m_productionbatch_id)";
noOfLinesDeleted = DB.executeUpdate(sql, docType_PlannedOrder, trx);
log.fine("No. of Production Batch deleted " + noOfLinesDeleted);
sql = "DELETE FROM M_MovementLine ml USING M_Movement m " + " WHERE m.M_ProductionBatch_ID IS NOT NULL AND m.Processed = 'N' " + " AND NOT EXISTS (SELECT * FROM M_ProductionBatch b WHERE b.M_ProductionBatch_ID = m.M_ProductionBatch_ID)";
noOfLinesDeleted = DB.executeUpdate(sql, trx);
log.fine("No. of Movement Lines cleaned : " + noOfLinesDeleted);
sql = "DELETE FROM M_Movement m " + " WHERE m.M_ProductionBatch_ID IS NOT NULL AND m.Processed = 'N'" + " AND NOT EXISTS (SELECT * FROM M_ProductionBatch b WHERE b.M_ProductionBatch_ID = m.M_ProductionBatch_ID)";
noOfLinesDeleted = DB.executeUpdate(sql, trx);
log.fine("No. of Inventory Movements cleaned : " + noOfLinesDeleted);
}
if (run.isDeletePlannedPO()) {
String selectRequisition = " (SELECT M_Requisition_ID FROM M_Requisition WHERE DocStatus IN ('DR') AND Processed='N' AND AD_Client_ID = ? AND C_DocType_ID = ?) ";
sql = "DELETE FROM PP_MRP WHERE M_Requisition_ID IN " + selectRequisition;
noOfLinesDeleted = DB.executeUpdateEx(sql, new Object[] { AD_Client_ID, docType_MRPRequisition }, trx);
log.fine("No. of Material Requirement Planning (PP_MRP) Line deleted : " + noOfLinesDeleted);
sql = "DELETE FROM M_RequisitionLine WHERE M_Requisition_ID IN " + selectRequisition;
noOfLinesDeleted = DB.executeUpdateEx(sql, new Object[] { AD_Client_ID, docType_MRPRequisition }, trx);
log.fine("No. of MRP Requisition Line deleted : " + noOfLinesDeleted);
sql = "DELETE FROM M_Requisition WHERE DocStatus IN ('DR') AND Processed='N' AND AD_Client_ID = ? AND C_DocType_ID = ? ";
noOfLinesDeleted = DB.executeUpdateEx(sql, new Object[] { AD_Client_ID, docType_MRPRequisition }, trx);
log.fine("No. of MRP Requisition deleted : " + noOfLinesDeleted);
}
Map<Integer, MiniMRPProduct> miniMrpProducts = new TreeMap<Integer, MiniMRPProduct>();
Set<Integer> productIds = new TreeSet<Integer>();
// Collect all the Products required to be processed.
log.config("START generateProductInfo:" + sdf.format(new Date()));
generateProductInfo(miniMrpProducts, productIds);
log.config("END generateProductInfo:" + sdf.format(new Date()));
// Process Demand
log.config("START doRunProductsSO:" + sdf.format(new Date()));
doRunProductsSO(miniMrpProducts, productIds);
log.config("END doRunProductsSO:" + sdf.format(new Date()));
// Creating Requisition Order and Planned Production Order
log.config("START doRunCreatePOandProductionOrder:" + sdf.format(new Date()));
doRunCreatePOandProductionOrder(miniMrpProducts);
log.config("END doRunCreatePOandProductionOrder:" + sdf.format(new Date()));
// Save Requisition Lines to Database
for (Date date : mapRequisition.keySet()) {
MRequisition requisition = mapRequisition.get(date);
//requisition.saveLineQueue();
log.config("START: Write to DB Requisition Line " + requisition.toString() + " " + sdf.format(new Date()));
log.config("END: Write to DB Requisition Line " + requisition.toString() + " " + sdf.format(new Date()));
}
// Process Supply
log.config("START doRunOpenOrders:TYPE_PO" + sdf.format(new Date()));
doRunOpenOrders(miniMrpProducts, productIds, TYPE_PO);
log.config("END doRunOpenOrders:TYPE_MO" + sdf.format(new Date()));
log.config("START doRunOpenOrders:TYPE_MO" + sdf.format(new Date()));
doRunOpenOrders(miniMrpProducts, productIds, TYPE_MO);
log.config("END doRunOpenOrders:TYPE_MO" + sdf.format(new Date()));
log.config("START doRunOpenOrders:TYPE_RQ" + sdf.format(new Date()));
doRunOpenOrders(miniMrpProducts, productIds, TYPE_RQ);
log.config("END doRunOpenOrders:TYPE_RQ" + sdf.format(new Date()));
log.config("START renderPeggingReport" + sdf.format(new Date()));
renderPeggingReport(miniMrpProducts);
log.config("END renderPeggingReport" + sdf.format(new Date()));
log.config("START updateHasSupplyDemand" + sdf.format(new Date()));
updateHasSupplyDemand();
log.config("END updateHasSupplyDemand" + sdf.format(new Date()));
Timestamp t2 = new Timestamp(System.currentTimeMillis());
log.config("END DoIt: " + sdf.format(t2) + "\n\n Time Diff Millis: " + new DecimalFormat("###,###").format(t2.getTime() - t1.getTime()));
return infoMsg.toString();
}
Aggregations