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.List;
019    
020    import org.kuali.kfs.module.purap.PurapConstants;
021    import org.kuali.kfs.module.purap.PurapKeyConstants;
022    import org.kuali.kfs.module.purap.businessobject.PurApAccountingLine;
023    import org.kuali.kfs.module.purap.businessobject.PurApItem;
024    import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
025    import org.kuali.kfs.module.purap.document.service.PurchaseOrderService;
026    import org.kuali.kfs.sys.context.SpringContext;
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    
031    public class PurchaseOrderAmendmentAccountValidation extends GenericValidation {
032    
033        public boolean validate(AttributedDocumentEvent event) {
034    
035            boolean valid = true;
036            PurchaseOrderDocument poaDocument = (PurchaseOrderDocument)event.getDocument();
037            List<PurApItem> items = poaDocument.getItemsActiveOnly();
038    
039            PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(poaDocument.getPurapDocumentIdentifier());
040            List<PurApItem> poItems = po.getItems();
041                    
042            for (PurApItem item : items) {
043                String identifierString = item.getItemIdentifierString();
044                
045                // check to see if the account has been expired or inactive
046                if (item.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE) && item.getSourceAccountingLines() != null && item.getSourceAccountingLines().size() > 0) {            
047    
048                    // check only if there are changes on the item
049                    if (isItemChanged(item, poItems)) {
050                        List<PurApAccountingLine> accountingLines = item.getSourceAccountingLines();
051                        for (PurApAccountingLine accountingLine : accountingLines) {                
052                            if (accountingLine.getAccount().isExpired()) {
053                                valid = false;
054                                GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNT_EXPIRED, accountingLine.getAccount().getAccountNumber());
055                                break;
056                            }
057                            if (!accountingLine.getAccount().isActive()) {
058                                valid = false;
059                                GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNT_INACTIVE, accountingLine.getAccount().getAccountNumber());
060                                break;
061                            }
062                        }
063                    } 
064                }            
065            }   
066            
067            return valid;
068        }
069    
070        private boolean isItemChanged(PurApItem poaItem, List<PurApItem> poItems) {
071            
072            boolean changed = false;
073            
074            int poaItemId = poaItem.getItemLineNumber().intValue();
075                            
076            for (PurApItem poItem : poItems) {
077            
078                if (poItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE) && poaItemId == poItem.getItemLineNumber().intValue()) {
079                    // required fields so they cannot be null
080                    if (poaItem.getItemQuantity().intValue() != poItem.getItemQuantity().intValue()) {
081                        changed = true;
082                    }
083                    if (!poaItem.getItemUnitOfMeasureCode().equals(poItem.getItemUnitOfMeasureCode())) {
084                        changed = true;
085                    }
086                    if  (poaItem.getItemUnitPrice().floatValue() != poItem.getItemUnitPrice().floatValue()) {
087                        changed = true;
088                    }
089                    if (poaItem.getTotalAmount().floatValue() != poItem.getTotalAmount().floatValue()) {
090                        changed = true;
091                    }
092                    if (poaItem.getItemAssignedToTradeInIndicator() != poItem.getItemAssignedToTradeInIndicator()) {
093                        changed = true;
094                    }
095                    // optional fields
096                    if ((poaItem.getItemCatalogNumber() != null && !poaItem.getItemCatalogNumber().equals(poItem.getItemCatalogNumber())) ||
097                        (poItem.getItemCatalogNumber() != null && !poItem.getItemCatalogNumber().equals(poaItem.getItemCatalogNumber()))) {
098                        changed = true;
099                    }
100                    if ((poaItem.getItemDescription() != null && !poaItem.getItemDescription().equals(poItem.getItemDescription())) || 
101                        (poItem.getItemDescription() != null && !poItem.getItemDescription().equals(poaItem.getItemDescription()))) {
102                        changed = true;
103                    }
104                    if ((poaItem.getExtendedPrice() != null && poItem.getExtendedPrice() != null && poaItem.getExtendedPrice().floatValue() != poItem.getExtendedPrice().floatValue()) ||
105                        (poaItem.getExtendedPrice() != null && poaItem.getExtendedPrice().floatValue() != 0 && poItem.getExtendedPrice() == null) ||
106                        (poaItem.getExtendedPrice() == null && poItem.getExtendedPrice() != null && poItem.getExtendedPrice().floatValue() != 0) ) {
107                        changed = true;
108                    }
109                    if ((poaItem.getItemTaxAmount() != null && poItem.getItemTaxAmount() != null && poaItem.getItemTaxAmount().floatValue() != poItem.getItemTaxAmount().floatValue()) ||
110                        (poaItem.getItemTaxAmount() != null && poaItem.getItemTaxAmount().floatValue() != 0 && poItem.getItemTaxAmount() != null) ||
111                        (poaItem.getItemTaxAmount() == null && poItem.getItemTaxAmount() != null && poItem.getItemTaxAmount().floatValue() != 0)) {
112                        changed = true;
113                    }
114                    
115                    break;
116                }
117            }
118            
119            return changed;
120        }
121    }