Search in sources :

Example 26 with MInvoiceLine

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

the class Match method createMatchRecord.

//  tableLoad
/**
	 *  Create Matching Record
	 *  @param invoice true if matching invoice false if matching PO
	 *  @param M_InOutLine_ID shipment line
	 *  @param Line_ID C_InvoiceLine_ID or C_OrderLine_ID
	 *  @param qty quantity
	 *  @param trxName 
	 *  @return true if created
	 */
protected boolean createMatchRecord(boolean invoice, int M_InOutLine_ID, int Line_ID, BigDecimal qty, String trxName) {
    if (qty.compareTo(Env.ZERO) == 0)
        return true;
    log.fine("IsInvoice=" + invoice + ", M_InOutLine_ID=" + M_InOutLine_ID + ", Line_ID=" + Line_ID + ", Qty=" + qty);
    //
    boolean success = false;
    MInOutLine sLine = new MInOutLine(Env.getCtx(), M_InOutLine_ID, trxName);
    if (//	Shipment - Invoice
    invoice) {
        //	Update Invoice Line
        MInvoiceLine iLine = new MInvoiceLine(Env.getCtx(), Line_ID, trxName);
        iLine.setM_InOutLine_ID(M_InOutLine_ID);
        if (sLine.getC_OrderLine_ID() != 0)
            iLine.setC_OrderLine_ID(sLine.getC_OrderLine_ID());
        iLine.saveEx();
        //	Create Shipment - Invoice Link
        if (iLine.getM_Product_ID() != 0) {
            MMatchInv match = new MMatchInv(iLine, null, qty);
            match.setM_InOutLine_ID(M_InOutLine_ID);
            if (match.save()) {
                success = true;
                if (MClient.isClientAccountingImmediate()) {
                    String ignoreError = DocumentEngine.postImmediate(match.getCtx(), match.getAD_Client_ID(), match.get_Table_ID(), match.get_ID(), true, match.get_TrxName());
                }
            } else
                log.log(Level.SEVERE, "Inv Match not created: " + match);
        } else
            success = true;
        //	Create PO - Invoice Link = corrects PO
        if (iLine.getC_OrderLine_ID() != 0 && iLine.getM_Product_ID() != 0) {
            MMatchPO matchPO = MMatchPO.create(iLine, sLine, null, qty);
            matchPO.setC_InvoiceLine_ID(iLine);
            matchPO.setM_InOutLine_ID(M_InOutLine_ID);
            if (!matchPO.save())
                log.log(Level.SEVERE, "PO(Inv) Match not created: " + matchPO);
            if (MClient.isClientAccountingImmediate()) {
                String ignoreError = DocumentEngine.postImmediate(matchPO.getCtx(), matchPO.getAD_Client_ID(), matchPO.get_Table_ID(), matchPO.get_ID(), true, matchPO.get_TrxName());
            }
        }
    } else //	Shipment - Order
    {
        //	Update Shipment Line
        sLine.setC_OrderLine_ID(Line_ID);
        sLine.saveEx();
        //	Update Order Line
        MOrderLine oLine = new MOrderLine(Env.getCtx(), Line_ID, trxName);
        if (//	other in MInOut.completeIt
        oLine.get_ID() != 0) {
            oLine.setQtyReserved(oLine.getQtyReserved().subtract(qty));
            if (!oLine.save())
                log.severe("QtyReserved not updated - C_OrderLine_ID=" + Line_ID);
        }
        //	Create PO - Shipment Link
        if (sLine.getM_Product_ID() != 0) {
            MMatchPO match = new MMatchPO(sLine, null, qty);
            if (!match.save())
                log.log(Level.SEVERE, "PO Match not created: " + match);
            else {
                success = true;
                //	Correct Ordered Qty for Stocked Products (see MOrder.reserveStock / MInOut.processIt)
                if (sLine.getProduct() != null && sLine.getProduct().isStocked())
                    success = MStorage.add(Env.getCtx(), sLine.getM_Warehouse_ID(), sLine.getM_Locator_ID(), sLine.getM_Product_ID(), sLine.getM_AttributeSetInstance_ID(), oLine.getM_AttributeSetInstance_ID(), null, null, qty.negate(), trxName);
            }
        } else
            success = true;
    }
    return success;
}
Also used : MInOutLine(org.compiere.model.MInOutLine) MMatchPO(org.compiere.model.MMatchPO) MInvoiceLine(org.compiere.model.MInvoiceLine) MMatchInv(org.compiere.model.MMatchInv) MOrderLine(org.compiere.model.MOrderLine)

Example 27 with MInvoiceLine

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

the class InvoiceCreateFrom method doIt.

