001/*
002 * Copyright 2007 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.gl.dataaccess.impl;
017
018import java.math.BigDecimal;
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022
023import org.kuali.ole.gl.GeneralLedgerConstants;
024import org.kuali.ole.gl.dataaccess.AccountBalanceObjectDao;
025import org.kuali.ole.gl.service.AccountBalanceService;
026import org.kuali.ole.sys.OLEConstants;
027import org.kuali.ole.sys.businessobject.SystemOptions;
028import org.kuali.ole.sys.businessobject.UniversityDate;
029import org.springframework.dao.IncorrectResultSizeDataAccessException;
030import org.springframework.jdbc.support.rowset.SqlRowSet;
031
032/**
033 * Calculate Balance By Object Balance Inquiry Screen
034 */
035public class AccountBalanceObjectDaoJdbc extends AccountBalanceDaoJdbcBase implements AccountBalanceObjectDao {
036    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountBalanceObjectDaoJdbc.class);
037
038    /**
039     * Returns a collection of report data for the account balance by object inquiry
040     * 
041     * @param universityFiscalYear the university fiscal year of reported on account balances
042     * @param chartOfAccountsCode the chart of accounts code of reported on account balances
043     * @param accountNumber the account number of reported on account balances
044     * @param financialObjectLevelCode the object level code of reported on account balances
045     * @param financialReportingSortCode the sort code for reported results
046     * @param isCostShareExcluded whether cost share account balances should be excluded from the query or not
047     * @param isConsolidated whether the results of the query should be consolidated
048     * @param pendingEntriesCode whether this query should account for no pending entries, approved pending entries, or all pending
049     *        entries
050     * @return a List of Maps with the results of the query
051     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAccountBalanceByObject(java.lang.Integer, java.lang.String,
052     *      java.lang.String, java.lang.String, java.lang.String, boolean, boolean, int)
053     */
054    @Override
055    public List findAccountBalanceByObject(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, String financialObjectLevelCode, String financialReportingSortCode, boolean isCostShareExcluded, boolean isConsolidated, int pendingEntriesCode, UniversityDate today, SystemOptions options) {
056        LOG.debug("findAccountBalanceByObject() started");
057
058        List<Map<String, Object>> data = null;
059        String sessionId = java.util.UUID.randomUUID().toString();
060        try {
061            // Delete any data for this session if it exists already
062            clearTempTable("FP_BAL_BY_OBJ_MT", "SESID", sessionId);
063            clearTempTable("FP_INTERIM1_OBJ_MT", "SESID", sessionId);
064            clearTempTable("FP_INTERIM2_OBJ_MT", "SESID", sessionId);
065
066            // Add in all the data we need
067            getSimpleJdbcTemplate().update("INSERT INTO FP_INTERIM1_OBJ_MT (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR,FIN_OBJECT_CD, FIN_SUB_OBJ_CD, CURR_BDLN_BAL_AMT," + " ACLN_ACTLS_BAL_AMT, ACLN_ENCUM_BAL_AMT, TIMESTAMP,SESID) " + " SELECT a.UNIV_FISCAL_YR, a.FIN_COA_CD, a.ACCOUNT_NBR, a.SUB_ACCT_NBR," + " a.FIN_OBJECT_CD, a.FIN_SUB_OBJ_CD, a.CURR_BDLN_BAL_AMT,a.ACLN_ACTLS_BAL_AMT, a.ACLN_ENCUM_BAL_AMT, a.TIMESTAMP, ?" + " FROM GL_ACCT_BALANCES_T a, CA_OBJECT_CODE_T o WHERE a.univ_fiscal_yr = ? " + " AND a.fin_coa_cd = ?" + " AND a.account_nbr = ?" + " AND a.univ_fiscal_yr = o.univ_fiscal_yr AND a.fin_coa_cd = o.fin_coa_cd AND a.fin_object_cd = o.fin_object_cd " + "AND o.fin_obj_level_cd = ?", sessionId, universityFiscalYear, chartOfAccountsCode, accountNumber, financialObjectLevelCode);
068
069            // Summarize pending entries into fp_interim1_level_mt if necessary
070            if ((pendingEntriesCode == AccountBalanceService.PENDING_ALL) || (pendingEntriesCode == AccountBalanceService.PENDING_APPROVED)) {
071                if (getMatchingPendingEntriesByObject(options, universityFiscalYear, chartOfAccountsCode, accountNumber, financialObjectLevelCode, isCostShareExcluded, pendingEntriesCode, sessionId, today)) {
072                    summarizePendingEntriesByObject(options, sessionId);
073                }
074            }
075
076            // Add some reference data
077            getSimpleJdbcTemplate().update(
078                    "INSERT INTO FP_INTERIM2_OBJ_MT (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, CURR_BDLN_BAL_AMT, ACLN_ACTLS_BAL_AMT, " + "ACLN_ENCUM_BAL_AMT, TIMESTAMP, SESID, FIN_REPORT_SORT_CD, ACCTG_CTGRY_CD ) " + 
079                            " SELECT a.UNIV_FISCAL_YR, a.FIN_COA_CD, a.ACCOUNT_NBR, a.SUB_ACCT_NBR,a.FIN_OBJECT_CD, a.FIN_SUB_OBJ_CD, a.CURR_BDLN_BAL_AMT, a.ACLN_ACTLS_BAL_AMT, " + "a.ACLN_ENCUM_BAL_AMT, a.TIMESTAMP, a.SESID, t.fin_report_sort_cd, t.acctg_ctgry_cd" + 
080                            " FROM FP_INTERIM1_OBJ_MT a, CA_OBJECT_CODE_T o, CA_OBJ_TYPE_T t " +
081                            " WHERE a.univ_fiscal_yr = o.univ_fiscal_yr " + 
082                            " AND a.fin_coa_cd = o.fin_coa_cd " +
083                            " AND a.fin_object_cd = o.fin_object_cd " +
084                            " AND o.fin_obj_typ_cd = t.fin_obj_typ_cd " + 
085                            " AND o.univ_fiscal_yr = ?" + 
086                            " AND o.fin_coa_cd = ?" + 
087                            " AND a.SESID = ?", universityFiscalYear, chartOfAccountsCode, sessionId);
088
089
090            // Delete what we don't need
091            if (isCostShareExcluded) {
092                purgeCostShareEntries("FP_INTERIM2_OBJ_MT", "sesid", sessionId);
093            }
094
095            // Summarize
096            if (isConsolidated) {
097                getSimpleJdbcTemplate().update("INSERT INTO FP_BAL_BY_OBJ_MT (SUB_ACCT_NBR, FIN_OBJECT_CD, ACCTG_CTGRY_CD, CURR_BDLN_BAL_AMT, ACLN_ACTLS_BAL_AMT, ACLN_ENCUM_BAL_AMT, FIN_REPORT_SORT_CD, " + "SESID) SELECT  '*ALL*',fin_object_cd, acctg_ctgry_cd, SUM(curr_bdln_bal_amt),SUM(acln_actls_bal_amt), SUM(acln_encum_bal_amt)," + " fin_report_sort_cd, ? " + " FROM FP_INTERIM2_OBJ_MT WHERE SESID  = ?" + " GROUP BY fin_object_cd, fin_report_sort_cd, acctg_ctgry_cd", sessionId, sessionId);
098            }
099            else {
100                getSimpleJdbcTemplate().update("INSERT INTO FP_BAL_BY_OBJ_MT (SUB_ACCT_NBR, FIN_OBJECT_CD, ACCTG_CTGRY_CD, CURR_BDLN_BAL_AMT, ACLN_ACTLS_BAL_AMT, ACLN_ENCUM_BAL_AMT, FIN_REPORT_SORT_CD, " + "SESID) SELECT  sub_acct_nbr, fin_object_cd, acctg_ctgry_cd, SUM(curr_bdln_bal_amt), SUM(acln_actls_bal_amt),SUM(acln_encum_bal_amt), " + " fin_report_sort_cd, ? " + " FROM FP_INTERIM2_OBJ_MT WHERE SESID = ? " + " GROUP BY sub_acct_nbr, fin_object_cd, fin_report_sort_cd, acctg_ctgry_cd", sessionId, sessionId);
101            }
102
103            // Here's the data
104            data = getSimpleJdbcTemplate().queryForList("select SUB_ACCT_NBR, FIN_OBJECT_CD, CURR_BDLN_BAL_AMT, ACLN_ACTLS_BAL_AMT, ACLN_ENCUM_BAL_AMT, ACCTG_CTGRY_CD, FIN_REPORT_SORT_CD from FP_BAL_BY_OBJ_MT " + " where SESID = ? " + " order by fin_object_cd", sessionId);
105        }
106        finally {
107            // Clean up everything
108            clearTempTable("FP_BAL_BY_OBJ_MT", "SESID", sessionId);
109            clearTempTable("FP_INTERIM1_OBJ_MT", "SESID", sessionId);
110            clearTempTable("FP_INTERIM2_OBJ_MT", "SESID", sessionId);
111            clearTempTable("GL_PENDING_ENTRY_MT", "SESID", sessionId);
112        }
113        return data;
114    }
115
116    /**
117     * Summarizes all the pending ledger entries that would need to be reported on by this inquiry, and saves all of that data to a
118     * temporary table
119     * 
120     * @param options a set of system options
121     * @param sessionId the unique web id of the currently inquiring user, used as a key for the temp table
122     */
123    protected void summarizePendingEntriesByObject(SystemOptions options, String sessionId) {
124        LOG.debug("summarizePendingEntriesByObject() started");
125
126        try {
127
128            String balanceStatementSql = "SELECT CURR_BDLN_BAL_AMT,ACLN_ACTLS_BAL_AMT,ACLN_ENCUM_BAL_AMT " + "FROM FP_INTERIM1_OBJ_MT " + "WHERE sesid = ? AND univ_fiscal_yr = ? AND fin_coa_cd = ? AND account_nbr = ? AND sub_acct_nbr = ?" + " AND fin_object_cd = ? AND fin_sub_obj_cd = ?";
129
130            String updateBalanceStatementSql = "UPDATE FP_INTERIM1_OBJ_MT " + " SET curr_bdln_bal_amt = ?,acln_actls_bal_amt = ?,acln_encum_bal_amt = ? " + " WHERE sesid = ? AND univ_fiscal_yr = ? AND fin_coa_cd = ? AND account_nbr = ? AND sub_acct_nbr = ?" + " AND fin_object_cd = ? AND fin_sub_obj_cd = ?";
131
132            String insertBalanceStatementSql = "INSERT INTO FP_INTERIM1_OBJ_MT (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, " + "FIN_SUB_OBJ_CD, CURR_BDLN_BAL_AMT, ACLN_ACTLS_BAL_AMT, ACLN_ENCUM_BAL_AMT, TIMESTAMP, SESID) " + "VALUES (?,?,?,?,?,?,?,?,?," + getDbPlatform().getCurTimeFunction() + ",?)";
133
134            SqlRowSet pendingEntryRowSet = getJdbcTemplate().queryForRowSet("SELECT b.FIN_OFFST_GNRTN_CD,t.FIN_OBJTYP_DBCR_CD,e.* " + "FROM GL_PENDING_ENTRY_MT e,CA_OBJ_TYPE_T t,CA_BALANCE_TYPE_T b " + "WHERE e.SESID = ?" + " AND e.FIN_OBJ_TYP_CD = t.FIN_OBJ_TYP_CD AND e.fin_balance_typ_cd = b.fin_balance_typ_cd " + "ORDER BY e.univ_fiscal_yr,e.account_nbr,e.sub_acct_nbr,e.fin_object_cd,e.fin_sub_obj_cd,e.fin_obj_typ_cd", new Object[] { sessionId });
135
136
137            int updateCount = 0;
138            int insertCount = 0;
139            while (pendingEntryRowSet.next()) {
140
141                Map<String, Object> balance = null;
142                try {
143                    balance = getSimpleJdbcTemplate().queryForMap(balanceStatementSql, sessionId, pendingEntryRowSet.getInt(GeneralLedgerConstants.ColumnNames.UNIVERSITY_FISCAL_YEAR), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.CHART_OF_ACCOUNTS_CODE), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.ACCOUNT_NUMBER), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.SUB_ACCOUNT_NUMBER), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.OBJECT_CODE), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.SUB_OBJECT_CODE));
144                }
145                catch (IncorrectResultSizeDataAccessException ex) {
146                    if (ex.getActualSize() != 0) {
147                        LOG.error("balance request sql returned more than one row, aborting", ex);
148                        throw ex;
149                    }
150                    // no rows returned - that's ok
151                }
152
153                String balanceType = pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.BALANCE_TYPE_CODE);
154                String debitCreditCode = pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.DEBIT_CREDIT_CODE);
155                String objectTypeDebitCreditCode = pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.OBJECT_TYPE_DEBIT_CREDIT_CODE);
156                String offsetGenerationCode = pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.OFFSET_GENERATION_CODE);
157
158                if (balance != null) {
159                    updateCount++;
160
161                    BigDecimal budget = (BigDecimal) balance.get(GeneralLedgerConstants.ColumnNames.CURRENT_BDLN_BALANCE_AMOUNT);
162                    BigDecimal actual = (BigDecimal) balance.get(GeneralLedgerConstants.ColumnNames.ACCOUNTING_LINE_ACTUALS_BALANCE_AMOUNT);
163                    BigDecimal encumb = (BigDecimal) balance.get(GeneralLedgerConstants.ColumnNames.ACCOUNTING_LINE_ENCUMBRANCE_BALANCE_AMOUNT);
164
165                    if (balanceType.equals(options.getBudgetCheckingBalanceTypeCd())) {
166                        budget = budget.add(pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT));
167                    }
168                    else if (balanceType.equals(options.getActualFinancialBalanceTypeCd())) {
169                        if (debitCreditCode.equals(objectTypeDebitCreditCode) || (("N".equals(offsetGenerationCode) && OLEConstants.GL_BUDGET_CODE.equals(debitCreditCode)))) {
170                            actual = actual.add(pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT));
171                        }
172                        else {
173                            actual = actual.subtract(pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT));
174                        }
175                    }
176                    else if (balanceType.equals(options.getExtrnlEncumFinBalanceTypCd()) || balanceType.equals(options.getIntrnlEncumFinBalanceTypCd()) || balanceType.equals(options.getPreencumbranceFinBalTypeCd()) || "CE".equals(balanceType)) {
177                        if (debitCreditCode.equals(objectTypeDebitCreditCode) || (("N".equals(offsetGenerationCode) && OLEConstants.GL_BUDGET_CODE.equals(debitCreditCode)))) {
178                            encumb = encumb.add(pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT));
179                        }
180                        else {
181                            encumb = encumb.subtract(pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT));
182                        }
183                    }
184
185                    // A balance exists, so we need to update it
186                    getSimpleJdbcTemplate().update(updateBalanceStatementSql, budget, actual, encumb, sessionId, pendingEntryRowSet.getInt(GeneralLedgerConstants.ColumnNames.UNIVERSITY_FISCAL_YEAR), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.CHART_OF_ACCOUNTS_CODE), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.ACCOUNT_NUMBER), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.SUB_ACCOUNT_NUMBER), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.OBJECT_CODE), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.SUB_OBJECT_CODE));
187                }
188                else {
189                    insertCount++;
190
191                    BigDecimal budget = new BigDecimal("0");
192                    BigDecimal actual = new BigDecimal("0");
193                    BigDecimal encumb = new BigDecimal("0");
194
195                    if (balanceType.equals(options.getBudgetCheckingBalanceTypeCd())) {
196                        budget = pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT);
197                    }
198                    else if (balanceType.equals(options.getActualFinancialBalanceTypeCd())) {
199                        if (debitCreditCode.equals(objectTypeDebitCreditCode) || (("N".equals(offsetGenerationCode) && OLEConstants.GL_BUDGET_CODE.equals(debitCreditCode)))) {
200                            actual = pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT);
201                        }
202                        else {
203                            actual = pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT).negate();
204                        }
205                    }
206                    else if (balanceType.equals(options.getExtrnlEncumFinBalanceTypCd()) || balanceType.equals(options.getIntrnlEncumFinBalanceTypCd()) || balanceType.equals(options.getPreencumbranceFinBalTypeCd()) || "CE".equals(balanceType)) {
207                        if (debitCreditCode.equals(objectTypeDebitCreditCode) || (("N".equals(offsetGenerationCode) && OLEConstants.GL_BUDGET_CODE.equals(debitCreditCode)))) {
208                            encumb = pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT);
209                        }
210                        else {
211                            encumb = pendingEntryRowSet.getBigDecimal(GeneralLedgerConstants.ColumnNames.TRANSACTION_LEDGER_ENTRY_AMOUNT).negate();
212                        }
213                    }
214
215                    // No balance exists, so we need to insert one
216                    getSimpleJdbcTemplate().update(insertBalanceStatementSql, pendingEntryRowSet.getInt(GeneralLedgerConstants.ColumnNames.UNIVERSITY_FISCAL_YEAR), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.CHART_OF_ACCOUNTS_CODE), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.ACCOUNT_NUMBER), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.SUB_ACCOUNT_NUMBER), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.OBJECT_CODE), pendingEntryRowSet.getString(GeneralLedgerConstants.ColumnNames.SUB_OBJECT_CODE), budget, actual, encumb, sessionId);
217                }
218            }
219            LOG.info("summarizePendingEntriesByObject() INSERTS: " + insertCount);
220            LOG.info("summarizePendingEntriesByObject() UPDATES: " + updateCount);
221        }
222        catch (RuntimeException ex) {
223            LOG.error("summarizePendingEntriesByObject() Exception running sql", ex);
224            throw ex;
225        }
226    }
227
228    /**
229     * Get any matching pending entries. Return true if there were some, false if not.
230     * 
231     * @param universityFiscalYear the university fiscal year of pending entries to summarize
232     * @param chartOfAccountsCode the chart of accounts code of pending entries to summarize
233     * @param accountNumber the account number of pending entries to summarize
234     * @param financialObjectLevelCode the object level code of pending entries to summarize
235     * @param pendingEntriesCode whether to summarize all, approved, or no pending entries
236     * @return true if any matching pending entries were found, false otherwise
237     */
238    protected boolean getMatchingPendingEntriesByObject(SystemOptions options, Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, String financialObjectLevelCode, boolean isCostShareExcluded, int pendingEntriesCode, String sessionId, UniversityDate today) {
239        LOG.debug("getMatchingPendingEntriesByObject() started");
240
241        // If they have specified this year, we will get all the pending entries where the year is equal or the year is null
242        // (because most eDocs don't fill in the year field).
243        // If they have specified a previous year, we will get all the pending entries where the year is equal to their selection
244        // without the nulls (because we will post eDocs
245        // with blank years tonight most probably.
246
247        clearTempTable("GL_PENDING_ENTRY_MT", "SESID", sessionId);
248
249        List<Object> params = new ArrayList<Object>(20);
250
251        String insertSql = "insert into GL_PENDING_ENTRY_MT (SESID, FS_ORIGIN_CD, FDOC_NBR, TRN_ENTR_SEQ_NBR, " + "FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, FIN_BALANCE_TYP_CD," + "FIN_OBJ_TYP_CD, UNIV_FISCAL_YR, UNIV_FISCAL_PRD_CD, TRN_LDGR_ENTR_DESC, TRN_LDGR_ENTR_AMT, TRN_DEBIT_CRDT_CD," + "TRANSACTION_DT, FDOC_TYP_CD, ORG_DOC_NBR, PROJECT_CD, ORG_REFERENCE_ID, FDOC_REF_TYP_CD, FS_REF_ORIGIN_CD,FDOC_REF_NBR, " + "FDOC_REVERSAL_DT, TRN_ENCUM_UPDT_CD, FDOC_APPROVED_CD, ACCT_SF_FINOBJ_CD, TRN_ENTR_OFST_CD,TRNENTR_PROCESS_TM) ";
252
253        String selectSql = "SELECT ?, p.FS_ORIGIN_CD, p.FDOC_NBR, p.TRN_ENTR_SEQ_NBR, p.FIN_COA_CD, p.ACCOUNT_NBR, " + getDbPlatform().getIsNullFunction("p.SUB_ACCT_NBR", "'-----'") + ", " + " p.FIN_OBJECT_CD, " + getDbPlatform().getIsNullFunction("p.FIN_SUB_OBJ_CD", "'---'") + ", p.FIN_BALANCE_TYP_CD,p.FIN_OBJ_TYP_CD, p.UNIV_FISCAL_YR, " + " p.UNIV_FISCAL_PRD_CD, p.TRN_LDGR_ENTR_DESC, p.TRN_LDGR_ENTR_AMT, p.TRN_DEBIT_CRDT_CD," + "p.TRANSACTION_DT, p.FDOC_TYP_CD, p.ORG_DOC_NBR, PROJECT_CD, p.ORG_REFERENCE_ID, p.FDOC_REF_TYP_CD, p.FS_REF_ORIGIN_CD,p.FDOC_REF_NBR, p.FDOC_REVERSAL_DT, p.TRN_ENCUM_UPDT_CD, p.FDOC_APPROVED_CD, p.ACCT_SF_FINOBJ_CD, p.TRN_ENTR_OFST_CD,p.TRNENTR_PROCESS_TM " + "FROM GL_PENDING_ENTRY_T p,CA_OBJECT_CODE_T o,KRNS_DOC_HDR_T d,FS_DOC_HEADER_T fd " + "WHERE o.FIN_COA_CD = p.FIN_COA_CD AND o.FIN_OBJECT_CD = p.FIN_OBJECT_CD AND o.FIN_OBJ_LEVEL_CD = ?" + " AND p.fdoc_nbr = d.DOC_HDR_ID AND d.DOC_HDR_ID = fd.fdoc_nbr AND " + " p.FIN_COA_CD = ?"
254                + " and p.account_nbr = ?" + " and o.univ_fiscal_yr = ?";
255        params.add(sessionId);
256        params.add(financialObjectLevelCode);
257        params.add(chartOfAccountsCode);
258        params.add(accountNumber);
259        params.add(universityFiscalYear);
260
261        if (pendingEntriesCode == AccountBalanceService.PENDING_ALL) {
262            selectSql = selectSql + " AND fd.fdoc_status_cd <> '" + OLEConstants.DocumentStatusCodes.DISAPPROVED + "' ";
263        }
264        else {
265            selectSql = selectSql + " AND fd.fdoc_status_cd = '" + OLEConstants.DocumentStatusCodes.APPROVED + "' ";
266        }
267        selectSql = selectSql + " AND fd.fdoc_status_cd <> '" + OLEConstants.DocumentStatusCodes.CANCELLED + "' ";
268        selectSql = selectSql + " AND p.FDOC_APPROVED_CD <> '" + OLEConstants.DocumentStatusCodes.CANCELLED + "' ";
269
270        if (today.getUniversityFiscalYear().equals(universityFiscalYear)) {
271            selectSql = selectSql + "AND (p.univ_fiscal_yr is null OR p.univ_fiscal_yr = ? )";
272            params.add(universityFiscalYear);
273        }
274        else {
275            selectSql = selectSql + "AND p.univ_fiscal_yr = ?";
276            params.add(universityFiscalYear);
277        }
278
279        getSimpleJdbcTemplate().update(insertSql + selectSql, params.toArray());
280
281        if (isCostShareExcluded) {
282            purgeCostShareEntries("GL_PENDING_ENTRY_MT", "sesid", sessionId);
283        }
284
285        if (!hasEntriesInPendingTable(sessionId)) {
286            return false;
287        }
288
289        fixPendingEntryDisplay(options.getUniversityFiscalYear(), sessionId);
290
291        return true;
292    }
293}