001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.module.purap.document.validation.impl;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.kfs.module.purap.PurapConstants;
022    import org.kuali.kfs.module.purap.PurapKeyConstants;
023    import org.kuali.kfs.module.purap.businessobject.PaymentRequestItem;
024    import org.kuali.kfs.module.purap.businessobject.PurApItem;
025    import org.kuali.kfs.module.purap.document.PaymentRequestDocument;
026    import org.kuali.kfs.sys.KFSPropertyConstants;
027    import org.kuali.kfs.sys.document.validation.GenericValidation;
028    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
029    import org.kuali.rice.kns.util.GlobalVariables;
030    import org.kuali.rice.kns.util.KualiDecimal;
031    import org.kuali.rice.kns.util.ObjectUtils;
032    
033    public class PaymentRequestTotalsValidation extends GenericValidation {
034    
035        public boolean validate(AttributedDocumentEvent event) {
036            PaymentRequestDocument document = (PaymentRequestDocument)event.getDocument();
037            GlobalVariables.getMessageMap().clearErrorPath();
038            GlobalVariables.getMessageMap().addToErrorPath(KFSPropertyConstants.DOCUMENT);
039    
040            List excludeDiscount = new ArrayList();
041            excludeDiscount.add(PurapConstants.ItemTypeCodes.ITEM_TYPE_PMT_TERMS_DISCOUNT_CODE);
042            if ((ObjectUtils.isNull(document.getVendorInvoiceAmount())) || (this.getTotalExcludingItemTypes(document.getItems(), excludeDiscount).compareTo(document.getVendorInvoiceAmount()) != 0 && !document.isUnmatchedOverride())) {
043                if (!GlobalVariables.getMessageList().contains(PurapKeyConstants.WARNING_PAYMENT_REQUEST_VENDOR_INVOICE_AMOUNT_INVALID)) {
044                    GlobalVariables.getMessageList().add(PurapKeyConstants.WARNING_PAYMENT_REQUEST_VENDOR_INVOICE_AMOUNT_INVALID);                
045                }
046            }
047            
048            flagLineItemTotals(document.getItems());
049            
050            GlobalVariables.getMessageMap().clearErrorPath();
051            
052            //always returns true, as this is a warning, not an error
053            return true;
054        }
055    
056        /**
057         * Calculates a total but excludes passed in item types from the totalling.
058         * 
059         * @param itemList - list of purap items
060         * @param excludedItemTypes - list of item types to exclude from totalling
061         * @return
062         */
063        protected KualiDecimal getTotalExcludingItemTypes(List<PurApItem> itemList, List<String> excludedItemTypes) {
064            KualiDecimal total = KualiDecimal.ZERO;
065            for (PurApItem item : itemList) {
066                if (item.getTotalAmount() != null && item.getTotalAmount().isNonZero()) {
067                    boolean skipThisItem = false;
068                    if (excludedItemTypes.contains(item.getItemTypeCode())) {
069                        // this item type is excluded
070                        skipThisItem = true;
071                        break;
072                    }
073                    if (skipThisItem) {
074                        continue;
075                    }
076                    total = total.add(item.getTotalAmount());
077                }
078            }
079            return total;
080        }
081    
082        /**
083         * Flags with an erorr the item totals whos calculated extended price does not equal its extended price.
084         * 
085         * @param itemList - list of purap items
086         */
087        protected void flagLineItemTotals(List<PurApItem> itemList) {
088            for (PurApItem purApItem : itemList) {
089                PaymentRequestItem item = (PaymentRequestItem) purApItem;
090                if (item.getItemQuantity() != null && item.getExtendedPrice() !=null) {
091                    if (item.calculateExtendedPrice().compareTo(item.getExtendedPrice()) != 0) {
092                        GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_PAYMENT_REQUEST_ITEM_TOTAL_NOT_EQUAL, item.getItemIdentifierString());
093                    }
094                }
095            }
096        }
097    
098    }