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, principalId, -1L, asOfDate); 053 054 if(deptLunchRule!=null){ 055 return deptLunchRule; 056 } 057 deptLunchRule = deptLunchRuleDao.getDepartmentLunchRule(dept, -1L, "%", -1L, asOfDate); 058 return deptLunchRule; 059 } 060 061 @Override 062 public DeptLunchRule getDepartmentLunchRuleNoWildCards(String dept, Long workArea, 063 String principalId, Long jobNumber, Date asOfDate) { 064 return deptLunchRuleDao.getDepartmentLunchRule(dept, workArea, principalId, jobNumber, asOfDate); 065 } 066 067 /** 068 * If the hours is greater or equal than the shift hours, deduct the hour from the deduction_mins field 069 */ 070 @Override 071 public void applyDepartmentLunchRule(List<TimeBlock> timeblocks) { 072 for(TimeBlock timeBlock : timeblocks) { 073 if (timeBlock.isLunchDeleted()) { 074 continue; 075 } 076 TimesheetDocumentHeader doc = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(timeBlock.getDocumentId()); 077 String dept = TkServiceLocator.getJobService().getJob(doc.getPrincipalId(), timeBlock.getJobNumber(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime())).getDept(); 078 DeptLunchRule deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule(dept, timeBlock.getWorkArea(), doc.getPrincipalId(), timeBlock.getJobNumber(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime())); 079 if(timeBlock.getClockLogCreated() && deptLunchRule!= null && deptLunchRule.getDeductionMins() != null && timeBlock.getHours().compareTo(deptLunchRule.getShiftHours()) >= 0) { 080 applyLunchRuleToDetails(timeBlock, deptLunchRule); 081 } 082 } 083 } 084 085 private void applyLunchRuleToDetails(TimeBlock block, DeptLunchRule rule) { 086 List<TimeHourDetail> details = block.getTimeHourDetails(); 087 // TODO : Assumption here is that there will be one time hour detail -- May not be accurate. 088 if (details.size() == 1) { 089 TimeHourDetail detail = details.get(0); 090 091 BigDecimal lunchHours = TKUtils.convertMinutesToHours(rule.getDeductionMins()); 092 BigDecimal newHours = detail.getHours().subtract(lunchHours).setScale(TkConstants.BIG_DECIMAL_SCALE, TkConstants.BIG_DECIMAL_SCALE_ROUNDING); 093 detail.setHours(newHours); 094 095 TimeHourDetail lunchDetail = new TimeHourDetail(); 096 lunchDetail.setHours(lunchHours.multiply(TkConstants.BIG_DECIMAL_NEGATIVE_ONE)); 097 lunchDetail.setEarnCode(TkConstants.LUNCH_EARN_CODE); 098 lunchDetail.setTkTimeBlockId(block.getTkTimeBlockId()); 099 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 }