001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.time.dept.lunch.service; 017 018 import org.apache.log4j.Logger; 019 import org.kuali.hr.time.dept.lunch.DeptLunchRule; 020 import org.kuali.hr.time.dept.lunch.dao.DepartmentLunchRuleDao; 021 import org.kuali.hr.time.service.base.TkServiceLocator; 022 import org.kuali.hr.time.timeblock.TimeBlock; 023 import org.kuali.hr.time.timeblock.TimeHourDetail; 024 import org.kuali.hr.time.util.TKUtils; 025 import org.kuali.hr.time.util.TkConstants; 026 import org.kuali.hr.time.workflow.TimesheetDocumentHeader; 027 028 import java.math.BigDecimal; 029 import java.sql.Date; 030 import java.util.List; 031 032 public class DepartmentLunchRuleServiceImpl implements DepartmentLunchRuleService { 033 public DepartmentLunchRuleDao deptLunchRuleDao; 034 private static final Logger LOG = Logger.getLogger(DepartmentLunchRuleServiceImpl.class); 035 036 @Override 037 public DeptLunchRule getDepartmentLunchRule(String dept, Long workArea, 038 String principalId, Long jobNumber, Date asOfDate) { 039 DeptLunchRule deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, workArea, principalId, jobNumber, asOfDate); 040 if(deptLunchRule!=null){ 041 return deptLunchRule; 042 } 043 deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, workArea, principalId, -1L, asOfDate); 044 if(deptLunchRule!=null){ 045 return deptLunchRule; 046 } 047 deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, workArea, "%", -1L, asOfDate); 048 049 if(deptLunchRule!=null){ 050 return deptLunchRule; 051 } 052 deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, -1L, "%", -1L, asOfDate); 053 054 if(deptLunchRule!=null){ 055 return deptLunchRule; 056 } 057 deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, -1L, principalId, -1L, asOfDate); 058 return deptLunchRule; 059 } 060 061 /** 062 * If the hours is greater or equal than the shift hours, deduct the hour from the deduction_mins field 063 */ 064 @Override 065 public void applyDepartmentLunchRule(List<TimeBlock> timeblocks) { 066 for(TimeBlock timeBlock : timeblocks) { 067 if (timeBlock.isLunchDeleted()) { 068 continue; 069 } 070 TimesheetDocumentHeader doc = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(timeBlock.getDocumentId()); 071 String dept = TkServiceLocator.getJobService().getJob(doc.getPrincipalId(), timeBlock.getJobNumber(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime())).getDept(); 072 DeptLunchRule deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule(dept, timeBlock.getWorkArea(), doc.getPrincipalId(), timeBlock.getJobNumber(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime())); 073 if(timeBlock.getClockLogCreated() && deptLunchRule!= null && deptLunchRule.getDeductionMins() != null && timeBlock.getHours().compareTo(deptLunchRule.getShiftHours()) >= 0) { 074 applyLunchRuleToDetails(timeBlock, deptLunchRule); 075 } 076 } 077 } 078 079 private void applyLunchRuleToDetails(TimeBlock block, DeptLunchRule rule) { 080 List<TimeHourDetail> details = block.getTimeHourDetails(); 081 // TODO : Assumption here is that there will be one time hour detail -- May not be accurate. 082 if (details.size() == 1) { 083 TimeHourDetail detail = details.get(0); 084 085 BigDecimal lunchHours = TKUtils.convertMinutesToHours(rule.getDeductionMins()); 086 BigDecimal newHours = detail.getHours().subtract(lunchHours).setScale(TkConstants.BIG_DECIMAL_SCALE, TkConstants.BIG_DECIMAL_SCALE_ROUNDING); 087 detail.setHours(newHours); 088 089 TimeHourDetail lunchDetail = new TimeHourDetail(); 090 lunchDetail.setHours(lunchHours.multiply(TkConstants.BIG_DECIMAL_NEGATIVE_ONE)); 091 lunchDetail.setEarnCode(TkConstants.LUNCH_EARN_CODE); 092 lunchDetail.setTkTimeBlockId(block.getTkTimeBlockId()); 093 094 //Deduct from total for worked hours 095 block.setHours(block.getHours().subtract(lunchHours,TkConstants.MATH_CONTEXT)); 096 097 details.add(lunchDetail); 098 } else { 099 // 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 @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 }