Search in sources :

Example 1 with MOrder

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

the class RMACreateOrder method doIt.

@Override
protected String doIt() throws Exception {
    // Load RMA
    MRMA rma = new MRMA(getCtx(), rmaId, get_TrxName());
    // Load Original Order
    MOrder originalOrder = rma.getOriginalOrder();
    if (rma.get_ID() == 0) {
        throw new Exception("No RMA defined");
    }
    if (originalOrder == null) {
        throw new Exception("Could not load the original order");
    }
    // Create new order and set the different values based on original order/RMA doc
    MOrder order = new MOrder(getCtx(), 0, get_TrxName());
    order.setAD_Org_ID(rma.getAD_Org_ID());
    order.setC_BPartner_ID(originalOrder.getC_BPartner_ID());
    order.setC_BPartner_Location_ID(originalOrder.getC_BPartner_Location_ID());
    order.setAD_User_ID(originalOrder.getAD_User_ID());
    order.setBill_BPartner_ID(originalOrder.getBill_BPartner_ID());
    order.setBill_Location_ID(originalOrder.getBill_Location_ID());
    order.setBill_User_ID(originalOrder.getBill_User_ID());
    order.setSalesRep_ID(rma.getSalesRep_ID());
    order.setM_PriceList_ID(originalOrder.getM_PriceList_ID());
    order.setIsSOTrx(originalOrder.isSOTrx());
    order.setM_Warehouse_ID(originalOrder.getM_Warehouse_ID());
    order.setC_DocTypeTarget_ID(originalOrder.getC_DocTypeTarget_ID());
    order.setC_PaymentTerm_ID(originalOrder.getC_PaymentTerm_ID());
    order.setDeliveryRule(originalOrder.getDeliveryRule());
    if (!order.save()) {
        throw new IllegalStateException("Could not create order");
    }
    MRMALine[] lines = rma.getLines(true);
    for (MRMALine line : lines) {
        if (line.getShipLine() != null && line.getShipLine().getC_OrderLine_ID() != 0) {
            // Create order lines if the RMA Doc line has a shipment line 
            MOrderLine orderLine = new MOrderLine(order);
            MOrderLine originalOLine = new MOrderLine(getCtx(), line.getShipLine().getC_OrderLine_ID(), null);
            orderLine.setAD_Org_ID(line.getAD_Org_ID());
            orderLine.setM_Product_ID(originalOLine.getM_Product_ID());
            orderLine.setM_AttributeSetInstance_ID(originalOLine.getM_AttributeSetInstance_ID());
            orderLine.setC_UOM_ID(originalOLine.getC_UOM_ID());
            orderLine.setC_Tax_ID(originalOLine.getC_Tax_ID());
            orderLine.setM_Warehouse_ID(originalOLine.getM_Warehouse_ID());
            orderLine.setC_Currency_ID(originalOLine.getC_Currency_ID());
            orderLine.setQty(line.getQty());
            orderLine.setC_Project_ID(originalOLine.getC_Project_ID());
            orderLine.setC_Activity_ID(originalOLine.getC_Activity_ID());
            orderLine.setC_Campaign_ID(originalOLine.getC_Campaign_ID());
            orderLine.setPrice();
            orderLine.setPrice(line.getAmt());
            if (!orderLine.save()) {
                throw new IllegalStateException("Could not create Order Line");
            }
        }
    }
    rma.setC_Order_ID(order.getC_Order_ID());
    if (!rma.save()) {
        throw new IllegalStateException("Could not update RMA document");
    }
    return "Order Created: " + order.getDocumentNo();
}
Also used : MOrder(org.compiere.model.MOrder) MRMALine(org.compiere.model.MRMALine) MOrderLine(org.compiere.model.MOrderLine) MRMA(org.compiere.model.MRMA)

Example 2 with MOrder

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

the class SB_InvoiceGenerateFromOrderLine method doIt.

//	prepare
/**
	 * 	Generate Invoices
	 *	@return info
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    StringBuffer orderClause = new StringBuffer();
    m_invoices = new ArrayList<MInvoice>();
    if (!p_ConsolidateDocument)
        orderClause.append("C_BPartner_ID, C_Order_ID, line");
    else
        orderClause.append("C_BPartner_ID");
    String whereClause = "EXISTS (SELECT T_Selection_ID FROM T_Selection WHERE  T_Selection.AD_PInstance_ID=? " + " AND T_Selection.T_Selection_ID=c_orderLine.C_OrderLine_ID)";
    m_records = new Query(getCtx(), MOrderLine.Table_Name, whereClause, get_TrxName()).setParameters(getAD_PInstance_ID()).setOrderBy(orderClause.toString()).setClient_ID().list();
    ordersToInvoice = new ArrayList<MOrder>();
    for (MOrderLine orderLine : m_records) {
        Boolean isadded = false;
        for (MOrder order : ordersToInvoice) {
            if (order.getC_Order_ID() == orderLine.getC_Order_ID()) {
                isadded = true;
                break;
            }
        }
        if (!isadded)
            ordersToInvoice.add(orderLine.getParent());
    }
    for (MOrder order : ordersToInvoice) {
        generate(order);
    }
    String result = "Fact. No";
    for (MInvoice inv : m_invoices) {
        Env.setContext(getCtx(), "@WhereClause@", whereClause);
        result = result + ", " + inv.getDocumentInfo();
    }
    return result;
}
Also used : MOrder(org.compiere.model.MOrder) Query(org.compiere.model.Query) MInvoice(org.compiere.model.MInvoice) MOrderLine(org.compiere.model.MOrderLine)

Example 3 with MOrder

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

the class CreateFromInvoice method getOrderData.

/**
	 *  Load Data - Order
	 *  @param C_Order_ID Order
	 *  @param forInvoice true if for invoice vs. delivery qty
	 */
protected Vector<Vector<Object>> getOrderData(int C_Order_ID, boolean forInvoice) {
    /**
		 *  Selected        - 0
		 *  Qty             - 1
		 *  C_UOM_ID        - 2
		 *  M_Product_ID    - 3
		 *  VendorProductNo - 4
		 *  OrderLine       - 5
		 *  ShipmentLine    - 6
		 *  InvoiceLine     - 7
		 */
    log.config("C_Order_ID=" + C_Order_ID);
    p_order = new MOrder(Env.getCtx(), C_Order_ID, null);
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    StringBuffer sql = new StringBuffer("SELECT " + //	1
    "l.QtyOrdered-SUM(COALESCE(m.Qty,0))," + //	2
    "CASE WHEN l.QtyOrdered=0 THEN 0 ELSE l.QtyEntered/l.QtyOrdered END," + //	3..4
    " l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name)," + //	5..7
    " COALESCE(l.M_Product_ID,0),COALESCE(p.Name,c.Name),po.VendorProductNo," + //	8..9
    " l.C_OrderLine_ID,l.Line " + "FROM C_OrderLine l" + " LEFT OUTER JOIN M_Product_PO po ON (l.M_Product_ID = po.M_Product_ID AND l.C_BPartner_ID = po.C_BPartner_ID) " + " LEFT OUTER JOIN M_MatchPO m ON (l.C_OrderLine_ID=m.C_OrderLine_ID AND ");
    sql.append(forInvoice ? "m.C_InvoiceLine_ID" : "m.M_InOutLine_ID");
    sql.append(" IS NOT NULL)").append(" LEFT OUTER JOIN M_Product p ON (l.M_Product_ID=p.M_Product_ID)" + " LEFT OUTER JOIN C_Charge c ON (l.C_Charge_ID=c.C_Charge_ID)");
    if (Env.isBaseLanguage(Env.getCtx(), "C_UOM"))
        sql.append(" LEFT OUTER JOIN C_UOM uom ON (l.C_UOM_ID=uom.C_UOM_ID)");
    else
        sql.append(" LEFT OUTER JOIN C_UOM_Trl uom ON (l.C_UOM_ID=uom.C_UOM_ID AND uom.AD_Language='").append(Env.getAD_Language(Env.getCtx())).append("')");
    //
    sql.append(//	#1
    " WHERE l.C_Order_ID=? " + "GROUP BY l.QtyOrdered,CASE WHEN l.QtyOrdered=0 THEN 0 ELSE l.QtyEntered/l.QtyOrdered END, " + "l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name),po.VendorProductNo, " + "l.M_Product_ID,COALESCE(p.Name,c.Name), l.Line,l.C_OrderLine_ID " + "ORDER BY l.Line");
    //
    log.finer(sql.toString());
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql.toString(), null);
        pstmt.setInt(1, C_Order_ID);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            Vector<Object> line = new Vector<Object>();
            //  0-Selection
            line.add(new Boolean(false));
            BigDecimal qtyOrdered = rs.getBigDecimal(1);
            BigDecimal multiplier = rs.getBigDecimal(2);
            BigDecimal qtyEntered = qtyOrdered.multiply(multiplier);
            //  1-Qty
            line.add(qtyEntered);
            KeyNamePair pp = new KeyNamePair(rs.getInt(3), rs.getString(4).trim());
            //  2-UOM
            line.add(pp);
            pp = new KeyNamePair(rs.getInt(5), rs.getString(6));
            //  3-Product
            line.add(pp);
            // 4-VendorProductNo
            line.add(rs.getString(7));
            pp = new KeyNamePair(rs.getInt(8), rs.getString(9));
            //  5-OrderLine
            line.add(pp);
            //  6-Ship
            line.add(null);
            //  7-Invoice
            line.add(null);
            data.add(line);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, sql.toString(), e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return data;
}
Also used : MOrder(org.compiere.model.MOrder) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) KeyNamePair(org.compiere.util.KeyNamePair) Vector(java.util.Vector) BigDecimal(java.math.BigDecimal) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 4 with MOrder

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

the class CreateFromInvoice method getShipmentData.

/**
	 *  Load Data - Shipment not invoiced
	 *  @param M_InOut_ID InOut
	 */
protected Vector<Vector<Object>> getShipmentData(int M_InOut_ID) {
    log.config("M_InOut_ID=" + M_InOut_ID);
    MInOut inout = new MInOut(Env.getCtx(), M_InOut_ID, null);
    p_order = null;
    if (inout.getC_Order_ID() != 0)
        p_order = new MOrder(Env.getCtx(), inout.getC_Order_ID(), null);
    m_rma = null;
    if (inout.getM_RMA_ID() != 0)
        m_rma = new MRMA(Env.getCtx(), inout.getM_RMA_ID(), null);
    //
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    StringBuffer sql = new StringBuffer(//	QtyEntered
    "SELECT " + "l.MovementQty-SUM(NVL(mi.Qty, 0)), l.QtyEntered/l.MovementQty," + //  3..4
    " l.C_UOM_ID, COALESCE(uom.UOMSymbol, uom.Name)," + //  5..9
    " l.M_Product_ID, p.Name, po.VendorProductNo, l.M_InOutLine_ID, l.Line," + //  10
    " l.C_OrderLine_ID " + " FROM M_InOutLine l ");
    if (Env.isBaseLanguage(Env.getCtx(), "C_UOM"))
        sql.append(" LEFT OUTER JOIN C_UOM uom ON (l.C_UOM_ID=uom.C_UOM_ID)");
    else
        sql.append(" LEFT OUTER JOIN C_UOM_Trl uom ON (l.C_UOM_ID=uom.C_UOM_ID AND uom.AD_Language='").append(Env.getAD_Language(Env.getCtx())).append("')");
    sql.append(" LEFT OUTER JOIN M_Product p ON (l.M_Product_ID=p.M_Product_ID)").append(" INNER JOIN M_InOut io ON (l.M_InOut_ID=io.M_InOut_ID)").append(" LEFT OUTER JOIN M_Product_PO po ON (l.M_Product_ID = po.M_Product_ID AND io.C_BPartner_ID = po.C_BPartner_ID)").append(" LEFT OUTER JOIN M_MatchInv mi ON (l.M_InOutLine_ID=mi.M_InOutLine_ID)").append(" WHERE l.M_InOut_ID=? AND l.MovementQty<>0 ").append("GROUP BY l.MovementQty, l.QtyEntered/l.MovementQty, " + "l.C_UOM_ID, COALESCE(uom.UOMSymbol, uom.Name), " + "l.M_Product_ID, p.Name, po.VendorProductNo, l.M_InOutLine_ID, l.Line, l.C_OrderLine_ID ").append("ORDER BY l.Line");
    try {
        PreparedStatement pstmt = DB.prepareStatement(sql.toString(), null);
        pstmt.setInt(1, M_InOut_ID);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Vector<Object> line = new Vector<Object>(7);
            //  0-Selection
            line.add(new Boolean(false));
            BigDecimal qtyMovement = rs.getBigDecimal(1);
            BigDecimal multiplier = rs.getBigDecimal(2);
            BigDecimal qtyEntered = qtyMovement.multiply(multiplier);
            //  1-Qty
            line.add(qtyEntered);
            KeyNamePair pp = new KeyNamePair(rs.getInt(3), rs.getString(4).trim());
            //  2-UOM
            line.add(pp);
            pp = new KeyNamePair(rs.getInt(5), rs.getString(6));
            //  3-Product
            line.add(pp);
            // 4-VendorProductNo
            line.add(rs.getString(7));
            int C_OrderLine_ID = rs.getInt(10);
            if (rs.wasNull())
                //  5-Order
                line.add(null);
            else
                line.add(new KeyNamePair(C_OrderLine_ID, "."));
            pp = new KeyNamePair(rs.getInt(8), rs.getString(9));
            //  6-Ship
            line.add(pp);
            //  7-RMA
            line.add(null);
            data.add(line);
        }
        rs.close();
        pstmt.close();
    } catch (SQLException e) {
        log.log(Level.SEVERE, sql.toString(), e);
    }
    return data;
}
Also used : MInOut(org.compiere.model.MInOut) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal) MOrder(org.compiere.model.MOrder) ResultSet(java.sql.ResultSet) KeyNamePair(org.compiere.util.KeyNamePair) MRMA(org.compiere.model.MRMA) Vector(java.util.Vector)

Example 5 with MOrder

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

the class MyValidator method modelChange.

//	initialize
/**
     *	Model Change of a monitored Table.
     *	Called after PO.beforeSave/PO.beforeDelete
     *	when you called addModelChange for the table
     *	@param po persistent object
     *	@param type TYPE_
     *	@return error message or null
     *	@exception Exception if the recipient wishes the change to be not accept.
     */
public String modelChange(PO po, int type) throws Exception {
    if (po.get_TableName().equals("C_Order") && type == TYPE_CHANGE) {
        MOrder order = (MOrder) po;
        log.info(po.toString());
    }
    return null;
}
Also used : MOrder(org.compiere.model.MOrder)

Aggregations

MOrder (org.compiere.model.MOrder)95 MOrderLine (org.compiere.model.MOrderLine)42 BigDecimal (java.math.BigDecimal)32 MInOut (org.compiere.model.MInOut)18 MBPartner (org.compiere.model.MBPartner)17 ResultSet (java.sql.ResultSet)16 MInvoice (org.compiere.model.MInvoice)15 PreparedStatement (java.sql.PreparedStatement)12 Timestamp (java.sql.Timestamp)11 MInOutLine (org.compiere.model.MInOutLine)9 SQLException (java.sql.SQLException)8 MProduct (org.compiere.model.MProduct)8 AdempiereException (org.adempiere.exceptions.AdempiereException)6 MPayment (org.compiere.model.MPayment)6 KeyNamePair (org.compiere.util.KeyNamePair)6 MDocType (org.compiere.model.MDocType)5 ArrayList (java.util.ArrayList)4 Vector (java.util.Vector)4 MRMA (org.compiere.model.MRMA)4 MWarehouse (org.compiere.model.MWarehouse)4