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.math.BigDecimal;
019    
020    import org.kuali.kfs.module.purap.PurapKeyConstants;
021    import org.kuali.kfs.module.purap.PurapPropertyConstants;
022    import org.kuali.kfs.module.purap.PurapConstants.ItemFields;
023    import org.kuali.kfs.module.purap.PurapConstants.ItemTypeCodes;
024    import org.kuali.kfs.module.purap.businessobject.PurApItem;
025    import org.kuali.kfs.sys.KFSKeyConstants;
026    import org.kuali.kfs.sys.document.validation.GenericValidation;
027    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
028    import org.kuali.rice.kns.service.DataDictionaryService;
029    import org.kuali.rice.kns.util.GlobalVariables;
030    import org.kuali.rice.kns.util.ObjectUtils;
031    
032    public class PurchasingItemUnitPriceValidation extends GenericValidation {
033    
034        private PurApItem itemForValidation;
035        private DataDictionaryService dataDictionaryService;
036        
037        /**
038         * Validates the unit price for all applicable item types. It validates that the unit price field was
039         * entered on the item, and that the price is in the right range for the item type.
040         */
041        public boolean validate(AttributedDocumentEvent event) {
042            boolean valid = true;
043            if (itemForValidation.getItemType().isLineItemIndicator()) {
044                if (ObjectUtils.isNull(itemForValidation.getItemUnitPrice())) {
045                    valid = false;
046                    String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(itemForValidation.getClass().getName()).
047                                            getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_PRICE).getLabel();
048                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, KFSKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + itemForValidation.getItemIdentifierString());
049                }
050            }    
051    
052            if (ObjectUtils.isNotNull(itemForValidation.getItemUnitPrice())) {
053                if ((BigDecimal.ZERO.compareTo(itemForValidation.getItemUnitPrice()) > 0) && ((!itemForValidation.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE)) && (!itemForValidation.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE)))) {
054                    // If the item type is not full order discount or trade in items, don't allow negative unit price.
055                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_AMOUNT_BELOW_ZERO, ItemFields.UNIT_COST, itemForValidation.getItemIdentifierString());
056                    valid = false;
057                }
058                else if ((BigDecimal.ZERO.compareTo(itemForValidation.getItemUnitPrice()) < 0) && ((itemForValidation.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE)) || (itemForValidation.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE)))) {
059                    // If the item type is full order discount or trade in items, its unit price must be negative.
060                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_AMOUNT_NOT_BELOW_ZERO, ItemFields.UNIT_COST, itemForValidation.getItemIdentifierString());
061                    valid = false;
062                }
063            }
064    
065            return valid;
066        }
067    
068        public PurApItem getItemForValidation() {
069            return itemForValidation;
070        }
071    
072        public void setItemForValidation(PurApItem itemForValidation) {
073            this.itemForValidation = itemForValidation;
074        }
075    
076        public DataDictionaryService getDataDictionaryService() {
077            return dataDictionaryService;
078        }
079    
080        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
081            this.dataDictionaryService = dataDictionaryService;
082        }
083    
084    }