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.bc.document.service.impl;
20  
21  import java.math.BigDecimal;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.kuali.kfs.coa.businessobject.Chart;
29  import org.kuali.kfs.module.bc.BCKeyConstants;
30  import org.kuali.kfs.module.bc.businessobject.BudgetConstructionObjectPick;
31  import org.kuali.kfs.module.bc.businessobject.BudgetConstructionOrgSalaryStatisticsReport;
32  import org.kuali.kfs.module.bc.businessobject.BudgetConstructionSalaryTotal;
33  import org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionSalaryStatisticsReportDao;
34  import org.kuali.kfs.module.bc.document.service.BudgetConstructionOrganizationReportsService;
35  import org.kuali.kfs.module.bc.document.service.BudgetConstructionReportsServiceHelper;
36  import org.kuali.kfs.module.bc.document.service.BudgetConstructionSalaryStatisticsReportService;
37  import org.kuali.kfs.module.bc.report.BudgetConstructionReportHelper;
38  import org.kuali.kfs.sys.KFSPropertyConstants;
39  import org.kuali.rice.core.api.config.property.ConfigurationService;
40  import org.kuali.rice.krad.service.BusinessObjectService;
41  import org.kuali.rice.krad.service.PersistenceService;
42  import org.springframework.transaction.annotation.Transactional;
43  
44  /**
45   * Service implementation of BudgetConstructionAccountSummaryReportService.
46   */
47  @Transactional
48  public class BudgetConstructionSalaryStatisticsReportServiceImpl implements BudgetConstructionSalaryStatisticsReportService {
49  
50      protected BudgetConstructionSalaryStatisticsReportDao budgetConstructionSalaryStatisticsReportDao;
51      protected BudgetConstructionOrganizationReportsService budgetConstructionOrganizationReportsService;
52      protected BudgetConstructionReportsServiceHelper budgetConstructionReportsServiceHelper;
53      protected ConfigurationService kualiConfigurationService;
54      protected BusinessObjectService businessObjectService;
55      protected PersistenceService persistenceServiceOjb;
56  
57      public void updateSalaryStatisticsReport(String principalName, Integer universityFiscalYear) {
58          budgetConstructionSalaryStatisticsReportDao.updateReportsSalaryStatisticsTable(principalName, universityFiscalYear-1);
59      }
60  
61      public Collection<BudgetConstructionOrgSalaryStatisticsReport> buildReports(Integer universityFiscalYear, String principalName) {
62          Collection<BudgetConstructionOrgSalaryStatisticsReport> reportSet = new ArrayList();
63  
64          // force OJB to go to DB since it is populated using JDBC
65          // normally done in BudgetConstructionReportsServiceHelperImpl.getDataForBuildingReports
66          persistenceServiceOjb.clearCache();
67  
68          // build searchCriteria
69          Map<String, Object> searchCriteria = new HashMap<String, Object>();
70          searchCriteria.put(KFSPropertyConstants.KUALI_USER_PERSON_UNIVERSAL_IDENTIFIER, principalName);
71  
72          // build order list
73          List<String> orderList = buildOrderByList();
74          Collection<BudgetConstructionSalaryTotal> salaryStatisticsList = budgetConstructionOrganizationReportsService.getBySearchCriteriaOrderByList(BudgetConstructionSalaryTotal.class, searchCriteria, orderList);
75          
76          // get object codes
77          searchCriteria.clear();
78          searchCriteria.put(KFSPropertyConstants.PERSON_UNIVERSAL_IDENTIFIER, principalName);
79          Collection<BudgetConstructionObjectPick> objectPickList = businessObjectService.findMatching(BudgetConstructionObjectPick.class, searchCriteria);
80          
81          String objectCodes = budgetConstructionReportsServiceHelper.getSelectedObjectCodes(principalName);
82          
83          // build reports
84          for (BudgetConstructionSalaryTotal salaryStatisticsEntry : salaryStatisticsList) {
85              BudgetConstructionOrgSalaryStatisticsReport orgSalaryStatisticsReportEntry = new BudgetConstructionOrgSalaryStatisticsReport();
86              buildReportsHeader(universityFiscalYear, objectCodes, orgSalaryStatisticsReportEntry, salaryStatisticsEntry);
87              buildReportsBody(orgSalaryStatisticsReportEntry, salaryStatisticsEntry);
88              reportSet.add(orgSalaryStatisticsReportEntry);
89          }
90          
91          return reportSet;
92      }
93  
94  
95      /**
96       * builds report Header
97       * 
98       * @param BudgetConstructionObjectDump bcod
99       */
100     public void buildReportsHeader(Integer universityFiscalYear, String objectCodes, BudgetConstructionOrgSalaryStatisticsReport orgSalaryStatisticsReportEntry, BudgetConstructionSalaryTotal salaryTotalEntry) {
101 
102         // set fiscal year
103         Integer prevFiscalyear = universityFiscalYear - 1;
104         orgSalaryStatisticsReportEntry.setFiscalYear(prevFiscalyear.toString() + "-" + universityFiscalYear.toString().substring(2, 4));
105         // get Chart with orgChartCode
106         Map searchCriteria = new HashMap();
107         searchCriteria.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, salaryTotalEntry.getOrganizationChartOfAccountsCode());
108         Chart chart = (Chart) businessObjectService.findByPrimaryKey(Chart.class, searchCriteria);
109 
110         // set OrgCode and Desc
111         String orgName = salaryTotalEntry.getOrganization().getOrganizationName();
112         orgSalaryStatisticsReportEntry.setOrganizationCode(salaryTotalEntry.getOrganizationCode());
113         if (orgName == null) {
114             orgSalaryStatisticsReportEntry.setOrganizationName(kualiConfigurationService.getPropertyValueAsString(BCKeyConstants.ERROR_REPORT_GETTING_ORGANIZATION_NAME));
115         }
116         else {
117             orgSalaryStatisticsReportEntry.setOrganizationName(orgName);
118         }
119         // set ChartCode and Desc
120         if (chart == null) {
121             orgSalaryStatisticsReportEntry.setChartOfAccountDescription(kualiConfigurationService.getPropertyValueAsString(BCKeyConstants.ERROR_REPORT_GETTING_CHART_DESCRIPTION));
122             orgSalaryStatisticsReportEntry.setChartOfAccountsCode(kualiConfigurationService.getPropertyValueAsString(BCKeyConstants.ERROR_REPORT_GETTING_CHART_DESCRIPTION));
123         }
124         else {
125             orgSalaryStatisticsReportEntry.setChartOfAccountsCode(chart.getChartOfAccountsCode());
126             orgSalaryStatisticsReportEntry.setChartOfAccountDescription(chart.getFinChartOfAccountDescription());
127         }
128         Integer prevPrevFiscalyear = prevFiscalyear - 1;
129         orgSalaryStatisticsReportEntry.setObjectCodes(objectCodes);
130 
131         // place holder for possible threshold use
132         orgSalaryStatisticsReportEntry.setThreshold("");
133     }
134 
135 
136     public void buildReportsBody(BudgetConstructionOrgSalaryStatisticsReport orgSalaryStatisticsReportEntry, BudgetConstructionSalaryTotal salaryTotalEntry) {
137         orgSalaryStatisticsReportEntry.setInitialRequestedFteQuantity(salaryTotalEntry.getInitialRequestedFteQuantity());
138         
139         orgSalaryStatisticsReportEntry.setTotalInitialRequestedAmount(BudgetConstructionReportHelper.convertKualiInteger(salaryTotalEntry.getInitialRequestedAmount()));
140 
141         BigDecimal averageAmount = BudgetConstructionReportHelper.calculateDivide(salaryTotalEntry.getInitialRequestedAmount().bigDecimalValue(), salaryTotalEntry.getInitialRequestedFteQuantity());
142         orgSalaryStatisticsReportEntry.setTotalAverageAmount(BudgetConstructionReportHelper.setDecimalDigit(averageAmount, 0, false).intValue());
143         
144         BigDecimal requestedFteQuantity = salaryTotalEntry.getAppointmentRequestedFteQuantity().setScale(5, BigDecimal.ROUND_HALF_UP);
145         orgSalaryStatisticsReportEntry.setAppointmentRequestedFteQuantity(requestedFteQuantity);
146         
147         orgSalaryStatisticsReportEntry.setTotalCsfAmount(BudgetConstructionReportHelper.convertKualiInteger(salaryTotalEntry.getCsfAmount()));
148         orgSalaryStatisticsReportEntry.setTotalAppointmentRequestedAmount(BudgetConstructionReportHelper.convertKualiInteger(salaryTotalEntry.getAppointmentRequestedAmount()));
149 
150         BigDecimal csfAmount = new BigDecimal(BudgetConstructionReportHelper.convertKualiInteger(salaryTotalEntry.getCsfAmount()));
151         BigDecimal averageCfsAmount = BudgetConstructionReportHelper.calculateDivide(csfAmount, salaryTotalEntry.getAppointmentRequestedFteQuantity());
152         orgSalaryStatisticsReportEntry.setAverageCsfAmount(BudgetConstructionReportHelper.setDecimalDigit(averageCfsAmount, 0, false));
153         
154         BigDecimal appointmentRequestedAmount = new BigDecimal(BudgetConstructionReportHelper.convertKualiInteger(salaryTotalEntry.getAppointmentRequestedAmount()));
155         BigDecimal averageRequestedAmount = BudgetConstructionReportHelper.calculateDivide(appointmentRequestedAmount, requestedFteQuantity);
156         orgSalaryStatisticsReportEntry.setAverageAppointmentRequestedAmount(BudgetConstructionReportHelper.setDecimalDigit(averageRequestedAmount, 0, false));
157         
158         BigDecimal averageChange = orgSalaryStatisticsReportEntry.getAverageAppointmentRequestedAmount().subtract(orgSalaryStatisticsReportEntry.getAverageCsfAmount());
159         orgSalaryStatisticsReportEntry.setAverageChange(averageChange);
160       
161         BigDecimal percentChange = BudgetConstructionReportHelper.calculatePercent(orgSalaryStatisticsReportEntry.getAverageChange(), orgSalaryStatisticsReportEntry.getAverageCsfAmount());
162         orgSalaryStatisticsReportEntry.setPercentChange(percentChange);
163     }
164 
165     /**
166      * builds orderByList for sort order.
167      * 
168      * @return returnList
169      */
170     public List<String> buildOrderByList() {
171         List<String> returnList = new ArrayList();
172         returnList.add(KFSPropertyConstants.ORGANIZATION_CHART_OF_ACCOUNTS_CODE);
173         returnList.add(KFSPropertyConstants.ORGANIZATION_CODE);
174         return returnList;
175     }
176 
177 
178     public void setBudgetConstructionSalaryStatisticsReportDao(BudgetConstructionSalaryStatisticsReportDao budgetConstructionSalaryStatisticsReportDao) {
179         this.budgetConstructionSalaryStatisticsReportDao = budgetConstructionSalaryStatisticsReportDao;
180     }
181 
182     public void setBudgetConstructionOrganizationReportsService(BudgetConstructionOrganizationReportsService budgetConstructionOrganizationReportsService) {
183         this.budgetConstructionOrganizationReportsService = budgetConstructionOrganizationReportsService;
184     }
185 
186 
187     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
188         this.businessObjectService = businessObjectService;
189     }
190 
191     public void setConfigurationService(ConfigurationService kualiConfigurationService) {
192         this.kualiConfigurationService = kualiConfigurationService;
193     }
194 
195     /**
196      * Sets the budgetConstructionReportsServiceHelper attribute value.
197      * @param budgetConstructionReportsServiceHelper The budgetConstructionReportsServiceHelper to set.
198      */
199     public void setBudgetConstructionReportsServiceHelper(BudgetConstructionReportsServiceHelper budgetConstructionReportsServiceHelper) {
200         this.budgetConstructionReportsServiceHelper = budgetConstructionReportsServiceHelper;
201     }
202 
203     /**
204      * Gets the persistenceServiceOjb attribute.
205      * 
206      * @return Returns the persistenceServiceOjb
207      */
208     
209     public PersistenceService getPersistenceServiceOjb() {
210         return persistenceServiceOjb;
211     }
212 
213     /**	
214      * Sets the persistenceServiceOjb attribute.
215      * 
216      * @param persistenceServiceOjb The persistenceServiceOjb to set.
217      */
218     public void setPersistenceServiceOjb(PersistenceService persistenceServiceOjb) {
219         this.persistenceServiceOjb = persistenceServiceOjb;
220     }
221 
222 }
223