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.kuali.ole.module.purap.PurapConstants;
19  import org.kuali.ole.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
21  import org.kuali.ole.module.purap.service.PurapAccountingService;
22  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
23  import org.kuali.ole.sys.document.validation.GenericValidation;
24  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  public class PurchasingAccountsPayableCheckNegativeAccountsValidation extends GenericValidation {
32  
33      private PurapAccountingService purapAccountingService;
34  
35      public boolean validate(AttributedDocumentEvent event) {
36          boolean valid = true;
37          PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) event.getDocument();
38  
39          GlobalVariables.getMessageMap().clearErrorPath();
40          //GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
41  
42          // if this was set somewhere on the doc(for later use) in prepare for save we could avoid this call
43          purapAccountingService.updateAccountAmounts(document);
44  
45          //be sure to exclude trade in values from the negative check as they're allowed to be negative
46          Set excludedItemTypeCodes = new HashSet();
47          excludedItemTypeCodes.add(PurapConstants.ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE);
48          List<SourceAccountingLine> sourceLines = purapAccountingService.generateSummaryExcludeItemTypes(document.getItems(), excludedItemTypeCodes);
49  
50          for (SourceAccountingLine sourceAccountingLine : sourceLines) {
51              // check if the summary account is for tax withholding
52              boolean isTaxAccount = purapAccountingService.isTaxAccount(document, sourceAccountingLine);
53  
54              // exclude tax withholding accounts from non-negative requirement
55              if (!isTaxAccount && sourceAccountingLine.getAmount().isNegative()) {
56  
57                  String subAccountNumber = (sourceAccountingLine.getSubAccountNumber() == null) ? "" : sourceAccountingLine.getSubAccountNumber();
58                  String subObjectCode = (sourceAccountingLine.getFinancialSubObjectCode() == null) ? "" : sourceAccountingLine.getFinancialSubObjectCode();
59                  String projCode = (sourceAccountingLine.getProjectCode() == null) ? "" : sourceAccountingLine.getProjectCode();
60                  String orgRefId = (sourceAccountingLine.getOrganizationReferenceId() == null) ? "" : sourceAccountingLine.getOrganizationReferenceId();
61  
62                  String accountString = sourceAccountingLine.getChartOfAccountsCode() + " - " + sourceAccountingLine.getAccountNumber() + " - " + subAccountNumber + " - " + sourceAccountingLine.getFinancialObjectCode() + " - " + subObjectCode + " - " + projCode + " - " + orgRefId;
63                  GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ACCOUNT_AMOUNT_TOTAL, accountString, sourceAccountingLine.getAmount() + "");
64                  valid &= false;
65              }
66          }
67  
68          GlobalVariables.getMessageMap().clearErrorPath();
69          return valid;
70      }
71  
72      public PurapAccountingService getPurapAccountingService() {
73          return purapAccountingService;
74      }
75  
76      public void setPurapAccountingService(PurapAccountingService purapAccountingService) {
77          this.purapAccountingService = purapAccountingService;
78      }
79  
80  }