1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.timeblock.service;
17
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.commons.lang.ObjectUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.log4j.Logger;
29 import org.joda.time.DateTime;
30 import org.joda.time.DateTimeConstants;
31 import org.joda.time.DateTimeZone;
32 import org.joda.time.Interval;
33 import org.joda.time.LocalDate;
34 import org.kuali.kpme.core.KPMENamespace;
35 import org.kuali.kpme.core.assignment.Assignment;
36 import org.kuali.kpme.core.earncode.EarnCode;
37 import org.kuali.kpme.core.earncode.security.EarnCodeSecurity;
38 import org.kuali.kpme.core.job.Job;
39 import org.kuali.kpme.core.paytype.PayType;
40 import org.kuali.kpme.core.role.KPMERole;
41 import org.kuali.kpme.core.service.HrServiceLocator;
42 import org.kuali.kpme.core.util.HrConstants;
43 import org.kuali.kpme.core.util.HrContext;
44 import org.kuali.kpme.core.util.TKUtils;
45 import org.kuali.kpme.tklm.common.TkConstants;
46 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
47 import org.kuali.kpme.core.block.CalendarBlockPermissions;
48 import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
49 import org.kuali.kpme.tklm.time.timeblock.TimeBlockHistory;
50 import org.kuali.kpme.tklm.time.timeblock.dao.TimeBlockDao;
51 import org.kuali.kpme.tklm.time.timehourdetail.TimeHourDetail;
52 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
53 import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
54 import org.kuali.rice.krad.service.KRADServiceLocator;
55 import org.kuali.rice.krad.util.GlobalVariables;
56
57 public class TimeBlockServiceImpl implements TimeBlockService {
58
59 private static final Logger LOG = Logger.getLogger(TimeBlockServiceImpl.class);
60 private TimeBlockDao timeBlockDao;
61
62 public void setTimeBlockDao(TimeBlockDao timeBlockDao) {
63 this.timeBlockDao = timeBlockDao;
64 }
65
66
67 public List<TimeBlock> buildTimeBlocksSpanDates(Assignment assignment, String earnCode, TimesheetDocument timesheetDocument,
68 DateTime beginDateTime, DateTime endDateTime, BigDecimal hours, BigDecimal amount,
69 Boolean getClockLogCreated, Boolean getLunchDeleted, String userPrincipalId,
70 String clockLogBeginId, String clockLogEndId) {
71 DateTimeZone zone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
72 DateTime beginDt = beginDateTime.withZone(zone);
73 DateTime endDt = beginDt.toLocalDate().toDateTime(endDateTime.withZone(zone).toLocalTime(), zone);
74 if (endDt.isBefore(beginDt) || endDt.isEqual(beginDt.toDateMidnight())) endDt = endDt.plusDays(1);
75
76 List<Interval> dayInt = TKUtils.getDaySpanForCalendarEntry(timesheetDocument.getCalendarEntry());
77 TimeBlock firstTimeBlock = new TimeBlock();
78 List<TimeBlock> lstTimeBlocks = new ArrayList<TimeBlock>();
79
80 DateTime endOfFirstDay = null;
81 long diffInMillis = 0;
82
83 for (Interval dayIn : dayInt) {
84 if (dayIn.contains(beginDt)) {
85 if (dayIn.contains(endDt) || dayIn.getEnd().equals(endDt)) {
86 firstTimeBlock = createTimeBlock(timesheetDocument, beginDateTime, endDt, assignment, earnCode,
87 hours, amount, getClockLogCreated, getLunchDeleted, userPrincipalId, clockLogBeginId, clockLogEndId);
88 lstTimeBlocks.add(firstTimeBlock);
89 } else {
90
91
92 }
93 }
94 }
95
96 DateTime endTime = endDateTime.withZone(zone);
97 if (firstTimeBlock.getEndDateTime() != null) {
98 endOfFirstDay = firstTimeBlock.getEndDateTime().withZone(zone);
99 diffInMillis = endOfFirstDay.minus(beginDt.getMillis()).getMillis();
100 }
101 DateTime currTime = beginDt.plusDays(1);
102 while (currTime.isBefore(endTime) || currTime.isEqual(endTime)) {
103 TimeBlock tb = createTimeBlock(timesheetDocument, currTime, currTime.plus(diffInMillis), assignment, earnCode,
104 hours, amount, getClockLogCreated, getLunchDeleted, userPrincipalId, clockLogBeginId, clockLogEndId);
105 lstTimeBlocks.add(tb);
106 currTime = currTime.plusDays(1);
107 }
108 return lstTimeBlocks;
109 }
110
111
112 public List<TimeBlock> buildTimeBlocks(Assignment assignment, String earnCode, TimesheetDocument timesheetDocument,
113 DateTime beginDateTime, DateTime endDateTime, BigDecimal hours, BigDecimal amount,
114 Boolean getClockLogCreated, Boolean getLunchDeleted, String userPrincipalId,
115 String clockLogBeginId, String clockLogEndId) {
116
117
118
119 Interval firstDay = null;
120 DateTimeZone userTimeZone = DateTimeZone.forID(HrServiceLocator.getTimezoneService().getUserTimezone(timesheetDocument.getPrincipalId()));
121 if(userTimeZone == null)
122 userTimeZone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
123
124 List<Interval> dayIntervals = TKUtils.getDaySpanForCalendarEntry(timesheetDocument.getCalendarEntry(), userTimeZone);
125
126 List<TimeBlock> lstTimeBlocks = new ArrayList<TimeBlock>();
127 DateTime currentDateTime = beginDateTime;
128
129 for (Interval dayInt : dayIntervals) {
130
131 if (firstDay != null) {
132 if(!dayInt.contains(endDateTime)){
133 currentDateTime = dayInt.getStart();
134 } else if((dayInt.getStartMillis() - endDateTime.getMillis()) != 0){
135 TimeBlock tb = createTimeBlock(timesheetDocument, dayInt.getStart(), endDateTime, assignment, earnCode,
136 hours, amount, getClockLogCreated, getLunchDeleted, userPrincipalId, clockLogBeginId, clockLogEndId);
137 lstTimeBlocks.add(tb);
138 break;
139 }
140 }
141 if (dayInt.contains(currentDateTime)) {
142 firstDay = dayInt;
143
144
145
146 if (dayInt.contains(endDateTime) || (endDateTime.getMillis() == dayInt.getEnd().getMillis())) {
147
148 TimeBlock tb = createTimeBlock(timesheetDocument, currentDateTime, endDateTime, assignment, earnCode,
149 hours, amount, getClockLogCreated, getLunchDeleted, userPrincipalId, clockLogBeginId, clockLogEndId);
150 tb.setBeginDateTime(currentDateTime);
151 tb.setEndDateTime(endDateTime);
152 lstTimeBlocks.add(tb);
153 break;
154 } else {
155
156 TimeBlock tb = createTimeBlock(timesheetDocument, currentDateTime, dayInt.getEnd(), assignment, earnCode,
157 hours, amount, getClockLogCreated, getLunchDeleted, userPrincipalId, clockLogBeginId, clockLogEndId);
158 tb.setBeginDateTime(currentDateTime);
159 tb.setEndDateTime(firstDay.getEnd());
160 lstTimeBlocks.add(tb);
161 }
162 }
163 }
164 return lstTimeBlocks;
165 }
166
167 public void saveTimeBlocks(List<TimeBlock> oldTimeBlocks, List<TimeBlock> newTimeBlocks, String userPrincipalId) {
168 List<TimeBlock> alteredTimeBlocks = new ArrayList<TimeBlock>();
169 for (TimeBlock tb : newTimeBlocks) {
170 boolean persist = true;
171 for (TimeBlock tbOld : oldTimeBlocks) {
172 HrServiceLocator.getHRPermissionService().updateTimeBlockPermissions(CalendarBlockPermissions.newInstance(tbOld.getTkTimeBlockId()));
173 if (tb.equals(tbOld)) {
174 persist = false;
175 break;
176 }
177 }
178 if (persist) {
179 alteredTimeBlocks.add(tb);
180 }
181 }
182
183 Set<String> timeBlockIds = new HashSet<String>();
184
185 for (TimeBlock timeBlock : alteredTimeBlocks) {
186 if(timeBlock.getTkTimeBlockId() != null) {
187 timeBlockIds.add(timeBlock.getTkTimeBlockId());
188 }
189 TkServiceLocator.getTimeHourDetailService().removeTimeHourDetails(timeBlock.getTkTimeBlockId());
190 timeBlock.setUserPrincipalId(userPrincipalId);
191 }
192
193 List<TimeBlock> savedTimeBlocks = (List<TimeBlock>) KRADServiceLocator.getBusinessObjectService().save(alteredTimeBlocks);
194
195 for (TimeBlock timeBlock : savedTimeBlocks) {
196 if(!timeBlockIds.contains(timeBlock.getTkTimeBlockId())) {
197 timeBlock.setTimeBlockHistories(createTimeBlockHistories(timeBlock, TkConstants.ACTIONS.ADD_TIME_BLOCK));
198 KRADServiceLocator.getBusinessObjectService().save(timeBlock.getTimeBlockHistories());
199 } else {
200 timeBlock.setTimeBlockHistories(createTimeBlockHistories(timeBlock, TkConstants.ACTIONS.UPDATE_TIME_BLOCK));
201 KRADServiceLocator.getBusinessObjectService().save(timeBlock.getTimeBlockHistories());
202 }
203 }
204 }
205
206 public void saveTimeBlocks(List<TimeBlock> tbList) {
207 for (TimeBlock tb : tbList) {
208 if (StringUtils.isNotEmpty(tb.getTkTimeBlockId())) {
209 HrServiceLocator.getHRPermissionService().updateTimeBlockPermissions(CalendarBlockPermissions.newInstance(tb.getTkTimeBlockId()));
210 }
211 TkServiceLocator.getTimeHourDetailService().removeTimeHourDetails(tb.getTkTimeBlockId());
212 timeBlockDao.saveOrUpdate(tb);
213 for(TimeBlockHistory tbh : tb.getTimeBlockHistories()){
214 TkServiceLocator.getTimeBlockHistoryService().saveTimeBlockHistory(tbh);
215 }
216 }
217 }
218
219 public void updateTimeBlock(TimeBlock tb) {
220 timeBlockDao.saveOrUpdate(tb);
221 }
222
223
224 public TimeBlock createTimeBlock(TimesheetDocument timesheetDocument, DateTime beginDateTime, DateTime endDateTime, Assignment assignment, String earnCode, BigDecimal hours, BigDecimal amount, Boolean clockLogCreated, Boolean lunchDeleted, String userPrincipalId) {
225 DateTimeZone timezone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
226 EarnCode earnCodeObj = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, timesheetDocument.getAsOfDate());
227
228 TimeBlock tb = new TimeBlock();
229 tb.setDocumentId(timesheetDocument.getDocumentHeader().getDocumentId());
230 tb.setPrincipalId(timesheetDocument.getPrincipalId());
231 tb.setJobNumber(assignment.getJobNumber());
232 tb.setWorkArea(assignment.getWorkArea());
233 tb.setTask(assignment.getTask());
234 tb.setEarnCode(earnCode);
235 tb.setBeginDateTime(beginDateTime);
236 tb.setEndDateTime(endDateTime);
237 tb.setBeginTimeDisplay(tb.getBeginDateTime().withZone(timezone));
238 tb.setEndTimeDisplay(tb.getEndDateTime().withZone(timezone));
239
240 if(hours == null || hours.compareTo(BigDecimal.ZERO) == 0) {
241 hours = TKUtils.getHoursBetween(beginDateTime.getMillis(), endDateTime.getMillis());
242 }
243 tb.setAmount(amount);
244
245
246
247
248 tb.setEarnCodeType(earnCodeObj.getEarnCodeType());
249 tb.setHours(hours);
250 tb.setClockLogCreated(clockLogCreated);
251 tb.setUserPrincipalId(userPrincipalId);
252 tb.setTimestamp(TKUtils.getCurrentTimestamp());
253 tb.setLunchDeleted(lunchDeleted);
254
255 tb.setTimeHourDetails(this.createTimeHourDetails(earnCodeObj, hours, amount, tb.getTkTimeBlockId(),true));
256
257 return tb;
258 }
259
260 public TimeBlock createTimeBlock(TimesheetDocument timesheetDocument, DateTime beginDateTime, DateTime endDateTime,
261 Assignment assignment, String earnCode, BigDecimal hours, BigDecimal amount,
262 Boolean clockLogCreated, Boolean lunchDeleted, String userPrincipalId,
263 String clockLogBeginId, String clockLogEndId) {
264 TimeBlock tb = createTimeBlock(timesheetDocument, beginDateTime, endDateTime, assignment, earnCode, hours, amount, clockLogCreated,
265 lunchDeleted, userPrincipalId);
266 tb.setClockLogBeginId(clockLogBeginId);
267 tb.setClockLogEndId(clockLogEndId);
268 tb.assignClockedByMissedPunch();
269
270 return tb;
271 }
272
273 public TimeBlock getTimeBlock(String tkTimeBlockId) {
274 return timeBlockDao.getTimeBlock(tkTimeBlockId);
275 }
276
277 @Override
278 public void deleteTimeBlock(TimeBlock timeBlock) {
279 timeBlockDao.deleteTimeBlock(timeBlock);
280
281 }
282
283 public void resetTimeHourDetail(List<TimeBlock> origTimeBlocks) {
284 Collections.sort(origTimeBlocks,new Comparator<TimeBlock>() {
285
286 @Override
287 public int compare(TimeBlock o1, TimeBlock o2) {
288 return o1.getEndDateTime().compareTo(o2.getEndDateTime().toInstant());
289 }
290
291 });
292 TimeBlock previousTimeBlock = null;
293 for (TimeBlock tb : origTimeBlocks) {
294 EarnCode earnCodeObj = HrServiceLocator.getEarnCodeService().getEarnCode(tb.getEarnCode(), tb.getBeginDateTime().toLocalDate());
295 if (tb.getBeginTime() != null && tb.getEndTime() != null && StringUtils.equals(tb.getEarnCodeType(), HrConstants.EARN_CODE_TIME)) {
296 BigDecimal hours = TKUtils.getHoursBetween(tb.getBeginTime().getTime(), tb.getEndTime().getTime());
297
298
299
300
301 if(previousTimeBlock != null && StringUtils.equals(earnCodeObj.getEarnCode(),previousTimeBlock.getEarnCode()) &&
302 (tb.getBeginTime().getTime() - previousTimeBlock.getEndTime().getTime() == 0L)) {
303 List<TimeHourDetail> newDetails = new ArrayList<TimeHourDetail>();
304 BigDecimal prevTimeBlockHours = TKUtils.getHoursBetween(previousTimeBlock.getBeginTime().getTime(), previousTimeBlock.getEndTime().getTime());
305 previousTimeBlock.setHours(prevTimeBlockHours);
306 BigDecimal cummulativeHours = prevTimeBlockHours.add(hours, HrConstants.MATH_CONTEXT);
307
308 previousTimeBlock.setTimeHourDetails(this.createTimeHourDetails(earnCodeObj, prevTimeBlockHours, previousTimeBlock.getAmount(), previousTimeBlock.getTkTimeBlockId(),false));
309
310 if (earnCodeObj.getInflateMinHours() != null) {
311 if ((earnCodeObj.getInflateMinHours().compareTo(BigDecimal.ZERO) != 0) &&
312 earnCodeObj.getInflateMinHours().compareTo(cummulativeHours) > 0) {
313
314
315
316
317 if(earnCodeObj.getInflateMinHours().compareTo(cummulativeHours) > 0) {
318
319 newDetails = this.createTimeHourDetails(earnCodeObj,cummulativeHours,tb.getAmount(),tb.getTkTimeBlockId(),false);
320 TimeHourDetail detail = newDetails.get(0);
321
322 detail.setHours(earnCodeObj.getInflateMinHours().subtract(prevTimeBlockHours));
323 newDetails.clear();
324 newDetails.add(detail);
325 tb.setTimeHourDetails(newDetails);
326 }
327 }
328 }
329
330
331 if(earnCodeObj.getInflateFactor() != null && earnCodeObj.getInflateFactor().compareTo(BigDecimal.ZERO) != 0) {
332
333 TimeHourDetail detail = new TimeHourDetail();
334 if(newDetails.isEmpty()) {
335
336 newDetails = this.createTimeHourDetails(earnCodeObj,hours,tb.getAmount(),tb.getTkTimeBlockId(),false);
337 }
338 detail = newDetails.get(0);
339 BigDecimal newHours = detail.getHours().multiply(earnCodeObj.getInflateFactor(),HrConstants.MATH_CONTEXT);
340 detail.setHours(newHours);
341 newDetails.clear();
342 newDetails.add(detail);
343 tb.setTimeHourDetails(newDetails);
344 newDetails = previousTimeBlock.getTimeHourDetails();
345 detail = newDetails.get(0);
346 detail.setHours(prevTimeBlockHours.multiply(earnCodeObj.getInflateFactor(),HrConstants.MATH_CONTEXT));
347 newDetails.clear();
348 newDetails.add(detail);
349 previousTimeBlock.setTimeHourDetails(newDetails);
350 }
351 }
352 else {
353 tb.setTimeHourDetails(this.createTimeHourDetails(earnCodeObj,tb.getHours(),tb.getAmount(),tb.getTkTimeBlockId(),true));
354 }
355
356 tb.setHours(hours);
357 } else {
358
359 tb.setTimeHourDetails(this.createTimeHourDetails(earnCodeObj,tb.getHours(),tb.getAmount(),tb.getTkTimeBlockId(),true));
360 }
361
362 for(TimeBlockHistory tbh : tb.getTimeBlockHistories()) {
363 TkServiceLocator.getTimeBlockHistoryService().addTimeBlockHistoryDetails(tbh,tb);
364 }
365 previousTimeBlock = tb;
366 }
367 }
368
369 private List<TimeHourDetail> createTimeHourDetails(EarnCode earnCode, BigDecimal hours, BigDecimal amount, String timeBlockId, boolean inflate) {
370 List<TimeHourDetail> timeHourDetails = new ArrayList<TimeHourDetail>();
371
372 TimeHourDetail timeHourDetail = new TimeHourDetail();
373 timeHourDetail.setEarnCode(earnCode.getEarnCode());
374 if(inflate) {
375 timeHourDetail.setHours(this.applyInflateMinHoursAndFactor(earnCode, hours));
376 }
377 else {
378 timeHourDetail.setHours(hours);
379 }
380 timeHourDetail.setAmount(amount);
381 timeHourDetail.setTkTimeBlockId(timeBlockId);
382 timeHourDetails.add(timeHourDetail);
383
384 return timeHourDetails;
385 }
386
387 public List<TimeBlockHistory> createTimeBlockHistories(TimeBlock tb, String actionHistory) {
388 List<TimeBlockHistory> tbhs = new ArrayList<TimeBlockHistory>();
389
390 TimeBlockHistory tbh = new TimeBlockHistory(tb);
391 tbh.setActionHistory(actionHistory);
392
393 TkServiceLocator.getTimeBlockHistoryService().addTimeBlockHistoryDetails(tbh, tb);
394
395 tbhs.add(tbh);
396
397 return tbhs;
398 }
399
400
401
402 public List<TimeBlock> getTimeBlocks(String documentId) {
403 List<TimeBlock> timeBlocks = timeBlockDao.getTimeBlocks(documentId);
404 TimesheetDocumentHeader tdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(documentId);
405 DateTimeZone timezone = DateTimeZone.forID(HrServiceLocator.getTimezoneService().getUserTimezone(tdh.getPrincipalId()));
406 if (timezone == null)
407 timezone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
408 for(TimeBlock tb : timeBlocks) {
409 String earnCodeType = HrServiceLocator.getEarnCodeService().getEarnCodeType(tb.getEarnCode(), tb.getBeginDateTime().toLocalDate());
410 tb.setEarnCodeType(earnCodeType);
411 if(ObjectUtils.equals(timezone, TKUtils.getSystemDateTimeZone())){
412 tb.setBeginTimeDisplay(tb.getBeginDateTime());
413 tb.setEndTimeDisplay(tb.getEndDateTime());
414 }
415 else {
416 tb.setBeginTimeDisplay(tb.getBeginDateTime().withZone(timezone));
417 tb.setEndTimeDisplay(tb.getEndDateTime().withZone(timezone));
418 }
419 }
420
421 return timeBlocks;
422 }
423
424 public List<TimeBlock> getTimeBlocksForAssignment(Assignment assign) {
425 List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>();
426 if(assign != null) {
427 timeBlocks = timeBlockDao.getTimeBlocksForAssignment(assign);
428 }
429 DateTimeZone timezone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
430 for(TimeBlock tb : timeBlocks) {
431 String earnCodeType = HrServiceLocator.getEarnCodeService().getEarnCodeType(tb.getEarnCode(), tb.getBeginDateTime().toLocalDate());
432 tb.setEarnCodeType(earnCodeType);
433 if(ObjectUtils.equals(timezone, TKUtils.getSystemDateTimeZone())){
434 tb.setBeginTimeDisplay(tb.getBeginDateTime());
435 tb.setEndTimeDisplay(tb.getEndDateTime());
436 }
437 else {
438 tb.setBeginTimeDisplay(tb.getBeginDateTime().withZone(timezone));
439 tb.setEndTimeDisplay(tb.getEndDateTime().withZone(timezone));
440 }
441 }
442 return timeBlocks;
443 }
444
445
446 @Override
447 public void deleteTimeBlocksAssociatedWithDocumentId(String documentId) {
448 timeBlockDao.deleteTimeBlocksAssociatedWithDocumentId(documentId);
449 }
450
451 @Override
452
453 public Boolean getTimeBlockEditable(TimeBlock timeBlock) {
454 String userId = GlobalVariables.getUserSession().getPrincipalId();
455
456 if(userId != null) {
457
458 if(HrContext.isSystemAdmin()) {
459 return true;
460 }
461 DateTime date = LocalDate.now().toDateTimeAtStartOfDay();
462 if (HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(userId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), timeBlock.getWorkArea(), date)
463 || HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(userId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), timeBlock.getWorkArea(), date)
464 || HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(userId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), timeBlock.getWorkArea(), date)) {
465
466 Job job = HrServiceLocator.getJobService().getJob(HrContext.getTargetPrincipalId(),timeBlock.getJobNumber(), timeBlock.getEndDateTime().toLocalDate());
467 PayType payType = HrServiceLocator.getPayTypeService().getPayType(job.getHrPayType(), timeBlock.getEndDateTime().toLocalDate());
468 if(StringUtils.equals(payType.getRegEarnCode(), timeBlock.getEarnCode())){
469 return true;
470 }
471
472 List<EarnCodeSecurity> deptEarnCodes = HrServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), timeBlock.getEndDateTime().toLocalDate());
473 for(EarnCodeSecurity dec : deptEarnCodes){
474 if(dec.isApprover() && StringUtils.equals(dec.getEarnCode(), timeBlock.getEarnCode())){
475 return true;
476 }
477 }
478 }
479
480 if(userId.equals(HrContext.getTargetPrincipalId())) {
481 Job job = HrServiceLocator.getJobService().getJob(HrContext.getTargetPrincipalId(),timeBlock.getJobNumber(), timeBlock.getEndDateTime().toLocalDate());
482 PayType payType = HrServiceLocator.getPayTypeService().getPayType(job.getHrPayType(), timeBlock.getEndDateTime().toLocalDate());
483 if(StringUtils.equals(payType.getRegEarnCode(), timeBlock.getEarnCode())){
484 return true;
485 }
486
487 List<EarnCodeSecurity> deptEarnCodes = HrServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), timeBlock.getEndDateTime().toLocalDate());
488 for(EarnCodeSecurity dec : deptEarnCodes){
489 if(dec.isEmployee() && StringUtils.equals(dec.getEarnCode(), timeBlock.getEarnCode())){
490 return true;
491 }
492 }
493
494 }
495
496
497 }
498 return false;
499 }
500
501 @Override
502 public List<TimeBlock> getTimeBlocksForClockLogEndId(String tkClockLogId) {
503 return timeBlockDao.getTimeBlocksForClockLogEndId(tkClockLogId);
504 }
505 @Override
506 public List<TimeBlock> getTimeBlocksForClockLogBeginId(String tkClockLogId) {
507 return timeBlockDao.getTimeBlocksForClockLogBeginId(tkClockLogId);
508 }
509
510 @Override
511 public List<TimeBlock> getLatestEndTimestampForEarnCode(String earnCode){
512 return timeBlockDao.getLatestEndTimestampForEarnCode(earnCode);
513 }
514
515 @Override
516 public List<TimeBlock> getOvernightTimeBlocks(String clockLogEndId) {
517 return timeBlockDao.getOvernightTimeBlocks(clockLogEndId);
518 }
519
520 @Override
521 public void deleteLunchDeduction(String tkTimeHourDetailId) {
522 TimeHourDetail thd = TkServiceLocator.getTimeHourDetailService().getTimeHourDetail(tkTimeHourDetailId);
523 TimeBlock tb = getTimeBlock(thd.getTkTimeBlockId());
524
525 HrServiceLocator.getHRPermissionService().updateTimeBlockPermissions(CalendarBlockPermissions.newInstance(tb.getTkTimeBlockId()));
526
527 tb.setLunchDeleted(true);
528
529 timeBlockDao.saveOrUpdate(tb);
530
531 TkServiceLocator.getTimeHourDetailService().removeTimeHourDetail(thd.getTkTimeHourDetailId());
532 }
533 @Override
534 public List<TimeBlock> getTimeBlocksWithEarnCode(String earnCode, DateTime effDate) {
535 return timeBlockDao.getTimeBlocksWithEarnCode(earnCode, effDate);
536 }
537
538 private BigDecimal applyInflateMinHoursAndFactor(EarnCode earnCodeObj, BigDecimal blockHours) {
539 if(earnCodeObj != null) {
540
541
542
543 if (earnCodeObj.getInflateMinHours() != null) {
544 if ((earnCodeObj.getInflateMinHours().compareTo(BigDecimal.ZERO) != 0) &&
545 earnCodeObj.getInflateMinHours().compareTo(blockHours) > 0) {
546 blockHours = earnCodeObj.getInflateMinHours();
547 }
548 }
549
550 if (earnCodeObj.getInflateFactor() != null) {
551 if ((earnCodeObj.getInflateFactor().compareTo(new BigDecimal(1.0)) != 0)
552 && (earnCodeObj.getInflateFactor().compareTo(BigDecimal.ZERO)!= 0) ) {
553 blockHours = earnCodeObj.getInflateFactor().multiply(blockHours, HrConstants.MATH_CONTEXT).setScale(HrConstants.BIG_DECIMAL_SCALE);
554 }
555 }
556 }
557 return blockHours;
558 }
559
560 @Override
561 public List<TimeBlock> getTimeBlocksForLookup(String documentId,
562 String principalId, String userPrincipalId, LocalDate fromDate,
563 LocalDate toDate) {
564 return timeBlockDao.getTimeBlocksForLookup(documentId,principalId,userPrincipalId,fromDate,toDate);
565 }
566
567 }