View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.dept.lunch.service;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.hr.time.dept.lunch.DeptLunchRule;
20  import org.kuali.hr.time.dept.lunch.dao.DepartmentLunchRuleDao;
21  import org.kuali.hr.time.service.base.TkServiceLocator;
22  import org.kuali.hr.time.timeblock.TimeBlock;
23  import org.kuali.hr.time.timeblock.TimeHourDetail;
24  import org.kuali.hr.time.util.TKUtils;
25  import org.kuali.hr.time.util.TkConstants;
26  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
27  
28  import java.math.BigDecimal;
29  import java.sql.Date;
30  import java.util.List;
31  
32  public class DepartmentLunchRuleServiceImpl implements DepartmentLunchRuleService {
33  	public DepartmentLunchRuleDao deptLunchRuleDao;
34  	private static final Logger LOG = Logger.getLogger(DepartmentLunchRuleServiceImpl.class);
35  
36  	@Override
37  	public DeptLunchRule getDepartmentLunchRule(String dept, Long workArea,
38  			String principalId, Long jobNumber, Date asOfDate) {
39  		DeptLunchRule deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, workArea, principalId, jobNumber, asOfDate);
40  		if(deptLunchRule!=null){
41  			return deptLunchRule;
42  		}
43  		deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, workArea, principalId, -1L, asOfDate);
44  		if(deptLunchRule!=null){
45  			return deptLunchRule;
46  		}
47  		deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, workArea, "%", -1L, asOfDate);
48  
49  		if(deptLunchRule!=null){
50  			return deptLunchRule;
51  		}
52          deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, -1L, principalId, -1L, asOfDate);
53  
54  		if(deptLunchRule!=null){
55  			return deptLunchRule;
56  		}
57          deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, -1L, "%", -1L, asOfDate);
58  		return deptLunchRule;
59  	}
60  
61      @Override
62      public DeptLunchRule getDepartmentLunchRuleNoWildCards(String dept, Long workArea,
63                                                  String principalId, Long jobNumber, Date asOfDate) {
64          return deptLunchRuleDao.getDepartmentLunchRule(dept, workArea, principalId, jobNumber, asOfDate);
65      }
66  
67  	/**
68  	 * If the hours is greater or equal than the shift hours, deduct the hour from the deduction_mins field
69  	 */
70  	@Override
71  	public void applyDepartmentLunchRule(List<TimeBlock> timeblocks) {
72  		for(TimeBlock timeBlock : timeblocks) {
73              if (timeBlock.isLunchDeleted()) {
74                  continue;
75              }
76  			TimesheetDocumentHeader doc = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(timeBlock.getDocumentId());
77  			String dept = TkServiceLocator.getJobService().getJob(doc.getPrincipalId(), timeBlock.getJobNumber(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime())).getDept();
78  			DeptLunchRule deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule(dept, timeBlock.getWorkArea(), doc.getPrincipalId(), timeBlock.getJobNumber(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime()));
79  			if(timeBlock.getClockLogCreated() && deptLunchRule!= null && deptLunchRule.getDeductionMins() != null && timeBlock.getHours().compareTo(deptLunchRule.getShiftHours()) >= 0) {
80                  applyLunchRuleToDetails(timeBlock, deptLunchRule);
81  			}
82  		}
83  	}
84  
85      private void applyLunchRuleToDetails(TimeBlock block, DeptLunchRule rule) {
86          List<TimeHourDetail> details = block.getTimeHourDetails();
87          // TODO : Assumption here is that there will be one time hour detail -- May not be accurate.
88          if (details.size() == 1) {
89              TimeHourDetail detail = details.get(0);
90  
91              BigDecimal lunchHours = TKUtils.convertMinutesToHours(rule.getDeductionMins());
92              BigDecimal newHours = detail.getHours().subtract(lunchHours).setScale(TkConstants.BIG_DECIMAL_SCALE, TkConstants.BIG_DECIMAL_SCALE_ROUNDING);
93              detail.setHours(newHours);
94  
95              TimeHourDetail lunchDetail = new TimeHourDetail();
96              lunchDetail.setHours(lunchHours.multiply(TkConstants.BIG_DECIMAL_NEGATIVE_ONE));
97              lunchDetail.setEarnCode(TkConstants.LUNCH_EARN_CODE);
98              lunchDetail.setTkTimeBlockId(block.getTkTimeBlockId());
99              
100             //Deduct from total for worked hours
101             block.setHours(block.getHours().subtract(lunchHours,TkConstants.MATH_CONTEXT));
102             
103             details.add(lunchDetail);
104         } else {
105             // TODO : Determine what to do in this case.
106             LOG.warn("Hour details size > 1 in Lunch rule application.");
107         }
108     }
109 
110 	public DepartmentLunchRuleDao getDeptLunchRuleDao() {
111 		return deptLunchRuleDao;
112 	}
113 
114 
115 	public void setDeptLunchRuleDao(DepartmentLunchRuleDao deptLunchRuleDao) {
116 		this.deptLunchRuleDao = deptLunchRuleDao;
117 	}
118 
119 	@Override
120 	public DeptLunchRule getDepartmentLunchRule(String tkDeptLunchRuleId) {
121 		return deptLunchRuleDao.getDepartmentLunchRule(tkDeptLunchRuleId);
122 	}
123 
124     @Override
125     public List<DeptLunchRule> getDepartmentLunchRules(String dept, String workArea, String principalId, String jobNumber,
126                                                        Date fromEffdt, Date toEffdt, String active, String showHistory) {
127         return deptLunchRuleDao.getDepartmentLunchRules(dept, workArea, principalId, jobNumber, fromEffdt, toEffdt, active, showHistory);
128     }
129 }