Search in sources :

Example 1 with MDistributionRun

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

the class DistributionRun method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message (text with variables)
	 *  @throws Exception if not successful
	 */
protected String doIt() throws Exception {
    log.info("M_DistributionRun_ID=" + p_M_DistributionRun_ID + ", C_DocType_ID=" + p_C_DocType_ID + ", DatePromised=" + p_DatePromised + ", Test=" + p_IsTest);
    //	Distribution Run
    if (p_M_DistributionRun_ID == 0)
        throw new IllegalArgumentException("No Distribution Run ID");
    m_run = new MDistributionRun(getCtx(), p_M_DistributionRun_ID, get_TrxName());
    if (m_run.get_ID() == 0)
        throw new Exception("Distribution Run not found -  M_DistributionRun_ID=" + p_M_DistributionRun_ID);
    m_runLines = m_run.getLines(true);
    if (m_runLines == null || m_runLines.length == 0)
        throw new Exception("No active, non-zero Distribution Run Lines found");
    //	Document Type
    if (p_C_DocType_ID == 0)
        throw new IllegalArgumentException("No Document Type ID");
    //	outside trx
    m_docType = new MDocType(getCtx(), p_C_DocType_ID, null);
    if (m_docType.get_ID() == 0)
        throw new Exception("Document Type not found -  C_DocType_ID=" + p_C_DocType_ID);
    //
    m_DateOrdered = new Timestamp(System.currentTimeMillis());
    if (p_DatePromised == null)
        p_DatePromised = m_DateOrdered;
    if (m_docType.getDocBaseType().equals(MDocType.DOCBASETYPE_DistributionOrder) & p_M_Warehouse_ID > 0) {
        if (p_BasedInDamnd) {
            if (insertDetailsDistributionDemand() == 0)
                throw new Exception("No Lines");
        } else //Create Temp Lines
        {
            if (insertDetailsDistribution() == 0)
                throw new Exception("No Lines");
        }
    } else {
        //	Create Temp Lines
        if (insertDetails() == 0)
            throw new Exception("No Lines");
    }
    //	Order By Distribution Run Line
    m_details = MDistributionRunDetail.get(getCtx(), p_M_DistributionRun_ID, false, get_TrxName());
    //	First Run -- Add & Round
    addAllocations();
    //	Do Allocation
    int loops = 0;
    while (!isAllocationEqTotal()) {
        adjustAllocation();
        addAllocations();
        if (++loops > 10)
            throw new Exception("Loop detected - more than 10 Allocation attempts");
    }
    //	Order By Business Partner
    m_details = MDistributionRunDetail.get(getCtx(), p_M_DistributionRun_ID, true, get_TrxName());
    //Implement Distribution Order
    if (m_docType.getDocBaseType().equals(MDocType.DOCBASETYPE_DistributionOrder)) {
        distributionOrders();
    } else {
        //	Create Orders
        createOrders();
    }
    return "@Created@ #" + m_counter;
}
Also used : MDocType(org.compiere.model.MDocType) MDistributionRun(org.compiere.model.MDistributionRun) Timestamp(java.sql.Timestamp) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 2 with MDistributionRun

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

the class DistributionRunOrders method generateDistribution.

//Create Distribution Run Line
public boolean generateDistribution() {
    m_run = new MDistributionRun(this.getCtx(), 0, this.get_TrxName());
    m_run.setName("Generate from DRP " + p_DatePromised);
    //m_run.setDescription("Generate from DRP");
    m_run.save();
    StringBuffer sql = new StringBuffer("SELECT M_Product_ID , SUM (QtyOrdered-QtyDelivered) AS TotalQty, l.M_Warehouse_ID FROM DD_OrderLine ol INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) ");
    sql.append(" WHERE o.DocStatus IN ('DR','IN') AND ol.DatePromised <= ? AND l.M_Warehouse_ID=? GROUP BY M_Product_ID");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        pstmt.setTimestamp(1, p_DatePromised);
        //pstmt.setTimestamp(2, p_DatePromised_To);
        pstmt.setInt(2, p_M_Warehouse_ID);
        rs = pstmt.executeQuery();
        int line = 10;
        while (rs.next()) {
            int M_Product_ID = rs.getInt("M_Product_ID");
            BigDecimal QtyAvailable = MStorage.getQtyAvailable(p_M_Warehouse_ID, 0, M_Product_ID, 0, get_TrxName());
            BigDecimal QtyOrdered = rs.getBigDecimal("TotalQty");
            MDistributionRunLine m_runLine = new MDistributionRunLine(getCtx(), 0, get_TrxName());
            m_runLine.setM_DistributionRun_ID(m_run.getM_DistributionRun_ID());
            m_runLine.setAD_Org_ID(p_AD_Org_ID);
            m_runLine.setM_DistributionList_ID(p_M_DistributionList_ID);
            m_runLine.setLine(line);
            m_runLine.setM_Product_ID(M_Product_ID);
            m_runLine.setDescription(Msg.getMsg(getCtx(), "QtyAvailable") + " : " + QtyAvailable + " " + Msg.getMsg(getCtx(), "QtyOrdered") + " : " + QtyOrdered);
            if (QtyOrdered.compareTo(QtyAvailable) > 0) {
                QtyOrdered = QtyAvailable;
            }
            m_runLine.setTotalQty(QtyOrdered);
            m_runLine.save();
            line += 10;
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "doIt - " + sql, e);
        return false;
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return true;
}
Also used : MDistributionRun(org.compiere.model.MDistributionRun) MDistributionRunLine(org.compiere.model.MDistributionRunLine) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 3 with MDistributionRun

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

the class DistributionRunOrders method groovy.