@Override
protected String doIt() throws Exception {
    // Valid Record Identifier
    if (getRecord_ID() == 0)
        return "";
    //	Get Shipment
    MInvoice invoice = new MInvoice(getCtx(), getRecord_ID(), get_TrxName());
    AtomicInteger referenceId = new AtomicInteger(0);
    AtomicInteger created = new AtomicInteger(0);
    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");
        BigDecimal qtyEntered = getSelectionAsBigDecimal(key, "CF_QtyEntered");
        log.fine("Line QtyEntered=" + qtyEntered + ", Product=" + productId + ", CreateFromType=" + createFromType + ", Key=" + key);
        MInvoiceLine invoiceLine = new MInvoiceLine(invoice);
        BigDecimal qtyInvoiced = null;
        int precision = 2;
        if (productId > 0) {
            MProduct product = MProduct.get(Env.getCtx(), productId);
            if (product != null) {
                invoiceLine.setM_Product_ID(product.getM_Product_ID(), uomId);
                precision = product.getUOMPrecision();
                if (product.getC_UOM_ID() != uomId) {
                    qtyEntered = qtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
                    qtyInvoiced = MUOMConversion.convertProductFrom(Env.getCtx(), productId, uomId, qtyEntered);
                }
            }
        } else if (chargeId != 0) {
            invoiceLine.setC_Charge_ID(chargeId);
        }
        qtyEntered = qtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
        if (qtyInvoiced == null)
            qtyInvoiced = qtyEntered;
        invoiceLine.setQty(qtyEntered);
        invoiceLine.setQtyInvoiced(qtyInvoiced);
        if (createFromType.equals(ORDER)) {
            MOrderLine orderLine = new MOrderLine(getCtx(), key, get_TrxName());
            referenceId.set(orderLine.getC_Order_ID());
            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[] inOutLines = MInOutLine.getOfOrderLine(Env.getCtx(), key, whereClause, get_TrxName());
            log.fine("Receipt Lines with OrderLine = #" + inOutLines.length);
            final BigDecimal qty = qtyEntered;
            MInOutLine inOutLine = Arrays.stream(inOutLines).filter(ioLine -> ioLine != null && ioLine.getQtyEntered().compareTo(qty) == 0).findFirst().orElse(inOutLines.length > 0 ? inOutLines[0] : null);
            if (inOutLine != null)
                invoiceLine.setShipLine(inOutLine);
            else
                invoiceLine.setOrderLine(orderLine);
        } else if (createFromType.equals(INVOICE)) {
            MInvoiceLine fromLine = new MInvoiceLine(getCtx(), key, get_TrxName());
            referenceId.set(invoiceLine.getParent().getC_Invoice_ID());
            PO.copyValues(fromLine, invoiceLine);
            invoiceLine.setC_Invoice_ID(invoiceLine.getParent().getC_Invoice_ID());
            invoiceLine.setAD_Org_ID(fromLine.getAD_Org_ID());
            invoiceLine.setC_OrderLine_ID(0);
            invoiceLine.setRef_InvoiceLine_ID(0);
            invoiceLine.setM_InOutLine_ID(0);
            invoiceLine.setA_Asset_ID(0);
            invoiceLine.setM_AttributeSetInstance_ID(0);
            invoiceLine.setS_ResourceAssignment_ID(0);
            if (invoiceLine.getParent().getC_BPartner_ID() != fromLine.getC_Invoice().getC_BPartner_ID())
                invoiceLine.setTax();
            invoiceLine.setProcessed(false);
        } else if (createFromType.equals(RMA)) {
            MRMALine rmaLine = new MRMALine(getCtx(), key, get_TrxName());
            referenceId.set(rmaLine.getM_RMA_ID());
            invoiceLine.setRMALine(rmaLine);
        } else if (createFromType.equals(RECEIPT)) {
            MInOutLine inOutLine = new MInOutLine(getCtx(), key, get_TrxName());
            referenceId.set(inOutLine.getM_InOut_ID());
            invoiceLine.setShipLine(inOutLine);
        }
        invoiceLine.saveEx();
        if (createFromType.equals(INVOICE)) {
            MInvoiceLine fromLine = new MInvoiceLine(getCtx(), key, get_TrxName());
            invoiceLine.copyLandedCostFrom(fromLine);
            invoiceLine.allocateLandedCosts();
        }
        created.updateAndGet(createNo -> createNo + 1);
    });
    //	Add reference to Order / Invoice / RMA
    addReference(invoice, createFromType, referenceId.get());
    return "@Created@ " + created.get();
}
Also used : 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) MOrderLine(org.compiere.model.MOrderLine) MRMALine(org.compiere.model.MRMALine)

Example 28 with MInvoiceLine

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

the class VProductConfigurationBOM 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 29 with MInvoiceLine

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

the class CommissionAPInvoice method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message (variables are parsed)
	 *  @throws Exception if not successful
	 */
