Search in sources :

Example 1 with DocLine

use of org.compiere.acct.DocLine in project lar_361 by comitsrl.

the class LCO_Validator method accountingForInvoiceWithholdingOnPayment.

private String accountingForInvoiceWithholdingOnPayment(MAllocationHdr ah) {
    // Accounting like Doc_Allocation
    // (Write off) vs (invoice withholding where iscalconpayment=Y)
    // 20070807 - globalqss - instead of adding a new WriteOff post, find the
    // current WriteOff and subtract from the posting
    Doc doc = ah.getDoc();
    ArrayList<Fact> facts = doc.getFacts();
    // one fact per acctschema
    for (int i = 0; i < facts.size(); i++) {
        Fact fact = facts.get(i);
        MAcctSchema as = fact.getAcctSchema();
        MAllocationLine[] alloc_lines = ah.getLines(false);
        for (int j = 0; j < alloc_lines.length; j++) {
            BigDecimal tottax = new BigDecimal(0);
            MAllocationLine alloc_line = alloc_lines[j];
            doc.setC_BPartner_ID(alloc_line.getC_BPartner_ID());
            int inv_id = alloc_line.getC_Invoice_ID();
            if (inv_id <= 0)
                continue;
            MInvoice invoice = null;
            invoice = new MInvoice(ah.getCtx(), alloc_line.getC_Invoice_ID(), ah.get_TrxName());
            if (invoice == null || invoice.getC_Invoice_ID() == 0)
                continue;
            String sql = "SELECT i.C_Tax_ID, NVL(SUM(i.TaxBaseAmt),0) AS TaxBaseAmt, NVL(SUM(i.TaxAmt),0) AS TaxAmt, t.Name, t.Rate, t.IsSalesTax " + " FROM LCO_InvoiceWithholding i, C_Tax t " + " WHERE i.C_Invoice_ID = ? AND " + "i.IsCalcOnPayment = 'Y' AND " + "i.IsActive = 'Y' AND " + "i.Processed = 'Y' AND " + "i.C_AllocationLine_ID = ? AND " + "i.C_Tax_ID = t.C_Tax_ID " + "GROUP BY i.C_Tax_ID, t.Name, t.Rate, t.IsSalesTax";
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try {
                pstmt = DB.prepareStatement(sql, ah.get_TrxName());
                pstmt.setInt(1, invoice.getC_Invoice_ID());
                pstmt.setInt(2, alloc_line.getC_AllocationLine_ID());
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    int tax_ID = rs.getInt(1);
                    BigDecimal taxBaseAmt = rs.getBigDecimal(2);
                    BigDecimal amount = rs.getBigDecimal(3);
                    String name = rs.getString(4);
                    BigDecimal rate = rs.getBigDecimal(5);
                    boolean salesTax = rs.getString(6).equals("Y") ? true : false;
                    DocTax taxLine = new DocTax(tax_ID, name, rate, taxBaseAmt, amount, salesTax);
                    if (amount != null && amount.signum() != 0) {
                        FactLine tl = null;
                        if (invoice.isSOTrx()) {
                            tl = fact.createLine(null, taxLine.getAccount(DocTax.ACCTTYPE_TaxDue, as), as.getC_Currency_ID(), amount, null);
                        } else {
                            tl = fact.createLine(null, taxLine.getAccount(taxLine.getAPTaxType(), as), as.getC_Currency_ID(), null, amount);
                        }
                        if (tl != null)
                            tl.setC_Tax_ID(taxLine.getC_Tax_ID());
                        tottax = tottax.add(amount);
                    }
                }
            } catch (Exception e) {
                log.log(Level.SEVERE, sql, e);
                return "Error posting C_InvoiceTax from LCO_InvoiceWithholding";
            } finally {
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
            }
            // Write off		DR
            if (Env.ZERO.compareTo(tottax) != 0) {
                // First try to find the WriteOff posting record
                FactLine[] factlines = fact.getLines();
                boolean foundflwriteoff = false;
                for (int ifl = 0; ifl < factlines.length; ifl++) {
                    FactLine fl = factlines[ifl];
                    if (fl.getAccount().equals(doc.getAccount(Doc.ACCTTYPE_WriteOff, as))) {
                        foundflwriteoff = true;
                        // old balance = DB - CR
                        BigDecimal balamt = fl.getAmtSourceDr().subtract(fl.getAmtSourceCr());
                        // new balance = old balance +/- tottax
                        BigDecimal newbalamt = Env.ZERO;
                        if (invoice.isSOTrx())
                            newbalamt = balamt.subtract(tottax);
                        else
                            newbalamt = balamt.add(tottax);
                        if (Env.ZERO.compareTo(newbalamt) == 0) {
                            // both zeros, remove the line
                            fact.remove(fl);
                        } else if (Env.ZERO.compareTo(newbalamt) > 0) {
                            fl.setAmtAcct(fl.getC_Currency_ID(), Env.ZERO, newbalamt);
                            fl.setAmtSource(fl.getC_Currency_ID(), Env.ZERO, newbalamt);
                        } else {
                            fl.setAmtAcct(fl.getC_Currency_ID(), newbalamt, Env.ZERO);
                            fl.setAmtSource(fl.getC_Currency_ID(), newbalamt, Env.ZERO);
                        }
                        break;
                    }
                }
                if (!foundflwriteoff) {
                    // Create a new line
                    DocLine line = new DocLine(alloc_line, doc);
                    FactLine fl = null;
                    if (invoice.isSOTrx()) {
                        fl = fact.createLine(line, doc.getAccount(Doc.ACCTTYPE_WriteOff, as), as.getC_Currency_ID(), null, tottax);
                    } else {
                        fl = fact.createLine(line, doc.getAccount(Doc.ACCTTYPE_WriteOff, as), as.getC_Currency_ID(), tottax, null);
                    }
                    if (fl != null)
                        fl.setAD_Org_ID(ah.getAD_Org_ID());
                }
            }
        }
    }
    return null;
}
Also used : DocLine(org.compiere.acct.DocLine) FactLine(org.compiere.acct.FactLine) DocTax(org.compiere.acct.DocTax) MInvoice(org.compiere.model.MInvoice) PreparedStatement(java.sql.PreparedStatement) Fact(org.compiere.acct.Fact) BigDecimal(java.math.BigDecimal) SQLException(java.sql.SQLException) MAcctSchema(org.compiere.model.MAcctSchema) ResultSet(java.sql.ResultSet) Doc(org.compiere.acct.Doc) MAllocationLine(org.compiere.model.MAllocationLine)

Aggregations

BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Doc (org.compiere.acct.Doc)1 DocLine (org.compiere.acct.DocLine)1 DocTax (org.compiere.acct.DocTax)1 Fact (org.compiere.acct.Fact)1 FactLine (org.compiere.acct.FactLine)1 MAcctSchema (org.compiere.model.MAcctSchema)1 MAllocationLine (org.compiere.model.MAllocationLine)1 MInvoice (org.compiere.model.MInvoice)1