📄 doc.java
字号:
sql.append(get_TableName()).append( " SET Processing='Y' WHERE ")
.append(get_TableName()).append("_ID=").append(get_ID())
.append(" AND Processed='Y' AND IsActive='Y'");
if (!force)
sql.append(" AND (Processing='N' OR Processing IS NULL)");
if (!repost)
sql.append(" AND Posted='N'");
if (DB.executeUpdate(sql.toString(), getTrxName()) == 1)
log.info("Locked: " + get_TableName() + "_ID=" + get_ID());
else
{
log.log(Level.SEVERE, "Resubmit - Cannot lock " + get_TableName() + "_ID="
+ get_ID() + ", Force=" + force + ",RePost=" + repost);
return "Cannot Lock - Resubmit";
}
p_Error = loadDocumentDetails();
if (p_Error != null)
return p_Error;
// Delete existing Accounting
if (repost)
{
if (isPosted() && !isPeriodOpen()) // already posted - don't delete if period closed
{
log.log(Level.SEVERE, toString() + " - Period Closed for already posed document");
return "PeriodClosed";
}
// delete it
sql = new StringBuffer ("DELETE Fact_Acct WHERE AD_Table_ID=");
sql.append(get_Table_ID()).append(" AND Record_ID=").append(p_po.get_ID());
int no = DB.executeUpdate(sql.toString(), getTrxName());
if (no != 0)
log.info("deleted=" + no);
}
else if (isPosted())
{
log.log(Level.SEVERE, toString() + " - Document already posted");
return "AlreadyPosted";
}
p_Status = STATUS_NotPosted;
// Create Fact per AcctSchema
m_fact = m_fact = new ArrayList<Fact>();
// for all Accounting Schema
boolean OK = true;
try
{
for (int i = 0; OK && i < m_ass.length; i++)
{
// if acct schema has "only" org, skip
boolean skip = false;
if (m_ass[i].getAD_OrgOnly_ID() != 0)
{
if (m_ass[i].getOnlyOrgs() == null)
m_ass[i].setOnlyOrgs(MReportTree.getChildIDs(getCtx(),
0, MAcctSchemaElement.ELEMENTTYPE_Organization,
m_ass[i].getAD_OrgOnly_ID()));
// Header Level Org
skip = m_ass[i].isSkipOrg(getAD_Org_ID());
// Line Level Org
for (int line = 0; skip && line < p_lines.length; line++)
{
skip = m_ass[i].isSkipOrg(p_lines[line].getAD_Org_ID());
if (!skip)
break;
}
}
if (skip)
continue;
// post
log.info("(" + i + ") " + p_po);
p_Status = postLogic (i);
if (!p_Status.equals(STATUS_Posted))
OK = false;
}
}
catch (Exception e)
{
log.log(Level.SEVERE, "", e);
p_Status = STATUS_Error;
p_Error = e.toString();
OK = false;
}
// commitFact
p_Status = postCommit (p_Status);
// Create Note
if (!p_Status.equals(STATUS_Posted))
{
// Insert Note
String AD_MessageValue = "PostingError-" + p_Status;
int AD_User_ID = p_po.getUpdatedBy();
MNote note = new MNote (getCtx(), AD_MessageValue, AD_User_ID,
getAD_Client_ID(), getAD_Org_ID(), null);
note.setRecord(p_po.get_Table_ID(), p_po.get_ID());
// Reference
note.setReference(toString()); // Document
// Text
StringBuffer Text = new StringBuffer (Msg.getMsg(Env.getCtx(), AD_MessageValue));
if (p_Error != null)
Text.append(" (").append(p_Error).append(")");
String cn = getClass().getName();
Text.append(" - ").append(cn.substring(cn.lastIndexOf('.')))
.append(" (").append(getDocumentType())
.append(" - DocumentNo=").append(getDocumentNo())
.append(", DateAcct=").append(getDateAcct().toString().substring(0,10))
.append(", Amount=").append(getAmount())
.append(", Sta=").append(p_Status)
.append(" - PeriodOpen=").append(isPeriodOpen())
.append(", Balanced=").append(isBalanced());
note.setTextMsg(Text.toString());
note.save();
}
// dispose facts
for (int i = 0; i < m_fact.size(); i++)
{
Fact fact = m_fact.get(i);
if (fact != null)
fact.dispose();
}
p_lines = null;
if (p_Status.equals(STATUS_Posted))
return null;
return p_Error;
} // post
/**
* Posting logic for Accounting Schema index
* @param index Accounting Schema index
* @return posting status/error code
*/
private final String postLogic (int index)
{
log.info("(" + index + ") " + p_po);
// rejectUnbalanced
if (!m_ass[index].isSuspenseBalancing() && !isBalanced())
return STATUS_NotBalanced;
// rejectUnconvertible
if (!isConvertible(m_ass[index]))
return STATUS_NotConvertible;
// rejectPeriodClosed
if (!isPeriodOpen())
return STATUS_PeriodClosed;
// createFacts
ArrayList<Fact> facts = createFacts (m_ass[index]);
if (facts == null)
return STATUS_Error;
for (int f = 0; f < facts.size(); f++)
{
Fact fact = facts.get(f);
if (fact == null)
return STATUS_Error;
m_fact.add(fact);
//
p_Status = STATUS_PostPrepared;
// check accounts
if (!fact.checkAccounts())
return STATUS_InvalidAccount;
// distribute
if (!fact.distribute())
return STATUS_Error;
// balanceSource
if (!fact.isSourceBalanced())
{
fact.balanceSource();
if (!fact.isSourceBalanced())
return STATUS_NotBalanced;
}
// balanceSegments
if (!fact.isSegmentBalanced())
{
fact.balanceSegments();
if (!fact.isSegmentBalanced())
return STATUS_NotBalanced;
}
// balanceAccounting
if (!fact.isAcctBalanced())
{
fact.balanceAccounting();
if (!fact.isAcctBalanced())
return STATUS_NotBalanced;
}
} // for all facts
return STATUS_Posted;
} // postLogic
/**
* Post Commit.
* Save Facts & Document
* @param status status
* @return Posting Status
*/
private final String postCommit (String status)
{
log.info("Sta=" + status + " DT=" + getDocumentType()
+ " ID=" + p_po.get_ID());
p_Status = status;
Trx trx = Trx.get(getTrxName(), true);
try
{
// *** Transaction Start ***
// Commit Facts
if (status.equals(STATUS_Posted))
{
for (int i = 0; i < m_fact.size(); i++)
{
Fact fact = m_fact.get(i);
if (fact == null)
;
else if (fact.save(getTrxName()))
;
else
{
log.log(Level.SEVERE, "(fact not saved) ... rolling back");
trx.rollback();
trx.close();
unlock();
return STATUS_Error;
}
}
}
// Commit Doc
if (!save(getTrxName())) // contains unlock & document status update
{
log.log(Level.SEVERE, "(doc not saved) ... rolling back");
trx.rollback();
trx.close();
unlock();
return STATUS_Error;
}
// Success
trx.commit();
trx.close();
trx = null;
// *** Transaction End ***
}
catch (Exception e)
{
log.log(Level.SEVERE, "... rolling back", e);
status = STATUS_Error;
try {
if (trx != null)
trx.rollback();
} catch (Exception e2) {}
try {
if (trx != null)
trx.close();
trx = null;
} catch (Exception e3) {}
unlock();
}
p_Status = status;
return status;
} // postCommit
/**
* Get Trx Name and create Transaction
* @return Trx Name
*/
protected String getTrxName()
{
return m_trxName;
} // getTrxName
/**
* Unlock Document
*/
private void unlock()
{
StringBuffer sql = new StringBuffer ("UPDATE ");
sql.append(get_TableName()).append( " SET Processing='N' WHERE ")
.append(get_TableName()).append("_ID=").append(p_po.get_ID());
DB.executeUpdate(sql.toString(), null); // no trx
} // unlock
/**************************************************************************
* Load Document Type and GL Info.
* Set p_DocumentType and p_GL_Category_ID
*/
protected String getDocumentType()
{
if (m_DocumentType == null)
setDocumentType(null);
return m_DocumentType;
} // getDocumentType
/**
* Load Document Type and GL Info.
* Set p_DocumentType and p_GL_Category_ID
* @param DocumentType
*/
protected void setDocumentType (String DocumentType)
{
if (DocumentType != null)
m_DocumentType = DocumentType;
// No Document Type defined
if (m_DocumentType == null && getC_DocType_ID() != 0)
{
String sql = "SELECT DocBaseType, GL_Category_ID FROM C_DocType WHERE C_DocType_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, getC_DocType_ID());
ResultSet rsDT = pstmt.executeQuery();
if (rsDT.next())
{
m_DocumentType = rsDT.getString(1);
m_GL_Category_ID = rsDT.getInt(2);
}
rsDT.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
}
if (m_DocumentType == null)
{
log.log(Level.SEVERE, "No DocBaseType setup for C_DocType_ID="
+ getC_DocType_ID() + ", DocumentNo=" + getDocumentNo());
}
// We have a document Type, but no GL info - search for DocType
if (m_GL_Category_ID == 0)
{
String sql = "SELECT GL_Category_ID FROM C_DocType "
+ "WHERE AD_Client_ID=? AND DocBaseType=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, getAD_Client_ID());
pstmt.setString(2, m_DocumentType);
ResultSet rsDT = pstmt.executeQuery();
if (rsDT.next())
m_GL_Category_ID = rsDT.getInt(1);
rsDT.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
}
// Still no GL_Category - get Default GL Category
if (m_GL_Category_ID == 0)
{
String sql = "SELECT GL_Category_ID FROM GL_Category "
+ "WHERE AD_Client_ID=? "
+ "ORDER BY IsDefault DESC";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, getAD_Client_ID());
ResultSet rsDT = pstmt.executeQuery();
if (rsDT.next())
m_GL_Category_ID = rsDT.getInt(1);
rsDT.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
}
//
if (m_GL_Category_ID == 0)
log.log(Level.SEVERE, "No default GL_Category - " + toString());
if (m_DocumentType == null)
throw new IllegalStateException("Document Type not found");
} // setDocumentType
/**************************************************************************
* Is the Source Document Balanced
* @return true if (source) baanced
*/
public boolean isBalanced()
{
// Multi-Currency documents are source balanced by definition
if (isMultiCurrency())
return true;
//
boolean retValue = getBalance().signum() == 0;
if (retValue)
log.fine("Yes " + toString());
else
log.warning("NO - " + toString());
return retValue;
} // isBalanced
/**
* Is Document convertible to currency and Conversion Type
* @param acctSchema accounting schema
* @return true, if vonvertable to accounting currency
*/
public boolean isConvertible (MAcctSchema acctSchema)
{
// No Currency in document
if (getC_Currency_ID() == NO_CURRENCY)
{
log.fine("(none) - " + toString());
return true;
}
// Get All Currencies
HashSet<Integer> set = new HashSet<Integer>();
set.add(new Integer(getC_Currency_ID()));
for (int i = 0; p_lines != null && i < p_lines.length; i++)
{
int C_Currency_ID = p_lines[i].getC_Currency_ID();
if (C_Currency_ID != NO_CURRENCY)
set.add(new Integer(C_Currency_ID));
}
// just one and the same
if (set.size() == 1 && acctSchema.getC_Currency_ID() == getC_Currency_ID())
{
log.fine("(same) Cur=" + getC_Currency_ID() + " - " + toString());
return true;
}
boolean convertible = true;
Iterator it = set.iterator();
while (it.hasNext() && convertible)
{
int C_Currency_ID = ((Integer)it.next()).intValue();
if (C_Currency_ID != acctSchema.getC_Currency_ID())
{
BigDecimal amt = MConversionRate.getRate (C_Currency_ID, acctSchema.getC_Currency_ID(),
getDateAcct(), getC_ConversionType_ID(), getAD_Client_ID(), getAD_Org_ID());
if (amt == null)
{
convertible = false;
log.warning ("NOT from C_Currency_ID=" + C_Currency_ID
+ " to " + acctSchema.getC_Currency_ID()
+ " - " + toString());
}
else
log.fine("From C_Currency_ID=" + C_Currency_ID);
}
}
log.fine("Convertible=" + convertible + ", AcctSchema C_Currency_ID=" + acctSchema.getC_Currency_ID() + " - " + toString());
return convertible;
} // isConvertible
/**
* Calculate Period from DateAcct.
* m_C_Period_ID is set to -1 of not open to 0 if not found
*/
public void setPeriod()
{
if (m_period != null)
return;
// Period defined in GL Journal (e.g. adjustment period)
int index = p_po.get_ColumnIndex("C_Period_ID");
if (index != -1)
{
Integer ii = (Integer)p_po.get_Value(index);
if (ii != null)
m_period = MPeriod.get(getCtx(), ii.intValue(), m_trxName);
}
if (m_period == null)
m_period = MPeriod.get(getCtx(), getDateAcct());
// Is Period Open?
if (m_period != null
&& m_period.isOpen(getDocumentType()))
m_C_Period_ID = m_period.getC_Period_ID();
else
m_C_Period_ID = -1;
//
log.fine( // + AD_Client_ID + " - "
getDateAcct() + " - " + getDocumentType() + " => " + m_C_Period_ID);
} // setC_Period_ID
/**
* Get C_Period_ID
* @return period
*/
public int getC_Period_ID()
{
if (m_period == null)
setPeriod();
return m_C_Period_ID;
} // getC_Period_ID
/**
* Is Period Open
* @return true if period is open
*/
public boolean isPeriodOpen()
{
setPeriod();
boolean open = m_C_Period_ID > 0;
if (open)
log.fine("Yes - " + toString());
else
log.warning("NO - " + toString());
return open;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -