View Javadoc
1   /*
2    * Copyright 2013 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.document.service.impl;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.ole.coa.businessobject.Account;
20  import org.kuali.ole.gl.businessobject.Balance;
21  import org.kuali.ole.module.purap.businessobject.InvoiceAccount;
22  import org.kuali.ole.module.purap.businessobject.InvoiceItem;
23  import org.kuali.ole.module.purap.businessobject.PaymentRequestAccount;
24  import org.kuali.ole.select.OleSelectNotificationConstant;
25  import org.kuali.ole.select.businessobject.OleInvoiceItem;
26  import org.kuali.ole.select.businessobject.OlePaymentRequestItem;
27  import org.kuali.ole.select.businessobject.OleSufficientFundCheck;
28  import org.kuali.ole.select.document.OleInvoiceDocument;
29  import org.kuali.ole.select.document.OlePaymentRequestDocument;
30  import org.kuali.ole.select.document.service.OleInvoiceFundCheckService;
31  import org.kuali.ole.select.document.service.OleInvoiceService;
32  import org.kuali.ole.select.document.service.OleSelectDocumentService;
33  import org.kuali.ole.sys.OLEConstants;
34  import org.kuali.ole.sys.OLEPropertyConstants;
35  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
36  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
37  import org.kuali.ole.sys.context.SpringContext;
38  import org.kuali.ole.sys.service.UniversityDateService;
39  import org.kuali.rice.core.api.config.property.ConfigurationService;
40  import org.kuali.rice.core.api.util.RiceKeyConstants;
41  import org.kuali.rice.core.api.util.type.KualiDecimal;
42  import org.kuali.rice.kew.api.WorkflowDocument;
43  import org.kuali.rice.kim.api.identity.PersonService;
44  import org.kuali.rice.krad.UserSession;
45  import org.kuali.rice.krad.service.BusinessObjectService;
46  import org.kuali.rice.krad.service.DocumentHeaderService;
47  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
48  import org.kuali.rice.krad.util.GlobalVariables;
49  
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Map;
53  
54  public class OleInvoiceFundCheckServiceImpl implements OleInvoiceFundCheckService {
55      protected static final Logger LOG = Logger.getLogger(OleInvoiceFundCheckServiceImpl.class);
56      protected ConfigurationService kualiConfigurationService;
57      private OleSelectDocumentService oleSelectDocumentService;
58  
59      @Override
60      public boolean hasSufficientFundCheckRequired(SourceAccountingLine accLine) {
61          boolean hasSufficientFundRequired = false;
62          Map searchMap = new HashMap();
63          Map<String, Object> key = new HashMap<String, Object>();
64          String chartCode = accLine.getChartOfAccountsCode();
65          String accNo = accLine.getAccountNumber();
66          String objectCd = accLine.getFinancialObjectCode();
67          String fundCodeType = getFundCode(chartCode, accNo);
68          KualiDecimal budgetAllocation = KualiDecimal.ZERO;
69          KualiDecimal expenseAmt = KualiDecimal.ZERO;
70          Map<String, Object> fundKey = new HashMap<String, Object>();
71          String notificationOption = null;
72          fundKey.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
73          fundKey.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
74          OleSufficientFundCheck account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
75                  OleSufficientFundCheck.class, fundKey);
76          if (account != null) {
77              notificationOption = account.getNotificationOption();
78          }
79          if (fundCodeType != null && fundCodeType.equals(OLEConstants.ACCOUNT_FUND_CODE)) {
80              if (notificationOption.equals(OLEPropertyConstants.BLOCK_USE)
81                      || notificationOption.equals(OLEPropertyConstants.WARNING_MSG)) {
82                  budgetAllocation = getBudgetAllocationForAccount(chartCode, accNo, objectCd);
83                  expenseAmt = getSumPaidInvoicesForAccount(chartCode, accNo, objectCd);
84                  budgetAllocation = budgetAllocation.subtract(expenseAmt);
85              } else if (notificationOption.equals(OLEPropertyConstants.BUD_REVIEW)
86                      || notificationOption.equals(OLEPropertyConstants.NOTIFICATION)) {
87                  budgetAllocation = getBudgetAllocationForAccount(chartCode, accNo, objectCd);
88                  expenseAmt = getSumPaidInvoicesForAccount(chartCode, accNo, objectCd);
89                  expenseAmt = expenseAmt.subtract(accLine.getAmount());
90                  budgetAllocation = budgetAllocation.subtract(expenseAmt);
91  
92              }
93              if (accLine.getAmount().isGreaterThan(budgetAllocation)) {
94                  hasSufficientFundRequired = true;
95              } else {
96                  hasSufficientFundRequired = false;
97              }
98          } else if (fundCodeType != null && fundCodeType.equals(OLEConstants.OBJECT_FUND_CODE)) {
99                  if (notificationOption.equals(OLEPropertyConstants.BLOCK_USE)
100                         || notificationOption.equals(OLEPropertyConstants.WARNING_MSG)) {
101                     budgetAllocation = getBudgetAllocationForObject(chartCode, accNo, objectCd);
102                     expenseAmt = getSumPaidInvoicesForObject(chartCode, accNo, objectCd);
103                     budgetAllocation = budgetAllocation.subtract(expenseAmt);
104                 } else if (notificationOption.equals(OLEPropertyConstants.BUD_REVIEW)
105                         || notificationOption.equals(OLEPropertyConstants.NOTIFICATION)) {
106                     budgetAllocation = getBudgetAllocationForObject(chartCode, accNo, objectCd);
107                     expenseAmt = getSumPaidInvoicesForObject(chartCode, accNo, objectCd);
108                     expenseAmt = expenseAmt.subtract(accLine.getAmount());
109                     budgetAllocation = budgetAllocation.subtract(expenseAmt);
110                 }
111             if (accLine.getAmount().isGreaterThan(budgetAllocation)) {
112                 hasSufficientFundRequired = true;
113             } else {
114                 hasSufficientFundRequired = false;
115             }
116         }
117         return hasSufficientFundRequired;
118     }
119 
120     private String getFundCode(String chartCode, String accountNumber) {
121         Map accountMap = new HashMap();
122         accountMap.put("chartOfAccountsCode", chartCode);
123         accountMap.put("accountNumber", accountNumber);
124         Account account = SpringContext.getBean(BusinessObjectService.class)
125                 .findByPrimaryKey(Account.class, accountMap);
126         boolean fundCheckIndication = false;
127         if (account != null) {
128             fundCheckIndication = account.isPendingAcctSufficientFundsIndicator();
129         }
130 
131         if (fundCheckIndication) {
132             String fundCode = account.getAccountSufficientFundsCode();
133             return fundCode;
134         }
135         return null;
136     }
137 
138     public KualiDecimal getSumPaidInvoicesForAccount(String chartCode, String accountNo, String objectCode) {
139         KualiDecimal paidInvoices = KualiDecimal.ZERO;
140         List<OleInvoiceDocument> invDocList = (List<OleInvoiceDocument>) SpringContext.getBean(
141                 BusinessObjectService.class).findAll(OleInvoiceDocument.class);
142         if (invDocList.size() > 0) {
143             for (OleInvoiceDocument oleInvoiceDocument : invDocList) {
144                 Integer payReqId = oleInvoiceDocument.getPurapDocumentIdentifier();
145                 Map docMap = new HashMap();
146                 docMap.put(OLEConstants.PUR_AP_IDEN, payReqId);
147                 docMap.put(OLEConstants.ITM_TYP_CD_KEY, OLEConstants.ITM_TYP_CD);
148                 List<OleInvoiceItem> itemList = (List<OleInvoiceItem>) SpringContext.getBean(
149                         BusinessObjectService.class).findMatching(OleInvoiceItem.class, docMap);
150                 HashMap acctMap = new HashMap();
151                 for (OleInvoiceItem oleInvoiceItem : itemList) {
152                     Integer itemIdentifier = oleInvoiceItem.getItemIdentifier();
153                     acctMap.put("itemIdentifier", oleInvoiceItem.getItemIdentifier());
154                     acctMap.put("chartOfAccountsCode", chartCode);
155                     acctMap.put("accountNumber", accountNo);
156                     List<InvoiceAccount> oleInvoiceAccount = (List<InvoiceAccount>) SpringContext
157                             .getBean(BusinessObjectService.class).findMatching(InvoiceAccount.class, acctMap);
158                     for (InvoiceAccount invAcct : oleInvoiceAccount) {
159                         try {
160                             OleInvoiceDocument paidInvDoc =  SpringContext.getBean(
161                                     BusinessObjectService.class).findBySinglePrimaryKey(OleInvoiceDocument.class, oleInvoiceItem.getPurapDocumentIdentifier());
162 
163                             if (paidInvDoc.getDocumentHeader().getDocumentNumber() == null) {
164                                 paidInvDoc.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(paidInvDoc.getDocumentNumber()));
165                             }
166                             WorkflowDocument workflowDocument = null;
167                             //GlobalVariables.setUserSession(new UserSession("ole-khuntley"));
168                             String user = null;
169                             if (GlobalVariables.getUserSession() == null) {
170                                 kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
171                                 user = kualiConfigurationService.getPropertyValueAsString(getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
172                                 GlobalVariables.setUserSession(new UserSession(user));
173                             }
174 
175 
176                             workflowDocument = KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(paidInvDoc.getDocumentNumber(), SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId()));
177                             if(!(workflowDocument.isCanceled() || workflowDocument.isDisapproved() || workflowDocument.isException())) {
178                                 paidInvoices = paidInvoices.add(invAcct.getAmount());
179                             }
180                         }
181                         catch(Exception e) {
182                             LOG.error("Exception while calculating the Sufficient Fund for Invoice Document"+e);
183                             throw new RuntimeException(e);
184                         }
185                     }
186                 }
187             }
188         }
189 
190         List<OlePaymentRequestDocument> payDocList = (List<OlePaymentRequestDocument>) SpringContext.getBean(
191                 BusinessObjectService.class).findAll(OlePaymentRequestDocument.class);
192         if (payDocList.size() > 0) {
193             for (OlePaymentRequestDocument olePaymentRequestDocument : payDocList) {
194                 Integer invIdentifier = olePaymentRequestDocument.getInvoiceIdentifier();
195                 Integer payReqId = olePaymentRequestDocument.getPurapDocumentIdentifier();
196                 if (invIdentifier == null) {
197                     Map docMap = new HashMap();
198                     docMap.put(OLEConstants.PUR_AP_IDEN, payReqId);
199                     docMap.put(OLEConstants.ITM_TYP_CD_KEY, OLEConstants.ITM_TYP_CD);
200                     List<OlePaymentRequestItem> itemList = (List<OlePaymentRequestItem>) SpringContext.getBean(
201                             BusinessObjectService.class).findMatching(OlePaymentRequestItem.class, docMap);
202                     HashMap acctMap = new HashMap();
203                     for (OlePaymentRequestItem olePaymentRequestItem : itemList) {
204                         Integer itemIdentifier = olePaymentRequestItem.getItemIdentifier();
205                         acctMap.put("itemIdentifier", olePaymentRequestItem.getItemIdentifier());
206                         acctMap.put("chartOfAccountsCode", chartCode);
207                         acctMap.put("accountNumber", accountNo);
208                         List<PaymentRequestAccount> olePaymentRequestAccount = (List<PaymentRequestAccount>) SpringContext
209                                 .getBean(BusinessObjectService.class).findMatching(PaymentRequestAccount.class, acctMap);
210                         for (PaymentRequestAccount payReqAcct : olePaymentRequestAccount) {
211                             try {
212                                 OlePaymentRequestDocument paidPaymentDoc =  SpringContext.getBean(
213                                         BusinessObjectService.class).findBySinglePrimaryKey(OlePaymentRequestDocument.class, olePaymentRequestItem.getPurapDocumentIdentifier());
214 
215                                 if (paidPaymentDoc.getDocumentHeader().getDocumentNumber() == null) {
216                                     paidPaymentDoc.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(paidPaymentDoc.getDocumentNumber()));
217                                 }
218                                 WorkflowDocument workflowDocument = null;
219                                 //GlobalVariables.setUserSession(new UserSession("ole-khuntley"));
220                                 String user = null;
221                                 if (GlobalVariables.getUserSession() == null) {
222                                     kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
223                                     user = kualiConfigurationService.getPropertyValueAsString(getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
224                                     GlobalVariables.setUserSession(new UserSession(user));
225                                 }
226 
227                                 workflowDocument = KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(paidPaymentDoc.getDocumentNumber(), SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId()));
228                                 if(!(workflowDocument.isCanceled() || workflowDocument.isDisapproved() || workflowDocument.isException())) {
229                                     paidInvoices = paidInvoices.add(payReqAcct.getAmount());
230                                 }
231                             }
232                             catch(Exception e) {
233                                 LOG.error("Exception while calculating the Sufficient Fund for Invoice Document"+e);
234                                 throw new RuntimeException(e);
235                             }
236                         }
237                     }
238                 }
239             }
240         }
241 
242 
243         return paidInvoices;
244     }
245 
246 
247     public KualiDecimal getSumPaidInvoicesForObject(String chartCode, String accountNo, String objectCode) {
248         KualiDecimal paidInvoices = KualiDecimal.ZERO;
249         List<OleInvoiceDocument> invDocList = (List<OleInvoiceDocument>) SpringContext.getBean(
250                 BusinessObjectService.class).findAll(OleInvoiceDocument.class);
251         if (invDocList.size() > 0) {
252             for (OleInvoiceDocument oleInvoiceDocument : invDocList) {
253                 Integer payReqId = oleInvoiceDocument.getPurapDocumentIdentifier();
254                 Map docMap = new HashMap();
255                 docMap.put(OLEConstants.PUR_AP_IDEN, payReqId);
256                 docMap.put(OLEConstants.ITM_TYP_CD_KEY, OLEConstants.ITM_TYP_CD);
257                 List<OleInvoiceItem> itemList = (List<OleInvoiceItem>) SpringContext.getBean(
258                         BusinessObjectService.class).findMatching(OleInvoiceItem.class, docMap);
259                 HashMap acctMap = new HashMap();
260                 for (OleInvoiceItem oleInvoiceItem : itemList) {
261                     acctMap.put("itemIdentifier", oleInvoiceItem.getItemIdentifier());
262                     acctMap.put("chartOfAccountsCode", chartCode);
263                     acctMap.put("accountNumber", accountNo);
264                     acctMap.put("financialObjectCode", objectCode);
265                     List<InvoiceAccount> oleInvoiceAccount = (List<InvoiceAccount>) SpringContext
266                             .getBean(BusinessObjectService.class).findMatching(InvoiceAccount.class, acctMap);
267                     for (InvoiceAccount invAcct : oleInvoiceAccount) {
268 
269                         try {
270                                 OleInvoiceDocument paidInvDoc =  SpringContext.getBean(
271                                         BusinessObjectService.class).findBySinglePrimaryKey(OleInvoiceDocument.class, oleInvoiceItem.getPurapDocumentIdentifier());
272 
273                             if (paidInvDoc.getDocumentHeader().getDocumentNumber() == null) {
274                                 paidInvDoc.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(paidInvDoc.getDocumentNumber()));
275                             }
276                                 WorkflowDocument workflowDocument = null;
277                                 //lobalVariables.setUserSession(new UserSession("ole-khuntley"));
278                             String user = null;
279                             if (GlobalVariables.getUserSession() == null) {
280                                 kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
281                                 user = kualiConfigurationService.getPropertyValueAsString(getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
282                                 GlobalVariables.setUserSession(new UserSession(user));
283                             }
284                                 workflowDocument = KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(paidInvDoc.getDocumentNumber(), SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId()));
285                             if(!(workflowDocument.isCanceled() || workflowDocument.isDisapproved() || workflowDocument.isException())) {
286                                     paidInvoices = paidInvoices.add(invAcct.getAmount());
287                             }
288                         }
289                             catch(Exception e) {
290                                 LOG.error("Exception while calculating the Sufficient Fund for Invoice Document"+e);
291                                 throw new RuntimeException(e);
292                         }
293                      }
294                 }
295             }
296 
297         }
298 
299 
300         List<OlePaymentRequestDocument> payDocList = (List<OlePaymentRequestDocument>) SpringContext.getBean(
301                 BusinessObjectService.class).findAll(OlePaymentRequestDocument.class);
302         if (payDocList.size() > 0) {
303             for (OlePaymentRequestDocument olePaymentRequestDocument : payDocList) {
304                 Integer invIdentifier = olePaymentRequestDocument.getInvoiceIdentifier();
305                 Integer payReqId = olePaymentRequestDocument.getPurapDocumentIdentifier();
306                 if (invIdentifier == null) {
307                     Map docMap = new HashMap();
308                     docMap.put(OLEConstants.PUR_AP_IDEN, payReqId);
309                     docMap.put(OLEConstants.ITM_TYP_CD_KEY, OLEConstants.ITM_TYP_CD);
310                     List<OlePaymentRequestItem> itemList = (List<OlePaymentRequestItem>) SpringContext.getBean(
311                             BusinessObjectService.class).findMatching(OlePaymentRequestItem.class, docMap);
312                     HashMap acctMap = new HashMap();
313                     for (OlePaymentRequestItem olePaymentRequestItem : itemList) {
314                         acctMap.put("itemIdentifier", olePaymentRequestItem.getItemIdentifier());
315                         acctMap.put("chartOfAccountsCode", chartCode);
316                         acctMap.put("accountNumber", accountNo);
317                         acctMap.put("financialObjectCode", objectCode);
318                         List<PaymentRequestAccount> olePaymentRequestAccount = (List<PaymentRequestAccount>) SpringContext
319                                 .getBean(BusinessObjectService.class).findMatching(PaymentRequestAccount.class, acctMap);
320                         for (PaymentRequestAccount payReqAcct : olePaymentRequestAccount) {
321                             try {
322                                 OlePaymentRequestDocument paidPaymentDoc =  SpringContext.getBean(
323                                         BusinessObjectService.class).findBySinglePrimaryKey(OlePaymentRequestDocument.class, olePaymentRequestItem.getPurapDocumentIdentifier());
324 
325                                 if (paidPaymentDoc.getDocumentHeader().getDocumentNumber() == null) {
326                                     paidPaymentDoc.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(paidPaymentDoc.getDocumentNumber()));
327                                 }
328                                 WorkflowDocument workflowDocument = null;
329                                 //GlobalVariables.setUserSession(new UserSession("ole-khuntley"));
330                                 String user = null;
331                                 if (GlobalVariables.getUserSession() == null) {
332                                     kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
333                                     user = kualiConfigurationService.getPropertyValueAsString(getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
334                                     GlobalVariables.setUserSession(new UserSession(user));
335                                 }
336                                 workflowDocument = KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(paidPaymentDoc.getDocumentNumber(), SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId()));
337                                 if(!(workflowDocument.isCanceled() || workflowDocument.isDisapproved() || workflowDocument.isException())) {
338                                     paidInvoices = paidInvoices.add(payReqAcct.getAmount());
339                                 }
340                             }
341                             catch(Exception e) {
342                                 LOG.error("Exception while calculating the Sufficient Fund for Invoice Document"+e);
343                                 throw new RuntimeException(e);
344                             }
345                         }
346                     }
347                 }
348             }
349         }
350         return paidInvoices;
351     }
352 
353     private KualiDecimal getBudgetAllocationForObject(String chartCode, String accountNumber, String objectCode) {
354         Map budgetMap = new HashMap();
355         UniversityDateService universityDateService = SpringContext.getBean(UniversityDateService.class);
356         budgetMap.put("universityFiscalYear",
357                 universityDateService.getCurrentUniversityDate().getUniversityFiscalYear());
358         budgetMap.put("chartOfAccountsCode", chartCode);
359         budgetMap.put("accountNumber", accountNumber);
360         budgetMap.put("objectCode", objectCode);
361         budgetMap.put(OLEPropertyConstants.BALANCE_TYPE_CODE, OLEPropertyConstants.BAL_TYP_CODE);
362         Balance balance = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(Balance.class, budgetMap);
363         KualiDecimal initialBudgetAmount = KualiDecimal.ZERO;
364         if (balance != null) {
365             initialBudgetAmount = balance.getAccountLineAnnualBalanceAmount();
366         }
367         Map encMap = new HashMap();
368         encMap.put("chartOfAccountsCode", chartCode);
369         encMap.put("accountNumber", accountNumber);
370         OleSufficientFundCheck oleSufficientFundCheck = SpringContext.getBean(BusinessObjectService.class)
371                 .findByPrimaryKey(OleSufficientFundCheck.class, encMap);
372         KualiDecimal amount = KualiDecimal.ZERO;
373         if (oleSufficientFundCheck != null) {
374             if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_PERCENTAGE.equals(oleSufficientFundCheck
375                     .getEncumbExpenseConstraintType())) {
376                 amount = new KualiDecimal(oleSufficientFundCheck.getExpenseAmount());
377                 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
378                         .getEncumbExpenseMethod())) {
379                     initialBudgetAmount = initialBudgetAmount.add((initialBudgetAmount.multiply(amount))
380                             .divide(new KualiDecimal(100)));
381                 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
382                         .getEncumbExpenseMethod())) {
383                     initialBudgetAmount = initialBudgetAmount.subtract((initialBudgetAmount.multiply(amount))
384                             .divide(new KualiDecimal(100)));
385 
386                 }
387             } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_CASH.equals(oleSufficientFundCheck
388                     .getEncumbExpenseConstraintType())) {
389                 amount = new KualiDecimal(oleSufficientFundCheck.getExpenseAmount());
390                 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
391                         .getEncumbExpenseMethod())) {
392                     initialBudgetAmount = initialBudgetAmount.add(amount);
393                 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
394                         .getEncumbExpenseMethod())) {
395                     initialBudgetAmount = initialBudgetAmount.subtract(amount);
396                 }
397             }
398         }
399         KualiDecimal budgetIncrease = getBudgetAdjustmentIncreaseForObject(chartCode,accountNumber,objectCode);
400         KualiDecimal budgetDecrease = getBudgetAdjustmentDecreaseForObject(chartCode,accountNumber,objectCode);
401         return initialBudgetAmount.add(budgetIncrease).subtract(budgetDecrease);
402     }
403 
404     private KualiDecimal getBudgetAllocationForAccount(String chartCode, String accountNumber, String objectCode) {
405         Map budgetMap = new HashMap();
406         KualiDecimal initialBudgetAmount = KualiDecimal.ZERO;
407         UniversityDateService universityDateService = SpringContext.getBean(UniversityDateService.class);
408         budgetMap.put("universityFiscalYear",
409                 universityDateService.getCurrentUniversityDate().getUniversityFiscalYear());
410         budgetMap.put("chartOfAccountsCode", chartCode);
411         budgetMap.put("accountNumber", accountNumber);
412         budgetMap.put(OLEPropertyConstants.BALANCE_TYPE_CODE, OLEPropertyConstants.BAL_TYP_CODE);
413         List<Balance> balance = (List<Balance>) SpringContext.getBean(BusinessObjectService.class).findMatching(
414                 Balance.class, budgetMap);
415         if (balance.size() > 0) {
416             for (Balance budgetBalance : balance) {
417                 initialBudgetAmount = initialBudgetAmount.add(budgetBalance.getAccountLineAnnualBalanceAmount());
418             }
419         }
420         Map encMap = new HashMap();
421         encMap.put("chartOfAccountsCode", chartCode);
422         encMap.put("accountNumber", accountNumber);
423         OleSufficientFundCheck oleSufficientFundCheck = SpringContext.getBean(BusinessObjectService.class)
424                 .findByPrimaryKey(OleSufficientFundCheck.class, encMap);
425         KualiDecimal amount = KualiDecimal.ZERO;
426         if (oleSufficientFundCheck != null) {
427             if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_PERCENTAGE.equals(oleSufficientFundCheck
428                     .getEncumbExpenseConstraintType())) {
429                 amount = new KualiDecimal(oleSufficientFundCheck.getExpenseAmount());
430                 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
431                         .getEncumbExpenseMethod())) {
432                     initialBudgetAmount = initialBudgetAmount.add((initialBudgetAmount.multiply(amount))
433                             .divide(new KualiDecimal(100)));
434                 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
435                         .getEncumbExpenseMethod())) {
436                     initialBudgetAmount = initialBudgetAmount.subtract((initialBudgetAmount.multiply(amount))
437                             .divide(new KualiDecimal(100)));
438 
439                 }
440             } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_CASH.equals(oleSufficientFundCheck
441                     .getEncumbExpenseConstraintType())) {
442                 amount = new KualiDecimal(oleSufficientFundCheck.getExpenseAmount());
443                 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
444                         .getEncumbExpenseMethod())) {
445                     initialBudgetAmount = initialBudgetAmount.add(amount);
446                 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
447                         .getEncumbExpenseMethod())) {
448                     initialBudgetAmount = initialBudgetAmount.subtract(amount);
449                 }
450             }
451         }
452         KualiDecimal budgetIncrease = getBudgetAdjustmentIncreaseForAccount(chartCode,accountNumber,objectCode);
453         KualiDecimal budgetDecrease = getBudgetAdjustmentDecreaseForAccount(chartCode,accountNumber,objectCode);
454         return initialBudgetAmount.add(budgetIncrease).subtract(budgetDecrease);
455     }
456 
457     public boolean isBudgetReviewRequired(OleInvoiceDocument oleInvoiceDocument) {
458     	
459     	 if((SpringContext.getBean(OleInvoiceService.class).getPaymentMethodType(oleInvoiceDocument.getPaymentMethodIdentifier())).equals(OLEConstants.DEPOSIT)) {
460              return false;
461          }
462         List<SourceAccountingLine> sourceAccountingLineList = oleInvoiceDocument.getSourceAccountingLines();
463         boolean sufficientFundCheck = false;
464         for (SourceAccountingLine accLine : sourceAccountingLineList) {
465             Map searchMap = new HashMap();
466             String notificationOption = null;
467             Map<String, Object> key = new HashMap<String, Object>();
468             String chartCode = accLine.getChartOfAccountsCode();
469             String accNo = accLine.getAccountNumber();
470             key.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
471             key.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
472             OleSufficientFundCheck account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
473                     OleSufficientFundCheck.class, key);
474             if (account != null) {
475                 notificationOption = account.getNotificationOption();
476             }
477             if (notificationOption != null && notificationOption.equals(OLEPropertyConstants.BLOCK_USE)) {
478                 sufficientFundCheck = hasSufficientFundCheckRequired(accLine);
479                 if (sufficientFundCheck) {
480                     GlobalVariables.getMessageMap().putError(
481                             OLEConstants.SufficientFundCheck.ERROR_MSG_FOR_INSUFF_FUND, RiceKeyConstants.ERROR_CUSTOM,
482                             OLEConstants.SufficientFundCheck.INSUFF_FUND_INV + accLine.getAccountNumber());
483                     return sufficientFundCheck;
484                 }
485             }
486         }
487 
488         return sufficientFundCheck;
489     }
490     public KualiDecimal getBudgetAdjustmentIncreaseForObject(String chartCode, String accountNo,
491                                                              String objectCode) {
492         Map searchMap = new HashMap();
493         searchMap.put("chartOfAccountsCode", chartCode);
494         searchMap.put("accountNumber", accountNo);
495         searchMap.put("financialObjectCode", objectCode);
496         searchMap.put("financialDocumentTypeCode", OLEConstants.DOC_TYP_CD);
497         searchMap.put("financialBalanceTypeCode", OLEConstants.BAL_TYP_CD);
498         searchMap.put("financialDocumentApprovedCode", OLEConstants.FDOC_APPR_CD);
499         List<GeneralLedgerPendingEntry> generalLedgerPendingEntryList = (List<GeneralLedgerPendingEntry>) SpringContext.getBean(
500                 BusinessObjectService.class).findMatching(GeneralLedgerPendingEntry.class, searchMap);
501         KualiDecimal budgetIncrease = KualiDecimal.ZERO;
502         if (generalLedgerPendingEntryList.size() > 0) {
503             for (GeneralLedgerPendingEntry entry : generalLedgerPendingEntryList) {
504                 if (entry.getTransactionLedgerEntryAmount().isGreaterThan(KualiDecimal.ZERO)) {
505                     budgetIncrease = budgetIncrease.add(entry.getTransactionLedgerEntryAmount());
506                 }
507             }
508         }
509 
510         return budgetIncrease;
511     }
512 
513     public KualiDecimal getBudgetAdjustmentDecreaseForObject(String chartCode, String accountNo,
514                                                              String objectCode) {
515         Map searchMap = new HashMap();
516         searchMap.put("chartOfAccountsCode", chartCode);
517         searchMap.put("accountNumber", accountNo);
518         searchMap.put("financialObjectCode", objectCode);
519         searchMap.put("financialDocumentTypeCode", OLEConstants.DOC_TYP_CD);
520         searchMap.put("financialBalanceTypeCode", OLEConstants.BAL_TYP_CD);
521         searchMap.put("financialDocumentApprovedCode", OLEConstants.FDOC_APPR_CD);
522         List<GeneralLedgerPendingEntry> generalLedgerPendingEntryList = (List<GeneralLedgerPendingEntry>) SpringContext.getBean(
523                 BusinessObjectService.class).findMatching(GeneralLedgerPendingEntry.class, searchMap);
524         KualiDecimal budgetDecrease = KualiDecimal.ZERO;
525         if (generalLedgerPendingEntryList.size() > 0) {
526             for (GeneralLedgerPendingEntry entry : generalLedgerPendingEntryList) {
527                 if (entry.getTransactionLedgerEntryAmount().isLessThan(KualiDecimal.ZERO)) {
528                     budgetDecrease = budgetDecrease.add(entry.getTransactionLedgerEntryAmount());
529                 }
530             }
531         }
532 
533         return budgetDecrease.negated();
534     }
535 
536 
537     public KualiDecimal getBudgetAdjustmentIncreaseForAccount(String chartCode, String accountNo,
538                                                               String objectCode) {
539         Map searchMap = new HashMap();
540         searchMap.put("chartOfAccountsCode", chartCode);
541         searchMap.put("accountNumber", accountNo);
542         searchMap.put("financialDocumentTypeCode", OLEConstants.DOC_TYP_CD);
543         searchMap.put("financialBalanceTypeCode", OLEConstants.BAL_TYP_CD);
544         searchMap.put("financialDocumentApprovedCode", OLEConstants.FDOC_APPR_CD);
545         List<GeneralLedgerPendingEntry> generalLedgerPendingEntryList = (List<GeneralLedgerPendingEntry>) SpringContext.getBean(
546                 BusinessObjectService.class).findMatching(GeneralLedgerPendingEntry.class, searchMap);
547         KualiDecimal budgetIncrease = KualiDecimal.ZERO;
548         if (generalLedgerPendingEntryList.size() > 0) {
549             for (GeneralLedgerPendingEntry entry : generalLedgerPendingEntryList) {
550                 if (entry.getTransactionLedgerEntryAmount().isGreaterThan(KualiDecimal.ZERO)) {
551                     budgetIncrease = budgetIncrease.add(entry.getTransactionLedgerEntryAmount());
552                 }
553             }
554         }
555 
556         return budgetIncrease;
557     }
558 
559     public KualiDecimal getBudgetAdjustmentDecreaseForAccount(String chartCode, String accountNo,
560                                                               String objectCode) {
561         Map searchMap = new HashMap();
562         searchMap.put("chartOfAccountsCode", chartCode);
563         searchMap.put("accountNumber", accountNo);
564         searchMap.put("financialDocumentTypeCode", OLEConstants.DOC_TYP_CD);
565         searchMap.put("financialBalanceTypeCode", OLEConstants.BAL_TYP_CD);
566         searchMap.put("financialDocumentApprovedCode", OLEConstants.FDOC_APPR_CD);
567         List<GeneralLedgerPendingEntry> generalLedgerPendingEntryList = (List<GeneralLedgerPendingEntry>) SpringContext.getBean(
568                 BusinessObjectService.class).findMatching(GeneralLedgerPendingEntry.class, searchMap);
569         KualiDecimal budgetDecrease = KualiDecimal.ZERO;
570         if (generalLedgerPendingEntryList.size() > 0) {
571             for (GeneralLedgerPendingEntry entry : generalLedgerPendingEntryList) {
572                 if (entry.getTransactionLedgerEntryAmount().isLessThan(KualiDecimal.ZERO)) {
573                     budgetDecrease = budgetDecrease.add(entry.getTransactionLedgerEntryAmount());
574                 }
575             }
576         }
577 
578         return budgetDecrease.negated();
579     }
580 
581     public OleSelectDocumentService getOleSelectDocumentService() {
582         if(oleSelectDocumentService == null){
583             oleSelectDocumentService = SpringContext.getBean(OleSelectDocumentService.class);
584         }
585         return oleSelectDocumentService;
586     }
587 
588     public void setOleSelectDocumentService(OleSelectDocumentService oleSelectDocumentService) {
589         this.oleSelectDocumentService = oleSelectDocumentService;
590     }
591 
592 }