Search in sources :

Example 21 with MInvoice

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

the class InOutCreateInvoice method doIt.

//	prepare
/**
	 * 	Create Invoice.
	 *	@return document no
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("M_InOut_ID=" + p_M_InOut_ID + ", M_PriceList_ID=" + p_M_PriceList_ID + ", InvoiceDocumentNo=" + p_InvoiceDocumentNo);
    if (p_M_InOut_ID == 0)
        throw new IllegalArgumentException("No Shipment");
    //
    MInOut ship = new MInOut(getCtx(), p_M_InOut_ID, get_TrxName());
    if (ship.get_ID() == 0)
        throw new IllegalArgumentException("Shipment not found");
    if (!MInOut.DOCSTATUS_Completed.equals(ship.getDocStatus()))
        throw new IllegalArgumentException("Shipment not completed");
    MInvoice invoice = new MInvoice(ship, null);
    // Should not override pricelist for RMA
    if (p_M_PriceList_ID != 0 && ship.getM_RMA_ID() == 0)
        invoice.setM_PriceList_ID(p_M_PriceList_ID);
    if (p_InvoiceDocumentNo != null && p_InvoiceDocumentNo.length() > 0)
        invoice.setDocumentNo(p_InvoiceDocumentNo);
    if (!invoice.save())
        throw new IllegalArgumentException("Cannot save Invoice");
    MInOutLine[] shipLines = ship.getLines(false);
    for (int i = 0; i < shipLines.length; i++) {
        MInOutLine sLine = shipLines[i];
        MInvoiceLine line = new MInvoiceLine(invoice);
        line.setShipLine(sLine);
        if (sLine.sameOrderLineUOM())
            line.setQtyEntered(sLine.getQtyEntered());
        else
            line.setQtyEntered(sLine.getMovementQty());
        line.setQtyInvoiced(sLine.getMovementQty());
        if (!line.save())
            throw new IllegalArgumentException("Cannot save Invoice Line");
    }
    return invoice.getDocumentNo();
}
Also used : MInOut(org.compiere.model.MInOut) MInOutLine(org.compiere.model.MInOutLine) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice)

Example 22 with MInvoice

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

the class InvoiceBatchProcess method doIt.

//  prepare
/**
	 * 	Process Invoice Batch
	 *	@return message
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("C_InvoiceBatch_ID=" + p_C_InvoiceBatch_ID + ", DocAction=" + p_DocAction);
    if (p_C_InvoiceBatch_ID == 0)
        throw new AdempiereUserError("C_InvoiceBatch_ID = 0");
    MInvoiceBatch batch = new MInvoiceBatch(getCtx(), p_C_InvoiceBatch_ID, get_TrxName());
    if (batch.get_ID() == 0)
        throw new AdempiereUserError("@NotFound@: @C_InvoiceBatch_ID@ - " + p_C_InvoiceBatch_ID);
    if (batch.isProcessed())
        throw new AdempiereUserError("@Processed@");
    //
    if (batch.getControlAmt().signum() != 0 && batch.getControlAmt().compareTo(batch.getDocumentAmt()) != 0)
        throw new AdempiereUserError("@ControlAmt@ <> @DocumentAmt@");
    //
    MInvoiceBatchLine[] lines = batch.getLines(false);
    for (int i = 0; i < lines.length; i++) {
        MInvoiceBatchLine line = lines[i];
        if (line.getC_Invoice_ID() != 0 || line.getC_InvoiceLine_ID() != 0)
            continue;
        if ((m_oldDocumentNo != null && !m_oldDocumentNo.equals(line.getDocumentNo())) || m_oldC_BPartner_ID != line.getC_BPartner_ID() || m_oldC_BPartner_Location_ID != line.getC_BPartner_Location_ID())
            completeInvoice();
        //	New Invoice
        if (m_invoice == null) {
            m_invoice = new MInvoice(batch, line);
            if (!m_invoice.save())
                throw new AdempiereUserError("Cannot save Invoice");
            //
            m_oldDocumentNo = line.getDocumentNo();
            m_oldC_BPartner_ID = line.getC_BPartner_ID();
            m_oldC_BPartner_Location_ID = line.getC_BPartner_Location_ID();
        }
        if (line.isTaxIncluded() != m_invoice.isTaxIncluded()) {
            //	rollback
            throw new AdempiereUserError("Line " + line.getLine() + " TaxIncluded inconsistent");
        }
        //	Add Line
        MInvoiceLine invoiceLine = new MInvoiceLine(m_invoice);
        invoiceLine.setDescription(line.getDescription());
        invoiceLine.setC_Charge_ID(line.getC_Charge_ID());
        // Entered/Invoiced
        invoiceLine.setQty(line.getQtyEntered());
        invoiceLine.setPrice(line.getPriceEntered());
        invoiceLine.setC_Tax_ID(line.getC_Tax_ID());
        invoiceLine.setTaxAmt(line.getTaxAmt());
        invoiceLine.setLineNetAmt(line.getLineNetAmt());
        invoiceLine.setLineTotalAmt(line.getLineTotalAmt());
        if (!invoiceLine.save()) {
            //	rollback
            throw new AdempiereUserError("Cannot save Invoice Line");
        }
        //	Update Batch Line
        line.setC_Invoice_ID(m_invoice.getC_Invoice_ID());
        line.setC_InvoiceLine_ID(invoiceLine.getC_InvoiceLine_ID());
        line.saveEx();
    }
    //	for all lines
    completeInvoice();
    //
    batch.setProcessed(true);
    batch.saveEx();
    return "#" + m_count;
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MInvoiceBatch(org.compiere.model.MInvoiceBatch) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) MInvoiceBatchLine(org.compiere.model.MInvoiceBatchLine)

Example 23 with MInvoice

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

the class InvoiceCreateFrom method addReference.

/**
	 * Add Reference to Order / Invoice / RMA
	 * @param invoice
	 * @param createFromType
	 * @param referenceId
     */
