Search in sources :

Example 36 with MInvoice

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

the class InfoInvoicePanel method initInfo.

/**
     *  General Init
     */
private void initInfo(int record_id, String value) {
    if (!(record_id == 0) && value != null && value.length() > 0) {
        log.severe("Received both a record_id and a value: " + record_id + " - " + value);
    }
    //  Set values
    if (// A record is defined
    !(record_id == 0)) {
        fieldID = record_id;
        // Have to set isPaid and isSOTrx to match or the query will return no results
        String trxName = Trx.createTrxName();
        MInvoice mi = new MInvoice(Env.getCtx(), record_id, trxName);
        fIsPaid.setSelected(mi.isPaid());
        fIsSOTrx.setSelected(mi.isSOTrx());
        mi = null;
        Trx.get(trxName, false).close();
    } else // Try to find other criteria in the context
    {
        String id;
        //  C_BPartner_ID
        id = Env.getContext(Env.getCtx(), p_WindowNo, p_TabNo, "C_BPartner_ID", true);
        if (id != null && id.length() != 0 && (new Integer(id).intValue() > 0))
            fBPartner_ID.setValue(new Integer(id));
        //  C_Order_ID
        id = Env.getContext(Env.getCtx(), p_WindowNo, p_TabNo, "C_Order_ID", true);
        if (id != null && id.length() != 0 && (new Integer(id).intValue() > 0))
            fOrder_ID.setValue(new Integer(id));
        //  IsSOTrx - Window context
        id = Env.getContext(Env.getCtx(), p_WindowNo, "IsSOTrx", true);
        if (id != null && id.length() != 0 && (id == "Y" || id == "N")) {
            fIsSOTrx.setSelected(id == "Y");
        }
        //  The value passed in from the field
        if (value != null && value.length() > 0) {
            fDocumentNo.setValue(value);
        } else {
            //  C_Invoice_ID
            id = Env.getContext(Env.getCtx(), p_WindowNo, p_TabNo, "C_Invoice_ID", true);
            if (id != null && id.length() != 0 && (new Integer(id).intValue() > 0)) {
                fieldID = new Integer(id).intValue();
                // Have to set isPaid and isSOTrx to match or the query will return no results
                String trxName = Trx.createTrxName();
                MInvoice mi = new MInvoice(Env.getCtx(), record_id, trxName);
                fIsPaid.setSelected(mi.isPaid());
                fIsSOTrx.setSelected(mi.isSOTrx());
                mi = null;
                Trx.get(trxName, false).close();
            }
        }
    }
}
Also used : MInvoice(org.compiere.model.MInvoice)

Example 37 with MInvoice

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

the class Doc_Invoice method loadDocumentDetails.

/**
	 *  Load Specific Document Details
	 *  @return error message or null
	 */
protected String loadDocumentDetails() {
    MInvoice invoice = (MInvoice) getPO();
    setDateDoc(invoice.getDateInvoiced());
    setIsTaxIncluded(invoice.isTaxIncluded());
    //	Amounts
    setAmount(Doc.AMTTYPE_Gross, invoice.getGrandTotal());
    setAmount(Doc.AMTTYPE_Net, invoice.getTotalLines());
    setAmount(Doc.AMTTYPE_Charge, invoice.getChargeAmt());
    //	Contained Objects
    m_taxes = loadTaxes();
    p_lines = loadLines(invoice);
    log.fine("Lines=" + p_lines.length + ", Taxes=" + m_taxes.length);
    return null;
}
Also used : MInvoice(org.compiere.model.MInvoice)

Example 38 with MInvoice

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

the class Doc_AllocationTax method createFacts.

