Search in sources :

Example 1 with MFactAcct

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

the class FactLine method updateReverseLine.

//  createRevenueRecognition
/**************************************************************************
	 * 	Update Line with reversed Original Amount in Accounting Currency.
	 * 	Also copies original dimensions like Project, etc.
	 * 	Called from Doc_MatchInv
	 * 	@param AD_Table_ID table
	 * 	@param Record_ID record
	 * 	@param Line_ID line
     * 	@param quantity Line
	 * 	@param multiplier targetQty/documentQty
	 * 	@return true if success
	 */
public boolean updateReverseLine(int AD_Table_ID, int Record_ID, int Line_ID, BigDecimal quantity, BigDecimal multiplier) {
    boolean success = false;
    String sql = "SELECT * " + "FROM Fact_Acct " + "WHERE C_AcctSchema_ID=? AND AD_Table_ID=? AND Record_ID=?" + " AND Line_ID=? AND Account_ID=? AND Qty=?";
    // for Inventory Move
    if (MMovement.Table_ID == AD_Table_ID)
        sql += " AND M_Locator_ID=?";
    // end MZ
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, get_TrxName());
        pstmt.setInt(1, getC_AcctSchema_ID());
        pstmt.setInt(2, AD_Table_ID);
        pstmt.setInt(3, Record_ID);
        pstmt.setInt(4, Line_ID);
        pstmt.setInt(5, m_acct.getAccount_ID());
        // Negate quantity to get the credit account fact
        pstmt.setBigDecimal(6, quantity.negate());
        // for Inventory Move
        if (MMovement.Table_ID == AD_Table_ID)
            pstmt.setInt(7, getM_Locator_ID());
        // end MZ
        rs = pstmt.executeQuery();
        if (rs.next()) {
            MFactAcct fact = new MFactAcct(getCtx(), rs, get_TrxName());
            //  Accounted Amounts - reverse
            BigDecimal dr = fact.getAmtAcctDr();
            BigDecimal cr = fact.getAmtAcctCr();
            if (MMatchInv.Table_ID == getAD_Table_ID()) {
                dr = fact.getAmtAcctCr();
                cr = fact.getAmtAcctDr();
            }
            // setAmtAcctDr (cr.multiply(multiplier));
            // setAmtAcctCr (dr.multiply(multiplier));
            setAmtAcct(fact.getC_Currency_ID(), dr.multiply(multiplier), cr.multiply(multiplier));
            //  
            //  Bayu Sistematika - Source Amounts
            //  Fixing source amounts
            BigDecimal drSourceAmt = fact.getAmtSourceDr();
            BigDecimal crSourceAmt = fact.getAmtSourceCr();
            if (MMatchInv.Table_ID == getAD_Table_ID()) {
                drSourceAmt = fact.getAmtSourceCr();
                crSourceAmt = fact.getAmtSourceDr();
            }
            setAmtSource(fact.getC_Currency_ID(), drSourceAmt.multiply(multiplier), crSourceAmt.multiply(multiplier));
            //  end Bayu Sistematika
            //
            success = true;
            log.fine(new StringBuffer("(Table=").append(AD_Table_ID).append(",Record_ID=").append(Record_ID).append(",Line=").append(Record_ID).append(", Account=").append(m_acct).append(",dr=").append(dr).append(",cr=").append(cr).append(") - DR=").append(getAmtSourceDr()).append("|").append(getAmtAcctDr()).append(", CR=").append(getAmtSourceCr()).append("|").append(getAmtAcctCr()).toString());
            //	Dimensions
            setAD_OrgTrx_ID(fact.getAD_OrgTrx_ID());
            setC_Project_ID(fact.getC_Project_ID());
            setC_ProjectPhase_ID(fact.getC_ProjectPhase_ID());
            setC_ProjectTask_ID(fact.getC_ProjectTask_ID());
            setC_Activity_ID(fact.getC_Activity_ID());
            setC_Campaign_ID(fact.getC_Campaign_ID());
            setC_SalesRegion_ID(fact.getC_SalesRegion_ID());
            setC_LocFrom_ID(fact.getC_LocFrom_ID());
            setC_LocTo_ID(fact.getC_LocTo_ID());
            setM_Product_ID(fact.getM_Product_ID());
            setM_Locator_ID(fact.getM_Locator_ID());
            setUser1_ID(fact.getUser1_ID());
            setUser2_ID(fact.getUser2_ID());
            setUser3_ID(fact.getUser3_ID());
            setUser4_ID(fact.getUser4_ID());
            setC_UOM_ID(fact.getC_UOM_ID());
            setC_Tax_ID(fact.getC_Tax_ID());
            //	Org for cross charge
            setAD_Org_ID(fact.getAD_Org_ID());
        } else
            log.warning(new StringBuffer("Not Found (try later) ").append(",C_AcctSchema_ID=").append(getC_AcctSchema_ID()).append(", AD_Table_ID=").append(AD_Table_ID).append(",Record_ID=").append(Record_ID).append(",Line_ID=").append(Line_ID).append(", Account_ID=").append(m_acct.getAccount_ID()).toString());
    } catch (SQLException e) {
        log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return success;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) MFactAcct(org.compiere.model.MFactAcct) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 2 with MFactAcct

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

