001/*
002 * Copyright 2013 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 */
016package org.kuali.ole.select.document.validation.impl;
017
018import org.kuali.ole.module.purap.PurapKeyConstants;
019import org.kuali.ole.module.purap.PurapPropertyConstants;
020import org.kuali.ole.select.businessobject.OleCreditMemoItem;
021import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
022import org.kuali.ole.sys.OLEPropertyConstants;
023import org.kuali.ole.sys.document.validation.GenericValidation;
024import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
025import org.kuali.rice.core.api.util.type.KualiDecimal;
026import org.kuali.rice.kns.service.DataDictionaryService;
027import org.kuali.rice.krad.util.GlobalVariables;
028
029public class OleVendorCreditMemoItemExtendedPriceValidation extends GenericValidation {
030
031    private DataDictionaryService dataDictionaryService;
032    private OleCreditMemoItem itemForValidation;
033
034    @Override
035    public boolean validate(AttributedDocumentEvent event) {
036        boolean valid = true;
037        OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
038
039        String errorKeyPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + (itemForValidation.getItemLineNumber() - 1) + "].";
040        String errorKey = errorKeyPrefix + PurapPropertyConstants.EXTENDED_PRICE;
041
042        if (itemForValidation.getExtendedPrice() != null) {
043            if (itemForValidation.getExtendedPrice().isNegative() && cmDocument.getInvoiceIdentifier() == null) {
044                String label = dataDictionaryService.getAttributeErrorLabel(OleCreditMemoItem.class, PurapPropertyConstants.EXTENDED_PRICE);
045                GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_AMOUNT_NONPOSITIVE, label);
046                valid = false;
047            }
048            if (!cmDocument.isSourceVendor()) {
049                // check cm extended price is not greater than total invoiced amount
050                KualiDecimal invoicedAmount = null;
051                if (cmDocument.isSourceDocumentPurchaseOrder()) {
052                    invoicedAmount = itemForValidation.getPoTotalAmount();
053                } else {
054                    invoicedAmount = itemForValidation.getPreqTotalAmount();
055                }
056
057                if (invoicedAmount == null) {
058                    invoicedAmount = KualiDecimal.ZERO;
059                }
060
061                if (itemForValidation.getTotalAmount().isGreaterThan(invoicedAmount) && (itemForValidation.getItemSurcharge() == null)) {
062                    GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_EXTENDEDPRICE_TOOMUCH);
063                    valid = false;
064                }
065            }
066
067        }
068
069        return valid;
070    }
071
072    public DataDictionaryService getDataDictionaryService() {
073        return dataDictionaryService;
074    }
075
076    public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
077        this.dataDictionaryService = dataDictionaryService;
078    }
079
080    public OleCreditMemoItem getItemForValidation() {
081        return itemForValidation;
082    }
083
084    public void setItemForValidation(OleCreditMemoItem itemForValidation) {
085        this.itemForValidation = itemForValidation;
086    }
087
088
089}