//  getBalance
/**
	 *  Create Facts (the accounting logic) for
	 *  CMA.
	 *  <pre>
	 *  AR_Invoice_Payment
	 *      UnAllocatedCash DR
	 *      or C_Prepayment
	 *      DiscountExp     DR
	 *      WriteOff        DR
	 *      Receivables             CR
	 *  AR_Invoice_Cash
	 *      CashTransfer    DR
	 *      DiscountExp     DR
	 *      WriteOff        DR
	 *      Receivables             CR
	 * 
	 *  AP_Invoice_Payment
	 *      Liability       DR
	 *      DiscountRev             CR
	 *      WriteOff                CR
	 *      PaymentSelect           CR
	 *      or V_Prepayment
	 *  AP_Invoice_Cash
	 *      Liability       DR
	 *      DiscountRev             CR
	 *      WriteOff                CR
	 *      CashTransfer            CR
	 *  CashBankTransfer
	 *      -
	 *  ==============================
	 *  Realized Gain & Loss
	 * 		AR/AP			DR		CR
	 * 		Realized G/L	DR		CR
	 * 
	 *
	 *  </pre>
	 *  Tax needs to be corrected for discount & write-off;
	 *  Currency gain & loss is realized here.
	 *  @param as accounting schema
	 *  @return Fact
	 */