the class Doc_AllocationTax method createTaxCorrection.

//	createRealizedGainLoss
/**************************************************************************
	 * 	Create Tax Correction.
	 * 	Requirement: Adjust the tax amount, if you did not receive the full
	 * 	amount of the invoice (payment discount, write-off).
	 * 	Applies to many countries with VAT.
	 * 	Example:
	 * 		Invoice:	Net $100 + Tax1 $15 + Tax2 $5 = Total $120
	 * 		Payment:	$115 (i.e. $5 underpayment)
	 * 		Tax Adjustment = Tax1 = 0.63 (15/120*5) Tax2 = 0.21 (5/120/5) 
	 * 
	 * 	@param as accounting schema
	 * 	@param fact fact
	 * 	@param line Allocation line
	 *	@param DiscountAccount discount acct
	 *	@param WriteOffAccoint write off acct
	 *	@return true if created
	 */
private boolean createTaxCorrection(MAcctSchema as, Fact fact, DocLine_Allocation line, MAccount DiscountAccount, MAccount WriteOffAccoint, boolean isSOTrx) {
    log.info(line.toString());
    BigDecimal discount = Env.ZERO;
    if (as.isTaxCorrectionDiscount())
        discount = line.getDiscountAmt();
    BigDecimal writeOff = Env.ZERO;
    if (as.isTaxCorrectionWriteOff())
        writeOff = line.getWriteOffAmt();
    Doc_AllocationTax tax = new Doc_AllocationTax(DiscountAccount, discount, WriteOffAccoint, writeOff, isSOTrx);
    //	Get Source Amounts with account
    String sql = "SELECT * " + "FROM Fact_Acct " + //	Invoice
    "WHERE AD_Table_ID=318 AND Record_ID=?" + " AND C_AcctSchema_ID=?" + //	header lines like tax or total
    " AND Line_ID IS NULL";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, getTrxName());
        pstmt.setInt(1, line.getC_Invoice_ID());
        pstmt.setInt(2, as.getC_AcctSchema_ID());
        rs = pstmt.executeQuery();
        while (rs.next()) tax.addInvoiceFact(new MFactAcct(getCtx(), rs, fact.get_TrxName()));
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //	Invoice Not posted
    if (tax.getLineCount() == 0) {
        log.warning("Invoice not posted yet - " + line);
        return false;
    }
    //	size = 1 if no tax
    if (tax.getLineCount() < 2)
        return true;
    return tax.createEntries(as, fact, line);
}
Also used : ResultSet(java.sql.ResultSet) MFactAcct(org.compiere.model.MFactAcct) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 3 with MFactAcct

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

the class Doc_AllocationTax method createEntries.

//	getLineCount
/**
	 * 	Create Accounting Entries
	 *	@param as account schema
	 *	@param fact fact to add lines
	 *	@param line line
	 *	@return true if created
	 */
