Search in sources :

Example 11 with MInvoiceLine

use of org.compiere.model.MInvoiceLine 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 12 with MInvoiceLine

use of org.compiere.model.MInvoiceLine 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 13 with MInvoiceLine

use of org.compiere.model.MInvoiceLine 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)

Example 14 with MInvoiceLine

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

the class CostResult method createInvoiceVendor.

public MInvoice createInvoiceVendor(Timestamp documentDate, BigDecimal qty, BigDecimal price, int M_InOutLine_ID) {
    MInvoice invoice = new MInvoice(getCtx(), 0, trxName);
    invoice.setAD_Org_ID(Env.getAD_Org_ID(getCtx()));
    invoice.setC_DocType_ID(MDocType.getDocType(MDocType.DOCBASETYPE_APInvoice));
    invoice.setIsSOTrx(false);
    invoice.setC_BPartner_ID(bp.getC_BPartner_ID());
    invoice.setDateInvoiced(documentDate);
    invoice.setDateAcct(documentDate);
    invoice.setDocStatus(DocAction.STATUS_Drafted);
    invoice.setDocAction(DocAction.ACTION_Complete);
    invoice.saveEx();
    MInvoiceLine invoiceLine = new MInvoiceLine(invoice);
    invoiceLine.setM_Product_ID(product.getM_Product_ID());
    invoiceLine.setM_InOutLine_ID(M_InOutLine_ID);
    invoiceLine.setQty(qty);
    invoiceLine.setPriceActual(price);
    invoiceLine.saveEx();
    invoice.processIt(DocAction.ACTION_Complete);
    invoice.saveEx();
    return invoice;
}
Also used : MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice)

Example 15 with MInvoiceLine

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

the class InOutCreateFrom method doIt.