public ArrayList<Fact> createFacts(MAcctSchema as) {
    m_facts = new ArrayList<Fact>();
    //  create Fact Header
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    // dummy fact (not posted) to calculate Realized Gain & Loss
    Fact factForRGL = new Fact(this, as, Fact.POST_Actual);
    boolean isInterOrg = isInterOrg(as);
    for (int i = 0; i < p_lines.length; i++) {
        DocLine_Allocation line = (DocLine_Allocation) p_lines[i];
        setC_BPartner_ID(line.getC_BPartner_ID());
        //  CashBankTransfer - all references null and Discount/WriteOff = 0
        if (line.getC_Payment_ID() != 0 && line.getC_Invoice_ID() == 0 && line.getC_Order_ID() == 0 && line.getC_CashLine_ID() == 0 && line.getC_BPartner_ID() == 0 && Env.ZERO.compareTo(line.getDiscountAmt()) == 0 && Env.ZERO.compareTo(line.getWriteOffAmt()) == 0)
            continue;
        //	Receivables/Liability Amt
        BigDecimal allocationSource = line.getAmtSource().add(line.getDiscountAmt()).add(line.getWriteOffAmt());
        // for realized gain & loss purposes
        BigDecimal allocationSourceForRGL = allocationSource;
        // AR/AP balance corrected
        BigDecimal allocationAccounted = Env.ZERO;
        // for realized gain & loss purposes
        BigDecimal allocationAccountedForRGL = Env.ZERO;
        FactLine fl = null;
        FactLine flForRGL = null;
        //	Liability/Receivables
        MAccount bpAcct = null;
        //
        MPayment payment = null;
        if (line.getC_Payment_ID() != 0)
            payment = new MPayment(getCtx(), line.getC_Payment_ID(), getTrxName());
        MInvoice invoice = null;
        if (line.getC_Invoice_ID() != 0)
            invoice = new MInvoice(getCtx(), line.getC_Invoice_ID(), getTrxName());
        //	No Invoice
        if (invoice == null) {
            /**
				   * Adaxa: Paul
				   * Allocate to charges
				   */
            if (line.getC_Invoice_ID() == 0 && line.getC_Payment_ID() == 0 && line.getC_Charge_ID() != 0) {
                fl = fact.createLine(line, line.getChargeAccount(as, line.getAmtSource()), getC_Currency_ID(), line.getAmtSource());
            } else //	Payment Only
            if (line.getC_Invoice_ID() == 0 && line.getC_Payment_ID() != 0) {
                if (line.getAmtSource().signum() >= 0)
                    fl = fact.createLine(line, getPaymentAcct(as, line.getC_Payment_ID()), getC_Currency_ID(), line.getAmtSource(), null);
                else
                    fl = fact.createLine(line, getPaymentAcct(as, line.getC_Payment_ID()), getC_Currency_ID(), null, line.getAmtSource().negate());
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            } else {
                p_Error = "Cannot determine SO/PO";
                log.log(Level.SEVERE, p_Error);
                return null;
            }
        } else //	Sales Invoice	
        if (invoice.isSOTrx()) {
            // Avoid usage of clearing accounts
            // If both accounts Unallocated Cash and Receivable are equal
            // then don't post
            MAccount acct_unallocated_cash = null;
            if (line.getC_Payment_ID() != 0)
                acct_unallocated_cash = getPaymentAcct(as, line.getC_Payment_ID());
            else if (line.getC_CashLine_ID() != 0)
                acct_unallocated_cash = getCashAcct(as, line.getC_CashLine_ID());
            MAccount acct_receivable = getAccount(Doc.ACCTTYPE_C_Receivable, as);
            if ((!as.isPostIfClearingEqual()) && acct_unallocated_cash != null && acct_unallocated_cash.equals(acct_receivable) && (!isInterOrg)) {
                // if not using clearing accounts, then don't post amtsource
                // change the allocationsource to be writeoff + discount
                allocationSource = line.getDiscountAmt().add(line.getWriteOffAmt());
            } else {
                //	Payment/Cash	DR
                if (line.getC_Payment_ID() != 0) {
                    fl = fact.createLine(line, getPaymentAcct(as, line.getC_Payment_ID()), getC_Currency_ID(), line.getAmtSource(), null);
                    if (fl != null && payment != null)
                        fl.setAD_Org_ID(payment.getAD_Org_ID());
                } else if (line.getC_CashLine_ID() != 0) {
                    fl = fact.createLine(line, getCashAcct(as, line.getC_CashLine_ID()), getC_Currency_ID(), line.getAmtSource(), null);
                    MCashLine cashLine = new MCashLine(getCtx(), line.getC_CashLine_ID(), getTrxName());
                    if (fl != null && cashLine.get_ID() != 0)
                        fl.setAD_Org_ID(cashLine.getAD_Org_ID());
                }
            }
            //	Discount		DR
            if (Env.ZERO.compareTo(line.getDiscountAmt()) != 0) {
                fl = fact.createLine(line, getAccount(Doc.ACCTTYPE_DiscountExp, as), getC_Currency_ID(), line.getDiscountAmt(), null);
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            }
            //	Write off		DR
            if (Env.ZERO.compareTo(line.getWriteOffAmt()) != 0) {
                fl = fact.createLine(line, getAccount(Doc.ACCTTYPE_WriteOff, as), getC_Currency_ID(), line.getWriteOffAmt(), null);
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            }
            //	AR Invoice Amount	CR
            if (as.isAccrual()) {
                bpAcct = getAccount(Doc.ACCTTYPE_C_Receivable, as);
                if (allocationSource.signum() >= 0)
                    //	payment currency
                    fl = fact.createLine(line, bpAcct, getC_Currency_ID(), null, allocationSource);
                else
                    //	payment currency
                    fl = fact.createLine(line, bpAcct, getC_Currency_ID(), allocationSource.negate(), null);
                if (fl != null)
                    allocationAccounted = fl.getAcctBalance().negate();
                if (fl != null && invoice != null)
                    fl.setAD_Org_ID(invoice.getAD_Org_ID());
                // for Realized Gain & Loss
                flForRGL = factForRGL.createLine(line, bpAcct, getC_Currency_ID(), null, //	payment currency 
                allocationSourceForRGL);
                if (flForRGL != null)
                    allocationAccountedForRGL = flForRGL.getAcctBalance().negate();
            } else //	Cash Based
            {
                allocationAccounted = createCashBasedAcct(as, fact, invoice, allocationSource);
                allocationAccountedForRGL = allocationAccounted;
            }
        } else //	Purchase Invoice
        {
            // Avoid usage of clearing accounts
            // If both accounts Payment Select and Liability are equal
            // then don't post
            MAccount acct_payment_select = null;
            if (line.getC_Payment_ID() != 0)
                acct_payment_select = getPaymentAcct(as, line.getC_Payment_ID());
            else if (line.getC_CashLine_ID() != 0)
                acct_payment_select = getCashAcct(as, line.getC_CashLine_ID());
            MAccount acct_liability = getAccount(Doc.ACCTTYPE_V_Liability, as);
            boolean isUsingClearing = true;
            // Save original allocation source for realized gain & loss purposes
            allocationSourceForRGL = allocationSourceForRGL.negate();
            if ((!as.isPostIfClearingEqual()) && acct_payment_select != null && acct_payment_select.equals(acct_liability) && (!isInterOrg)) {
                // if not using clearing accounts, then don't post amtsource
                // change the allocationsource to be writeoff + discount
                allocationSource = line.getDiscountAmt().add(line.getWriteOffAmt());
                isUsingClearing = false;
            }
            // End Avoid usage of clearing accounts
            //	allocation is negative
            allocationSource = allocationSource.negate();
            //	AP Invoice Amount	DR
            if (as.isAccrual()) {
                bpAcct = getAccount(Doc.ACCTTYPE_V_Liability, as);
                if (allocationSource.signum() >= 0)
                    //	payment currency
                    fl = fact.createLine(line, bpAcct, getC_Currency_ID(), allocationSource, null);
                else
                    //	payment currency
                    fl = fact.createLine(line, bpAcct, getC_Currency_ID(), null, allocationSource.negate());
                if (fl != null)
                    allocationAccounted = fl.getAcctBalance();
                if (fl != null && invoice != null)
                    fl.setAD_Org_ID(invoice.getAD_Org_ID());
                // for Realized Gain & Loss
                flForRGL = factForRGL.createLine(line, bpAcct, getC_Currency_ID(), allocationSourceForRGL, //	payment currency
                null);
                if (flForRGL != null)
                    allocationAccountedForRGL = flForRGL.getAcctBalance();
            } else //	Cash Based
            {
                allocationAccounted = createCashBasedAcct(as, fact, invoice, allocationSource);
                allocationAccountedForRGL = allocationAccounted;
            }
            //	Discount		CR
            if (Env.ZERO.compareTo(line.getDiscountAmt()) != 0) {
                fl = fact.createLine(line, getAccount(Doc.ACCTTYPE_DiscountRev, as), getC_Currency_ID(), null, line.getDiscountAmt().negate());
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            }
            //	Write off		CR
            if (Env.ZERO.compareTo(line.getWriteOffAmt()) != 0) {
                fl = fact.createLine(line, getAccount(Doc.ACCTTYPE_WriteOff, as), getC_Currency_ID(), null, line.getWriteOffAmt().negate());
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            }
            //	Payment/Cash	CR
            if (// Avoid usage of clearing accounts
            isUsingClearing && line.getC_Payment_ID() != 0) {
                fl = fact.createLine(line, getPaymentAcct(as, line.getC_Payment_ID()), getC_Currency_ID(), null, line.getAmtSource().negate());
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            } else if (// Avoid usage of clearing accounts
            isUsingClearing && line.getC_CashLine_ID() != 0) {
                fl = fact.createLine(line, getCashAcct(as, line.getC_CashLine_ID()), getC_Currency_ID(), null, line.getAmtSource().negate());
                MCashLine cashLine = new MCashLine(getCtx(), line.getC_CashLine_ID(), getTrxName());
                if (fl != null && cashLine.get_ID() != 0)
                    fl.setAD_Org_ID(cashLine.getAD_Org_ID());
            }
        }
        //	VAT Tax Correction
        if (invoice != null && as.isTaxCorrection()) {
            BigDecimal taxCorrectionAmt = Env.ZERO;
            if (as.isTaxCorrectionDiscount())
                taxCorrectionAmt = line.getDiscountAmt();
            if (as.isTaxCorrectionWriteOff())
                taxCorrectionAmt = taxCorrectionAmt.add(line.getWriteOffAmt());
            //
            if (taxCorrectionAmt.signum() != 0) {
                if (!createTaxCorrection(as, fact, line, getAccount(invoice.isSOTrx() ? Doc.ACCTTYPE_DiscountExp : Doc.ACCTTYPE_DiscountRev, as), getAccount(Doc.ACCTTYPE_WriteOff, as), invoice.isSOTrx())) {
                    p_Error = "Cannot create Tax correction";
                    return null;
                }
            }
        }
        //	Realized Gain & Loss
        if (invoice != null && (//	payment allocation in foreign currency
        getC_Currency_ID() != as.getC_Currency_ID() || //	allocation <> invoice currency
        getC_Currency_ID() != line.getInvoiceC_Currency_ID())) {
            p_Error = createRealizedGainLoss(line, as, fact, bpAcct, invoice, allocationSource, allocationAccounted);
            if (p_Error != null)
                return null;
        }
    }
    // FR [ 1840016 ] Avoid usage of clearing accounts - subject to C_AcctSchema.IsPostIfClearingEqual
    if ((!as.isPostIfClearingEqual()) && p_lines.length > 0 && (!isInterOrg)) {
        boolean allEquals = true;
        // more than one line (i.e. crossing one payment+ with a payment-, or an invoice against a credit memo)
        // verify if the sum of all facts is zero net
        FactLine[] factlines = fact.getLines();
        BigDecimal netBalance = Env.ZERO;
        FactLine prevFactLine = null;
        for (FactLine factLine : factlines) {
            netBalance = netBalance.add(factLine.getAmtSourceDr()).subtract(factLine.getAmtSourceCr());
            if (prevFactLine != null) {
                if (!equalFactLineIDs(prevFactLine, factLine)) {
                    allEquals = false;
                    break;
                }
            }
            prevFactLine = factLine;
        }
        if (netBalance.compareTo(Env.ZERO) == 0 && allEquals) {
            // delete the postings
            for (FactLine factline : factlines) fact.remove(factline);
        }
    }
    //	reset line info
    setC_BPartner_ID(0);
    //
    m_facts.add(fact);
    return m_facts;
}
Also used : MAccount(org.compiere.model.MAccount) MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) BigDecimal(java.math.BigDecimal) MCashLine(org.compiere.model.MCashLine)

