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 org.kuali.kfs.module.purap.PurapKeyConstants;
019    import org.kuali.kfs.module.purap.PurapPropertyConstants;
020    import org.kuali.kfs.module.purap.businessobject.CreditMemoItem;
021    import org.kuali.kfs.module.purap.document.VendorCreditMemoDocument;
022    import org.kuali.kfs.sys.KFSKeyConstants;
023    import org.kuali.kfs.sys.KFSPropertyConstants;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.sys.document.validation.GenericValidation;
026    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027    import org.kuali.rice.kns.service.DataDictionaryService;
028    import org.kuali.rice.kns.util.GlobalVariables;
029    import org.kuali.rice.kns.util.KualiDecimal;
030    
031    public class VendorCreditMemoItemQuantityValidation extends GenericValidation {
032    
033        private DataDictionaryService dataDictionaryService;
034        private CreditMemoItem itemForValidation;
035    
036        public boolean validate(AttributedDocumentEvent event) {
037            boolean valid = true;
038            VendorCreditMemoDocument cmDocument = (VendorCreditMemoDocument)event.getDocument();
039            String errorKeyPrefix = KFSPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + (itemForValidation.getItemLineNumber() - 1) + "].";
040            String errorKey = errorKeyPrefix + PurapPropertyConstants.QUANTITY;
041            
042            if (itemForValidation.getItemQuantity() != null) {
043                if (itemForValidation.getItemQuantity().isNegative()) {
044                    String label = dataDictionaryService.getAttributeErrorLabel(CreditMemoItem.class, PurapPropertyConstants.QUANTITY);
045                    GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_AMOUNT_NONPOSITIVE, label);
046                    valid = false;
047                }
048    
049                // check cm quantity is not greater than invoiced quantity
050                KualiDecimal invoicedQuantity = getSourceTotalInvoiceQuantity(cmDocument, itemForValidation);
051                if (itemForValidation.getItemQuantity().isGreaterThan(invoicedQuantity)) {
052                    GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_QUANTITY_TOOMUCH);
053                    valid = false;
054                }
055            }
056            else {
057                // check if quantity should be required
058                KualiDecimal invoicedQuantity = getSourceTotalInvoiceQuantity(cmDocument, itemForValidation);
059                if (itemForValidation.getItemType().isQuantityBasedGeneralLedgerIndicator() && (invoicedQuantity != null && invoicedQuantity.isGreaterThan(KualiDecimal.ZERO)) && (itemForValidation.getExtendedPrice() != null && itemForValidation.getExtendedPrice().isGreaterThan(KualiDecimal.ZERO))) {
060                    String label = dataDictionaryService.getAttributeErrorLabel(CreditMemoItem.class, PurapPropertyConstants.QUANTITY);
061                    GlobalVariables.getMessageMap().putError(errorKey, KFSKeyConstants.ERROR_REQUIRED, label);
062                    valid = false;
063                }
064            }
065    
066            return valid;
067        }
068    
069        /**
070         * Returns the total invoiced quantity for the item line based on the type of credit memo.
071         * 
072         * @param cmDocument - credit memo document
073         * @param item - credit memo item line to return total invoice quantity
074         * @return KualiDecimal - total invoiced quantity
075         */
076        protected KualiDecimal getSourceTotalInvoiceQuantity(VendorCreditMemoDocument cmDocument, CreditMemoItem item) {
077            KualiDecimal invoicedQuantity = null;
078    
079            if (cmDocument.isSourceDocumentPurchaseOrder()) {
080                invoicedQuantity = item.getPoInvoicedTotalQuantity();
081            }
082            else {
083                invoicedQuantity = item.getPreqInvoicedTotalQuantity();
084            }
085    
086            return invoicedQuantity;
087        }
088    
089        public DataDictionaryService getDataDictionaryService() {
090            return dataDictionaryService;
091        }
092    
093        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
094            this.dataDictionaryService = dataDictionaryService;
095        }
096    
097        public CreditMemoItem getItemForValidation() {
098            return itemForValidation;
099        }
100    
101        public void setItemForValidation(CreditMemoItem itemForValidation) {
102            this.itemForValidation = itemForValidation;
103        }
104    
105    }