public boolean createEntries(MAcctSchema as, Fact fact, DocLine line) {
    //	get total index (the Receivables/Liabilities line)
    BigDecimal total = Env.ZERO;
    for (int i = 0; i < m_facts.size(); i++) {
        MFactAcct factAcct = (MFactAcct) m_facts.get(i);
        if (factAcct.getAmtSourceDr().compareTo(total) > 0) {
            total = factAcct.getAmtSourceDr();
            m_totalIndex = i;
        }
        if (factAcct.getAmtSourceCr().compareTo(total) > 0) {
            total = factAcct.getAmtSourceCr();
            m_totalIndex = i;
        }
    }
    MFactAcct factAcct = (MFactAcct) m_facts.get(m_totalIndex);
    log.info("Total Invoice = " + total + " - " + factAcct);
    int precision = as.getStdPrecision();
    for (int i = 0; i < m_facts.size(); i++) {
        //	No Tax Line
        if (i == m_totalIndex)
            continue;
        factAcct = (MFactAcct) m_facts.get(i);
        log.info(i + ": " + factAcct);
        //	Create Tax Account
        MAccount taxAcct = factAcct.getMAccount();
        if (taxAcct == null || taxAcct.get_ID() == 0) {
            log.severe("Tax Account not found/created");
            return false;
        }
        //			Discount Amount	
        if (m_DiscountAmt.signum() != 0) {
            //	Original Tax is DR - need to correct it CR
            if (Env.ZERO.compareTo(factAcct.getAmtSourceDr()) != 0) {
                BigDecimal amount = calcAmount(factAcct.getAmtSourceDr(), total, m_DiscountAmt, precision);
                if (amount.signum() != 0) {
                    //for sales actions
                    if (m_IsSOTrx) {
                        fact.createLine(line, m_DiscountAccount, as.getC_Currency_ID(), amount, null);
                        fact.createLine(line, taxAcct, as.getC_Currency_ID(), null, amount);
                    } else {
                        //for purchase actions
                        fact.createLine(line, m_DiscountAccount, as.getC_Currency_ID(), amount.negate(), null);
                        fact.createLine(line, taxAcct, as.getC_Currency_ID(), null, amount.negate());
                    }
                }
            } else //	Original Tax is CR - need to correct it DR
            {
                BigDecimal amount = calcAmount(factAcct.getAmtSourceCr(), total, m_DiscountAmt, precision);
                if (amount.signum() != 0) {
                    //						for sales actions
                    if (m_IsSOTrx) {
                        fact.createLine(line, taxAcct, as.getC_Currency_ID(), amount, null);
                        fact.createLine(line, m_DiscountAccount, as.getC_Currency_ID(), null, amount);
                    } else {
                        fact.createLine(line, taxAcct, as.getC_Currency_ID(), amount.negate(), null);
                        fact.createLine(line, m_DiscountAccount, as.getC_Currency_ID(), null, amount.negate());
                    }
                }
            }
        }
        //	WriteOff Amount	
        if (m_WriteOffAmt.signum() != 0) {
            //	Original Tax is DR - need to correct it CR
            if (Env.ZERO.compareTo(factAcct.getAmtSourceDr()) != 0) {
                BigDecimal amount = calcAmount(factAcct.getAmtSourceDr(), total, m_WriteOffAmt, precision);
                if (amount.signum() != 0) {
                    fact.createLine(line, m_WriteOffAccount, as.getC_Currency_ID(), amount, null);
                    fact.createLine(line, taxAcct, as.getC_Currency_ID(), null, amount);
                }
            } else //	Original Tax is CR - need to correct it DR
            {
                BigDecimal amount = calcAmount(factAcct.getAmtSourceCr(), total, m_WriteOffAmt, precision);
                if (amount.signum() != 0) {
                    fact.createLine(line, taxAcct, as.getC_Currency_ID(), amount, null);
                    fact.createLine(line, m_WriteOffAccount, as.getC_Currency_ID(), null, amount);
                }
            }
        }
    //	WriteOff
    }
    //	for all lines
    return true;
}
Also used : MAccount(org.compiere.model.MAccount) MFactAcct(org.compiere.model.MFactAcct) BigDecimal(java.math.BigDecimal)

