View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ld.document.validation.impl;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.kuali.kfs.module.ld.LaborConstants;
26  import org.kuali.kfs.module.ld.LaborKeyConstants;
27  import org.kuali.kfs.module.ld.LaborPropertyConstants;
28  import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
29  import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
30  import org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService;
31  import org.kuali.kfs.sys.KFSPropertyConstants;
32  import org.kuali.kfs.sys.context.SpringContext;
33  import org.kuali.kfs.sys.document.AccountingDocument;
34  import org.kuali.kfs.sys.document.validation.GenericValidation;
35  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
36  import org.kuali.rice.core.api.search.SearchOperator;
37  import org.kuali.rice.krad.document.Document;
38  import org.kuali.rice.krad.util.GlobalVariables;
39  
40  /**
41   * Validates that an accounting document does not have any pending
42   * labor ledger entries with the same emplID, periodCode, accountNumber, objectCode 
43   */
44  public class SalaryExpenseTransferPendingLegerEntryValidation extends GenericValidation {
45      private Document documentForValidation;
46      
47      /**
48       * Validates that the accounting lines in the accounting document does not have 
49       * any pending labor ledger entries with the same emplID, periodCode, accountNumber, objectCode 
50       * <strong>Expects an accounting document as the first a parameter</strong>
51       * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
52       */
53      public boolean validate(AttributedDocumentEvent event) {
54          boolean result = true;
55          
56          Document documentForValidation = getDocumentForValidation();
57          AccountingDocument accountingDocument = (AccountingDocument) documentForValidation;
58          
59          result = !hasPendingLedgerEntry(accountingDocument);
60          return result ;    
61      }
62  
63      /**
64       * Checks whether amounts by object codes are unchanged
65       * 
66       * @param accountingDocumentForValidation The accounting document from which the amounts by objects codes are checked
67       * @return True if the given accounting documents amounts by object code are unchanged, false otherwise.
68       */ 
69      protected boolean hasPendingLedgerEntry(AccountingDocument accountingDocument) {
70          boolean entriesExist = false ;
71          
72          LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
73          List<ExpenseTransferAccountingLine> sourceAccountingLines = expenseTransferDocument.getSourceAccountingLines();
74  
75          Map<String, String> fieldValues = new HashMap<String, String>();
76          for (ExpenseTransferAccountingLine sourceAccountingLine : sourceAccountingLines) {
77              String payPeriodCode = sourceAccountingLine.getPayrollEndDateFiscalPeriodCode();
78              String accountNumber = sourceAccountingLine.getAccountNumber();
79              String objectCode = sourceAccountingLine.getFinancialObjectCode();
80              String emplId = sourceAccountingLine.getEmplid();
81              String documentNumber = accountingDocument.getDocumentNumber();
82  
83              fieldValues.put(LaborPropertyConstants.PAYROLL_END_DATE_FISCAL_PERIOD_CODE, payPeriodCode);
84              fieldValues.put(KFSPropertyConstants.ACCOUNT_NUMBER, accountNumber);
85              fieldValues.put(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, objectCode);
86              fieldValues.put(KFSPropertyConstants.EMPLID, emplId);
87              fieldValues.put(KFSPropertyConstants.DOCUMENT_NUMBER, SearchOperator.NOT.op() + documentNumber);
88              
89              if (SpringContext.getBean(LaborLedgerPendingEntryService.class).hasPendingLaborLedgerEntry(fieldValues)) {
90                  GlobalVariables.getMessageMap().putError(LaborConstants.EMPLOYEE_LOOKUP_ERRORS, LaborKeyConstants.PENDING_SALARY_TRANSFER_ERROR, emplId, payPeriodCode, accountNumber, objectCode);
91                  return true;
92              }                        
93          }
94          return entriesExist ;
95      }
96  
97      /**
98       * Gets the documentForValidation attribute. 
99       * @return Returns the documentForValidation.
100      */
101     public Document getDocumentForValidation() {
102         return documentForValidation;
103     }
104 
105     /**
106      * Sets the accountingDocumentForValidation attribute value.
107      * @param documentForValidation The documentForValidation to set.
108      */
109     public void setDocumentForValidation(Document documentForValidation) {
110         this.documentForValidation = documentForValidation;
111     }    
112 }