Search in sources :

Example 6 with WeeklyPlanning

use of com.axelor.apps.base.db.WeeklyPlanning in project axelor-open-suite by axelor.

the class TimesheetServiceImpl method checkEmptyPeriod.

public void checkEmptyPeriod(Timesheet timesheet) throws AxelorException {
    LeaveService leaveService = Beans.get(LeaveService.class);
    PublicHolidayHrService publicHolidayHrService = Beans.get(PublicHolidayHrService.class);
    User user = timesheet.getUser();
    Employee employee = user.getEmployee();
    if (employee == null) {
        return;
    }
    if (employee.getPublicHolidayEventsPlanning() == null) {
        throw new AxelorException(timesheet, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.TIMESHEET_EMPLOYEE_PUBLIC_HOLIDAY_EVENTS_PLANNING), user.getName());
    }
    WeeklyPlanning planning = employee.getWeeklyPlanning();
    if (planning == null) {
        throw new AxelorException(timesheet, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.TIMESHEET_EMPLOYEE_DAY_PLANNING), user.getName());
    }
    List<DayPlanning> dayPlanningList = planning.getWeekDays();
    Map<Integer, String> correspMap = getCorresMap();
    List<TimesheetLine> timesheetLines = timesheet.getTimesheetLineList();
    timesheetLines.sort(Comparator.comparing(TimesheetLine::getDate));
    for (int i = 0; i < timesheetLines.size(); i++) {
        if (i + 1 < timesheetLines.size()) {
            LocalDate date1 = timesheetLines.get(i).getDate();
            LocalDate date2 = timesheetLines.get(i + 1).getDate();
            LocalDate missingDay = date1.plusDays(1);
            while (ChronoUnit.DAYS.between(date1, date2) > 1) {
                if (isWorkedDay(missingDay, correspMap, dayPlanningList) && !leaveService.isLeaveDay(user, missingDay) && !publicHolidayHrService.checkPublicHolidayDay(missingDay, employee)) {
                    throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, "Line for %s is missing.", missingDay);
                }
                date1 = missingDay;
                missingDay = missingDay.plusDays(1);
            }
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) User(com.axelor.auth.db.User) TimesheetLine(com.axelor.apps.hr.db.TimesheetLine) WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) DayPlanning(com.axelor.apps.base.db.DayPlanning) LocalDate(java.time.LocalDate) Employee(com.axelor.apps.hr.db.Employee) PublicHolidayHrService(com.axelor.apps.hr.service.publicHoliday.PublicHolidayHrService) LeaveService(com.axelor.apps.hr.service.leave.LeaveService)

Example 7 with WeeklyPlanning

use of com.axelor.apps.base.db.WeeklyPlanning in project axelor-open-suite by axelor.

the class TimesheetReportServiceImpl method getTimesheetReportList.

public List<Map<String, Object>> getTimesheetReportList(String timesheetReportId) {
    List<Map<String, Object>> list = new ArrayList<>();
    WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 5);
    TimesheetReport timesheetReport = timesheetReportRepository.find(Long.parseLong(timesheetReportId.toString()));
    int numOfDays = timesheetReport.getFromDate().until(timesheetReport.getToDate()).getDays();
    List<LocalDate> daysRange = Stream.iterate(timesheetReport.getFromDate(), date -> date.plusDays(1)).limit(numOfDays + 1).collect(Collectors.toList());
    List<User> users = getUsers(timesheetReport);
    for (User user : users) {
        Employee employee = user.getEmployee();
        BigDecimal dailyWorkingHours = employee.getDailyWorkHours();
        WeeklyPlanning weeklyPlanning = employee.getWeeklyPlanning();
        Integer weekNumber = 1;
        int lastDayIndex = -1;
        int daysInWeek = 0;
        try {
            for (LocalDate date : daysRange) {
                DayPlanning dayPlanning = weeklyPlanningService.findDayPlanning(weeklyPlanning, date);
                if (dayPlanning == null) {
                    continue;
                }
                int dayIndex = date.get(weekFields.dayOfWeek()) - 1;
                if (lastDayIndex < dayIndex) {
                    lastDayIndex = dayIndex;
                    if (weeklyPlanningService.getWorkingDayValueInDays(weeklyPlanning, date) != 0) {
                        daysInWeek++;
                    }
                } else {
                    lastDayIndex = -1;
                    daysInWeek = 1;
                    weekNumber++;
                }
                BigDecimal weeklyWorkHours = daysInWeek <= 5 ? employee.getWeeklyWorkHours().multiply(BigDecimal.valueOf(daysInWeek / 5.00)).setScale(2, RoundingMode.HALF_UP) : employee.getWeeklyWorkHours();
                Map<String, Object> map = getTimesheetMap(user, date, dailyWorkingHours);
                map.put("weeklyWorkHours", weeklyWorkHours);
                map.put("weekNumber", weekNumber.toString());
                list.add(map);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    return list;
}
Also used : User(com.axelor.auth.db.User) TimesheetReport(com.axelor.apps.hr.db.TimesheetReport) WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) ArrayList(java.util.ArrayList) DayPlanning(com.axelor.apps.base.db.DayPlanning) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) Employee(com.axelor.apps.hr.db.Employee) WeekFields(java.time.temporal.WeekFields) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with WeeklyPlanning

use of com.axelor.apps.base.db.WeeklyPlanning in project axelor-open-suite by axelor.

the class LeaveServiceImpl method computeDurationInDays.

/**
 * Computes the duration in days of a leave, according to the input planning.
 *
 * @param leave
 * @param employee
 * @param fromDate
 * @param toDate
 * @param startOn
 * @param endOn
 * @return
 * @throws AxelorException
 */
protected BigDecimal computeDurationInDays(LeaveRequest leave, Employee employee, LocalDate fromDate, LocalDate toDate, int startOn, int endOn) throws AxelorException {
    BigDecimal duration = BigDecimal.ZERO;
    WeeklyPlanning weeklyPlanning = getWeeklyPlanning(leave, employee);
    EventsPlanning holidayPlanning = getPublicHolidayEventsPlanning(leave, employee);
    // If the leave request is only for 1 day
    if (fromDate.isEqual(toDate)) {
        if (startOn == endOn) {
            if (startOn == LeaveRequestRepository.SELECT_MORNING) {
                duration = duration.add(BigDecimal.valueOf(weeklyPlanningService.getWorkingDayValueInDaysWithSelect(weeklyPlanning, fromDate, true, false)));
            } else {
                duration = duration.add(BigDecimal.valueOf(weeklyPlanningService.getWorkingDayValueInDaysWithSelect(weeklyPlanning, fromDate, false, true)));
            }
        } else {
            duration = duration.add(BigDecimal.valueOf(weeklyPlanningService.getWorkingDayValueInDaysWithSelect(weeklyPlanning, fromDate, true, true)));
        }
    // Else if it's on several days
    } else {
        duration = duration.add(BigDecimal.valueOf(computeStartDateWithSelect(fromDate, startOn, weeklyPlanning)));
        LocalDate itDate = fromDate.plusDays(1);
        while (!itDate.isEqual(toDate) && !itDate.isAfter(toDate)) {
            duration = duration.add(BigDecimal.valueOf(weeklyPlanningService.getWorkingDayValueInDays(weeklyPlanning, itDate)));
            itDate = itDate.plusDays(1);
        }
        duration = duration.add(BigDecimal.valueOf(computeEndDateWithSelect(toDate, endOn, weeklyPlanning)));
    }
    if (holidayPlanning != null) {
        duration = duration.subtract(publicHolidayHrService.computePublicHolidayDays(fromDate, toDate, weeklyPlanning, holidayPlanning));
    }
    return duration;
}
Also used : EventsPlanning(com.axelor.apps.base.db.EventsPlanning) WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 9 with WeeklyPlanning

use of com.axelor.apps.base.db.WeeklyPlanning in project axelor-open-suite by axelor.

the class LeaveServiceImpl method createEvents.

@Override
@Transactional(rollbackOn = { Exception.class })
public LeaveRequest createEvents(LeaveRequest leave) throws AxelorException {
    Employee employee = leave.getUser().getEmployee();
    if (employee == null) {
        throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_USER_EMPLOYEE), leave.getUser().getName());
    }
    WeeklyPlanning weeklyPlanning = employee.getWeeklyPlanning();
    if (weeklyPlanning == null) {
        throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_PLANNING), employee.getName());
    }
    LocalDateTime fromDateTime;
    LocalDateTime toDateTime;
    if (leave.getLeaveReason().getUnitSelect() == LeaveReasonRepository.UNIT_SELECT_DAYS) {
        fromDateTime = getDefaultStart(weeklyPlanning, leave);
        toDateTime = getDefaultEnd(weeklyPlanning, leave);
    } else {
        fromDateTime = leave.getFromDateT();
        toDateTime = leave.getToDateT();
    }
    ICalendarEvent event = icalendarService.createEvent(fromDateTime, toDateTime, leave.getUser(), leave.getComments(), 4, leave.getLeaveReason().getName() + " " + leave.getUser().getFullName());
    icalEventRepo.save(event);
    leave.setIcalendarEvent(event);
    return leave;
}
Also used : LocalDateTime(java.time.LocalDateTime) ICalendarEvent(com.axelor.apps.base.db.ICalendarEvent) AxelorException(com.axelor.exception.AxelorException) Employee(com.axelor.apps.hr.db.Employee) WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) Transactional(com.google.inject.persist.Transactional)

Example 10 with WeeklyPlanning

use of com.axelor.apps.base.db.WeeklyPlanning in project axelor-open-suite by axelor.

the class EmployeeServiceImpl method getDaysWorksInPeriod.

@Override
public BigDecimal getDaysWorksInPeriod(Employee employee, LocalDate fromDate, LocalDate toDate) throws AxelorException {
    Company company = employee.getMainEmploymentContract().getPayCompany();
    BigDecimal duration = BigDecimal.ZERO;
    WeeklyPlanning weeklyPlanning = employee.getWeeklyPlanning();
    if (weeklyPlanning == null) {
        HRConfig conf = company.getHrConfig();
        if (conf != null) {
            weeklyPlanning = conf.getWeeklyPlanning();
        }
    }
    if (weeklyPlanning == null) {
        throw new AxelorException(employee, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_PLANNING), employee.getName());
    }
    EventsPlanning publicHolidayPlanning = employee.getPublicHolidayEventsPlanning();
    if (publicHolidayPlanning == null) {
        HRConfig conf = company.getHrConfig();
        if (conf != null) {
            publicHolidayPlanning = conf.getPublicHolidayEventsPlanning();
        }
    }
    if (publicHolidayPlanning == null) {
        throw new AxelorException(employee, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_PUBLIC_HOLIDAY), employee.getName());
    }
    LocalDate date = fromDate;
    while (!date.isAfter(toDate)) {
        duration = duration.add(BigDecimal.valueOf(weeklyPlanningService.getWorkingDayValueInDays(weeklyPlanning, date)));
        date = date.plusDays(1);
    }
    duration = duration.subtract(Beans.get(PublicHolidayHrService.class).computePublicHolidayDays(fromDate, toDate, weeklyPlanning, publicHolidayPlanning));
    return duration;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) HRConfig(com.axelor.apps.hr.db.HRConfig) EventsPlanning(com.axelor.apps.base.db.EventsPlanning) PublicHolidayHrService(com.axelor.apps.hr.service.publicHoliday.PublicHolidayHrService) WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Aggregations

WeeklyPlanning (com.axelor.apps.base.db.WeeklyPlanning)11 LocalDate (java.time.LocalDate)8 BigDecimal (java.math.BigDecimal)6 Employee (com.axelor.apps.hr.db.Employee)5 AxelorException (com.axelor.exception.AxelorException)5 EventsPlanning (com.axelor.apps.base.db.EventsPlanning)4 User (com.axelor.auth.db.User)4 DayPlanning (com.axelor.apps.base.db.DayPlanning)3 LeaveService (com.axelor.apps.hr.service.leave.LeaveService)3 PublicHolidayHrService (com.axelor.apps.hr.service.publicHoliday.PublicHolidayHrService)3 HRConfig (com.axelor.apps.hr.db.HRConfig)2 TimesheetLine (com.axelor.apps.hr.db.TimesheetLine)2 Transactional (com.google.inject.persist.Transactional)2 LocalDateTime (java.time.LocalDateTime)2 ArrayList (java.util.ArrayList)2 AppTimesheet (com.axelor.apps.base.db.AppTimesheet)1 Company (com.axelor.apps.base.db.Company)1 ICalendarEvent (com.axelor.apps.base.db.ICalendarEvent)1 PriceList (com.axelor.apps.base.db.PriceList)1 PublicHolidayService (com.axelor.apps.base.service.publicHoliday.PublicHolidayService)1