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