protected String doIt() throws Exception {
    log.info("doIt - C_CommissionRun_ID=" + getRecord_ID());
    //	Load Data
    MCommissionRun comRun = new MCommissionRun(getCtx(), getRecord_ID(), get_TrxName());
    if (comRun.get_ID() == 0)
        throw new IllegalArgumentException("CommissionAPInvoice - No Commission Run");
    if (Env.ZERO.compareTo(comRun.getGrandTotal()) == 0)
        throw new IllegalArgumentException("@GrandTotal@ = 0");
    MCommission com = new MCommission(getCtx(), comRun.getC_Commission_ID(), get_TrxName());
    if (com.get_ID() == 0)
        throw new IllegalArgumentException("CommissionAPInvoice - No Commission");
    if (com.getC_Charge_ID() == 0)
        throw new IllegalArgumentException("CommissionAPInvoice - No Charge on Commission");
    MBPartner bp = new MBPartner(getCtx(), com.getC_BPartner_ID(), get_TrxName());
    if (bp.get_ID() == 0)
        throw new IllegalArgumentException("CommissionAPInvoice - No BPartner");
    //	Create Invoice
    MInvoice invoice = new MInvoice(getCtx(), 0, null);
    invoice.setClientOrg(com.getAD_Client_ID(), com.getAD_Org_ID());
    //	API
    invoice.setC_DocTypeTarget_ID(MDocType.DOCBASETYPE_APInvoice);
    invoice.setBPartner(bp);
    //	invoice.setDocumentNo (comRun.getDocumentNo());		//	may cause unique constraint
    //	caller
    invoice.setSalesRep_ID(getAD_User_ID());
    //
    if (com.getC_Currency_ID() != invoice.getC_Currency_ID())
        throw new IllegalArgumentException("CommissionAPInvoice - Currency of PO Price List not Commission Currency");
    //		
    if (!invoice.save())
        throw new IllegalStateException("CommissionAPInvoice - cannot save Invoice");
    //	Create Invoice Line
    MInvoiceLine iLine = new MInvoiceLine(invoice);
    iLine.setC_Charge_ID(com.getC_Charge_ID());
    iLine.setQty(1);
    iLine.setPrice(comRun.getGrandTotal());
    iLine.setTax();
    if (!iLine.save())
        throw new IllegalStateException("CommissionAPInvoice - cannot save Invoice Line");
    //
    return "@C_Invoice_ID@ = " + invoice.getDocumentNo();
}
Also used : MCommission(org.compiere.model.MCommission) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) MBPartner(org.compiere.model.MBPartner) MCommissionRun(org.compiere.model.MCommissionRun)

Example 30 with MInvoiceLine

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

the class OrderRePrice method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message (clear text)
	 *  @throws Exception if not successful
	 */
protected String doIt() throws Exception {
    log.info("C_Order_ID=" + p_C_Order_ID + ", C_Invoice_ID=" + p_C_Invoice_ID);
    if (p_C_Order_ID == 0 && p_C_Invoice_ID == 0)
        throw new IllegalArgumentException("Nothing to do");
    String retValue = "";
    if (p_C_Order_ID != 0) {
        MOrder order = new MOrder(getCtx(), p_C_Order_ID, get_TrxName());
        BigDecimal oldPrice = order.getGrandTotal();
        MOrderLine[] lines = order.getLines();
        for (int i = 0; i < lines.length; i++) {
            lines[i].setPrice(order.getM_PriceList_ID());
            lines[i].saveEx();
        }
        order = new MOrder(getCtx(), p_C_Order_ID, get_TrxName());
        BigDecimal newPrice = order.getGrandTotal();
        retValue = order.getDocumentNo() + ":  " + oldPrice + " -> " + newPrice;
    }
    if (p_C_Invoice_ID != 0) {
        MInvoice invoice = new MInvoice(getCtx(), p_C_Invoice_ID, null);
        BigDecimal oldPrice = invoice.getGrandTotal();
        MInvoiceLine[] lines = invoice.getLines(false);
        for (int i = 0; i < lines.length; i++) {
            lines[i].setPrice(invoice.getM_PriceList_ID(), invoice.getC_BPartner_ID());
            lines[i].saveEx();
        }
        invoice = new MInvoice(getCtx(), p_C_Invoice_ID, null);
        BigDecimal newPrice = invoice.getGrandTotal();
        if (retValue.length() > 0)
            retValue += Env.NL;
        retValue += invoice.getDocumentNo() + ":  " + oldPrice + " -> " + newPrice;
    }
    //
    return retValue;
}
Also used : MOrder(org.compiere.model.MOrder) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) MOrderLine(org.compiere.model.MOrderLine) BigDecimal(java.math.BigDecimal)

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