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.ld.document.validation.impl;
017    
018    import java.util.HashMap;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.kfs.module.ld.LaborConstants;
023    import org.kuali.kfs.module.ld.LaborKeyConstants;
024    import org.kuali.kfs.module.ld.LaborPropertyConstants;
025    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
026    import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
027    import org.kuali.kfs.module.ld.document.SalaryExpenseTransferDocument;
028    import org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.KFSPropertyConstants;
031    import org.kuali.kfs.sys.context.SpringContext;
032    import org.kuali.kfs.sys.document.AccountingDocument;
033    import org.kuali.kfs.sys.document.validation.GenericValidation;
034    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
035    import org.kuali.rice.kns.document.Document;
036    import org.kuali.rice.kns.util.GlobalVariables;
037    import org.kuali.rice.kns.util.KNSConstants;
038    
039    /**
040     * Validates that an accounting document does not have any pending
041     * labor ledger entries with the same emplID, periodCode, accountNumber, objectCode 
042     */
043    public class BenefitExpenseTransferPendingLegerEntryValidation extends GenericValidation {
044        private Document documentForValidation;
045        
046        /**
047         * Validates that the accounting lines in the accounting document does not have 
048         * any pending labor ledger entries with the same account, sub-account, object, sub-object, fiscal year, fiscal period  
049         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
050         */
051        public boolean validate(AttributedDocumentEvent event) {
052            boolean result = true;
053            
054            Document documentForValidation = getDocumentForValidation();
055            AccountingDocument accountingDocument = (AccountingDocument) documentForValidation;
056            
057            result = !hasPendingLedgerEntry(accountingDocument);
058            return result ;    
059        }
060    
061        /**
062         * Checks whether amounts by object codes are unchanged
063         * 
064         * @param accountingDocumentForValidation The accounting document from which the Pending Entries are checked
065         * @return True if the given accounting documents Pending Entries do not conflict with current PEs, false otherwise.
066         */ 
067        protected boolean hasPendingLedgerEntry(AccountingDocument accountingDocument) {
068            boolean entriesExist = false ;
069            
070            LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
071            List<ExpenseTransferAccountingLine> sourceAccountingLines = expenseTransferDocument.getSourceAccountingLines();
072    
073            Map<String, String> fieldValues = new HashMap<String, String>();
074            for (ExpenseTransferAccountingLine sourceAccountingLine : sourceAccountingLines) {
075                //account, sub-account, object and sub-object and the fiscal year and fiscal period 
076                
077                String subAccountNumber = sourceAccountingLine.getSubAccountNumber();
078                String accountNumber = sourceAccountingLine.getAccountNumber();
079                String objectCode = sourceAccountingLine.getFinancialObjectCode();
080                String subObjectCode = sourceAccountingLine.getFinancialSubObjectCode();
081                Integer fiscalYear = sourceAccountingLine.getPayrollEndDateFiscalYear();
082                String periodCode = sourceAccountingLine.getPayrollEndDateFiscalPeriodCode();
083                String documentNumber = accountingDocument.getDocumentNumber();
084    
085                fieldValues.put(LaborPropertyConstants.PAYROLL_END_DATE_FISCAL_PERIOD_CODE, periodCode);
086                fieldValues.put(LaborPropertyConstants.PAYROLL_END_DATE_FISCAL_YEAR, fiscalYear+"");
087                fieldValues.put(KFSPropertyConstants.ACCOUNT_NUMBER, accountNumber);
088                fieldValues.put(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, subAccountNumber);
089                fieldValues.put(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, objectCode);
090                fieldValues.put(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE, subObjectCode);
091                fieldValues.put(KFSPropertyConstants.DOCUMENT_NUMBER, KNSConstants.NOT_LOGICAL_OPERATOR + documentNumber);
092                
093                if (SpringContext.getBean(LaborLedgerPendingEntryService.class).hasPendingLaborLedgerEntry(fieldValues)) {
094                    GlobalVariables.getMessageMap().putError(LaborConstants.EMPLOYEE_LOOKUP_ERRORS, LaborKeyConstants.PENDING_BENEFIT_TRANSFER_ERROR, accountNumber, objectCode, periodCode, fiscalYear+"");
095                    return true;
096                }                        
097            }
098            return entriesExist ;
099        }
100    
101        /**
102         * Gets the documentForValidation attribute. 
103         * @return Returns the documentForValidation.
104         */
105        public Document getDocumentForValidation() {
106            return documentForValidation;
107        }
108    
109        /**
110         * Sets the accountingDocumentForValidation attribute value.
111         * @param documentForValidation The documentForValidation to set.
112         */
113        public void setDocumentForValidation(Document documentForValidation) {
114            this.documentForValidation = documentForValidation;
115        }    
116    }