Search in sources :

Example 1 with MInvoiceBatch

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

Aggregations

MInvoice (org.compiere.model.MInvoice)1 MInvoiceBatch (org.compiere.model.MInvoiceBatch)1 MInvoiceBatchLine (org.compiere.model.MInvoiceBatchLine)1 MInvoiceLine (org.compiere.model.MInvoiceLine)1 AdempiereUserError (org.compiere.util.AdempiereUserError)1