001/*
002 * Copyright 2013 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 1.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/ecl1.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.select.document.service.impl;
017
018import org.apache.log4j.Logger;
019import org.kuali.ole.coa.businessobject.Account;
020import org.kuali.ole.gl.businessobject.Balance;
021import org.kuali.ole.module.purap.businessobject.PurchaseOrderType;
022import org.kuali.ole.module.purap.businessobject.RequisitionItem;
023import org.kuali.ole.module.purap.document.RequisitionDocument;
024import org.kuali.ole.select.OleSelectNotificationConstant;
025import org.kuali.ole.select.businessobject.OleRequisitionAccount;
026import org.kuali.ole.select.businessobject.OleSufficientFundCheck;
027import org.kuali.ole.select.document.service.OleRequisitionDocumentService;
028import org.kuali.ole.select.document.service.OleSelectDocumentService;
029import org.kuali.ole.sys.OLEConstants;
030import org.kuali.ole.sys.OLEPropertyConstants;
031import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
032import org.kuali.ole.sys.businessobject.SourceAccountingLine;
033import org.kuali.ole.sys.context.SpringContext;
034import org.kuali.ole.sys.service.UniversityDateService;
035import org.kuali.rice.core.api.config.property.ConfigurationService;
036import org.kuali.rice.core.api.util.type.KualiDecimal;
037import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
038import org.kuali.rice.coreservice.api.parameter.Parameter;
039import org.kuali.rice.coreservice.api.parameter.ParameterKey;
040import org.kuali.rice.kew.api.WorkflowDocument;
041import org.kuali.rice.kim.api.identity.PersonService;
042import org.kuali.rice.krad.UserSession;
043import org.kuali.rice.krad.service.BusinessObjectService;
044import org.kuali.rice.krad.service.DocumentHeaderService;
045import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
046import org.kuali.rice.krad.util.GlobalVariables;
047
048import java.util.HashMap;
049import java.util.List;
050import java.util.Map;
051
052public class OleRequisitionDocumentServiceImpl implements OleRequisitionDocumentService {
053    protected static final Logger LOG = Logger.getLogger(OleInvoiceFundCheckServiceImpl.class);
054    protected ConfigurationService kualiConfigurationService;
055    private OleSelectDocumentService oleSelectDocumentService;
056    private BusinessObjectService businessObjectService;
057    @Override
058    public boolean hasSufficientFundsOnRequisition(SourceAccountingLine accLine) {
059        boolean hasSufficientFundRequired = false;
060        String chartCode = accLine.getChartOfAccountsCode();
061        String accNo = accLine.getAccountNumber();
062        String objectCd = accLine.getFinancialObjectCode();
063        String fundCodeType = getFundCode(chartCode, accNo);
064        KualiDecimal budgetAllocation = KualiDecimal.ZERO;
065        KualiDecimal encumbranceAmt = KualiDecimal.ZERO;
066        Map<String, Object> fundKey = new HashMap<String, Object>();
067        String notificationOption = null;
068        fundKey.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
069        fundKey.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
070        OleSufficientFundCheck account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
071                OleSufficientFundCheck.class, fundKey);
072        if (account != null) {
073            notificationOption = account.getNotificationOption();
074        }
075
076        if (fundCodeType != null && fundCodeType.equals(OLEConstants.ACCOUNT_FUND_CODE)) {
077
078            if (notificationOption.equals(OLEPropertyConstants.BLOCK_USE)
079                    || notificationOption.equals(OLEPropertyConstants.WARNING_MSG)) {
080                budgetAllocation = getBudgetAllocationForAccount(chartCode, accNo, objectCd);
081                encumbranceAmt = getEncumbranceForAccount(chartCode, accNo, objectCd);
082                budgetAllocation = budgetAllocation.subtract(encumbranceAmt);
083            } else if (notificationOption.equals(OLEPropertyConstants.BUD_REVIEW)
084                    || notificationOption.equals(OLEPropertyConstants.NOTIFICATION)) {
085                budgetAllocation = getBudgetAllocationForAccount(chartCode, accNo, objectCd);
086                encumbranceAmt = getEncumbranceForAccount(chartCode, accNo, objectCd);
087                encumbranceAmt = encumbranceAmt.subtract(accLine.getAmount());
088                budgetAllocation = budgetAllocation.subtract(encumbranceAmt);
089            }
090
091            if (accLine.getAmount().isGreaterThan(budgetAllocation)) {
092                hasSufficientFundRequired = true;
093            } else {
094                hasSufficientFundRequired = false;
095            }
096
097        } else if (fundCodeType != null && fundCodeType.equals(OLEConstants.OBJECT_FUND_CODE)) {
098            if (notificationOption.equals(OLEPropertyConstants.BLOCK_USE)
099                    || notificationOption.equals(OLEPropertyConstants.WARNING_MSG)) {
100                budgetAllocation = getBudgetAllocationForObject(chartCode, accNo, objectCd);
101                encumbranceAmt = getEncumbranceForObject(chartCode, accNo, objectCd);
102                budgetAllocation = budgetAllocation.subtract(encumbranceAmt);
103            } else if (notificationOption.equals(OLEPropertyConstants.BUD_REVIEW)
104                    || notificationOption.equals(OLEPropertyConstants.NOTIFICATION)) {
105                budgetAllocation = getBudgetAllocationForObject(chartCode, accNo, objectCd);
106                encumbranceAmt = getEncumbranceForObject(chartCode, accNo, objectCd);
107                encumbranceAmt = encumbranceAmt.subtract(accLine.getAmount());
108                budgetAllocation = budgetAllocation.subtract(encumbranceAmt);
109            }
110            if (accLine.getAmount().isGreaterThan(budgetAllocation)) {
111                hasSufficientFundRequired = true;
112            } else {
113                hasSufficientFundRequired = false;
114            }
115        }
116        return hasSufficientFundRequired;
117    }
118
119    private String getFundCode(String chartCode, String accountNumber) {
120        Map accountMap = new HashMap();
121        accountMap.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
122        accountMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
123        Account account = SpringContext.getBean(BusinessObjectService.class)
124                .findByPrimaryKey(Account.class, accountMap);
125        boolean fundCheckIndication = false;
126        if (account != null) {
127            fundCheckIndication = account.isPendingAcctSufficientFundsIndicator();
128        }
129
130        if (fundCheckIndication) {
131            String fundCode = account.getAccountSufficientFundsCode();
132            return fundCode;
133        }
134        return null;
135    }
136
137    private KualiDecimal getBudgetAllocationForObject(String chartCode, String accountNumber, String objectCode) {
138        Map budgetMap = new HashMap();
139        UniversityDateService universityDateService = SpringContext.getBean(UniversityDateService.class);
140        budgetMap.put(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR,
141                universityDateService.getCurrentUniversityDate().getUniversityFiscalYear());
142        budgetMap.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
143        budgetMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
144        budgetMap.put(OLEPropertyConstants.OBJECT_CODE, objectCode);
145        budgetMap.put(OLEPropertyConstants.BALANCE_TYPE_CODE, OLEPropertyConstants.BAL_TYP_CODE);
146        Balance balance = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(Balance.class, budgetMap);
147        KualiDecimal initialBudgetAmount = KualiDecimal.ZERO;
148        if (balance != null) {
149            initialBudgetAmount = balance.getAccountLineAnnualBalanceAmount();
150        }
151        Map encMap = new HashMap();
152        encMap.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
153        encMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
154        OleSufficientFundCheck oleSufficientFundCheck = SpringContext.getBean(BusinessObjectService.class)
155                .findByPrimaryKey(OleSufficientFundCheck.class, encMap);
156        KualiDecimal amount = KualiDecimal.ZERO;
157        if (oleSufficientFundCheck != null) {
158            if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_PERCENTAGE.equals(oleSufficientFundCheck
159                    .getEncumbExpenseConstraintType())) {
160                amount = new KualiDecimal(oleSufficientFundCheck.getEncumbranceAmount());
161                if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
162                        .getEncumbExpenseMethod())) {
163                    initialBudgetAmount = initialBudgetAmount.add((initialBudgetAmount.multiply(amount))
164                            .divide(new KualiDecimal(100)));
165                } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
166                        .getEncumbExpenseMethod())) {
167                    initialBudgetAmount = initialBudgetAmount.subtract((initialBudgetAmount.multiply(amount))
168                            .divide(new KualiDecimal(100)));
169
170                }
171            } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_CASH.equals(oleSufficientFundCheck
172                    .getEncumbExpenseConstraintType())) {
173                amount = new KualiDecimal(oleSufficientFundCheck.getEncumbranceAmount());
174                if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
175                        .getEncumbExpenseMethod())) {
176                    initialBudgetAmount = initialBudgetAmount.add(amount);
177                } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
178                        .getEncumbExpenseMethod())) {
179                    initialBudgetAmount = initialBudgetAmount.subtract(amount);
180                }
181            }
182        }
183        KualiDecimal budgetIncrease = getBudgetAdjustmentIncreaseForObject(chartCode,accountNumber,objectCode);
184        KualiDecimal budgetDecrease = getBudgetAdjustmentDecreaseForObject(chartCode,accountNumber,objectCode);
185        return initialBudgetAmount.add(budgetIncrease).subtract(budgetDecrease);
186        //return initialBudgetAmount;
187    }
188
189    private KualiDecimal getEncumbranceForAccount(String chartCode, String accountNumber, String objectCode) {
190        Map encMap = new HashMap();
191        Map itemMap = new HashMap();
192        Map reqsMap = new HashMap();
193        encMap.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
194        encMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
195        KualiDecimal encumAmount = KualiDecimal.ZERO;
196        List<OleRequisitionAccount> encumList = (List<OleRequisitionAccount>) SpringContext.getBean(
197                BusinessObjectService.class).findMatching(OleRequisitionAccount.class, encMap);
198        if (encumList.size() > 0) {
199            for (OleRequisitionAccount olePurchaseOrderAccount : encumList) {
200                itemMap.put(OLEConstants.PO_ITEM_ID, olePurchaseOrderAccount.getItemIdentifier());
201                List<RequisitionItem> encumReqsList = (List<RequisitionItem>) SpringContext.getBean(
202                        BusinessObjectService.class).findMatching(RequisitionItem.class, itemMap);
203                for(RequisitionItem reqsItm: encumReqsList)  {
204                    try {
205                        reqsMap.put(OLEConstants.PUR_DOC_IDENTIFIER,reqsItm.getPurapDocumentIdentifier());
206                        RequisitionDocument encumReqsDoc =  SpringContext.getBean(
207                                BusinessObjectService.class).findBySinglePrimaryKey(RequisitionDocument.class, reqsItm.getPurapDocumentIdentifier());
208
209                        if (encumReqsDoc.getDocumentHeader().getDocumentNumber() == null) {
210                            encumReqsDoc.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(encumReqsDoc.getDocumentNumber()));
211                        }
212                        WorkflowDocument  workflowDocument = null;
213                        //GlobalVariables.setUserSession(new UserSession("ole-khuntley"));
214
215                        String user = null;
216                        if (GlobalVariables.getUserSession() == null) {
217                            kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
218                            user = kualiConfigurationService.getPropertyValueAsString(getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
219                            GlobalVariables.setUserSession(new UserSession(user));
220                        }
221
222
223                        workflowDocument = KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(encumReqsDoc.getDocumentNumber(), SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId()));
224                        if(!(workflowDocument.isCanceled() || workflowDocument.isDisapproved() || workflowDocument.isException())) {
225                            encumAmount = encumAmount.add(olePurchaseOrderAccount.getAmount());
226                        }
227                    }
228                    catch(Exception e) {
229                        LOG.error("Exception while calculating the Sufficient Fund for Requisition Document"+e);
230                        throw new RuntimeException(e);
231                    }
232                }
233                itemMap.clear();
234                reqsMap.clear();
235
236            }
237        }
238        return encumAmount;
239    }
240
241    private KualiDecimal getBudgetAllocationForAccount(String chartCode, String accountNumber, String objectCode) {
242        Map budgetMap = new HashMap();
243        KualiDecimal initialBudgetAmount = KualiDecimal.ZERO;
244        UniversityDateService universityDateService = SpringContext.getBean(UniversityDateService.class);
245        budgetMap.put(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR,
246                universityDateService.getCurrentUniversityDate().getUniversityFiscalYear());
247        budgetMap.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
248        budgetMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
249        budgetMap.put(OLEPropertyConstants.BALANCE_TYPE_CODE, OLEPropertyConstants.BAL_TYP_CODE);
250        List<Balance> balance = (List<Balance>) SpringContext.getBean(BusinessObjectService.class).findMatching(
251                Balance.class, budgetMap);
252        if (balance.size() > 0) {
253            for (Balance budgetBalance : balance) {
254                initialBudgetAmount = initialBudgetAmount.add(budgetBalance.getAccountLineAnnualBalanceAmount());
255            }
256        }
257        Map encMap = new HashMap();
258        encMap.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
259        encMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
260        OleSufficientFundCheck oleSufficientFundCheck = SpringContext.getBean(BusinessObjectService.class)
261                .findByPrimaryKey(OleSufficientFundCheck.class, encMap);
262        KualiDecimal amount = KualiDecimal.ZERO;
263        if (oleSufficientFundCheck != null) {
264            if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_PERCENTAGE.equals(oleSufficientFundCheck
265                    .getEncumbExpenseConstraintType())) {
266                amount = new KualiDecimal(oleSufficientFundCheck.getEncumbranceAmount());
267                if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
268                        .getEncumbExpenseMethod())) {
269                    initialBudgetAmount = initialBudgetAmount.add((initialBudgetAmount.multiply(amount))
270                            .divide(new KualiDecimal(100)));
271                } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
272                        .getEncumbExpenseMethod())) {
273                    initialBudgetAmount = initialBudgetAmount.subtract((initialBudgetAmount.multiply(amount))
274                            .divide(new KualiDecimal(100)));
275
276                }
277            } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_CASH.equals(oleSufficientFundCheck
278                    .getEncumbExpenseConstraintType())) {
279                amount = new KualiDecimal(oleSufficientFundCheck.getEncumbranceAmount());
280                if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
281                        .getEncumbExpenseMethod())) {
282                    initialBudgetAmount = initialBudgetAmount.add(amount);
283                } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
284                        .getEncumbExpenseMethod())) {
285                    initialBudgetAmount = initialBudgetAmount.subtract(amount);
286                }
287            }
288        }
289
290        KualiDecimal budgetIncrease = getBudgetAdjustmentIncreaseForAccount(chartCode,accountNumber,objectCode);
291        KualiDecimal budgetDecrease = getBudgetAdjustmentDecreaseForAccount(chartCode,accountNumber,objectCode);
292        return initialBudgetAmount.add(budgetIncrease).subtract(budgetDecrease);
293    }
294
295    private KualiDecimal getEncumbranceForObject(String chartCode, String accountNumber, String objectCode) {
296        Map encMap = new HashMap();
297        Map itemMap = new HashMap();
298        Map reqsMap = new HashMap();
299        encMap.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
300        encMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
301        encMap.put(OLEPropertyConstants.FINANCIAL_OBJECT_CODE, objectCode);
302        KualiDecimal encumAmount = KualiDecimal.ZERO;
303        List<OleRequisitionAccount> encumList = (List<OleRequisitionAccount>) SpringContext.getBean(
304                BusinessObjectService.class).findMatching(OleRequisitionAccount.class, encMap);
305        if (encumList.size() > 0) {
306            for (OleRequisitionAccount olePurchaseOrderAccount : encumList) {
307                itemMap.put(OLEConstants.PO_ITEM_ID, olePurchaseOrderAccount.getItemIdentifier());
308                List<RequisitionItem> encumReqsList = (List<RequisitionItem>) SpringContext.getBean(
309                        BusinessObjectService.class).findMatching(RequisitionItem.class, itemMap);
310                for(RequisitionItem reqsItm: encumReqsList)  {
311                    try {
312                        reqsMap.put(OLEConstants.PUR_DOC_IDENTIFIER,reqsItm.getPurapDocumentIdentifier());
313                        RequisitionDocument encumReqsDoc =  SpringContext.getBean(
314                                BusinessObjectService.class).findBySinglePrimaryKey(RequisitionDocument.class, reqsItm.getPurapDocumentIdentifier());
315
316                        if (encumReqsDoc.getDocumentHeader().getDocumentNumber() == null) {
317                            encumReqsDoc.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(encumReqsDoc.getDocumentNumber()));
318                        }
319                        WorkflowDocument  workflowDocument = null;
320                        //GlobalVariables.setUserSession(new UserSession("ole-khuntley"));
321                        String user = null;
322                        if (GlobalVariables.getUserSession() == null) {
323                            kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
324                            user = kualiConfigurationService.getPropertyValueAsString(getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
325                            GlobalVariables.setUserSession(new UserSession(user));
326                        }
327                        workflowDocument = KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(encumReqsDoc.getDocumentNumber(), SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId()));
328                        if(!(workflowDocument.isCanceled() || workflowDocument.isDisapproved() || workflowDocument.isException())) {
329                            encumAmount = encumAmount.add(olePurchaseOrderAccount.getAmount());
330                        }
331                    }
332                    catch(Exception e) {
333                        LOG.error("Exception while calculating the Sufficient Fund for Requisition Document"+e);
334                        throw new RuntimeException(e);
335                    }
336                }
337                itemMap.clear();
338                reqsMap.clear();
339            }
340        }
341        return encumAmount;
342    }
343
344
345    public boolean hasSufficientFundsOnBlanketApproveRequisition(SourceAccountingLine accLine) {
346        boolean hasSufficientFundRequired = false;
347        String chartCode = accLine.getChartOfAccountsCode();
348        String accNo = accLine.getAccountNumber();
349        String objectCd = accLine.getFinancialObjectCode();
350        String fundCodeType = getFundCode(chartCode, accNo);
351        KualiDecimal budgetAllocation = KualiDecimal.ZERO;
352        KualiDecimal encumbranceAmt = KualiDecimal.ZERO;
353        Map<String, Object> fundKey = new HashMap<String, Object>();
354        String notificationOption = null;
355        fundKey.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
356        fundKey.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
357        OleSufficientFundCheck account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
358                OleSufficientFundCheck.class, fundKey);
359        if (account != null) {
360            notificationOption = account.getNotificationOption();
361        }
362
363        if (fundCodeType != null && fundCodeType.equals(OLEConstants.ACCOUNT_FUND_CODE)) {
364
365            if (notificationOption.equals(OLEPropertyConstants.BUD_REVIEW)) {
366                budgetAllocation = getBudgetAllocationForAccount(chartCode, accNo, objectCd);
367                encumbranceAmt = getEncumbranceForAccount(chartCode, accNo, objectCd);
368                budgetAllocation = budgetAllocation.subtract(encumbranceAmt);
369            }
370
371            if (accLine.getAmount().isGreaterThan(budgetAllocation)) {
372                hasSufficientFundRequired = true;
373            } else {
374                hasSufficientFundRequired = false;
375            }
376
377        } else if (fundCodeType != null && fundCodeType.equals(OLEConstants.OBJECT_FUND_CODE)) {
378           if (notificationOption.equals(OLEPropertyConstants.BUD_REVIEW)) {
379                budgetAllocation = getBudgetAllocationForObject(chartCode, accNo, objectCd);
380                encumbranceAmt = getEncumbranceForObject(chartCode, accNo, objectCd);
381                budgetAllocation = budgetAllocation.subtract(encumbranceAmt);
382            }
383            if (accLine.getAmount().isGreaterThan(budgetAllocation)) {
384                hasSufficientFundRequired = true;
385            } else {
386                hasSufficientFundRequired = false;
387            }
388        }
389        return hasSufficientFundRequired;
390    }
391
392
393    public KualiDecimal getBudgetAdjustmentIncreaseForObject(String chartCode, String accountNo,
394                                          String objectCode) {
395        Map searchMap = new HashMap();
396        searchMap.put("chartOfAccountsCode", chartCode);
397        searchMap.put("accountNumber", accountNo);
398        searchMap.put("financialObjectCode", objectCode);
399        searchMap.put("financialDocumentTypeCode", OLEConstants.DOC_TYP_CD);
400        searchMap.put("financialBalanceTypeCode", OLEConstants.BAL_TYP_CD);
401        searchMap.put("financialDocumentApprovedCode", OLEConstants.FDOC_APPR_CD);
402        List<GeneralLedgerPendingEntry> generalLedgerPendingEntryList = (List<GeneralLedgerPendingEntry>) SpringContext.getBean(
403                BusinessObjectService.class).findMatching(GeneralLedgerPendingEntry.class, searchMap);
404        KualiDecimal budgetIncrease = KualiDecimal.ZERO;
405        if (generalLedgerPendingEntryList.size() > 0) {
406            for (GeneralLedgerPendingEntry entry : generalLedgerPendingEntryList) {
407                if (entry.getTransactionLedgerEntryAmount().isGreaterThan(KualiDecimal.ZERO)) {
408                    budgetIncrease = budgetIncrease.add(entry.getTransactionLedgerEntryAmount());
409                }
410        }
411        }
412
413        return budgetIncrease;
414    }
415
416    public KualiDecimal getBudgetAdjustmentDecreaseForObject(String chartCode, String accountNo,
417                                            String objectCode) {
418        Map searchMap = new HashMap();
419        searchMap.put("chartOfAccountsCode", chartCode);
420        searchMap.put("accountNumber", accountNo);
421        searchMap.put("financialObjectCode", objectCode);
422        searchMap.put("financialDocumentTypeCode", OLEConstants.DOC_TYP_CD);
423        searchMap.put("financialBalanceTypeCode", OLEConstants.BAL_TYP_CD);
424        searchMap.put("financialDocumentApprovedCode", OLEConstants.FDOC_APPR_CD);
425        List<GeneralLedgerPendingEntry> generalLedgerPendingEntryList = (List<GeneralLedgerPendingEntry>) SpringContext.getBean(
426                BusinessObjectService.class).findMatching(GeneralLedgerPendingEntry.class, searchMap);
427        KualiDecimal budgetDecrease = KualiDecimal.ZERO;
428        if (generalLedgerPendingEntryList.size() > 0) {
429            for (GeneralLedgerPendingEntry entry : generalLedgerPendingEntryList) {
430                if (entry.getTransactionLedgerEntryAmount().isLessThan(KualiDecimal.ZERO)) {
431                    budgetDecrease = budgetDecrease.add(entry.getTransactionLedgerEntryAmount());
432                }
433            }
434        }
435
436        return budgetDecrease.negated();
437    }
438
439
440    public KualiDecimal getBudgetAdjustmentIncreaseForAccount(String chartCode, String accountNo,
441                                                    String objectCode) {
442        Map searchMap = new HashMap();
443        searchMap.put("chartOfAccountsCode", chartCode);
444        searchMap.put("accountNumber", accountNo);
445        searchMap.put("financialDocumentTypeCode", OLEConstants.DOC_TYP_CD);
446        searchMap.put("financialBalanceTypeCode", OLEConstants.BAL_TYP_CD);
447        searchMap.put("financialDocumentApprovedCode", OLEConstants.FDOC_APPR_CD);
448        List<GeneralLedgerPendingEntry> generalLedgerPendingEntryList = (List<GeneralLedgerPendingEntry>) SpringContext.getBean(
449                BusinessObjectService.class).findMatching(GeneralLedgerPendingEntry.class, searchMap);
450        KualiDecimal budgetIncrease = KualiDecimal.ZERO;
451        if (generalLedgerPendingEntryList.size() > 0) {
452            for (GeneralLedgerPendingEntry entry : generalLedgerPendingEntryList) {
453                if (entry.getTransactionLedgerEntryAmount().isGreaterThan(KualiDecimal.ZERO)) {
454                    budgetIncrease = budgetIncrease.add(entry.getTransactionLedgerEntryAmount());
455                }
456            }
457        }
458
459        return budgetIncrease;
460    }
461
462    public KualiDecimal getBudgetAdjustmentDecreaseForAccount(String chartCode, String accountNo,
463                                                    String objectCode) {
464        Map searchMap = new HashMap();
465        searchMap.put("chartOfAccountsCode", chartCode);
466        searchMap.put("accountNumber", accountNo);
467        searchMap.put("financialDocumentTypeCode", OLEConstants.DOC_TYP_CD);
468        searchMap.put("financialBalanceTypeCode", OLEConstants.BAL_TYP_CD);
469        searchMap.put("financialDocumentApprovedCode", OLEConstants.FDOC_APPR_CD);
470        List<GeneralLedgerPendingEntry> generalLedgerPendingEntryList = (List<GeneralLedgerPendingEntry>) SpringContext.getBean(
471                BusinessObjectService.class).findMatching(GeneralLedgerPendingEntry.class, searchMap);
472        KualiDecimal budgetDecrease = KualiDecimal.ZERO;
473        if (generalLedgerPendingEntryList.size() > 0) {
474            for (GeneralLedgerPendingEntry entry : generalLedgerPendingEntryList) {
475                if (entry.getTransactionLedgerEntryAmount().isLessThan(KualiDecimal.ZERO)) {
476                    budgetDecrease = budgetDecrease.add(entry.getTransactionLedgerEntryAmount());
477                }
478            }
479        }
480
481        return budgetDecrease.negated();
482    }
483
484    public OleSelectDocumentService getOleSelectDocumentService() {
485        if(oleSelectDocumentService == null){
486            oleSelectDocumentService = SpringContext.getBean(OleSelectDocumentService.class);
487        }
488        return oleSelectDocumentService;
489    }
490
491    public void setOleSelectDocumentService(OleSelectDocumentService oleSelectDocumentService) {
492        this.oleSelectDocumentService = oleSelectDocumentService;
493    }
494
495    public String getParameter(String key) {
496        ParameterKey parameterKey = ParameterKey.create(org.kuali.ole.OLEConstants.APPL_ID_OLE, org.kuali.ole.OLEConstants.SELECT_NMSPC, org.kuali.ole.OLEConstants.SELECT_CMPNT, key);
497        Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
498        return parameter != null ? parameter.getValue() : null;
499    }
500
501    public String getPurchaseOrderTypes() {
502        List<PurchaseOrderType> purchaseOrderTypes = ( List<PurchaseOrderType>)getBusinessObjectService().findAll(PurchaseOrderType.class);
503        StringBuffer orderTypes = new StringBuffer();
504        if(purchaseOrderTypes.size() > 0) {
505            for(PurchaseOrderType purchaseOrderType : purchaseOrderTypes) {
506                orderTypes.append(purchaseOrderType.getPurchaseOrderTypeId().toString());
507                orderTypes.append(":");
508                orderTypes.append(purchaseOrderType.getPurchaseOrderType());
509                orderTypes.append(";");
510            }
511        }
512        return orderTypes.toString();
513    }
514
515    public BusinessObjectService getBusinessObjectService() {
516        if(businessObjectService == null) {
517            businessObjectService = SpringContext.getBean(BusinessObjectService.class);
518        }
519        return businessObjectService;
520    }
521
522}