@Override
protected String doIt() throws Exception {
    // Valid Record Identifier
    if (getRecord_ID() == 0)
        return "";
    AtomicInteger referenceId = new AtomicInteger(0);
    AtomicInteger created = new AtomicInteger(0);
    //	Get Shipment
    MInOut inout = new MInOut(getCtx(), getRecord_ID(), get_TrxName());
    log.config(inout + ", C_Locator_ID=" + getLocator());
    //	Get Default Locator
    MLocator defaultLocator = MLocator.getDefault((MWarehouse) inout.getM_Warehouse());
    List<Integer> recordIds = getSelectionKeys();
    String createFromType = recordIds.size() > 0 ? getSelectionAsString(recordIds.get(0), "CF_CreateFromType") : null;
    log.fine("CreateFromType=" + createFromType);
    if (createFromType == null || createFromType.length() == 0)
        throw new AdempiereException("@CreateFromType@ @NotFound@");
    //	Loop
    recordIds.stream().forEach(key -> {
        int productId = getSelectionAsInt(key, "CF_M_Product_ID");
        int chargeId = getSelectionAsInt(key, "CF_C_Charge_ID");
        int uomId = getSelectionAsInt(key, "CF_C_UOM_ID");
        int locatorId = getSelectionAsInt(key, "CF_M_Locator_ID");
        BigDecimal qtyEntered = getSelectionAsBigDecimal(key, "CF_QtyEntered");
        locatorId = getValidLocator(locatorId, defaultLocator);
        MInvoiceLine invoiceLine = null;
        int precision = 2;
        if (productId != 0) {
            MProduct product = MProduct.get(getCtx(), productId);
            precision = product.getUOMPrecision();
        }
        qtyEntered = qtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
        log.fine("Line QtyEntered=" + qtyEntered + ", Product=" + productId + ", Key=" + key);
        MInOutLine inOutLine = new MInOutLine(inout);
        inOutLine.setM_Product_ID(productId, uomId);
        inOutLine.setQty(qtyEntered);
        if (createFromType.equals(ORDER)) {
            MOrderLine orderLine = new MOrderLine(getCtx(), key, get_TrxName());
            referenceId.set(orderLine.getC_Order_ID());
            inOutLine.setC_OrderLine_ID(key);
            if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0) {
                inOutLine.setMovementQty(qtyEntered.multiply(orderLine.getQtyOrdered()).divide(orderLine.getQtyEntered(), 12, BigDecimal.ROUND_HALF_UP));
                inOutLine.setC_UOM_ID(orderLine.getC_UOM_ID());
            }
            inOutLine.setM_AttributeSetInstance_ID(orderLine.getM_AttributeSetInstance_ID());
            inOutLine.setDescription(orderLine.getDescription());
            inOutLine.setC_Project_ID(orderLine.getC_Project_ID());
            inOutLine.setC_ProjectPhase_ID(orderLine.getC_ProjectPhase_ID());
            inOutLine.setC_ProjectTask_ID(orderLine.getC_ProjectTask_ID());
            inOutLine.setC_Activity_ID(orderLine.getC_Activity_ID());
            inOutLine.setC_Campaign_ID(orderLine.getC_Campaign_ID());
            inOutLine.setAD_OrgTrx_ID(orderLine.getAD_OrgTrx_ID());
            inOutLine.setUser1_ID(orderLine.getUser1_ID());
            inOutLine.setUser2_ID(orderLine.getUser2_ID());
            inOutLine.setUser3_ID(orderLine.getUser3_ID());
            inOutLine.setUser4_ID(orderLine.getUser4_ID());
        } else if (createFromType.equals(INVOICE)) {
            invoiceLine = new MInvoiceLine(getCtx(), key, get_TrxName());
            MInvoice invoice = invoiceLine.getParent();
            referenceId.getAndSet(invoice.getC_Invoice_ID());
            if (invoice.isCreditMemo()) {
                qtyEntered = qtyEntered.negate();
                inOutLine.setQty(qtyEntered);
            }
            if (invoiceLine.getQtyEntered().compareTo(invoiceLine.getQtyInvoiced()) != 0) {
                inOutLine.setMovementQty(qtyEntered.multiply(invoiceLine.getQtyInvoiced()).divide(invoiceLine.getQtyEntered(), 12, BigDecimal.ROUND_HALF_UP));
                inOutLine.setC_UOM_ID(invoiceLine.getC_UOM_ID());
            }
            inOutLine.setDescription(invoiceLine.getDescription());
            inOutLine.setC_Project_ID(invoiceLine.getC_Project_ID());
            inOutLine.setC_ProjectPhase_ID(invoiceLine.getC_ProjectPhase_ID());
            inOutLine.setC_ProjectTask_ID(invoiceLine.getC_ProjectTask_ID());
            inOutLine.setC_Activity_ID(invoiceLine.getC_Activity_ID());
            inOutLine.setC_Campaign_ID(invoiceLine.getC_Campaign_ID());
            inOutLine.setAD_OrgTrx_ID(invoiceLine.getAD_OrgTrx_ID());
            inOutLine.setUser1_ID(invoiceLine.getUser1_ID());
            inOutLine.setUser2_ID(invoiceLine.getUser2_ID());
            inOutLine.setUser3_ID(invoiceLine.getUser3_ID());
            inOutLine.setUser4_ID(invoiceLine.getUser4_ID());
        } else if (createFromType.equals(RMA)) {
            MRMALine rmal = new MRMALine(getCtx(), key, get_TrxName());
            referenceId.set(rmal.getM_RMA_ID());
            inOutLine.setM_RMALine_ID(key);
            inOutLine.setQtyEntered(qtyEntered);
            inOutLine.setDescription(rmal.getDescription());
            inOutLine.setM_AttributeSetInstance_ID(rmal.getM_AttributeSetInstance_ID());
            inOutLine.setC_Project_ID(rmal.getC_Project_ID());
            inOutLine.setC_ProjectPhase_ID(rmal.getC_ProjectPhase_ID());
            inOutLine.setC_ProjectTask_ID(rmal.getC_ProjectTask_ID());
            inOutLine.setC_Activity_ID(rmal.getC_Activity_ID());
            inOutLine.setAD_OrgTrx_ID(rmal.getAD_OrgTrx_ID());
            inOutLine.setUser1_ID(rmal.getUser1_ID());
            inOutLine.setUser2_ID(rmal.getUser2_ID());
            inOutLine.setUser3_ID(rmal.getUser3_ID());
            inOutLine.setUser4_ID(rmal.getUser4_ID());
        }
        if (chargeId != 0)
            inOutLine.setC_Charge_ID(chargeId);
        inOutLine.setM_Locator_ID(locatorId);
        inOutLine.saveEx();
        if (invoiceLine != null) {
            invoiceLine.setM_InOutLine_ID(inOutLine.getM_InOutLine_ID());
            invoiceLine.saveEx();
        }
        created.updateAndGet(createNo -> createNo + 1);
    });
    //	Add reference to Order / Invoice / RMA
    addReference(inout, createFromType, referenceId.get());
    //	
    return "@Created@ " + created.get();
}
Also used : MInOut(org.compiere.model.MInOut) MProduct(org.compiere.model.MProduct) MInOutLine(org.compiere.model.MInOutLine) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdempiereException(org.adempiere.exceptions.AdempiereException) MLocator(org.compiere.model.MLocator) MOrderLine(org.compiere.model.MOrderLine) MRMALine(org.compiere.model.MRMALine)

Aggregations

MInvoiceLine (org.compiere.model.MInvoiceLine)34 MInvoice (org.compiere.model.MInvoice)23 BigDecimal (java.math.BigDecimal)13 MInOutLine (org.compiere.model.MInOutLine)10 MProduct (org.compiere.model.MProduct)7 MBPartner (org.compiere.model.MBPartner)6 MOrderLine (org.compiere.model.MOrderLine)6 MRMALine (org.compiere.model.MRMALine)6 ArrayList (java.util.ArrayList)4 MInOut (org.compiere.model.MInOut)4 MLocation (org.compiere.model.MLocation)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MClient (org.compiere.model.MClient)3 MDocType (org.compiere.model.MDocType)3 SQLException (java.sql.SQLException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 FillMandatoryException (org.adempiere.exceptions.FillMandatoryException)2 ALayoutConstraint (org.compiere.apps.ALayoutConstraint)2