View Javadoc

1   /**
2    * Copyright 2004-2012 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, "%", -1L, asOfDate);
53  
54  		if(deptLunchRule!=null){
55  			return deptLunchRule;
56  		}
57  		deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, -1L, principalId, -1L, asOfDate);
58  		return deptLunchRule;
59  	}
60  
61  	/**
62  	 * If the hours is greater or equal than the shift hours, deduct the hour from the deduction_mins field
63  	 */
64  	@Override
65  	public void applyDepartmentLunchRule(List<TimeBlock> timeblocks) {
66  		for(TimeBlock timeBlock : timeblocks) {
67              if (timeBlock.isLunchDeleted()) {
68                  continue;
69              }
70  			TimesheetDocumentHeader doc = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(timeBlock.getDocumentId());
71  			String dept = TkServiceLocator.getJobService().getJob(doc.getPrincipalId(), timeBlock.getJobNumber(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime())).getDept();
72  			DeptLunchRule deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule(dept, timeBlock.getWorkArea(), doc.getPrincipalId(), timeBlock.getJobNumber(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime()));
73  			if(timeBlock.getClockLogCreated() && deptLunchRule!= null && deptLunchRule.getDeductionMins() != null && timeBlock.getHours().compareTo(deptLunchRule.getShiftHours()) >= 0) {
74                  applyLunchRuleToDetails(timeBlock, deptLunchRule);
75  			}
76  		}
77  	}
78  
79      private void applyLunchRuleToDetails(TimeBlock block, DeptLunchRule rule) {
80          List<TimeHourDetail> details = block.getTimeHourDetails();
81          // TODO : Assumption here is that there will be one time hour detail -- May not be accurate.
82          if (details.size() == 1) {
83              TimeHourDetail detail = details.get(0);
84  
85              BigDecimal lunchHours = TKUtils.convertMinutesToHours(rule.getDeductionMins());
86              BigDecimal newHours = detail.getHours().subtract(lunchHours).setScale(TkConstants.BIG_DECIMAL_SCALE, TkConstants.BIG_DECIMAL_SCALE_ROUNDING);
87              detail.setHours(newHours);
88  
89              TimeHourDetail lunchDetail = new TimeHourDetail();
90              lunchDetail.setHours(lunchHours.multiply(TkConstants.BIG_DECIMAL_NEGATIVE_ONE));
91              lunchDetail.setEarnCode(TkConstants.LUNCH_EARN_CODE);
92              lunchDetail.setTkTimeBlockId(block.getTkTimeBlockId());
93              
94              //Deduct from total for worked hours
95              block.setHours(block.getHours().subtract(lunchHours,TkConstants.MATH_CONTEXT));
96              
97              details.add(lunchDetail);
98          } else {
99              // TODO : Determine what to do in this case.
100             LOG.warn("Hour details size > 1 in Lunch rule application.");
101         }
102     }
103 
104 	public DepartmentLunchRuleDao getDeptLunchRuleDao() {
105 		return deptLunchRuleDao;
106 	}
107 
108 
109 	public void setDeptLunchRuleDao(DepartmentLunchRuleDao deptLunchRuleDao) {
110 		this.deptLunchRuleDao = deptLunchRuleDao;
111 	}
112 
113 	@Override
114 	public DeptLunchRule getDepartmentLunchRule(String tkDeptLunchRuleId) {
115 		return deptLunchRuleDao.getDepartmentLunchRule(tkDeptLunchRuleId);
116 	}
117 
118 }