1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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          
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              
95              block.setHours(block.getHours().subtract(lunchHours,TkConstants.MATH_CONTEXT));
96              
97              details.add(lunchDetail);
98          } else {
99              
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     @Override
119     public List<DeptLunchRule> getDepartmentLunchRules(String dept, String workArea, String principalId, String jobNumber, String active) {
120         return deptLunchRuleDao.getDepartmentLunchRules(dept, workArea, principalId, jobNumber, active);
121     }
122 }