Example 4 with MFactAcct

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

the class InvoiceNGL method createGLJournal.

//	doIt
/**
	 * 	Create GL Journal
	 * 	@return document info
	 */
private String createGLJournal() {
    //FR: [ 2214883 ] Remove SQL code and Replace for Query
    final String whereClause = "AD_PInstance_ID=?";
    List<X_T_InvoiceGL> list = new Query(getCtx(), X_T_InvoiceGL.Table_Name, whereClause, get_TrxName()).setParameters(getAD_PInstance_ID()).setOrderBy("AD_Org_ID").list();
    if (list.size() == 0)
        return " - No Records found";
    //
    MAcctSchema as = MAcctSchema.get(getCtx(), p_C_AcctSchema_ID);
    MAcctSchemaDefault asDefaultAccts = MAcctSchemaDefault.get(getCtx(), p_C_AcctSchema_ID);
    MGLCategory cat = MGLCategory.getDefaultSystem(getCtx());
    if (cat == null) {
        MDocType docType = MDocType.get(getCtx(), p_C_DocTypeReval_ID);
        cat = MGLCategory.get(getCtx(), docType.getGL_Category_ID());
    }
    //
    MJournalBatch batch = new MJournalBatch(getCtx(), 0, get_TrxName());
    batch.setDescription(getName());
    batch.setC_DocType_ID(p_C_DocTypeReval_ID);
    batch.setDateDoc(new Timestamp(System.currentTimeMillis()));
    batch.setDateAcct(p_DateReval);
    batch.setC_Currency_ID(as.getC_Currency_ID());
    if (!batch.save())
        return " - Could not create Batch";
    //
    MJournal journal = null;
    BigDecimal drTotal = Env.ZERO;
    BigDecimal crTotal = Env.ZERO;
    int AD_Org_ID = 0;
    for (int i = 0; i < list.size(); i++) {
        X_T_InvoiceGL gl = list.get(i);
        if (gl.getAmtRevalDrDiff().signum() == 0 && gl.getAmtRevalCrDiff().signum() == 0)
            continue;
        MInvoice invoice = new MInvoice(getCtx(), gl.getC_Invoice_ID(), null);
        if (invoice.getC_Currency_ID() == as.getC_Currency_ID())
            continue;
        //
        if (journal == null) {
            journal = new MJournal(batch);
            journal.setC_AcctSchema_ID(as.getC_AcctSchema_ID());
            journal.setC_Currency_ID(as.getC_Currency_ID());
            journal.setC_ConversionType_ID(p_C_ConversionTypeReval_ID);
            MOrg org = MOrg.get(getCtx(), gl.getAD_Org_ID());
            journal.setDescription(getName() + " - " + org.getName());
            journal.setGL_Category_ID(cat.getGL_Category_ID());
            if (!journal.save())
                return " - Could not create Journal";
        }
        //
        MJournalLine line = new MJournalLine(journal);
        line.setLine((i + 1) * 10);
        line.setDescription(invoice.getSummary());
        //
        MFactAcct fa = new MFactAcct(getCtx(), gl.getFact_Acct_ID(), null);
        line.setC_ValidCombination_ID(MAccount.get(fa));
        BigDecimal dr = gl.getAmtRevalDrDiff();
        BigDecimal cr = gl.getAmtRevalCrDiff();
        drTotal = drTotal.add(dr);
        crTotal = crTotal.add(cr);
        line.setAmtSourceDr(dr);
        line.setAmtAcctDr(dr);
        line.setAmtSourceCr(cr);
        line.setAmtAcctCr(cr);
        line.saveEx();
        //
        if (//	invoice org id
        AD_Org_ID == 0)
            AD_Org_ID = gl.getAD_Org_ID();
        //	Change in Org
        if (AD_Org_ID != gl.getAD_Org_ID()) {
            createBalancing(asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (i + 1) * 10);
            //
            AD_Org_ID = gl.getAD_Org_ID();
            drTotal = Env.ZERO;
            crTotal = Env.ZERO;
            journal = null;
        }
    }
    createBalancing(asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (list.size() + 1) * 10);
    return " - " + batch.getDocumentNo() + " #" + list.size();
}
Also used : MDocType(org.compiere.model.MDocType) Query(org.compiere.model.Query) X_T_InvoiceGL(org.compiere.model.X_T_InvoiceGL) MGLCategory(org.compiere.model.MGLCategory) MJournalLine(org.compiere.model.MJournalLine) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MAcctSchema(org.compiere.model.MAcctSchema) MOrg(org.compiere.model.MOrg) MAcctSchemaDefault(org.compiere.model.MAcctSchemaDefault) MFactAcct(org.compiere.model.MFactAcct) MJournal(org.compiere.model.MJournal) MJournalBatch(org.compiere.model.MJournalBatch)

