Search in sources :

Example 76 with MOrderLine

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

the class Doc_Order method getCommitmentsSales.

//	getCommitmentRelease
/**
	 * 	Get Commitments Sales
	 * 	@param doc document
	 * 	@param maxQty Qty invoiced/matched
	 * 	@param C_OrderLine_ID invoice line
	 *	@return commitments (order lines)
	 */
protected static DocLine[] getCommitmentsSales(Doc doc, BigDecimal maxQty, int M_InOutLine_ID) {
    int precision = -1;
    //
    ArrayList<DocLine> list = new ArrayList<DocLine>();
    String sql = "SELECT * FROM C_OrderLine ol " + "WHERE EXISTS " + "(SELECT * FROM M_InOutLine il " + "WHERE il.C_OrderLine_ID=ol.C_OrderLine_ID" + " AND il.M_InOutLine_ID=?)";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, M_InOutLine_ID);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            if (maxQty.signum() == 0)
                continue;
            MOrderLine line = new MOrderLine(doc.getCtx(), rs, null);
            DocLine docLine = new DocLine(line, doc);
            //	Currency
            if (precision == -1) {
                doc.setC_Currency_ID(docLine.getC_Currency_ID());
                precision = MCurrency.getStdPrecision(doc.getCtx(), docLine.getC_Currency_ID());
            }
            //	Qty
            BigDecimal Qty = line.getQtyOrdered().max(maxQty);
            docLine.setQty(Qty, false);
            //
            BigDecimal PriceActual = line.getPriceActual();
            BigDecimal PriceCost = line.getPriceCost();
            BigDecimal LineNetAmt = null;
            if (PriceCost != null && PriceCost.signum() != 0)
                LineNetAmt = Qty.multiply(PriceCost);
            else if (Qty.equals(maxQty))
                LineNetAmt = line.getLineNetAmt();
            else
                LineNetAmt = Qty.multiply(PriceActual);
            maxQty = maxQty.subtract(Qty);
            //	DR
            docLine.setAmount(LineNetAmt);
            BigDecimal PriceList = line.getPriceList();
            int C_Tax_ID = docLine.getC_Tax_ID();
            //	Correct included Tax
            if (C_Tax_ID != 0 && line.getParent().isTaxIncluded()) {
                MTax tax = MTax.get(doc.getCtx(), C_Tax_ID);
                if (!tax.isZeroTax()) {
                    BigDecimal LineNetAmtTax = tax.calculateTax(LineNetAmt, true, precision);
                    s_log.fine("LineNetAmt=" + LineNetAmt + " - Tax=" + LineNetAmtTax);
                    LineNetAmt = LineNetAmt.subtract(LineNetAmtTax);
                    BigDecimal PriceListTax = tax.calculateTax(PriceList, true, precision);
                    PriceList = PriceList.subtract(PriceListTax);
                }
            }
            //	correct included Tax
            docLine.setAmount(LineNetAmt, PriceList, Qty);
            list.add(docLine);
        }
    } catch (Exception e) {
        s_log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //	Return Array
    DocLine[] dl = new DocLine[list.size()];
    list.toArray(dl);
    return dl;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MTax(org.compiere.model.MTax) MOrderLine(org.compiere.model.MOrderLine) BigDecimal(java.math.BigDecimal) SQLException(java.sql.SQLException)

Example 77 with MOrderLine

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

the class Doc_MatchPO method loadDocumentDetails.

/**
	 *  Load Specific Document Details
	 *  @return error message or null
	 */
protected String loadDocumentDetails() {
    setC_Currency_ID(Doc.NO_CURRENCY);
    MMatchPO matchPO = (MMatchPO) getPO();
    setDateDoc(matchPO.getDateTrx());
    //
    m_M_AttributeSetInstance_ID = matchPO.getM_AttributeSetInstance_ID();
    setQty(matchPO.getQty());
    //
    m_C_OrderLine_ID = matchPO.getC_OrderLine_ID();
    m_oLine = new MOrderLine(getCtx(), m_C_OrderLine_ID, getTrxName());
    //
    m_M_InOutLine_ID = matchPO.getM_InOutLine_ID();
    m_ioLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
    m_C_InvoiceLine_ID = matchPO.getC_InvoiceLine_ID();
    //
    m_pc = new ProductCost(Env.getCtx(), getM_Product_ID(), m_M_AttributeSetInstance_ID, getTrxName());
    m_pc.setQty(getQty());
    return null;
}
Also used : ProductCost(org.compiere.model.ProductCost) MMatchPO(org.compiere.model.MMatchPO) MInOutLine(org.compiere.model.MInOutLine) MOrderLine(org.compiere.model.MOrderLine)

Example 78 with MOrderLine

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

the class DistributionRun method createOrders.

//	adjustAllocation
/**************************************************************************
	 * 	Create Orders
	 * 	@return true if created
	 */
private boolean createOrders() {
    //	Get Counter Org/BP
    int runAD_Org_ID = m_run.getAD_Org_ID();
    if (runAD_Org_ID == 0)
        runAD_Org_ID = Env.getAD_Org_ID(getCtx());
    MOrg runOrg = MOrg.get(getCtx(), runAD_Org_ID);
    int runC_BPartner_ID = runOrg.getLinkedC_BPartner_ID(get_TrxName());
    boolean counter = //	no single Order 
    !m_run.isCreateSingleOrder() && //	Org linked to BP
    runC_BPartner_ID > 0 && //	PO
    !m_docType.isSOTrx();
    MBPartner runBPartner = counter ? new MBPartner(getCtx(), runC_BPartner_ID, get_TrxName()) : null;
    if (!counter || runBPartner == null || runBPartner.get_ID() != runC_BPartner_ID)
        counter = false;
    if (counter)
        log.info("RunBP=" + runBPartner + " - " + m_docType);
    log.info("Single=" + m_run.isCreateSingleOrder() + " - " + m_docType + ",SO=" + m_docType.isSOTrx());
    log.fine("Counter=" + counter + ",C_BPartner_ID=" + runC_BPartner_ID + "," + runBPartner);
    //
    MBPartner bp = null;
    MOrder singleOrder = null;
    MProduct product = null;
    //	Consolidated Order
    if (m_run.isCreateSingleOrder()) {
        bp = new MBPartner(getCtx(), m_run.getC_BPartner_ID(), get_TrxName());
        if (bp.get_ID() == 0)
            throw new IllegalArgumentException("Business Partner not found - C_BPartner_ID=" + m_run.getC_BPartner_ID());
        //
        if (!p_IsTest) {
            singleOrder = new MOrder(getCtx(), 0, get_TrxName());
            singleOrder.setC_DocTypeTarget_ID(m_docType.getC_DocType_ID());
            singleOrder.setC_DocType_ID(m_docType.getC_DocType_ID());
            singleOrder.setIsSOTrx(m_docType.isSOTrx());
            singleOrder.setBPartner(bp);
            if (m_run.getC_BPartner_Location_ID() != 0)
                singleOrder.setC_BPartner_Location_ID(m_run.getC_BPartner_Location_ID());
            singleOrder.setDateOrdered(m_DateOrdered);
            singleOrder.setDatePromised(p_DatePromised);
            if (!singleOrder.save()) {
                log.log(Level.SEVERE, "Order not saved");
                return false;
            }
            m_counter++;
        }
    }
    int lastC_BPartner_ID = 0;
    int lastC_BPartner_Location_ID = 0;
    MOrder order = null;
    //	For all lines
    for (int i = 0; i < m_details.length; i++) {
        MDistributionRunDetail detail = m_details[i];
        //	Create Order Header
        if (m_run.isCreateSingleOrder())
            order = singleOrder;
        else //	New Business Partner
        if (lastC_BPartner_ID != detail.getC_BPartner_ID() || lastC_BPartner_Location_ID != detail.getC_BPartner_Location_ID()) {
            //	finish order
            order = null;
        }
        lastC_BPartner_ID = detail.getC_BPartner_ID();
        lastC_BPartner_Location_ID = detail.getC_BPartner_Location_ID();
        //	New Order
        if (order == null) {
            bp = new MBPartner(getCtx(), detail.getC_BPartner_ID(), get_TrxName());
            if (!p_IsTest) {
                order = new MOrder(getCtx(), 0, get_TrxName());
                order.setC_DocTypeTarget_ID(m_docType.getC_DocType_ID());
                order.setC_DocType_ID(m_docType.getC_DocType_ID());
                order.setIsSOTrx(m_docType.isSOTrx());
                //	Counter Doc
                if (counter && bp.getAD_OrgBP_ID_Int() > 0) {
                    log.fine("Counter - From_BPOrg=" + bp.getAD_OrgBP_ID_Int() + "-" + bp + ", To_BP=" + runBPartner);
                    order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
                    MOrgInfo oi = MOrgInfo.get(getCtx(), bp.getAD_OrgBP_ID_Int(), get_TrxName());
                    if (oi.getM_Warehouse_ID() > 0)
                        order.setM_Warehouse_ID(oi.getM_Warehouse_ID());
                    order.setBPartner(runBPartner);
                } else //	normal
                {
                    log.fine("From_Org=" + runAD_Org_ID + ", To_BP=" + bp);
                    order.setAD_Org_ID(runAD_Org_ID);
                    order.setBPartner(bp);
                    if (detail.getC_BPartner_Location_ID() != 0)
                        order.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID());
                }
                order.setDateOrdered(m_DateOrdered);
                order.setDatePromised(p_DatePromised);
                if (!order.save()) {
                    log.log(Level.SEVERE, "Order not saved");
                    return false;
                }
            }
        }
        //	Line
        if (product == null || product.getM_Product_ID() != detail.getM_Product_ID())
            product = MProduct.get(getCtx(), detail.getM_Product_ID());
        if (p_IsTest) {
            addLog(0, null, detail.getActualAllocation(), bp.getName() + " - " + product.getName());
            continue;
        }
        //	Create Order Line
        MOrderLine line = new MOrderLine(order);
        if (counter && bp.getAD_OrgBP_ID_Int() > 0)
            //	don't overwrite counter doc
            ;
        else //	normal - optionally overwrite
        {
            line.setC_BPartner_ID(detail.getC_BPartner_ID());
            if (detail.getC_BPartner_Location_ID() != 0)
                line.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID());
        }
        //
        line.setProduct(product);
        line.setQty(detail.getActualAllocation());
        line.setPrice();
        if (!line.save()) {
            log.log(Level.SEVERE, "OrderLine not saved");
            return false;
        }
        addLog(0, null, detail.getActualAllocation(), order.getDocumentNo() + ": " + bp.getName() + " - " + product.getName());
    }
    //	finish order
    order = null;
    return true;
}
Also used : MOrder(org.compiere.model.MOrder) MProduct(org.compiere.model.MProduct) MDistributionRunDetail(org.compiere.model.MDistributionRunDetail) MOrg(org.compiere.model.MOrg) MOrgInfo(org.compiere.model.MOrgInfo) MBPartner(org.compiere.model.MBPartner) MOrderLine(org.compiere.model.MOrderLine)

