use of org.compiere.acct.FactLine 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;
}
use of org.compiere.acct.FactLine in project lar_361 by comitsrl.
the class LAR_Validator method accountingForWithholdingOnPayment.
// voidWarehouseOrder
/**
* Process acounting for withholding on sales payment
*/
private String accountingForWithholdingOnPayment(final MAllocationHdr ah) {
final Doc doc = ah.getDoc();
final List<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[] allocLines = ah.getLines(false);
for (int j = 0; j < allocLines.length; j++) {
MAllocationLine al = allocLines[j];
// TODO is this line necesary?
doc.setC_BPartner_ID(al.getC_BPartner_ID());
int c_Payment_ID = al.getC_Payment_ID();
if (c_Payment_ID <= 0)
continue;
MPayment payment = new MPayment(ah.getCtx(), c_Payment_ID, ah.get_TrxName());
if (payment == null || payment.getC_Payment_ID() == 0)
continue;
// Se determina si se procesan cobros o pagos
if (payment.isReceipt()) {
// //////////////// PROCESA COBROS //////////////////
// Determine if is an withholding or not
int c_TaxWithholding_ID = payment.get_ValueAsInt("C_TaxWithholding_ID");
if (c_TaxWithholding_ID <= 0)
continue;
if (payment.getWriteOffAmt().compareTo(Env.ZERO) <= 0)
continue;
// Iterates over factlines, searching one with writeoff account
// in order to change it to the retrieved from processed payment
final FactLine[] factlines = fact.getLines();
for (int ifl = 0; ifl < factlines.length; ifl++) {
final FactLine fl = factlines[ifl];
// if factline account is WriteOff, change it
if (fl.getAccount().equals(doc.getAccount(Doc.ACCTTYPE_WriteOff, as))) {
// Creates factline with proper account (using c_taxwithholding_id from processed payment)
final BigDecimal withholdingAmt = payment.getWriteOffAmt();
final MTax tw = new MTax(ah.getCtx(), c_TaxWithholding_ID, ah.get_TrxName());
final DocTax taxLine = new DocTax(c_TaxWithholding_ID, tw.getName(), tw.getRate(), Env.ZERO, withholdingAmt, tw.isSalesTax());
final FactLine newFactLine = fact.createLine(null, taxLine.getAccount(DocTax.ACCTTYPE_TaxCredit, as), as.getC_Currency_ID(), withholdingAmt, null);
if (newFactLine != null)
newFactLine.setC_Tax_ID(c_TaxWithholding_ID);
// Removes factline with writeoff account from fact
log.info(String.format("Replace factline: %s -> %s", fl, newFactLine));
fact.remove(fl);
}
}
}
}
}
return null;
}
Aggregations