Search in sources :

Example 16 with MRequisition

use of org.compiere.model.MRequisition 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();
}
Also used : Calendar(java.util.Calendar) DecimalFormat(java.text.DecimalFormat) TreeMap(java.util.TreeMap) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException) Date(java.util.Date) TreeSet(java.util.TreeSet) MReplenishPlan(org.compiere.model.MReplenishPlan) MRequisition(org.compiere.model.MRequisition)

Example 17 with MRequisition

use of org.compiere.model.MRequisition in project adempiere by adempiere.

the class CalculateReplenishPlan method createRequisitionHeader.

/**
	 * Create Requisition without implementation of QtyBatchSize logic
	 * 
	 * @param mapRequisition
	 * @param date
	 * @return
	 */
private MRequisition createRequisitionHeader(Date date) {
    MRequisition requisition = null;
    if (!mapRequisition.containsKey(date)) {
        requisition = createRequisition(date);
        mapRequisition.put(date, requisition);
        log.severe("New Requisition Header created. " + requisition.toString());
    } else
        requisition = mapRequisition.get(date);
    return requisition;
}
Also used : MRequisition(org.compiere.model.MRequisition)

Example 18 with MRequisition

use of org.compiere.model.MRequisition in project adempiere by adempiere.

the class CalculateReplenishPlan method calculateBatchSizeWiseOrderCreation.

/**
	 * Create Planned Production / Requisition Order as per QtyBatchSize on
	 * Product.
	 * 
	 * @param mrp
	 * @param qty
	 * @param date
	 * @return qtyForPlan
	 */
private BigDecimal calculateBatchSizeWiseOrderCreation(MiniMRPProduct mrp, BigDecimal qty, Timestamp date) {
    int createNoOfOrder = 1;
    BigDecimal QtyPlan = qty;
    if (mrp.getQtyBatchSize().compareTo(Env.ZERO) == 0) {
        setRequirePlanningQty(mrp, date, qty);
        return qty;
    } else {
        createNoOfOrder = qty.divide(mrp.getQtyBatchSize(), 0, BigDecimal.ROUND_CEILING).intValue();
        QtyPlan = mrp.getQtyBatchSize();
    }
    BigDecimal qtyPlanned = QtyPlan.multiply(new BigDecimal(createNoOfOrder));
    mrp.setExtraQtyPlanned(mrp.getExtraQtyPlanned().add(qtyPlanned).subtract(qty));
    for (int i = 0; i < createNoOfOrder; i++) {
        if (mrp.isBOM())
            createProductionOrder(mrp, QtyPlan, date);
        else {
            //				MRequisition requisition = createRequisition(date);
            MRequisition requisition = createRequisitionHeader(date);
            createRequisitionLine(requisition, mrp, QtyPlan);
        }
    }
    return qtyPlanned;
}
Also used : BigDecimal(java.math.BigDecimal) MRequisition(org.compiere.model.MRequisition)

Aggregations

MRequisition (org.compiere.model.MRequisition)18 MRequisitionLine (org.compiere.model.MRequisitionLine)8 Timestamp (java.sql.Timestamp)5 BigDecimal (java.math.BigDecimal)3 TreeMap (java.util.TreeMap)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MOrder (org.compiere.model.MOrder)3 MOrderLine (org.compiere.model.MOrderLine)3 Date (java.util.Date)2 MForecastLine (org.compiere.model.MForecastLine)2 MWarehouse (org.compiere.model.MWarehouse)2 Query (org.compiere.model.Query)2 X_T_Replenish (org.compiere.model.X_T_Replenish)2 MDDOrder (org.eevolution.model.MDDOrder)2 MPPOrder (org.eevolution.model.MPPOrder)2 SQLException (java.sql.SQLException)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1