private void addReference(MInvoice invoice, String createFromType, int referenceId) {
    //	Valid Reference
    if (referenceId == 0)
        return;
    if (createFromType.equals(ORDER)) {
        MOrder order = new MOrder(getCtx(), referenceId, get_TrxName());
        invoice.setOrder(order);
    } else if (createFromType.equals(INVOICE)) {
        MInvoice fromInvoice = new MInvoice(getCtx(), referenceId, get_TrxName());
        invoice.setAD_OrgTrx_ID(fromInvoice.getAD_OrgTrx_ID());
        invoice.setC_Project_ID(fromInvoice.getC_Project_ID());
        invoice.setC_Campaign_ID(fromInvoice.getC_Campaign_ID());
        invoice.setC_Activity_ID(fromInvoice.getC_Activity_ID());
        invoice.setUser1_ID(fromInvoice.getUser1_ID());
        invoice.setUser2_ID(fromInvoice.getUser2_ID());
        invoice.setUser3_ID(fromInvoice.getUser3_ID());
        invoice.setUser4_ID(fromInvoice.getUser4_ID());
    } else if (createFromType.equals(RMA)) {
        MRMA rma = new MRMA(getCtx(), referenceId, get_TrxName());
        invoice.setRMA(rma);
    } else if (createFromType.equals(RECEIPT)) {
        MInOut inOut = new MInOut(getCtx(), referenceId, get_TrxName());
        invoice.setShipment(inOut);
    }
    //	Save
    invoice.saveEx();
}
Also used : MInOut(org.compiere.model.MInOut) MOrder(org.compiere.model.MOrder) MInvoice(org.compiere.model.MInvoice) MRMA(org.compiere.model.MRMA)

Example 24 with MInvoice

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

the class InvoiceCreateInOut method doIt.

//	prepare
/**
	 * 	Create Shipment
	 *	@return info
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("C_Invoice_ID=" + p_C_Invoice_ID + ", M_Warehouse_ID=" + p_M_Warehouse_ID);
    if (p_C_Invoice_ID <= 0)
        throw new FillMandatoryException("C_Invoice_ID");
    if (p_M_Warehouse_ID == 0)
        throw new FillMandatoryException(PARAM_M_Warehouse_ID);
    //
    MInvoice invoice = new MInvoice(getCtx(), p_C_Invoice_ID, null);
    if (invoice.get_ID() <= 0)
        throw new AdempiereException("@NotFound@ @C_Invoice_ID@");
    if (!MInvoice.DOCSTATUS_Completed.equals(invoice.getDocStatus()))
        throw new AdempiereException("@InvoiceCreateDocNotCompleted@");
    //
    for (MInvoiceLine invoiceLine : invoice.getLines(false)) {
        createLine(invoice, invoiceLine);
    }
    if (m_inout == null)
        throw new InvoiceFullyMatchedException();
    //
    return m_inout.getDocumentNo();
}
Also used : InvoiceFullyMatchedException(org.adempiere.exceptions.InvoiceFullyMatchedException) AdempiereException(org.adempiere.exceptions.AdempiereException) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) FillMandatoryException(org.adempiere.exceptions.FillMandatoryException)

Example 25 with MInvoice

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

the class InvoiceGenerate method createLine.

//	createLine
/**
	 * 	Create Invoice Line from Shipment
	 *	@param order order
	 *	@param ship shipment header
	 *	@param sLine shipment line
	 */
