View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.tem.document.validation.impl;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.kuali.kfs.module.tem.document.TravelDocument;
26  import org.kuali.kfs.sys.KFSConstants;
27  import org.kuali.kfs.sys.KFSKeyConstants;
28  import org.kuali.kfs.sys.KfsAuthorizationConstants;
29  import org.kuali.kfs.sys.document.AccountingDocument;
30  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
31  import org.kuali.kfs.sys.document.validation.impl.AccountingLineGroupTotalsUnchangedValidation;
32  import org.kuali.rice.core.web.format.CurrencyFormatter;
33  import org.kuali.rice.kim.api.identity.Person;
34  import org.kuali.rice.kns.document.authorization.TransactionalDocumentAuthorizer;
35  import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationController;
36  import org.kuali.rice.kns.service.DocumentHelperService;
37  import org.kuali.rice.krad.util.GlobalVariables;
38  
39  /**
40   * Mostly disallows totals on document from changing, but if user can change wire transfer or foreign draft, allows totals to change
41   */
42  public class TravelDocumentAccountingLineTotalsValidation extends AccountingLineGroupTotalsUnchangedValidation {
43      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TravelDocumentAccountingLineTotalsValidation.class);
44  
45      protected DocumentHelperService documentHelperService;
46  
47      /**
48       * @see org.kuali.kfs.sys.document.validation.impl.AccountingLineGroupTotalsUnchangedValidation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
49       */
50      @Override
51      public boolean validate(AttributedDocumentEvent event) {
52          if (LOG.isDebugEnabled()) {
53              LOG.debug("validate start");
54          }
55  
56          final TravelDocument travelDoc = (TravelDocument) event.getDocument();
57          final Person financialSystemUser = GlobalVariables.getUserSession().getPerson();
58          final Set<String> currentEditModes = getEditModesFromDocument(travelDoc, financialSystemUser);
59  
60          // amounts can only decrease
61          if (hasRequiredEditMode(currentEditModes, getCandidateEditModes())) {
62              //users in foreign or wire workgroup can increase or decrease amounts because of currency conversion
63              List<String> foreignDraftAndWireTransferEditModes = getForeignDraftAndWireTransferEditModes();
64              if (!this.hasRequiredEditMode(currentEditModes, foreignDraftAndWireTransferEditModes)) {
65                  TravelDocument persistedDocument = (TravelDocument) retrievePersistedDocument(travelDoc);
66                  if (persistedDocument == null) {
67                      handleNonExistentDocumentWhenApproving(travelDoc);
68                      return true;
69                  }
70                  // KFSMI- 5183
71                  if (persistedDocument.getDocumentHeader().getWorkflowDocument().isSaved() && persistedDocument.getTotalAccountLineAmount().isZero()) {
72                      return true;
73                  }
74  
75                  // check total cannot decrease
76                  if (!persistedDocument.getDocumentHeader().getWorkflowDocument().isCompletionRequested() && persistedDocument.getTotalAccountLineAmount().isLessThan(travelDoc.getTotalAccountLineAmount())) {
77                      final String persistedTotal = (String) new CurrencyFormatter().format(persistedDocument.getTotalAccountLineAmount());
78                      final String currentTotal = (String) new CurrencyFormatter().format(travelDoc.getTotalAccountLineAmount());
79                      GlobalVariables.getMessageMap().putError(KFSConstants.SOURCE_ACCOUNTING_LINE_ERRORS, KFSKeyConstants.ERROR_DOCUMENT_SINGLE_ACCOUNTING_LINE_SECTION_TOTAL_CHANGED, new String[] { persistedTotal, currentTotal });
80                      return false;
81                  }
82              }
83  
84              return true;
85          }
86  
87          return super.validate(event);
88      }
89  
90      /**
91       * determine whether the give user has permission to any edit mode defined in the given candidate edit modes
92       *
93       * @param currentEditModes the edit modes currently available to the given user on the document
94       * @param candidateEditEditModes the given candidate edit modes
95       * @return true if the give user has permission to any edit mode defined in the given candidate edit modes; otherwise, false
96       */
97      protected boolean hasRequiredEditMode(Set<String> currentEditModes, List<String> candidateEditModes) {
98          for (String editMode : candidateEditModes) {
99              if (currentEditModes.contains(editMode)) {
100                 return true;
101             }
102         }
103 
104         return false;
105     }
106 
107     /**
108      * Retrieves the current edit modes from the document
109      * @param accountingDocument the document to find edit modes of
110      * @param financialSystemUser the user requesting the edit modes
111      * @return the Set of current edit modes
112      */
113     protected Set<String> getEditModesFromDocument(AccountingDocument accountingDocument, Person financialSystemUser) {
114         final TransactionalDocumentAuthorizer documentAuthorizer = (TransactionalDocumentAuthorizer) getDocumentHelperService().getDocumentAuthorizer(accountingDocument);
115         final TransactionalDocumentPresentationController presentationController = (TransactionalDocumentPresentationController) getDocumentHelperService().getDocumentPresentationController(accountingDocument);
116 
117         final Set<String> presentationControllerEditModes = presentationController.getEditModes(accountingDocument);
118         final Set<String> editModes = documentAuthorizer.getEditModes(accountingDocument, financialSystemUser, presentationControllerEditModes);
119 
120         return editModes;
121     }
122 
123     /**
124      * define the possibly desired edit modes
125      *
126      * @return the possibly desired edit modes
127      */
128     protected List<String> getCandidateEditModes() {
129         List<String> candidateEdiModes = new ArrayList<String>();
130         candidateEdiModes.add(KfsAuthorizationConstants.TransactionalEditMode.FRN_ENTRY);
131         candidateEdiModes.add(KfsAuthorizationConstants.TransactionalEditMode.WIRE_ENTRY);
132 
133         return candidateEdiModes;
134     }
135 
136     /**
137      * get foreign draft And wire transfer edit mode names, as well as tax if the payee is a non-resident alien
138      * @param dvDocument the document we're validating
139      * @return foreign draft And wire transfer edit mode names
140      */
141     protected List<String> getForeignDraftAndWireTransferEditModes() {
142         List<String> foreignDraftAndWireTransferEditModes = new ArrayList<String>();
143         foreignDraftAndWireTransferEditModes.add(KfsAuthorizationConstants.TransactionalEditMode.FRN_ENTRY);
144         foreignDraftAndWireTransferEditModes.add(KfsAuthorizationConstants.TransactionalEditMode.WIRE_ENTRY);
145 
146         return foreignDraftAndWireTransferEditModes;
147     }
148 
149     public DocumentHelperService getDocumentHelperService() {
150         return documentHelperService;
151     }
152 
153     public void setDocumentHelperService(DocumentHelperService documentHelperService) {
154         this.documentHelperService = documentHelperService;
155     }
156 
157 }