001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.purap.rules;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.kuali.core.document.Document;
020: import org.kuali.core.rules.PreRulesContinuationBase;
021: import org.kuali.core.service.KualiConfigurationService;
022: import org.kuali.kfs.KFSConstants;
023: import org.kuali.kfs.context.SpringContext;
024: import org.kuali.module.purap.PurapConstants;
025: import org.kuali.module.purap.PurapKeyConstants;
026: import org.kuali.module.purap.document.AccountsPayableDocument;
027: import org.kuali.module.purap.service.PurapService;
028:
029: /**
030: * Performs prompts and other pre business rule checks for the Accounts Payable Document (and its children).
031: */
032: public class AccountsPayableDocumentPreRulesBase extends
033: PreRulesContinuationBase {
034: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
035: .getLogger(AccountsPayableDocumentPreRulesBase.class);
036:
037: public AccountsPayableDocumentPreRulesBase() {
038: super ();
039: }
040:
041: /**
042: * Asks for an override if the document hasn't reached full entry and the entered amount does not
043: * match the total amount of all items.
044: *
045: * @see org.kuali.core.rules.PreRulesContinuationBase#doRules(org.kuali.core.document.Document)
046: */
047: public boolean doRules(Document document) {
048:
049: boolean preRulesOK = true;
050:
051: AccountsPayableDocument accountsPayableDocument = (AccountsPayableDocument) document;
052:
053: // Ask the nomatch question if the document hasn't been completed.
054: if (SpringContext.getBean(PurapService.class)
055: .isFullDocumentEntryCompleted(accountsPayableDocument) == false) {
056: preRulesOK = confirmInvoiceNoMatchOverride(accountsPayableDocument);
057: } else if (SpringContext.getBean(PurapService.class)
058: .isFullDocumentEntryCompleted(accountsPayableDocument)) {
059: // if past full document entry complete, then set override to true to skip validation
060: accountsPayableDocument.setUnmatchedOverride(true);
061: }
062:
063: return preRulesOK;
064: }
065:
066: /**
067: * Checks whether the invoice from the initial screen and the document invoice are mismatched. If so, it prompts the
068: * user for confirmation to proceed.
069: *
070: * @param accountsPayableDocument - document to have its invoice/totals checked
071: * @return
072: */
073: private boolean confirmInvoiceNoMatchOverride(
074: AccountsPayableDocument accountsPayableDocument) {
075:
076: // If the values are mismatched, ask for confirmation.
077: if (validateInvoiceTotalsAreMismatched(accountsPayableDocument)) {
078: String questionText = SpringContext
079: .getBean(KualiConfigurationService.class)
080: .getPropertyString(
081: PurapKeyConstants.AP_QUESTION_CONFIRM_INVOICE_MISMATCH);
082: questionText = StringUtils.replace(questionText, "{0}",
083: getDocumentName());
084:
085: boolean confirmOverride = super
086: .askOrAnalyzeYesNoQuestion(
087: PurapConstants.AP_OVERRIDE_INVOICE_NOMATCH_QUESTION,
088: questionText);
089:
090: // Set a marker to record that this method has been used.
091: if (confirmOverride
092: && StringUtils.isBlank(event.getQuestionContext())) {
093: event
094: .setQuestionContext(PurapConstants.AP_OVERRIDE_INVOICE_NOMATCH_QUESTION);
095: accountsPayableDocument.setUnmatchedOverride(true);
096: }
097:
098: if (!confirmOverride) {
099: event.setActionForwardName(KFSConstants.MAPPING_BASIC);
100: return false;
101: }
102: }
103:
104: return true;
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: private boolean validateInvoiceTotalsAreMismatched(
114: AccountsPayableDocument accountsPayableDocument) {
115: boolean mismatched = false;
116: String[] excludeArray = { PurapConstants.ItemTypeCodes.ITEM_TYPE_PMT_TERMS_DISCOUNT_CODE };
117: if (accountsPayableDocument.getTotalDollarAmountAllItems(
118: excludeArray).compareTo(
119: accountsPayableDocument.getInitialAmount()) != 0
120: && !accountsPayableDocument.isUnmatchedOverride()) {
121: mismatched = true;
122: }
123:
124: return mismatched;
125: }
126:
127: /**
128: * Exists to be overriden by the child class and return the name of the document.
129: *
130: * @return
131: */
132: public String getDocumentName() {
133: return "";
134: }
135:
136: }
|