private void createLine(MOrder order, MInOut ship, MInOutLine sLine) {
    if (m_invoice == null) {
        m_invoice = new MInvoice(order, 0, p_DateInvoiced);
        if (!m_invoice.save())
            throw new IllegalStateException("Could not create Invoice (s)");
    }
    //	Create Shipment Comment Line
    if (m_ship == null || m_ship.getM_InOut_ID() != ship.getM_InOut_ID()) {
        MDocType dt = MDocType.get(getCtx(), ship.getC_DocType_ID());
        if (m_bp == null || m_bp.getC_BPartner_ID() != ship.getC_BPartner_ID())
            m_bp = new MBPartner(getCtx(), ship.getC_BPartner_ID(), get_TrxName());
        //	Reference: Delivery: 12345 - 12.12.12
        MClient client = MClient.get(getCtx(), order.getAD_Client_ID());
        String AD_Language = client.getAD_Language();
        if (client.isMultiLingualDocument() && m_bp.getAD_Language() != null)
            AD_Language = m_bp.getAD_Language();
        if (AD_Language == null)
            AD_Language = Language.getBaseAD_Language();
        java.text.SimpleDateFormat format = DisplayType.getDateFormat(DisplayType.Date, Language.getLanguage(AD_Language));
        String reference = dt.getPrintName(m_bp.getAD_Language()) + ": " + ship.getDocumentNo() + " - " + format.format(ship.getMovementDate());
        m_ship = ship;
        //
        MInvoiceLine line = new MInvoiceLine(m_invoice);
        line.setIsDescription(true);
        line.setDescription(reference);
        line.setLine(m_line + sLine.getLine() - 2);
        if (!line.save())
            throw new IllegalStateException("Could not create Invoice Comment Line (sh)");
        //	Optional Ship Address if not Bill Address
        if (order.getBill_Location_ID() != ship.getC_BPartner_Location_ID()) {
            MLocation addr = MLocation.getBPLocation(getCtx(), ship.getC_BPartner_Location_ID(), null);
            line = new MInvoiceLine(m_invoice);
            line.setIsDescription(true);
            line.setDescription(addr.toString());
            line.setLine(m_line + sLine.getLine() - 1);
            if (!line.save())
                throw new IllegalStateException("Could not create Invoice Comment Line 2 (sh)");
        }
    }
    //	
    MInvoiceLine line = new MInvoiceLine(m_invoice);
    line.setShipLine(sLine);
    if (sLine.sameOrderLineUOM())
        line.setQtyEntered(sLine.getQtyEntered());
    else
        line.setQtyEntered(sLine.getMovementQty());
    line.setQtyInvoiced(sLine.getMovementQty());
    line.setLine(m_line + sLine.getLine());
    //@Trifon - special handling when ShipLine.ToBeInvoiced='N'
    String toBeInvoiced = sLine.get_ValueAsString("ToBeInvoiced");
    if ("N".equals(toBeInvoiced)) {
        line.setPriceEntered(Env.ZERO);
        line.setPriceActual(Env.ZERO);
        line.setPriceLimit(Env.ZERO);
        line.setPriceList(Env.ZERO);
        //setC_Tax_ID(oLine.getC_Tax_ID());
        line.setLineNetAmt(Env.ZERO);
        line.setIsDescription(true);
    }
    if (!line.save())
        throw new IllegalStateException("Could not create Invoice Line (s)");
    //	Link
    sLine.setIsInvoiced(true);
    if (!sLine.save())
        throw new IllegalStateException("Could not update Shipment Line");
    log.fine(line.toString());
}
Also used : MDocType(org.compiere.model.MDocType) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) MBPartner(org.compiere.model.MBPartner) MLocation(org.compiere.model.MLocation) MClient(org.compiere.model.MClient)

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