Example 39 with MInvoice

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

the class WBOMDrop 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 = m_qtyList.get(i).getValue();
            int M_Product_ID = 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
    FDialog.info(-1, this, invoice.getDocumentInfo() + " " + Msg.translate(Env.getCtx(), "Inserted") + "=" + lineCount);
    log.config("#" + lineCount);
    return true;
}
Also used : MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) BigDecimal(java.math.BigDecimal)

Example 40 with MInvoice

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

the class Doc_MatchInv method createFacts.

//  getBalance
/**
	 *  Create Facts (the accounting logic) for
	 *  MXI.
	 * 	(single line)
	 *  <pre>
	 *      NotInvoicedReceipts     DR			(Receipt Org)
	 *      InventoryClearing               CR
	 *      InvoicePV               DR      CR  (difference)
	 *  Commitment
	 * 		Expense							CR
	 * 		Offset					DR
	 *  </pre>
	 *  @param as accounting schema
	 *  @return Fact
	 */
public ArrayList<Fact> createFacts(MAcctSchema as) {
    ArrayList<Fact> facts = new ArrayList<Fact>();
    //  Nothing to do
    if (//	no Product
    getM_Product_ID() == 0 || getQty().signum() == 0 || //	Qty = 0
    m_receiptLine.getMovementQty().signum() == 0) {
        log.fine("No Product/Qty - M_Product_ID=" + getM_Product_ID() + ",Qty=" + getQty() + ",InOutQty=" + m_receiptLine.getMovementQty());
        return facts;
    }
    MCostType ct = MCostType.get(as, getM_Product_ID(), getAD_Org_ID());
    //  create Fact Header
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    setC_Currency_ID(as.getC_Currency_ID());
    boolean isInterOrg = isInterOrg(as);
    /**	Needs to be handeled in PO Matching as no Receipt info
		if (m_pc.isService())
		{
			log.fine("Service - skipped");
			return fact;
		}
		**/
    if (!m_receiptLine.getM_Product().isStocked())
        return facts;
    //  NotInvoicedReceipt      DR
    //  From Receipt
    BigDecimal multiplier = getQty().divide(m_receiptLine.getMovementQty(), 12, BigDecimal.ROUND_HALF_UP).abs();
    FactLine dr = fact.createLine(null, getAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as), as.getC_Currency_ID(), Env.ONE, // updated below
    null);
    if (dr == null) {
        p_Error = "No Product Costs";
        return null;
    }
    dr.setM_Product_ID(m_receiptLine.getM_Product_ID());
    String documentBaseTypeReceipt = DB.getSQLValueString(m_receiptLine.get_TrxName(), "SELECT DocBaseType FROM C_DocType WHERE C_DocType_ID=?", m_receiptLine.getParent().getC_DocType_ID());
    BigDecimal quantityReceipt = MDocType.DOCBASETYPE_MaterialReceipt.equals(documentBaseTypeReceipt) ? getQty() : getQty().negate();
    dr.setQty(quantityReceipt);
    BigDecimal temp = dr.getAcctBalance();
    //	Set AmtAcctCr/Dr from Receipt (sets also Project)
    if (!//	Amt updated
    dr.updateReverseLine(//	Amt updated
    MInOut.Table_ID, m_receiptLine.getM_InOut_ID(), m_receiptLine.getM_InOutLine_ID(), quantityReceipt, multiplier)) {
        p_Error = "Mat.Receipt not posted yet";
        return null;
    }
    log.fine("CR - Amt(" + temp + "->" + dr.getAcctBalance() + ") - " + dr.toString());
    //  InventoryClearing               CR
    //  From Invoice
    MAccount expense = m_pc.getAccount(ProductCost.ACCTTYPE_P_InventoryClearing, as);
    if (m_pc.isService())
        expense = m_pc.getAccount(ProductCost.ACCTTYPE_P_Expense, as);
    BigDecimal LineNetAmt = m_invoiceLine.getLineNetAmt();
    multiplier = getQty().divide(m_invoiceLine.getQtyInvoiced(), 12, BigDecimal.ROUND_HALF_UP).abs();
    if (multiplier.compareTo(Env.ONE) != 0)
        LineNetAmt = LineNetAmt.multiply(multiplier);
    if (m_pc.isService())
        //	book out exact receipt amt
        LineNetAmt = dr.getAcctBalance();
    FactLine cr = null;
    if (as.isAccrual()) {
        cr = fact.createLine(null, expense, as.getC_Currency_ID(), null, //	updated below
        LineNetAmt);
        if (cr == null) {
            log.fine("Line Net Amt=0 - M_Product_ID=" + getM_Product_ID() + ",Qty=" + getQty() + ",InOutQty=" + m_receiptLine.getMovementQty());
            //  Invoice Price Variance
            BigDecimal ipv = dr.getSourceBalance().negate();
            if (ipv.signum() != 0) {
                BigDecimal costs = MCostDetail.getByDocLineMatchInv(m_invoiceLine, m_receiptLine, as.getC_AcctSchema_ID(), as.getM_CostType_ID());
                int ACCTTYPE_P = 0;
                if (MCostType.COSTINGMETHOD_StandardCosting.equals(ct.getCostingMethod()) || MCostType.COSTINGMETHOD_AverageInvoice.equals(ct.getCostingMethod()))
                    ACCTTYPE_P = ProductCost.ACCTTYPE_P_IPV;
                else
                    ACCTTYPE_P = ProductCost.ACCTTYPE_P_Asset;
                BigDecimal diff = ipv.subtract(costs);
                MInvoice m_invoice = m_invoiceLine.getParent();
                int C_Currency_ID = m_invoice.getC_Currency_ID();
                FactLine pv = fact.createLine(null, m_pc.getAccount(ACCTTYPE_P, as), C_Currency_ID, ipv);
                pv.setC_Activity_ID(m_invoiceLine.getC_Activity_ID());
                pv.setC_Campaign_ID(m_invoiceLine.getC_Campaign_ID());
                pv.setC_Project_ID(m_invoiceLine.getC_Project_ID());
                pv.setC_ProjectPhase_ID(m_invoiceLine.getC_ProjectPhase_ID());
                pv.setC_ProjectTask_ID(m_invoiceLine.getC_ProjectTask_ID());
                pv.setC_UOM_ID(m_invoiceLine.getC_UOM_ID());
                pv.setUser1_ID(m_invoiceLine.getUser1_ID());
                pv.setUser2_ID(m_invoiceLine.getUser2_ID());
                pv.setUser3_ID(m_invoiceLine.getUser3_ID());
                pv.setUser4_ID(m_invoiceLine.getUser4_ID());
                if (diff.compareTo(Env.ZERO) != 0 && MCostType.COSTINGMETHOD_AverageInvoice.equals(ct.getCostingMethod())) {
                    FactLine diffline = fact.createLine(null, m_pc.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), C_Currency_ID, diff.negate());
                    diffline.setC_Activity_ID(m_invoiceLine.getC_Activity_ID());
                    diffline.setC_Project_ID(m_invoiceLine.getC_Project_ID());
                    diffline.setC_ProjectPhase_ID(m_invoiceLine.getC_ProjectPhase_ID());
                    diffline.setC_ProjectTask_ID(m_invoiceLine.getC_ProjectTask_ID());
                    diffline.setC_UOM_ID(m_invoiceLine.getC_UOM_ID());
                    diffline.setUser1_ID(m_invoiceLine.getUser1_ID());
                    diffline.setUser2_ID(m_invoiceLine.getUser2_ID());
                    diffline.setUser3_ID(m_invoiceLine.getUser3_ID());
                    diffline.setUser4_ID(m_invoiceLine.getUser4_ID());
                }
            }
            log.fine("IPV=" + ipv + "; Balance=" + fact.getSourceBalance());
            facts.add(fact);
            return facts;
        }
        cr.setM_Product_ID(m_invoiceLine.getM_Product_ID());
        temp = cr.getAcctBalance();
        String documentBaseTypeInvoice = DB.getSQLValueString(m_invoiceLine.get_TrxName(), "SELECT DocBaseType FROM C_DocType WHERE C_DocType_ID=?", m_invoiceLine.getParent().getC_DocType_ID());
        BigDecimal quantityInvoice = MDocType.DOCBASETYPE_APInvoice.equals(documentBaseTypeInvoice) ? getQty().negate() : getQty();
        cr.setQty(quantityInvoice);
        //	Set AmtAcctCr/Dr from Invoice (sets also Project)
        if (as.isAccrual() && !//	Amt updated
        cr.updateReverseLine(//	Amt updated
        MInvoice.Table_ID, m_invoiceLine.getC_Invoice_ID(), m_invoiceLine.getC_InvoiceLine_ID(), quantityInvoice, multiplier)) {
            p_Error = "Invoice not posted yet";
            return null;
        }
        log.fine("DR - Amt(" + temp + "->" + cr.getAcctBalance() + ") - " + cr.toString());
    } else //	Cash Acct
    {
        MInvoice invoice = m_invoiceLine.getParent();
        if (as.getC_Currency_ID() != invoice.getC_Currency_ID())
            LineNetAmt = MConversionRate.convert(getCtx(), LineNetAmt, invoice.getC_Currency_ID(), as.getC_Currency_ID(), invoice.getDateAcct(), invoice.getC_ConversionType_ID(), invoice.getAD_Client_ID(), invoice.getAD_Org_ID());
        cr = fact.createLine(null, expense, as.getC_Currency_ID(), null, LineNetAmt);
        cr.setQty(getQty().multiply(multiplier).negate());
    }
    cr.setC_Activity_ID(m_invoiceLine.getC_Activity_ID());
    cr.setC_Campaign_ID(m_invoiceLine.getC_Campaign_ID());
    cr.setC_Project_ID(m_invoiceLine.getC_Project_ID());
    cr.setC_ProjectPhase_ID(m_invoiceLine.getC_ProjectPhase_ID());
    cr.setC_ProjectTask_ID(m_invoiceLine.getC_ProjectTask_ID());
    cr.setC_UOM_ID(m_invoiceLine.getC_UOM_ID());
    cr.setUser1_ID(m_invoiceLine.getUser1_ID());
    cr.setUser2_ID(m_invoiceLine.getUser2_ID());
    cr.setUser3_ID(m_invoiceLine.getUser3_ID());
    cr.setUser4_ID(m_invoiceLine.getUser4_ID());
    //see also Fact.java: checking for isMultiCurrency()
    if (dr.getC_Currency_ID() != cr.getC_Currency_ID())
        setIsMultiCurrency(true);
    //end AZ
    // Avoid usage of clearing accounts
    // If both accounts Not Invoiced Receipts and Inventory Clearing are equal
    // then remove the posting
    // not_invoiced_receipts
    MAccount acct_db = dr.getAccount();
    // inventory_clearing
    MAccount acct_cr = cr.getAccount();
    if ((!as.isPostIfClearingEqual()) && acct_db.equals(acct_cr) && (!isInterOrg)) {
        BigDecimal debit = dr.getAmtSourceDr();
        BigDecimal credit = cr.getAmtSourceCr();
        if (debit.compareTo(credit) == 0) {
            fact.remove(dr);
            fact.remove(cr);
        }
    }
    // End Avoid usage of clearing accounts
    //  Invoice Price Variance 	difference
    BigDecimal ipv = cr.getAcctBalance().add(dr.getAcctBalance()).negate();
    if (ipv.compareTo(Env.ZERO) == 0) {
        facts.add(fact);
        return facts;
    }
    if (!MCostType.COSTINGMETHOD_AverageInvoice.equals(ct.getCostingMethod())) {
        int ACCTTYPE_P = 0;
        if (MCostType.COSTINGMETHOD_StandardCosting.equals(ct.getCostingMethod()) || MCostType.COSTINGMETHOD_AverageInvoice.equals(ct.getCostingMethod()))
            ACCTTYPE_P = ProductCost.ACCTTYPE_P_IPV;
        else
            ACCTTYPE_P = ProductCost.ACCTTYPE_P_Asset;
        FactLine pv = fact.createLine(null, m_pc.getAccount(ACCTTYPE_P, as), as.getC_Currency_ID(), ipv);
        pv.setC_Activity_ID(m_invoiceLine.getC_Activity_ID());
        pv.setC_Campaign_ID(m_invoiceLine.getC_Campaign_ID());
        pv.setC_Project_ID(m_invoiceLine.getC_Project_ID());
        pv.setC_ProjectPhase_ID(m_invoiceLine.getC_ProjectPhase_ID());
        pv.setC_ProjectTask_ID(m_invoiceLine.getC_ProjectTask_ID());
        pv.setC_UOM_ID(m_invoiceLine.getC_UOM_ID());
        pv.setUser1_ID(m_invoiceLine.getUser1_ID());
        pv.setUser2_ID(m_invoiceLine.getUser2_ID());
        pv.setUser3_ID(m_invoiceLine.getUser3_ID());
        pv.setUser4_ID(m_invoiceLine.getUser4_ID());
    //
    } else {
        BigDecimal costs = MCostDetail.getByDocLineMatchInv(m_invoiceLine, m_receiptLine, as.getC_AcctSchema_ID(), as.getM_CostType_ID());
        FactLine pv = fact.createLine(null, m_pc.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), costs);
        if (pv != null) {
            pv.setC_Activity_ID(m_invoiceLine.getC_Activity_ID());
            pv.setC_Campaign_ID(m_invoiceLine.getC_Campaign_ID());
            pv.setC_Project_ID(m_invoiceLine.getC_Project_ID());
            pv.setC_ProjectPhase_ID(m_invoiceLine.getC_ProjectPhase_ID());
            pv.setC_ProjectTask_ID(m_invoiceLine.getC_ProjectTask_ID());
            pv.setC_UOM_ID(m_invoiceLine.getC_UOM_ID());
            pv.setUser1_ID(m_invoiceLine.getUser1_ID());
            pv.setUser2_ID(m_invoiceLine.getUser2_ID());
            pv.setUser3_ID(m_invoiceLine.getUser3_ID());
            pv.setUser4_ID(m_invoiceLine.getUser4_ID());
        }
        BigDecimal diff = ipv.subtract(costs);
        MInvoice m_invoice = m_invoiceLine.getParent();
        if (diff.compareTo(Env.ZERO) != 0) {
            FactLine diffline = fact.createLine(null, m_pc.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), as.getC_Currency_ID(), diff);
            if (diffline != null) {
                diffline.setC_Activity_ID(m_invoiceLine.getC_Activity_ID());
                diffline.setC_Project_ID(m_invoiceLine.getC_Project_ID());
                diffline.setC_ProjectPhase_ID(m_invoiceLine.getC_ProjectPhase_ID());
                diffline.setC_ProjectTask_ID(m_invoiceLine.getC_ProjectTask_ID());
                diffline.setC_UOM_ID(m_invoiceLine.getC_UOM_ID());
                diffline.setUser1_ID(m_invoiceLine.getUser1_ID());
                diffline.setUser2_ID(m_invoiceLine.getUser2_ID());
                diffline.setUser3_ID(m_invoiceLine.getUser3_ID());
                diffline.setUser4_ID(m_invoiceLine.getUser4_ID());
            }
        }
    }
    log.fine("IPV=" + ipv + "; Balance=" + fact.getSourceBalance());
    facts.add(fact);
    /** Commitment release										****/
    if (as.isAccrual() && as.isCreatePOCommitment()) {
        fact = Doc_Order.getCommitmentRelease(as, this, getQty(), m_invoiceLine.getC_InvoiceLine_ID(), Env.ONE);
        if (fact == null)
            return null;
        facts.add(fact);
    }
    return facts;
}
Also used : MAccount(org.compiere.model.MAccount) ArrayList(java.util.ArrayList) MInvoice(org.compiere.model.MInvoice) MCostType(org.compiere.model.MCostType) 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