Example 79 with MOrderLine

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

the class ExpenseSOrder method processLine.

//	doIt
/**
	 * 	Process Expense Line
	 *	@param te header
	 *	@param tel line
	 *	@param bp bp
	 */
private void processLine(MTimeExpense te, MTimeExpenseLine tel, MBPartner bp) {
    if (m_order == null) {
        log.info("New Order for " + bp + ", Project=" + tel.getC_Project_ID());
        m_order = new MOrder(getCtx(), 0, get_TrxName());
        m_order.setAD_Org_ID(tel.getAD_Org_ID());
        m_order.setC_DocTypeTarget_ID(MOrder.DocSubTypeSO_OnCredit);
        //
        m_order.setBPartner(bp);
        if (m_order.getC_BPartner_Location_ID() == 0) {
            log.log(Level.SEVERE, "No BP Location: " + bp);
            addLog(0, te.getDateReport(), null, "No Location: " + te.getDocumentNo() + " " + bp.getName());
            m_order = null;
            return;
        }
        m_order.setM_Warehouse_ID(te.getM_Warehouse_ID());
        if (tel.getC_Activity_ID() != 0)
            m_order.setC_Activity_ID(tel.getC_Activity_ID());
        if (tel.getC_Campaign_ID() != 0)
            m_order.setC_Campaign_ID(tel.getC_Campaign_ID());
        if (tel.getC_Project_ID() != 0) {
            m_order.setC_Project_ID(tel.getC_Project_ID());
            //	Optionally Overwrite BP Price list from Project
            MProject project = new MProject(getCtx(), tel.getC_Project_ID(), get_TrxName());
            if (project.getM_PriceList_ID() != 0)
                m_order.setM_PriceList_ID(project.getM_PriceList_ID());
        }
        m_order.setSalesRep_ID(te.getDoc_User_ID());
        //
        if (!m_order.save()) {
            throw new IllegalStateException("Cannot save Order");
        }
    } else {
        //	Update Header info
        if (tel.getC_Activity_ID() != 0 && tel.getC_Activity_ID() != m_order.getC_Activity_ID())
            m_order.setC_Activity_ID(tel.getC_Activity_ID());
        if (tel.getC_Campaign_ID() != 0 && tel.getC_Campaign_ID() != m_order.getC_Campaign_ID())
            m_order.setC_Campaign_ID(tel.getC_Campaign_ID());
        if (!m_order.save())
            new IllegalStateException("Cannot save Order");
    }
    //	OrderLine
    MOrderLine ol = new MOrderLine(m_order);
    //
    if (tel.getM_Product_ID() != 0)
        ol.setM_Product_ID(tel.getM_Product_ID(), tel.getC_UOM_ID());
    if (tel.getS_ResourceAssignment_ID() != 0)
        ol.setS_ResourceAssignment_ID(tel.getS_ResourceAssignment_ID());
    //	
    ol.setQty(tel.getQtyInvoiced());
    ol.setDescription(tel.getDescription());
    //
    ol.setC_Project_ID(tel.getC_Project_ID());
    ol.setC_ProjectPhase_ID(tel.getC_ProjectPhase_ID());
    ol.setC_ProjectTask_ID(tel.getC_ProjectTask_ID());
    ol.setC_Activity_ID(tel.getC_Activity_ID());
    ol.setC_Campaign_ID(tel.getC_Campaign_ID());
    //
    //	
    BigDecimal price = tel.getPriceInvoiced();
    if (price != null && price.compareTo(Env.ZERO) != 0) {
        if (tel.getC_Currency_ID() != m_order.getC_Currency_ID())
            price = MConversionRate.convert(getCtx(), price, tel.getC_Currency_ID(), m_order.getC_Currency_ID(), m_order.getAD_Client_ID(), m_order.getAD_Org_ID());
        ol.setPrice(price);
    } else
        ol.setPrice();
    if (tel.getC_UOM_ID() != 0 && ol.getC_UOM_ID() == 0)
        ol.setC_UOM_ID(tel.getC_UOM_ID());
    ol.setTax();
    if (!ol.save()) {
        throw new IllegalStateException("Cannot save Order Line");
    }
    //	Update TimeExpense Line
    tel.setC_OrderLine_ID(ol.getC_OrderLine_ID());
    if (tel.save())
        log.fine("Updated " + tel + " with C_OrderLine_ID");
    else
        log.log(Level.SEVERE, "Not Updated " + tel + " with C_OrderLine_ID");
}
Also used : MOrder(org.compiere.model.MOrder) MOrderLine(org.compiere.model.MOrderLine) BigDecimal(java.math.BigDecimal) MProject(org.compiere.model.MProject)

