📄 fact.java
字号:
// Amount & Account
if (difference.getBalance().signum() < 0)
{
if (difference.isReversal())
{
line.setAmtSource(m_doc.getC_Currency_ID(), Env.ZERO, difference.getPostBalance());
line.setAccount(m_acctSchema, m_acctSchema.getDueTo_Acct(elementType));
}
else
{
line.setAmtSource(m_doc.getC_Currency_ID(), difference.getPostBalance(), Env.ZERO);
line.setAccount(m_acctSchema, m_acctSchema.getDueFrom_Acct(elementType));
}
}
else
{
if (difference.isReversal())
{
line.setAmtSource(m_doc.getC_Currency_ID(), difference.getPostBalance(), Env.ZERO);
line.setAccount(m_acctSchema, m_acctSchema.getDueFrom_Acct(elementType));
}
else
{
line.setAmtSource(m_doc.getC_Currency_ID(), Env.ZERO, difference.getPostBalance());
line.setAccount(m_acctSchema, m_acctSchema.getDueTo_Acct(elementType));
}
}
line.convert();
line.setAD_Org_ID(key.intValue());
//
m_lines.add(line);
log.fine("(" + elementType + ") - " + line);
}
}
map.clear();
}
} // balanceSegment
/**************************************************************************
* Are the lines Accounting Balanced
* @return true if accounting lines are balanced
*/
public boolean isAcctBalanced()
{
// no lines -> balanced
if (m_lines.size() == 0)
return true;
BigDecimal balance = getAcctBalance();
boolean retValue = balance.signum() == 0;
if (retValue)
log.finer(toString());
else
log.warning("NO - Diff=" + balance + " - " + toString());
return retValue;
} // isAcctBalanced
/**
* Return Accounting Balance
* @return true if accounting lines are balanced
*/
protected BigDecimal getAcctBalance()
{
BigDecimal result = Env.ZERO;
for (int i = 0; i < m_lines.size(); i++)
{
FactLine line = (FactLine)m_lines.get(i);
result = result.add(line.getAcctBalance());
}
// log.fine(result.toString());
return result;
} // getAcctBalance
/**
* Balance Accounting Currency.
* If the accounting currency is not balanced,
* if Currency balancing is enabled
* create a new line using the currency balancing account with zero source balance
* or
* adjust the line with the largest balance sheet account
* or if no balance sheet account exist, the line with the largest amount
* @return FactLine
*/
public FactLine balanceAccounting()
{
BigDecimal diff = getAcctBalance(); // DR-CR
log.fine("Balance=" + diff
+ ", CurrBal=" + m_acctSchema.isCurrencyBalancing()
+ " - " + toString());
FactLine line = null;
// Create Currency Entry
if (m_acctSchema.isCurrencyBalancing())
{
line = new FactLine (m_doc.getCtx(), m_doc.get_Table_ID(),
m_doc.get_ID(), 0, m_trxName);
line.setDocumentInfo (m_doc, null);
line.setPostingType (m_postingType);
line.setAccount (m_acctSchema, m_acctSchema.getCurrencyBalancing_Acct());
// Amount
line.setAmtSource(m_doc.getC_Currency_ID(), Env.ZERO, Env.ZERO);
line.convert();
if (diff.signum() < 0)
line.setAmtAcct(diff.abs(), Env.ZERO);
else
line.setAmtAcct(Env.ZERO, diff.abs());
log.fine(line.toString());
m_lines.add(line);
}
else
// Adjust biggest (Balance Sheet) line amount
{
BigDecimal BSamount = Env.ZERO;
FactLine BSline = null;
BigDecimal PLamount = Env.ZERO;
FactLine PLline = null;
// Find line
for (int i = 0; i < m_lines.size(); i++)
{
FactLine l = (FactLine)m_lines.get(i);
BigDecimal amt = l.getAcctBalance().abs();
if (l.isBalanceSheet() && amt.compareTo(BSamount) > 0)
{
BSamount = amt;
BSline = l;
}
else if (!l.isBalanceSheet() && amt.compareTo(PLamount) > 0)
{
PLamount = amt;
PLline = l;
}
}
if (BSline != null)
line = BSline;
else
line = PLline;
if (line == null)
log.severe ("No Line found");
else
{
log.fine("Adjusting Amt=" + diff + "; Line=" + line);
line.currencyCorrect(diff);
log.fine(line.toString());
}
} // correct biggest amount
return line;
} // balanceAccounting
/**
* Check Accounts of Fact Lines
* @return true if success
*/
public boolean checkAccounts()
{
// no lines -> nothing to distribute
if (m_lines.size() == 0)
return true;
// For all fact lines
for (int i = 0; i < m_lines.size(); i++)
{
FactLine line = (FactLine)m_lines.get(i);
MAccount account = line.getAccount();
if (account == null)
{
log.warning("No Account for " + line);
return false;
}
MElementValue ev = account.getAccount();
if (ev == null)
{
log.warning("No Element Value for " + account
+ ": " + line);
return false;
}
if (ev.isSummary())
{
log.warning("Cannot post to Summary Account " + ev
+ ": " + line);
return false;
}
if (!ev.isActive())
{
log.warning("Cannot post to Inactive Account " + ev
+ ": " + line);
return false;
}
} // for all lines
return true;
} // checkAccounts
/**
* GL Distribution of Fact Lines
* @return true if success
*/
public boolean distribute()
{
// no lines -> nothing to distribute
if (m_lines.size() == 0)
return true;
ArrayList<FactLine> newLines = new ArrayList<FactLine>();
// For all fact lines
for (int i = 0; i < m_lines.size(); i++)
{
FactLine dLine = (FactLine)m_lines.get(i);
MDistribution[] distributions = MDistribution.get (dLine.getAccount(),
m_postingType, m_doc.getC_DocType_ID());
// No Distribution for this line
if (distributions == null || distributions.length == 0)
continue;
// Just the first
if (distributions.length > 1)
log.warning("More then one Distributiion for " + dLine.getAccount());
MDistribution distribution = distributions[0];
// Add Reversal
FactLine reversal = dLine.reverse(distribution.getName());
log.info("Reversal=" + reversal);
newLines.add(reversal); // saved in postCommit
// Prepare
distribution.distribute(dLine.getAccount(), dLine.getSourceBalance(), dLine.getC_Currency_ID());
MDistributionLine[] lines = distribution.getLines(false);
for (int j = 0; j < lines.length; j++)
{
MDistributionLine dl = lines[j];
if (!dl.isActive() || dl.getAmt().signum() == 0)
continue;
FactLine factLine = new FactLine (m_doc.getCtx(), m_doc.get_Table_ID(),
m_doc.get_ID(), 0, m_trxName);
// Set Info & Account
factLine.setDocumentInfo(m_doc, dLine.getDocLine());
factLine.setAccount(m_acctSchema, dl.getAccount());
factLine.setPostingType(m_postingType);
if (dl.isOverwriteOrg()) // set Org explicitly
factLine.setAD_Org_ID(dl.getOrg_ID());
//
if (dl.getAmt().signum() < 0)
factLine.setAmtSource(dLine.getC_Currency_ID(), null, dl.getAmt().abs());
else
factLine.setAmtSource(dLine.getC_Currency_ID(), dl.getAmt(), null);
// Convert
factLine.convert();
//
String description = distribution.getName() + " #" + dl.getLine();
if (dl.getDescription() != null)
description += " - " + dl.getDescription();
factLine.addDescription(description);
//
log.info(factLine.toString());
newLines.add(factLine);
}
} // for all lines
// Add Lines
for (int i = 0; i < newLines.size(); i++)
m_lines.add(newLines.get(i));
return true;
} // distribute
/**************************************************************************
* String representation
* @return String
*/
public String toString()
{
StringBuffer sb = new StringBuffer("Fact[");
sb.append(m_doc.toString());
sb.append(",").append(m_acctSchema.toString());
sb.append(",PostType=").append(m_postingType);
sb.append("]");
return sb.toString();
} // toString
/**
* Get Lines
* @return FactLine Array
*/
public FactLine[] getLines()
{
FactLine[] temp = new FactLine[m_lines.size()];
m_lines.toArray(temp);
return temp;
} // getLines
/**
* Save Fact
* @param trxName transaction
* @return true if all lines were saved
*/
public boolean save (String trxName)
{
// save Lines
for (int i = 0; i < m_lines.size(); i++)
{
FactLine fl = (FactLine)m_lines.get(i);
// log.fine("save - " + fl);
if (!fl.save(trxName)) // abort on first error
return false;
}
return true;
} // commit
/**
* Get Transaction
* @return trx
*/
public String get_TrxName()
{
return m_trxName;
} // getTrxName
/**
* Set Transaction name
* @param trxName
*/
private void set_TrxName(String trxName)
{
m_trxName = trxName;
} // set_TrxName
/**
* Fact Balance Utility
*
* @author Jorg Janke
* @version $Id: Fact.java,v 1.12 2005/11/01 16:38:56 jjanke Exp $
*/
public class Balance
{
/**
* New Balance
* @param dr DR
* @param cr CR
*/
public Balance (BigDecimal dr, BigDecimal cr)
{
DR = dr;
CR = cr;
}
/** DR Amount */
public BigDecimal DR = Env.ZERO;
/** CR Amount */
public BigDecimal CR = Env.ZERO;
/**
* Add
* @param dr DR
* @param cr CR
*/
public void add (BigDecimal dr, BigDecimal cr)
{
DR = DR.add(dr);
CR = CR.add(cr);
}
/**
* Get Balance
* @return balance
*/
public BigDecimal getBalance()
{
return DR.subtract(CR);
} // getBalance
/**
* Get Post Balance
* @return absolute balance - negative if reversal
*/
public BigDecimal getPostBalance()
{
BigDecimal bd = getBalance().abs();
if (isReversal())
return bd.negate();
return bd;
} // getPostBalance
/**
* Zero Balance
* @return true if 0
*/
public boolean isZeroBalance()
{
return getBalance().signum() == 0;
} // isZeroBalance
/**
* Reversal
* @return true if both DR/CR are negative or zero
*/
public boolean isReversal()
{
return DR.signum() <= 0 && CR.signum() <= 0;
} // isReversal
/**
* String Representation
* @return info
*/
public String toString ()
{
StringBuffer sb = new StringBuffer ("Balance[");
sb.append ("DR=").append(DR)
.append ("-CR=").append(CR)
.append(" = ").append(getBalance())
.append ("]");
return sb.toString ();
} // toString
} // Balance
} // Fact
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -