Search in sources :

Example 1 with PublicHolidayService

use of com.axelor.apps.base.service.publicHoliday.PublicHolidayService in project axelor-open-suite by axelor.

the class TimesheetServiceImpl method prefillLines.

@Override
public void prefillLines(Timesheet timesheet) throws AxelorException {
    PublicHolidayService holidayService = Beans.get(PublicHolidayService.class);
    LeaveService leaveService = Beans.get(LeaveService.class);
    WeeklyPlanningService weeklyPlanningService = Beans.get(WeeklyPlanningService.class);
    AppTimesheet appTimesheet = appHumanResourceService.getAppTimesheet();
    LocalDate fromDate = timesheet.getFromDate();
    LocalDate toDate = timesheet.getToDate();
    User user = timesheet.getUser();
    Employee employee = user.getEmployee();
    HRConfig config = timesheet.getCompany().getHrConfig();
    WeeklyPlanning weeklyPlanning = employee != null ? employee.getWeeklyPlanning() : config.getWeeklyPlanning();
    EventsPlanning holidayPlanning = employee != null ? employee.getPublicHolidayEventsPlanning() : config.getPublicHolidayEventsPlanning();
    for (LocalDate date = fromDate; !date.isAfter(toDate); date = date.plusDays(1)) {
        BigDecimal dayValueInHours = weeklyPlanningService.getWorkingDayValueInHours(weeklyPlanning, date, LocalTime.MIN, LocalTime.MAX);
        if (appTimesheet.getCreateLinesForHolidays() && holidayService.checkPublicHolidayDay(date, holidayPlanning)) {
            timesheetLineService.createTimesheetLine(user, date, timesheet, dayValueInHours, I18n.get(IExceptionMessage.TIMESHEET_HOLIDAY));
        } else if (appTimesheet.getCreateLinesForLeaves()) {
            List<LeaveRequest> leaveList = leaveService.getLeaves(user, date);
            BigDecimal totalLeaveHours = BigDecimal.ZERO;
            if (ObjectUtils.notEmpty(leaveList)) {
                for (LeaveRequest leave : leaveList) {
                    BigDecimal leaveHours = leaveService.computeDuration(leave, date, date);
                    if (leave.getLeaveReason().getUnitSelect() == LeaveReasonRepository.UNIT_SELECT_DAYS) {
                        leaveHours = leaveHours.multiply(dayValueInHours);
                    }
                    totalLeaveHours = totalLeaveHours.add(leaveHours);
                }
                timesheetLineService.createTimesheetLine(user, date, timesheet, totalLeaveHours, I18n.get(IExceptionMessage.TIMESHEET_DAY_LEAVE));
            }
        }
    }
}
Also used : User(com.axelor.auth.db.User) EventsPlanning(com.axelor.apps.base.db.EventsPlanning) WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AppTimesheet(com.axelor.apps.base.db.AppTimesheet) Employee(com.axelor.apps.hr.db.Employee) HRConfig(com.axelor.apps.hr.db.HRConfig) LeaveService(com.axelor.apps.hr.service.leave.LeaveService) WeeklyPlanningService(com.axelor.apps.base.service.weeklyplanning.WeeklyPlanningService) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) ArrayList(java.util.ArrayList) PublicHolidayService(com.axelor.apps.base.service.publicHoliday.PublicHolidayService)

Aggregations

AppTimesheet (com.axelor.apps.base.db.AppTimesheet)1 EventsPlanning (com.axelor.apps.base.db.EventsPlanning)1 PriceList (com.axelor.apps.base.db.PriceList)1 WeeklyPlanning (com.axelor.apps.base.db.WeeklyPlanning)1 PublicHolidayService (com.axelor.apps.base.service.publicHoliday.PublicHolidayService)1 WeeklyPlanningService (com.axelor.apps.base.service.weeklyplanning.WeeklyPlanningService)1 Employee (com.axelor.apps.hr.db.Employee)1 HRConfig (com.axelor.apps.hr.db.HRConfig)1 LeaveRequest (com.axelor.apps.hr.db.LeaveRequest)1 LeaveService (com.axelor.apps.hr.service.leave.LeaveService)1 User (com.axelor.auth.db.User)1 BigDecimal (java.math.BigDecimal)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1