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.PurapConstants;
019    import org.kuali.kfs.module.purap.PurapKeyConstants;
020    import org.kuali.kfs.module.purap.PurapPropertyConstants;
021    import org.kuali.kfs.module.purap.businessobject.PurApItem;
022    import org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem;
023    import org.kuali.kfs.module.purap.businessobject.PurchasingItemBase;
024    import org.kuali.kfs.module.purap.document.PurchaseOrderAmendmentDocument;
025    import org.kuali.kfs.module.purap.document.service.PurchaseOrderService;
026    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027    import org.kuali.rice.kns.util.GlobalVariables;
028    import org.kuali.rice.kns.util.KualiDecimal;
029    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
030    
031    public class PurchaseOrderAmendmentNewIndividualItemValidation extends PurchaseOrderNewIndividualItemValidation {
032    
033        private PurchaseOrderService purchaseOrderService;
034        
035        /**
036         * Overrides the method in PurchaseOrderNewIndividualItemValidation to add additional validations that are specific to Amendment.
037         * 
038         * @see org.kuali.kfs.module.purap.document.validation.impl.PurchaseOrderDocumentRule#newIndividualItemValidation(org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument, java.lang.String, org.kuali.kfs.module.purap.businessobject.PurApItem)
039         */
040        @Override
041        public boolean validate(AttributedDocumentEvent event) {
042            boolean valid = super.validate(event);
043            PurchaseOrderItem item = (PurchaseOrderItem) getItemForValidation();
044            String identifierString = item.getItemIdentifierString();
045            if ((item.getItemInvoicedTotalQuantity() != null) && (!(item.getItemInvoicedTotalQuantity()).isZero())) {
046                if (item.getItemQuantity() == null) {
047                    valid = false;
048                    GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_AMND_NULL, "Item Quantity", identifierString);
049                }
050                else if (item.getItemQuantity().compareTo(item.getItemInvoicedTotalQuantity()) < 0) {
051                    valid = false;
052                    GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_AMND_INVALID, "Item Quantity", identifierString);
053                }
054            }
055    
056            if (item.getItemInvoicedTotalAmount() != null) {
057                KualiDecimal total = item.getTotalAmount().abs();
058                if ((total == null) || total.compareTo(item.getItemInvoicedTotalAmount().abs()) < 0) {
059                    valid = false;
060                    GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_AMND_INVALID_AMT, "Item Extended Price", identifierString);
061                }
062            }
063            
064            PurchaseOrderAmendmentDocument document = (PurchaseOrderAmendmentDocument)event.getDocument();
065            KualiWorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
066            
067            // run additional accounting line check for items added to POA via receiving line, only after document in enroute
068            if ( !(workflowDocument.stateIsInitiated() || workflowDocument.stateIsSaved()) && purchaseOrderService.isNewUnorderedItem(item) ) {
069                
070                // check to see if the account lines are empty
071                if (item.getSourceAccountingLines() == null || item.getSourceAccountingLines().size() == 0) {            
072                    valid = false;
073                    GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_INCOMPLETE, identifierString, identifierString);            
074                }
075            }
076            
077            return valid;
078        }
079    
080        /**
081         * Overrides to provide validation for PurchaseOrderAmendmentDocument. 
082         * @see org.kuali.kfs.module.purap.document.validation.impl.PurchasingDocumentRuleBase#validateCommodityCodes(org.kuali.kfs.module.purap.businessobject.PurApItem, boolean)
083         */
084        @Override
085        protected boolean validateCommodityCodes(PurApItem item, boolean commodityCodeRequired) {
086            //If the item is inactive then don't need any of the following validations.
087            if (!((PurchaseOrderItem)item).isItemActiveIndicator()) {
088                return true;
089            }
090            else {
091                return super.validateCommodityCodes(item, commodityCodeRequired);
092            }
093        }
094    
095        /**
096         * Overrides the method in PurchasingDocumentRuleBase so that we'll return true
097         * if the item has been previously saved to the database and we'll only check for
098         * the commodity code active flag if the item has not been previously saved to
099         * the database. 
100         * 
101         * @param item
102         * @param commodityCodeRequired
103         * @return
104         */
105        protected boolean validateThatCommodityCodeIsActive(PurApItem item) {
106            if (item.getVersionNumber() != null) {
107                return true;
108            }
109            else {
110                if (!((PurchasingItemBase)item).getCommodityCode().isActive()) {
111                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in " + item.getItemIdentifierString());
112                    return false;
113                }
114                return true;
115            }
116        }
117    
118        public PurchaseOrderService getPurchaseOrderService() {
119            return purchaseOrderService;
120        }
121    
122        public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
123            this.purchaseOrderService = purchaseOrderService;
124        }
125    }