📄 fact.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.acct;
import java.util.*;
import java.math.*;
import java.sql.*;
import org.apache.log4j.Logger;
import org.compiere.util.Env;
import org.compiere.model.*;
/**
* Accounting Fact
*
* @author Jorg Janke
* @version $Id: Fact.java,v 1.17 2003/03/21 02:38:46 jjanke Exp $
*/
public final class Fact
{
/**
* Constructor
* @param document pointer to document
* @param acctSchema Account Schema to create accounts
* @param defaultPostingType the default Posting type (actual,..) for this posting
*/
public Fact (Doc document, AcctSchema acctSchema, String defaultPostingType)
{
m_doc = document;
m_acctSchema = acctSchema;
m_postingType = defaultPostingType;
m_docVO = m_doc.p_vo;
//
log.debug (toString());
} // Fact
/** Log */
private Logger log = Logger.getLogger(getClass());
/** Document */
private Doc m_doc = null;
/** Accounting Schema */
private AcctSchema m_acctSchema = null;
/** Posting Type */
private String m_postingType = null;
/** Actual Balance Type */
public static final String POST_Actual = "A";
/** Budget Balance Type */
public static final String POST_Budget = "B";
/** Encumbrance Posting */
public static final String POST_Commitment = "C";
/** Document VO */
private DocVO m_docVO = null;
/** Is Converted */
private boolean m_converted = false;
/** Lines */
private ArrayList m_lines = new ArrayList();
/**
* Dispose
*/
public void dispose()
{
for (int i = 0; i < m_lines.size(); i++)
((FactLine)m_lines.get(i)).dispose();
m_lines.clear();
m_lines = null;
} // dispose
/**
* Create and convert Fact Line.
* Used to create a DR and/or CR entry
*
* @param docLine the document line or null
* @param account if null, line is not created
* @param C_Currency_ID the currency
* @param debitAmt debit amount, can be null
* @param creditAmt credit amount, can be null
* @return Fact Line
*/
public FactLine createLine (DocLine docLine, Account account,
int C_Currency_ID, BigDecimal debitAmt, BigDecimal creditAmt)
{
// log.debug ("createLine - " + account + " - Dr=" + debitAmt + ", Cr=" + creditAmt);
// Data Check
if (account == null)
return null;
//
FactLine line = new FactLine (m_docVO.AD_Table_ID, m_docVO.Record_ID,
docLine == null ? 0 : docLine.getTrxLine_ID());
// Set Info & Account
line.setDocumentInfo(m_docVO, docLine);
line.setAccount(m_acctSchema.getC_AcctSchema_ID(), account);
// Amounts - one needs to be both not zero
if (!line.setAmtSource(C_Currency_ID, debitAmt, creditAmt))
return null;
// Convert
line.convert(m_acctSchema.getC_Currency_ID(), m_docVO.DateAcct, m_acctSchema.getCurrencyRateType());
// Optionally overwrite Acct Amount
if (docLine != null && docLine.getAmtAcctDr() != null && docLine.getAmtAcctDr() != null)
line.setAmtAcct(docLine.getAmtAcctDr(), docLine.getAmtAcctCr());
// Info
line.setJournalInfo(m_docVO.GL_Budget_ID, m_docVO.GL_Category_ID);
line.setPostingType(m_postingType);
//
log.debug ("createLine - " + line.toString());
add(line);
return line;
} // createLine
/**
* Add Fact Line
* @param line fact line
*/
void add (FactLine line)
{
m_lines.add(line);
} // add
/**
* Create and convert Fact Line.
* Used to create either a DR or CR entry
*
* @param docLine Document Line or null
* @param accountDr Account to be used if Amt is DR balance
* @param accountCr Account to be used if Amt is CR balance
* @param C_Currency_ID Currency
* @param Amt if negative Cr else Dr
* @return FactLine
*/
public FactLine createLine (DocLine docLine, Account accountDr, Account accountCr,
int C_Currency_ID, BigDecimal Amt)
{
if (Amt.compareTo(Env.ZERO) < 0)
return createLine (docLine, accountCr, C_Currency_ID, null, Amt.abs());
else
return createLine (docLine, accountDr, C_Currency_ID, Amt, null);
} // createLine
/**
* Create and convert Fact Line.
* Used to create either a DR or CR entry
*
* @param docLine Document line or null
* @param account Account to be used
* @param C_Currency_ID Currency
* @param Amt if negative Cr else Dr
* @return FactLine
*/
public FactLine createLine (DocLine docLine, Account account,
int C_Currency_ID, BigDecimal Amt)
{
if (Amt.compareTo(Env.ZERO) < 0)
return createLine (docLine, account, C_Currency_ID, null, Amt.abs());
else
return createLine (docLine, account, C_Currency_ID, Amt, null);
} // createLine
/**
* Is Posting Type
* @param PostingType - see POST_*
* @return true if document is posting type
*/
public boolean isPostingType (String PostingType)
{
return m_postingType.equals(PostingType);
} // isPostingType
/**
* Is converted
* @return true if converted
*/
public boolean isConverted()
{
return m_converted;
} // isConverted
/**
* Get AcctSchema
* @return AcctSchema
*/
public AcctSchema getAcctSchema()
{
return m_acctSchema;
} // getAcctSchema
/*************************************************************************/
/**
* Are the lines Source Balanced
* @return true if source lines balanced
*/
public boolean isSourceBalanced()
{
// No lines -> balanded
if (m_lines.size() == 0)
return true;
BigDecimal balance = getSourceBalance();
boolean retValue = balance.compareTo(Env.ZERO) == 0;
if (retValue)
log.debug ("isSourceBalanced - " + toString());
else
log.warn ("isSourceBalanced NO - " + toString() + ", Balance=" + balance);
return retValue;
} // isSourceBalanced
/**
* Return Source Balance
* @return source balance
*/
protected BigDecimal getSourceBalance()
{
BigDecimal result = Env.ZERO;
for (int i = 0; i < m_lines.size(); i++)
{
FactLine line = (FactLine)m_lines.get(i);
result = result.add (line.getSourceBalance());
}
// log.debug ("getSourceBalance - " + result.toString());
return result;
} // getSourceBalance
/**
* Create Source Line for Suspense Balancing.
* Only if Suspense Balancing is enabled and not a multi-currency document
* (double check as otherwise the rule should not have fired)
* If not balanced create balancing entry in currency of the document
* @return FactLine
*/
public FactLine balanceSource()
{
if (!m_acctSchema.isSuspenseBalancing() || m_docVO.MultiCurrency)
return null;
BigDecimal diff = getSourceBalance();
log.debug ("balanceSource = " + diff);
// new line
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
if (diff.compareTo(Env.ZERO) < 0) // negative balance => DR
line.setAmtSource(m_docVO.C_Currency_ID, diff.abs(), Env.ZERO);
else // positive balance => CR
line.setAmtSource(m_docVO.C_Currency_ID, Env.ZERO, diff);
// Convert
line.convert(m_acctSchema.getC_Currency_ID(), m_docVO.DateAcct, m_acctSchema.getCurrencyRateType());
line.setAccount(m_acctSchema.getC_AcctSchema_ID(), m_acctSchema.getSuspenseBalancing_Acct());
//
log.debug ("balanceSource - " + line.toString());
m_lines.add(line);
return line;
} // balancingSource
/*************************************************************************/
/**
* Are all segments balanced
* @return true if segments are balanced
*/
public boolean isSegmentBalanced()
{
if (m_lines.size() == 0)
return true;
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() && !isSegmentBalanced (ase.getSegmentType()))
return false;
}
return true;
} // isSegmentBalanced
/**
* Is Source Segment balanced.
* @param segmentType - see AcctSchemaElement.SEGMENT_*
* Implemented only for Org
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -