📄 fact.java
字号:
* Other sensible candidates are Project, User1/2
* @return true if segments are balanced
*/
public boolean isSegmentBalanced (String segmentType)
{
if (segmentType.equals(AcctSchemaElement.SEGMENT_Org))
{
HashMap map = new HashMap();
// Add up values by key
for (int i = 0; i < m_lines.size(); i++)
{
FactLine line = (FactLine)m_lines.get(i);
Integer key = new Integer(line.getAD_Org_ID());
BigDecimal bal = line.getSourceBalance();
BigDecimal oldBal = (BigDecimal)map.get(key);
if (oldBal != null)
bal = bal.add(oldBal);
map.put(key, bal);
// System.out.println("Add Key=" + key + ", Bal=" + bal + " <- " + line);
}
// check if all keys are zero
Iterator values = map.values().iterator();
while (values.hasNext())
{
BigDecimal bal = (BigDecimal)values.next();
if (bal.compareTo(Env.ZERO) != 0)
{
map.clear();
log.warn ("isSegmentBalanced (" + segmentType + ") NO - " + toString() + ", Balance=" + bal);
return false;
}
}
map.clear();
log.debug ("isSegmentBalanced (" + segmentType + ") - " + toString());
return true;
}
log.debug ("isSegmentBalanced (" + segmentType + ") (not checked) - " + toString());
return true;
} // isSegmentBalanced
/**
* Balance all segments.
* - For all balancing segments
* - For all segment values
* - If balance <> 0 create dueTo/dueFrom line
* overwriting the segment value
*/
public void balanceSegments()
{
log.debug("balanceSegments");
//
ArrayList elementList = m_acctSchema.getAcctSchemaElementList();
int size = elementList.size();
// check all balancing segments
for (int i = 0; i < size; i++)
{
AcctSchemaElement ase = (AcctSchemaElement)elementList.get(i);
if (ase.isBalanced())
balanceSegment (ase.getSegmentType());
}
} // balanceSegments
/**
* Balance Source Segment
* @param segmentType segment type
*/
private void balanceSegment (String segmentType)
{
// no lines -> balanced
if (m_lines.size() == 0)
return;
log.debug ("balanceSegment (" + segmentType + ") - " + toString());
// Org
if (segmentType.equals(AcctSchemaElement.SEGMENT_Org))
{
HashMap map = new HashMap();
// Add up values by key
for (int i = 0; i < m_lines.size(); i++)
{
FactLine line = (FactLine)m_lines.get(i);
Integer key = new Integer(line.getAD_Org_ID());
BigDecimal bal = line.getSourceBalance();
BigDecimal oldBal = (BigDecimal)map.get(key);
if (oldBal != null)
bal = bal.add(oldBal);
map.put(key, bal);
}
// Create entry for non-zero element
Iterator keys = map.keySet().iterator();
while (keys.hasNext())
{
Integer key = (Integer)keys.next();
BigDecimal diff = (BigDecimal)map.get(key);
//
if (diff.compareTo(Env.ZERO) != 0)
{
// Create Balancing Entry
FactLine line = new FactLine (m_docVO.AD_Table_ID, m_docVO.Record_ID, 0);
line.setDocumentInfo(m_docVO, null);
line.setJournalInfo(m_docVO.GL_Budget_ID, m_docVO.GL_Category_ID);
line.setPostingType(m_postingType);
// Amount & Account
if (diff.compareTo(Env.ZERO) < 0)
{
line.setAmtSource(m_docVO.C_Currency_ID, diff.abs(), Env.ZERO);
line.setAccount(m_acctSchema.getC_AcctSchema_ID(), m_acctSchema.getDueFrom_Acct(segmentType));
}
else
{
line.setAmtSource(m_docVO.C_Currency_ID, Env.ZERO, diff.abs());
line.setAccount(m_acctSchema.getC_AcctSchema_ID(), m_acctSchema.getDueTo_Acct(segmentType));
}
line.convert(m_acctSchema.getC_Currency_ID(), m_docVO.DateAcct, m_acctSchema.getCurrencyRateType());
line.setAD_Org_ID(key.intValue());
log.debug ("balanceSegment (" + segmentType + ") - " + toString());
m_lines.add(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.compareTo(Env.ZERO) == 0;
if (retValue)
log.debug("isAcctBalanced - " + toString());
else
log.warn("isAcctBalanced NO - " + toString() + ", Balance=" + balance);
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.debug ("getAcctBalance " + 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();
log.debug ("balanceAccounting - " + toString() + ", Balance=" + diff);
FactLine line = null;
// Create Currency Entry
if (m_acctSchema.isCurrencyBalancing())
{
line = new FactLine (m_docVO.AD_Table_ID, m_docVO.Record_ID, 0);
line.setDocumentInfo(m_docVO, null);
line.setJournalInfo(m_docVO.GL_Budget_ID, m_docVO.GL_Category_ID);
line.setPostingType(m_postingType);
// Amount
line.setAmtSource(m_docVO.C_Currency_ID, Env.ZERO, Env.ZERO);
line.convert(m_acctSchema.getC_Currency_ID(), m_docVO.DateAcct, m_acctSchema.getCurrencyRateType());
if (diff.compareTo(Env.ZERO) < 0)
line.setAmtAcct(diff.abs(), Env.ZERO);
else
line.setAmtAcct(Env.ZERO, diff.abs());
line.setAccount(m_acctSchema.getC_AcctSchema_ID(), m_acctSchema.getCurrencyBalancing_Acct());
log.debug ("balanceAccounting - " + 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.error ("balanceAccounting - No Line found");
else
{
log.debug ("Adjusting Amt=" + diff.toString() + "; Line=" + line.toString());
line.currencyCorrect(diff);
log.debug ("balanceAccounting - " + line.toString());
}
} // correct biggest amount
// Debug info only
this.isAcctBalanced();
return line;
} // balanceAccounting
/*************************************************************************/
/**
* Correct Tax
*/
public void createTaxCorrection()
{
log.debug ("createTaxCorrection - (NIY)" + toString());
} // correct Tax
/**
* Create Realized Gain/Loss
*/
public void createRealizedGainLoss()
{
log.debug ("createRealizedGainLoss - (NIY)" + toString());
} // createRealizedGainLoss
/**
* 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 con connection
* @return true if all lines were saved
*/
public boolean save (Connection con)
{
// save Lines
for (int i = 0; i < m_lines.size(); i++)
{
FactLine fl = (FactLine)m_lines.get(i);
if (!fl.save(con)) // abort on first error
return false;
}
return true;
} // commit
} // Fact
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -