Search in sources :

Example 6 with MInvoice

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

the class CreateFromInvoice method save.

/**
	 *  Save - Create Invoice Lines
	 *  @return true if saved
	 */
public boolean save(IMiniTable miniTable, String trxName) {
    //  Invoice
    //	Yamel Senih FR [ 114 ] get Record ID from record
    MInvoice invoice = new MInvoice(Env.getCtx(), m_Record_ID, trxName);
    log.config(invoice.toString());
    if (p_order != null) {
        //	overwrite header values
        invoice.setOrder(p_order);
        invoice.saveEx();
    }
    if (m_rma != null) {
        invoice.setM_RMA_ID(m_rma.getM_RMA_ID());
        invoice.saveEx();
    }
    //  Lines
    for (int i = 0; i < miniTable.getRowCount(); i++) {
        if (((Boolean) miniTable.getValueAt(i, 0)).booleanValue()) {
            MProduct product = null;
            //  variable values
            //  1-Qty
            BigDecimal QtyEntered = (BigDecimal) miniTable.getValueAt(i, 1);
            //  2-UOM
            KeyNamePair pp = (KeyNamePair) miniTable.getValueAt(i, 2);
            int C_UOM_ID = pp.getKey();
            //
            //  3-Product
            pp = (KeyNamePair) miniTable.getValueAt(i, 3);
            int M_Product_ID = 0;
            if (pp != null)
                M_Product_ID = pp.getKey();
            //
            int C_OrderLine_ID = 0;
            //  5-OrderLine
            pp = (KeyNamePair) miniTable.getValueAt(i, 5);
            if (pp != null)
                C_OrderLine_ID = pp.getKey();
            int M_InOutLine_ID = 0;
            //  6-Shipment
            pp = (KeyNamePair) miniTable.getValueAt(i, 6);
            if (pp != null)
                M_InOutLine_ID = pp.getKey();
            //
            int M_RMALine_ID = 0;
            //  7-RMALine
            pp = (KeyNamePair) miniTable.getValueAt(i, 7);
            if (pp != null)
                M_RMALine_ID = pp.getKey();
            //	Precision of Qty UOM
            int precision = 2;
            if (M_Product_ID != 0) {
                product = MProduct.get(Env.getCtx(), M_Product_ID);
                precision = product.getUOMPrecision();
            }
            QtyEntered = QtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
            //
            log.fine("Line QtyEntered=" + QtyEntered + ", Product_ID=" + M_Product_ID + ", OrderLine_ID=" + C_OrderLine_ID + ", InOutLine_ID=" + M_InOutLine_ID);
            //	Create new Invoice Line
            MInvoiceLine invoiceLine = new MInvoiceLine(invoice);
            //	Line UOM
            invoiceLine.setM_Product_ID(M_Product_ID, C_UOM_ID);
            //	Invoiced/Entered
            invoiceLine.setQty(QtyEntered);
            BigDecimal QtyInvoiced = null;
            if (M_Product_ID > 0 && product.getC_UOM_ID() != C_UOM_ID) {
                QtyInvoiced = MUOMConversion.convertProductFrom(Env.getCtx(), M_Product_ID, C_UOM_ID, QtyEntered);
            }
            if (QtyInvoiced == null)
                QtyInvoiced = QtyEntered;
            invoiceLine.setQtyInvoiced(QtyInvoiced);
            //  Info
            MOrderLine orderLine = null;
            if (C_OrderLine_ID != 0)
                orderLine = new MOrderLine(Env.getCtx(), C_OrderLine_ID, trxName);
            //
            MRMALine rmaLine = null;
            if (M_RMALine_ID > 0)
                rmaLine = new MRMALine(Env.getCtx(), M_RMALine_ID, null);
            //
            MInOutLine inoutLine = null;
            if (M_InOutLine_ID != 0) {
                inoutLine = new MInOutLine(Env.getCtx(), M_InOutLine_ID, trxName);
                if (orderLine == null && inoutLine.getC_OrderLine_ID() != 0) {
                    C_OrderLine_ID = inoutLine.getC_OrderLine_ID();
                    orderLine = new MOrderLine(Env.getCtx(), C_OrderLine_ID, trxName);
                }
            } else if (C_OrderLine_ID > 0) {
                String whereClause = "EXISTS (SELECT 1 FROM M_InOut io WHERE io.M_InOut_ID=M_InOutLine.M_InOut_ID AND io.DocStatus IN ('CO','CL'))";
                MInOutLine[] lines = MInOutLine.getOfOrderLine(Env.getCtx(), C_OrderLine_ID, whereClause, trxName);
                log.fine("Receipt Lines with OrderLine = #" + lines.length);
                if (lines.length > 0) {
                    for (int j = 0; j < lines.length; j++) {
                        MInOutLine line = lines[j];
                        if (line.getQtyEntered().compareTo(QtyEntered) == 0) {
                            inoutLine = line;
                            M_InOutLine_ID = inoutLine.getM_InOutLine_ID();
                            break;
                        }
                    }
                    if (inoutLine == null) {
                        //	first as default
                        inoutLine = lines[0];
                        M_InOutLine_ID = inoutLine.getM_InOutLine_ID();
                    }
                }
            } else if (M_RMALine_ID != 0) {
                String whereClause = "EXISTS (SELECT 1 FROM M_InOut io WHERE io.M_InOut_ID=M_InOutLine.M_InOut_ID AND io.DocStatus IN ('CO','CL'))";
                MInOutLine[] lines = MInOutLine.getOfRMALine(Env.getCtx(), M_RMALine_ID, whereClause, null);
                log.fine("Receipt Lines with RMALine = #" + lines.length);
                if (lines.length > 0) {
                    for (int j = 0; j < lines.length; j++) {
                        MInOutLine line = lines[j];
                        if (rmaLine.getQty().compareTo(QtyEntered) == 0) {
                            inoutLine = line;
                            M_InOutLine_ID = inoutLine.getM_InOutLine_ID();
                            break;
                        }
                    }
                    if (rmaLine == null) {
                        //	first as default
                        inoutLine = lines[0];
                        M_InOutLine_ID = inoutLine.getM_InOutLine_ID();
                    }
                }
            }
            //	Shipment Info
            if (inoutLine != null) {
                //	overwrites
                invoiceLine.setShipLine(inoutLine);
            } else {
                log.fine("No Receipt Line");
                //	Order Info
                if (orderLine != null) {
                    //	overwrites
                    invoiceLine.setOrderLine(orderLine);
                } else {
                    log.fine("No Order Line");
                    invoiceLine.setPrice();
                    invoiceLine.setTax();
                }
                //RMA Info
                if (rmaLine != null) {
                    //	overwrites
                    invoiceLine.setRMALine(rmaLine);
                } else
                    log.fine("No RMA Line");
            }
            invoiceLine.saveEx();
        }
    //   if selected
    }
    return true;
}
Also used : MProduct(org.compiere.model.MProduct) MInOutLine(org.compiere.model.MInOutLine) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) KeyNamePair(org.compiere.util.KeyNamePair) MOrderLine(org.compiere.model.MOrderLine) MRMALine(org.compiere.model.MRMALine) BigDecimal(java.math.BigDecimal)

Example 7 with MInvoice

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

the class CreateFromInvoice method getDocBaseType.

/**
	 * Get Document Base Type from Document Type of Invoice
	 * @return
	 */
protected String getDocBaseType() {
    if (m_Record_ID <= 0)
        return "";
    //	For other
    MInvoice invoice = new MInvoice(Env.getCtx(), m_Record_ID, null);
    MDocType docType = MDocType.get(Env.getCtx(), invoice.getC_DocTypeTarget_ID());
    if (docType != null) {
        return docType.getDocBaseType();
    }
    //	Default
    return "";
}
Also used : MDocType(org.compiere.model.MDocType) MInvoice(org.compiere.model.MInvoice)

Example 8 with MInvoice

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

the class MInvoiceTest method testQuery.

public void testQuery() throws Exception {
    MInvoice.setIsPaid(getCtx(), BPARTNER_TreeFarm, getTrxName());
    MInvoice[] invoices = MInvoice.getOfBPartner(getCtx(), BPARTNER_TreeFarm, getTrxName());
    assertTrue("Partner " + BPARTNER_TreeFarm + " should have invoices", invoices.length > 0);
    for (MInvoice invoice : invoices) {
        // test query
        invoice.getLines(true);
        // test query
        invoice.getTaxes(true);
    }
    //test MinvoiceLine getOfInOutLine
    //get InOutLine thats from InvoiceLine
    MInOutLine iol = new MInOutLine(getCtx(), 101, getTrxName());
    MInvoiceLine invl = MInvoiceLine.getOfInOutLine(iol);
    assertTrue("getOfInOutLine must work", invl.get_ID() > 0);
}
Also used : MInOutLine(org.compiere.model.MInOutLine) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice)

Example 9 with MInvoice

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

the class WebInfo method getInvoice.

//	getInvoices
/**
	 * 	Get Invoice.
	 * 	Needs to have ID set first
	 *	@return invoice with ID of BP
	 */
public MInvoice getInvoice() {
    m_infoMessage = null;
    MInvoice retValue = null;
    if (m_wu != null && !m_wu.hasBPAccess(X_AD_UserBPAccess.BPACCESSTYPE_BusinessDocuments, new Object[] { MDocType.DOCBASETYPE_APInvoice, MDocType.DOCBASETYPE_APCreditMemo, MDocType.DOCBASETYPE_ARInvoice, MDocType.DOCBASETYPE_ARCreditMemo })) {
        log.info("No Access");
        return null;
    }
    String sql = "SELECT * FROM C_Invoice WHERE C_BPartner_ID=? AND C_Invoice_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, getC_BPartner_ID());
        pstmt.setInt(2, m_id);
        rs = pstmt.executeQuery();
        if (rs.next())
            retValue = new MInvoice(m_ctx, rs, null);
    } catch (Exception e) {
        log.log(Level.SEVERE, "C_Invoice_ID=" + m_id, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    log.fine("C_Invoice_ID=" + m_id + " - " + retValue);
    return retValue;
}
Also used : ResultSet(java.sql.ResultSet) MInvoice(org.compiere.model.MInvoice) PreparedStatement(java.sql.PreparedStatement)

Example 10 with MInvoice

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

the class VBOMDrop method cmd_saveInvoice.

//	cmd_saveOrder
/**
	 * 	Save to Invoice
	 *	@param C_Invoice_ID id
	 *	@return true if saved
	 */
private boolean cmd_saveInvoice(int C_Invoice_ID) {
    log.config("C_Invoice_ID=" + C_Invoice_ID);
    MInvoice invoice = new MInvoice(Env.getCtx(), C_Invoice_ID, null);
    if (invoice.get_ID() == 0) {
        log.log(Level.SEVERE, "Not found - C_Invoice_ID=" + C_Invoice_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
            MInvoiceLine il = new MInvoiceLine(invoice);
            il.setM_Product_ID(M_Product_ID, true);
            il.setQty(qty);
            il.setPrice();
            il.setTax();
            if (il.save())
                lineCount++;
            else
                log.log(Level.SEVERE, "Line not saved");
        }
    //	line selected
    }
    //	for all bom lines
    log.config("#" + lineCount);
    return true;
}
Also used : MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) ALayoutConstraint(org.compiere.apps.ALayoutConstraint) BigDecimal(java.math.BigDecimal)

Aggregations

MInvoice (org.compiere.model.MInvoice)70 BigDecimal (java.math.BigDecimal)25 MInvoiceLine (org.compiere.model.MInvoiceLine)23 MPayment (org.compiere.model.MPayment)12 ResultSet (java.sql.ResultSet)10 Timestamp (java.sql.Timestamp)10 MBPartner (org.compiere.model.MBPartner)10 MInOut (org.compiere.model.MInOut)10 MOrder (org.compiere.model.MOrder)10 PreparedStatement (java.sql.PreparedStatement)9 MInOutLine (org.compiere.model.MInOutLine)8 MOrderLine (org.compiere.model.MOrderLine)6 ArrayList (java.util.ArrayList)5 AdempiereException (org.adempiere.exceptions.AdempiereException)5 MDocType (org.compiere.model.MDocType)5 KeyNamePair (org.compiere.util.KeyNamePair)5 ValueNamePair (org.compiere.util.ValueNamePair)5 MCashLine (org.compiere.model.MCashLine)4 MClient (org.compiere.model.MClient)4 MProduct (org.compiere.model.MProduct)4