View Javadoc
1   /*
2    * Copyright 2007 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.PurapKeyConstants;
21  import org.kuali.ole.module.purap.document.AccountsPayableDocument;
22  import org.kuali.ole.module.purap.document.service.PurapService;
23  import org.kuali.ole.sys.OLEConstants;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.rice.core.api.config.property.ConfigurationService;
26  import org.kuali.rice.krad.document.Document;
27  
28  /**
29   * Performs prompts and other pre business rule checks for the Accounts Payable Document (and its children).
30   */
31  public abstract class AccountsPayableDocumentPreRulesBase extends PurapDocumentPreRulesBase {
32      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountsPayableDocumentPreRulesBase.class);
33  
34      public AccountsPayableDocumentPreRulesBase() {
35          super();
36      }
37  
38      /**
39       * Asks for an override if the document hasn't reached full entry and the entered amount does not
40       * match the total amount of all items.
41       *
42       * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.krad.document.Document)
43       */
44      @Override
45      public boolean doPrompts(Document document) {
46  
47          boolean preRulesOK = true;
48  
49          AccountsPayableDocument accountsPayableDocument = (AccountsPayableDocument) document;
50  
51          // Ask the nomatch question if the document hasn't been completed.
52          if (SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted(accountsPayableDocument) == false) {
53              preRulesOK = confirmInvoiceNoMatchOverride(accountsPayableDocument);
54          } else if (SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted(accountsPayableDocument)) {
55              // if past full document entry complete, then set override to true to skip validation
56              accountsPayableDocument.setUnmatchedOverride(true);
57          }
58  
59          return preRulesOK;
60      }
61  
62      /**
63       * Checks whether the invoice from the initial screen and the document invoice are mismatched. If so, it prompts the
64       * user for confirmation to proceed.
65       *
66       * @param accountsPayableDocument - document to have its invoice/totals checked
67       * @return
68       */
69      protected boolean confirmInvoiceNoMatchOverride(AccountsPayableDocument accountsPayableDocument) {
70  
71          // If the values are mismatched, ask for confirmation.
72          if (validateInvoiceTotalsAreMismatched(accountsPayableDocument)) {
73  
74              String questionText = createInvoiceNoMatchQuestionText(accountsPayableDocument);
75  
76              boolean confirmOverride = super.askOrAnalyzeYesNoQuestion(PurapConstants.AP_OVERRIDE_INVOICE_NOMATCH_QUESTION, questionText);
77  
78              // Set a marker to record that this method has been used.
79              if (confirmOverride && StringUtils.isBlank(event.getQuestionContext())) {
80                  event.setQuestionContext(PurapConstants.AP_OVERRIDE_INVOICE_NOMATCH_QUESTION);
81                  accountsPayableDocument.setUnmatchedOverride(true);
82              }
83  
84              if (!confirmOverride) {
85                  event.setActionForwardName(OLEConstants.MAPPING_BASIC);
86                  return false;
87              }
88          }
89  
90          return true;
91      }
92  
93      /**
94       * Creates the text for the invoice no match question being asked of the user.
95       *
96       * @param accountsPayableDocument - to be used by overriding method.
97       * @return
98       */
99      public String createInvoiceNoMatchQuestionText(AccountsPayableDocument accountsPayableDocument) {
100 
101         String questionText = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(PurapKeyConstants.AP_QUESTION_CONFIRM_INVOICE_MISMATCH);
102         questionText = StringUtils.replace(questionText, "{0}", getDocumentName());
103 
104         return questionText;
105     }
106 
107     /**
108      * Determines if the amount entered on the init tab is mismatched with the grand total of the document.
109      *
110      * @param accountsPayableDocument
111      * @return
112      */
113     protected boolean validateInvoiceTotalsAreMismatched(AccountsPayableDocument accountsPayableDocument) {
114         boolean mismatched = false;
115         String[] excludeArray = {PurapConstants.ItemTypeCodes.ITEM_TYPE_PMT_TERMS_DISCOUNT_CODE};
116         if (accountsPayableDocument.getTotalDollarAmountAllItems(excludeArray).compareTo(accountsPayableDocument.getInitialAmount()) != 0 && !accountsPayableDocument.isUnmatchedOverride()) {
117             mismatched = true;
118         }
119 
120         return mismatched;
121     }
122 
123     /**
124      * Exists to be overriden by the child class and return the name of the document.
125      *
126      * @return
127      */
128     public String getDocumentName() {
129         return "";
130     }
131 
132 }