Example 80 with MOrderLine

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

the class VBOMDrop method cmd_saveOrder.

//	cmd_save
/**
	 * 	Save to Order
	 *	@param C_Order_ID id
	 *	@return true if saved
	 */
private boolean cmd_saveOrder(int C_Order_ID) {
    log.config("C_Order_ID=" + C_Order_ID);
    MOrder order = new MOrder(Env.getCtx(), C_Order_ID, null);
    if (order.get_ID() == 0) {
        log.log(Level.SEVERE, "Not found - C_Order_ID=" + C_Order_ID);
        return false;
    }
    int lineCount = 0;
    //	for all bom lines
    for (int i = 0; i < m_selectionList.size(); i++) {
        if (isSelectionSelected(m_selectionList.get(i))) {
            BigDecimal qty = (BigDecimal) ((VNumber) m_qtyList.get(i)).getValue();
            int M_Product_ID = ((Integer) m_productList.get(i)).intValue();
            //	Create Line
            MOrderLine ol = new MOrderLine(order);
            ol.setM_Product_ID(M_Product_ID, true);
            ol.setQty(qty);
            ol.setPrice();
            ol.setTax();
            if (ol.save())
                lineCount++;
            else
                log.log(Level.SEVERE, "Line not saved");
        }
    //	line selected
    }
    //	for all bom lines
    log.config("#" + lineCount);
    return true;
}
Also used : MOrder(org.compiere.model.MOrder) MOrderLine(org.compiere.model.MOrderLine) ALayoutConstraint(org.compiere.apps.ALayoutConstraint) BigDecimal(java.math.BigDecimal)

Aggregations

MOrderLine (org.compiere.model.MOrderLine)87 BigDecimal (java.math.BigDecimal)44 MOrder (org.compiere.model.MOrder)42 MInOutLine (org.compiere.model.MInOutLine)16 MProduct (org.compiere.model.MProduct)15 MBPartner (org.compiere.model.MBPartner)14 MInOut (org.compiere.model.MInOut)11 ResultSet (java.sql.ResultSet)10 ArrayList (java.util.ArrayList)8 Query (org.compiere.model.Query)8 PreparedStatement (java.sql.PreparedStatement)7 MInvoice (org.compiere.model.MInvoice)7 MInvoiceLine (org.compiere.model.MInvoiceLine)7 AdempiereException (org.adempiere.exceptions.AdempiereException)6 SQLException (java.sql.SQLException)5 MLocator (org.compiere.model.MLocator)5 Timestamp (java.sql.Timestamp)4 MRMALine (org.compiere.model.MRMALine)4 MBPartnerLocation (org.compiere.model.MBPartnerLocation)3 MDocType (org.compiere.model.MDocType)3