View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.module.purap.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapConstants;
20  import org.kuali.ole.module.purap.PurapConstants.InvoiceStatuses;
21  import org.kuali.ole.module.purap.PurapKeyConstants;
22  import org.kuali.ole.module.purap.PurapParameterConstants;
23  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
24  import org.kuali.ole.module.purap.businessobject.PurApItem;
25  import org.kuali.ole.module.purap.document.InvoiceDocument;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.document.AccountingDocument;
28  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29  import org.kuali.ole.sys.service.impl.OleParameterConstants;
30  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
31  import org.kuali.rice.krad.service.DataDictionaryService;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  
35  public class InvoiceNonZeroAccountingLineAmountValidation extends PurchasingAccountsPayableAccountingLineAccessibleValidation {
36  
37      private PurApItem itemForValidation;
38      private ParameterService parameterService;
39  
40      public boolean validate(AttributedDocumentEvent event) {
41          boolean valid = true;
42          String status = ((InvoiceDocument) event.getDocument()).getApplicationDocumentStatus();
43  
44          AccountingDocument accountingDocument = (AccountingDocument) event.getDocument();
45          this.setAccountingDocumentForValidation(accountingDocument);
46          this.setDataDictionaryService(SpringContext.getBean(DataDictionaryService.class));
47  
48          //Do this for AFOA only
49          if (StringUtils.equals(InvoiceStatuses.APPDOC_AWAITING_FISCAL_REVIEW, status)) {
50              for (PurApAccountingLine acct : itemForValidation.getSourceAccountingLines()) {
51                  this.setAccountingLineForValidation(acct);
52                  final boolean lineIsAccessible = lookupAccountingLineAuthorizer().hasEditPermissionOnAccountingLine(accountingDocument, acct, getAccountingLineCollectionProperty(), GlobalVariables.getUserSession().getPerson(), true);
53                  // if the current logged in user has edit permissions on this line then validate the line..
54                  if (lineIsAccessible) {
55                      //if amount is null, throw an error.  If amount is zero, check system parameter and determine
56                      //if the line can be approved.
57                      if (ObjectUtils.isNull(acct.getAmount())) {
58                          GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_AMOUNT_INVALID, itemForValidation.getItemIdentifierString());
59                          valid &= false;
60                      } else {
61                          if (acct.getAmount().isZero()) {
62                              if (!canApproveAccountingLinesWithZeroAmount()) {
63                                  GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_AMOUNT_INVALID, itemForValidation.getItemIdentifierString());
64                                  valid &= false;
65                              }
66                          }
67                      }
68                  }
69              }
70          }
71  
72          return valid;
73      }
74  
75      /**
76       * checks if an accounting line with zero dollar amount can be approved.  This will check
77       * the system parameter APPROVE_ACCOUNTING_LINES_WITH_ZERO_DOLLAR_AMOUNT_IND and determines if the
78       * line can be approved or not.
79       *
80       * @return true if the system parameter value is Y else returns N.
81       */
82      public boolean canApproveAccountingLinesWithZeroAmount() {
83          boolean canApproveLine = false;
84  
85          // get parameter to see if accounting line with zero dollar amount can be approved.
86          String approveZeroAmountLine = SpringContext.getBean(ParameterService.class).getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapParameterConstants.APPROVE_ACCOUNTING_LINES_WITH_ZERO_DOLLAR_AMOUNT_IND);
87  
88          if ("Y".equalsIgnoreCase(approveZeroAmountLine)) {
89              return true;
90          }
91  
92          return canApproveLine;
93      }
94  
95      public PurApItem getItemForValidation() {
96          return itemForValidation;
97      }
98  
99      public void setItemForValidation(PurApItem itemForValidation) {
100         this.itemForValidation = itemForValidation;
101     }
102 
103     /**
104      * Gets the parameterService attribute.
105      *
106      * @return Returns the parameterService
107      */
108 
109     public ParameterService getParameterService() {
110         return parameterService;
111     }
112 
113     /**
114      * Sets the parameterService attribute.
115      *
116      * @param parameterService The parameterService to set.
117      */
118     public void setParameterService(ParameterService parameterService) {
119         this.parameterService = parameterService;
120     }
121 }