public String groovy(String A_TrxName, Properties A_Ctx, int P_M_Warehouse_ID, int P_M_PriceList_Version_ID, int P_M_DistributionList_ID) {
    MPriceListVersion plv = new MPriceListVersion(A_Ctx, P_M_PriceList_Version_ID, A_TrxName);
    MPriceList pl = new MPriceList(A_Ctx, plv.getM_PriceList_ID(), A_TrxName);
    MWarehouse w = new MWarehouse(A_Ctx, P_M_Warehouse_ID, A_TrxName);
    MDistributionRun dr = new MDistributionRun(A_Ctx, 0, A_TrxName);
    dr.setName(plv.getName());
    dr.setIsActive(true);
    dr.setAD_Org_ID(w.getAD_Org_ID());
    dr.saveEx();
    MProductPrice[] products = plv.getProductPrice(true);
    int seq = 10;
    for (MProductPrice pp : products) {
        int M_Product_ID = pp.getM_Product_ID();
        BigDecimal QtyAvailable = MStorage.getQtyAvailable(P_M_Warehouse_ID, M_Product_ID, 0, 0, A_TrxName);
        BigDecimal QtyOnHand = MPPMRP.getQtyOnHand(A_Ctx, P_M_Warehouse_ID, M_Product_ID, A_TrxName);
        MDistributionRunLine drl = new MDistributionRunLine(A_Ctx, 0, A_TrxName);
        drl.setM_DistributionRun_ID(dr.get_ID());
        drl.setLine(seq);
        drl.setM_Product_ID(M_Product_ID);
        drl.setM_DistributionList_ID(P_M_DistributionList_ID);
        drl.setDescription(Msg.translate(A_Ctx, "QtyAvailable") + " = " + QtyAvailable + " | " + Msg.translate(A_Ctx, "QtyOnHand") + " = " + QtyOnHand);
        drl.setTotalQty(QtyAvailable);
        drl.saveEx();
    }
    return "";
}
Also used : MDistributionRun(org.compiere.model.MDistributionRun) MDistributionRunLine(org.compiere.model.MDistributionRunLine) MPriceListVersion(org.compiere.model.MPriceListVersion) MPriceList(org.compiere.model.MPriceList) MWarehouse(org.compiere.model.MWarehouse) BigDecimal(java.math.BigDecimal) MProductPrice(org.compiere.model.MProductPrice)

Example 4 with MDistributionRun

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

the class DistributionRunOrders method generateDistributionDemand.

//Create Distribution Run Line
public boolean generateDistributionDemand() {
    m_run = new MDistributionRun(this.getCtx(), 0, null);
    m_run.setName("Generate from DRP " + p_DatePromised);
    m_run.save();
    StringBuffer sql = new StringBuffer("SELECT M_Product_ID , SUM (TargetQty) AS MinQty, SUM (QtyOrdered-QtyDelivered) AS TotalQty FROM DD_OrderLine ol INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) ");
    sql.append(" WHERE o.DocStatus IN ('DR','IN') AND ol.DatePromised <= ? AND l.M_Warehouse_ID=? GROUP BY M_Product_ID");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        pstmt.setTimestamp(1, p_DatePromised);
        //pstmt.setTimestamp(2, p_DatePromised_To);
        pstmt.setInt(2, p_M_Warehouse_ID);
        rs = pstmt.executeQuery();
        int line = 10;
        while (rs.next()) {
            int M_Product_ID = rs.getInt("M_Product_ID");
            BigDecimal QtyAvailable = MStorage.getQtyAvailable(p_M_Warehouse_ID, 0, M_Product_ID, 0, get_TrxName());
            if (QtyAvailable.signum() <= 0)
                continue;
            BigDecimal QtyToDistribute = rs.getBigDecimal("TotalQty");
            if (QtyAvailable.compareTo(QtyToDistribute) >= 0)
                QtyAvailable = QtyToDistribute;
            else {
                BigDecimal QtyReserved = getTargetQty(M_Product_ID);
                QtyToDistribute = QtyAvailable.subtract(QtyReserved);
            }
            //if(QtyToDistribute.compareTo(Env.ZERO)==0)
            //	continue;
            MDistributionRunLine m_runLine = new MDistributionRunLine(getCtx(), 0, get_TrxName());
            m_runLine.setM_DistributionRun_ID(m_run.getM_DistributionRun_ID());
            m_runLine.setAD_Org_ID(p_AD_Org_ID);
            m_runLine.setM_DistributionList_ID(p_M_DistributionList_ID);
            m_runLine.setLine(line);
            m_runLine.setM_Product_ID(M_Product_ID);
            m_runLine.setDescription(Msg.translate(getCtx(), "QtyAvailable") + " : " + QtyAvailable + " " + Msg.translate(getCtx(), "QtyOrdered") + " : " + QtyToDistribute);
            //m_runLine.setMinQty(rs.getBigDecimal("MinQty"));
            m_runLine.setTotalQty(QtyToDistribute);
            m_runLine.saveEx();
            line += 10;
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "doIt - " + sql, e);
        return false;
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return true;
}
Also used : MDistributionRun(org.compiere.model.MDistributionRun) MDistributionRunLine(org.compiere.model.MDistributionRunLine) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Aggregations

MDistributionRun (org.compiere.model.MDistributionRun)4 BigDecimal (java.math.BigDecimal)3 MDistributionRunLine (org.compiere.model.MDistributionRunLine)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 Timestamp (java.sql.Timestamp)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 MDocType (org.compiere.model.MDocType)1 MPriceList (org.compiere.model.MPriceList)1 MPriceListVersion (org.compiere.model.MPriceListVersion)1 MProductPrice (org.compiere.model.MProductPrice)1 MWarehouse (org.compiere.model.MWarehouse)1