001/*
002 * Copyright 2007 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.ole.module.purap.document.validation.impl;
017
018import org.kuali.ole.module.purap.PurapConstants;
019import org.kuali.ole.module.purap.PurapParameterConstants;
020import org.kuali.ole.module.purap.document.AccountsPayableDocument;
021import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
022import org.kuali.ole.module.purap.document.VendorCreditMemoDocument;
023import org.kuali.ole.sys.context.SpringContext;
024import org.kuali.ole.sys.service.impl.OleParameterConstants;
025import org.kuali.rice.core.web.format.CurrencyFormatter;
026import org.kuali.rice.coreservice.framework.parameter.ParameterService;
027import org.kuali.rice.krad.document.Document;
028
029/**
030 * Business rule(s) applicable to the Credit Memo document.
031 */
032public class CreditMemoDocumentPreRules extends AccountsPayableDocumentPreRulesBase {
033
034    /**
035     * Default constructor
036     */
037    public CreditMemoDocumentPreRules() {
038        super();
039    }
040
041    /**
042     * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.krad.document.Document)
043     */
044    @Override
045    public boolean doPrompts(Document document) {
046        return super.doPrompts(document);
047    }
048
049    /**
050     * @see org.kuali.ole.module.purap.document.validation.impl.AccountsPayableDocumentPreRulesBase#getDocumentName()
051     */
052    @Override
053    public String getDocumentName() {
054        return "Credit Memo";
055    }
056
057    /**
058     * @see org.kuali.ole.module.purap.document.validation.impl.AccountsPayableDocumentPreRulesBase#createInvoiceNoMatchQuestionText(org.kuali.ole.module.purap.document.AccountsPayableDocument)
059     */
060    @Override
061    public String createInvoiceNoMatchQuestionText(AccountsPayableDocument accountsPayableDocument) {
062
063        String questionText = super.createInvoiceNoMatchQuestionText(accountsPayableDocument);
064
065        CurrencyFormatter cf = new CurrencyFormatter();
066        VendorCreditMemoDocument cm = (VendorCreditMemoDocument) accountsPayableDocument;
067        StringBuffer questionTextBuffer = new StringBuffer("");
068        questionTextBuffer.append(questionText);
069
070        questionTextBuffer.append("[br][br][b]Summary Detail Below[/b][br][br][table questionTable]");
071        questionTextBuffer.append("[tr][td leftTd]Credit Memo Amount entered on start screen:[/td][td rightTd]").append((String) cf.format(cm.getInitialAmount())).append("[/td][/tr]");
072        questionTextBuffer.append("[tr][td leftTd]Total credit processed prior to restocking fee:[/td][td rightTd]").append((String) cf.format(cm.getLineItemTotal())).append("[/td][/tr]");
073
074        //if sales tax is enabled, show additional summary lines
075        boolean salesTaxInd = SpringContext.getBean(ParameterService.class).getParameterValueAsBoolean(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapParameterConstants.ENABLE_SALES_TAX_IND);
076        if (salesTaxInd) {
077            questionTextBuffer.append("[tr][td leftTd]Grand Total Prior to Tax:[/td][td rightTd]").append((String) cf.format(cm.getGrandPreTaxTotal())).append("[/td][/tr]");
078            questionTextBuffer.append("[tr][td leftTd]Grand Total Tax:[/td][td rightTd]").append((String) cf.format(cm.getGrandTaxAmount())).append("[/td][/tr]");
079        }
080
081        questionTextBuffer.append("[tr][td leftTd]Grand Total:[/td][td rightTd]").append((String) cf.format(cm.getGrandTotal())).append("[/td][/tr][/table]");
082
083        return questionTextBuffer.toString();
084
085    }
086
087    @Override
088    protected boolean checkCAMSWarningStatus(PurchasingAccountsPayableDocument purapDocument) {
089        return PurapConstants.CAMSWarningStatuses.CREDIT_MEMO_STATUS_WARNING_NO_CAMS_DATA.contains(purapDocument.getApplicationDocumentStatus());
090    }
091
092    /**
093     * Determines if the amount entered on the init tab is mismatched with the grand total of the document.
094     *
095     * @param accountsPayableDocument
096     * @return
097     */
098    @Override
099    protected boolean validateInvoiceTotalsAreMismatched(AccountsPayableDocument accountsPayableDocument) {
100        boolean mismatched = false;
101        String[] excludeArray = {PurapConstants.ItemTypeCodes.ITEM_TYPE_PMT_TERMS_DISCOUNT_CODE};
102
103        //  if UseTax is included, then the invoiceInitialAmount should be compared against the 
104        // total amount NOT INCLUDING tax
105        if (accountsPayableDocument.isUseTaxIndicator()) {
106            if (accountsPayableDocument.getTotalPreTaxDollarAmountAllItems(excludeArray).compareTo(accountsPayableDocument.getInitialAmount()) != 0 && !accountsPayableDocument.isUnmatchedOverride()) {
107                mismatched = true;
108            }
109        }
110        //  if NO UseTax, then the invoiceInitialAmount should be compared against the 
111        // total amount INCLUDING sales tax (since if the vendor invoices with sales tax, then we pay it)
112        else {
113            if (accountsPayableDocument.getTotalDollarAmountAllItems(excludeArray).compareTo(accountsPayableDocument.getInitialAmount()) != 0 && !accountsPayableDocument.isUnmatchedOverride()) {
114                mismatched = true;
115            }
116        }
117        return mismatched;
118    }
119}