Example 5 with MFactAcct

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

the class TaxDeclarationCreate method create.

//	doIt
/**
	 * 	Create Data
	 *	@param invoice invoice
	 */
private void create(MInvoice invoice) {
    /**	Lines					**
		MInvoiceLine[] lines = invoice.getLines();
		for (int i = 0; i < lines.length; i++)
		{
			MInvoiceLine line = lines[i];
			if (line.isDescription())
				continue;
			//
			MTaxDeclarationLine tdl = new MTaxDeclarationLine (m_td, invoice, line);
			tdl.setLine((m_noLines+1) * 10);
			if (tdl.save())
				m_noLines++;
		}
		/** **/
    /** Invoice Tax				**/
    MInvoiceTax[] taxes = invoice.getTaxes(false);
    for (int i = 0; i < taxes.length; i++) {
        MInvoiceTax tLine = taxes[i];
        //
        MTaxDeclarationLine tdl = new MTaxDeclarationLine(m_td, invoice, tLine);
        tdl.setLine((m_noLines + 1) * 10);
        if (tdl.save())
            m_noLines++;
    }
    /** **/
    /**	Acct					**/
    String sql = "SELECT * FROM Fact_Acct WHERE AD_Table_ID=? AND Record_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, MInvoice.Table_ID);
        pstmt.setInt(2, invoice.getC_Invoice_ID());
        rs = pstmt.executeQuery();
        while (rs.next()) {
            //	no lock
            MFactAcct fact = new MFactAcct(getCtx(), rs, null);
            MTaxDeclarationAcct tda = new MTaxDeclarationAcct(m_td, fact);
            tda.setLine((m_noAccts + 1) * 10);
            if (tda.save())
                m_noAccts++;
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
/** **/
}
Also used : MTaxDeclarationAcct(org.compiere.model.MTaxDeclarationAcct) ResultSet(java.sql.ResultSet) MFactAcct(org.compiere.model.MFactAcct) PreparedStatement(java.sql.PreparedStatement) MInvoiceTax(org.compiere.model.MInvoiceTax) MTaxDeclarationLine(org.compiere.model.MTaxDeclarationLine)

Aggregations

MFactAcct (org.compiere.model.MFactAcct)5 BigDecimal (java.math.BigDecimal)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 MAccount (org.compiere.model.MAccount)1 MAcctSchema (org.compiere.model.MAcctSchema)1 MAcctSchemaDefault (org.compiere.model.MAcctSchemaDefault)1 MDocType (org.compiere.model.MDocType)1 MGLCategory (org.compiere.model.MGLCategory)1 MInvoice (org.compiere.model.MInvoice)1 MInvoiceTax (org.compiere.model.MInvoiceTax)1 MJournal (org.compiere.model.MJournal)1 MJournalBatch (org.compiere.model.MJournalBatch)1 MJournalLine (org.compiere.model.MJournalLine)1 MOrg (org.compiere.model.MOrg)1 MTaxDeclarationAcct (org.compiere.model.MTaxDeclarationAcct)1 MTaxDeclarationLine (org.compiere.model.MTaxDeclarationLine)1 Query (org.compiere.model.Query)1