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.HashSet;
019    import java.util.List;
020    import java.util.Set;
021    
022    import org.kuali.kfs.module.purap.PurapConstants;
023    import org.kuali.kfs.module.purap.PurapKeyConstants;
024    import org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument;
025    import org.kuali.kfs.module.purap.service.PurapAccountingService;
026    import org.kuali.kfs.sys.KFSPropertyConstants;
027    import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
028    import org.kuali.kfs.sys.document.validation.GenericValidation;
029    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
030    import org.kuali.rice.kns.util.GlobalVariables;
031    
032    public class PurchasingAccountsPayableCheckNegativeAccountsValidation extends GenericValidation {
033    
034        private PurapAccountingService purapAccountingService;
035        
036        public boolean validate(AttributedDocumentEvent event) {
037            boolean valid = true;
038            PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument)event.getDocument();
039            
040            GlobalVariables.getMessageMap().clearErrorPath();
041            //GlobalVariables.getMessageMap().addToErrorPath(KFSPropertyConstants.DOCUMENT);
042    
043            // if this was set somewhere on the doc(for later use) in prepare for save we could avoid this call
044            purapAccountingService.updateAccountAmounts(document);
045    
046            //be sure to exclude trade in values from the negative check as they're allowed to be negative
047            Set excludedItemTypeCodes = new HashSet();
048            excludedItemTypeCodes.add(PurapConstants.ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE);
049            List<SourceAccountingLine> sourceLines = purapAccountingService.generateSummaryExcludeItemTypes(document.getItems(), excludedItemTypeCodes);
050    
051            for (SourceAccountingLine sourceAccountingLine : sourceLines) {
052                // check if the summary account is for tax withholding
053                boolean isTaxAccount = purapAccountingService.isTaxAccount(document, sourceAccountingLine);
054                            
055                // exclude tax withholding accounts from non-negative requirement
056                if (!isTaxAccount && sourceAccountingLine.getAmount().isNegative()) {
057                    
058                    String subAccountNumber = (sourceAccountingLine.getSubAccountNumber() == null) ? "" : sourceAccountingLine.getSubAccountNumber();
059                    String subObjectCode = (sourceAccountingLine.getFinancialSubObjectCode() == null) ? "" : sourceAccountingLine.getFinancialSubObjectCode();
060                    String projCode = (sourceAccountingLine.getProjectCode() == null) ? "" : sourceAccountingLine.getProjectCode();
061                    String orgRefId = (sourceAccountingLine.getOrganizationReferenceId() == null) ? "" : sourceAccountingLine.getOrganizationReferenceId();
062                    
063                    String accountString = sourceAccountingLine.getChartOfAccountsCode() + " - " + sourceAccountingLine.getAccountNumber() + " - " + subAccountNumber + " - " + sourceAccountingLine.getFinancialObjectCode() + " - " + subObjectCode + " - " + projCode + " - " + orgRefId;
064                    GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ACCOUNT_AMOUNT_TOTAL, accountString, sourceAccountingLine.getAmount() + "");
065                    valid &= false;
066                }
067            }
068            
069            GlobalVariables.getMessageMap().clearErrorPath();
070            return valid;
071        }
072    
073        public PurapAccountingService getPurapAccountingService() {
074            return purapAccountingService;
075        }
076    
077        public void setPurapAccountingService(PurapAccountingService purapAccountingService) {
078            this.purapAccountingService = purapAccountingService;
079        }
080    
081    }