Search in sources :

Example 1 with WeeklyPlanning

use of com.axelor.apps.base.db.WeeklyPlanning 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)

Example 2 with WeeklyPlanning

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

the class LeaveServiceImpl method computeDurationInHours.

/**
 * Computes the duration in hours of a leave, according to the weekly and the holiday plannings.
 *
 * @param leave
 * @param employee
 * @param fromDateT
 * @param toDateT
 * @return
 * @throws AxelorException
 */
protected BigDecimal computeDurationInHours(LeaveRequest leave, Employee employee, LocalDateTime fromDateT, LocalDateTime toDateT) throws AxelorException {
    BigDecimal duration = BigDecimal.ZERO;
    WeeklyPlanning weeklyPlanning = getWeeklyPlanning(leave, employee);
    EventsPlanning holidayPlanning = getPublicHolidayEventsPlanning(leave, employee);
    LocalDate fromDate = fromDateT.toLocalDate();
    LocalDate toDate = toDateT.toLocalDate();
    if (toDate.equals(fromDate) && !publicHolidayHrService.checkPublicHolidayDay(fromDate, holidayPlanning)) {
        duration = duration.add(weeklyPlanningService.getWorkingDayValueInHours(weeklyPlanning, fromDate, fromDateT.toLocalTime(), toDateT.toLocalTime()));
    } else {
        // First day of leave
        if (!publicHolidayHrService.checkPublicHolidayDay(fromDate, holidayPlanning)) {
            duration = duration.add(weeklyPlanningService.getWorkingDayValueInHours(weeklyPlanning, fromDate, fromDateT.toLocalTime(), null));
        }
        fromDate = fromDate.plusDays(1);
        // Last day of leave
        if (!publicHolidayHrService.checkPublicHolidayDay(toDate, holidayPlanning)) {
            duration = duration.add(weeklyPlanningService.getWorkingDayValueInHours(weeklyPlanning, toDate, null, toDateT.toLocalTime()));
        }
        // Daily leave duration of the other days between from and to date
        for (LocalDate date = fromDate; date.isBefore(toDate); date = date.plusDays(1)) {
            if (!publicHolidayHrService.checkPublicHolidayDay(date, holidayPlanning)) {
                duration = duration.add(weeklyPlanningService.getWorkingDayValueInHours(weeklyPlanning, date, null, null));
            }
        }
    }
    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 3 with WeeklyPlanning

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

the class LeaveServiceImpl method computeLeaveDaysByLeaveRequest.

@Override
public BigDecimal computeLeaveDaysByLeaveRequest(LocalDate fromDate, LocalDate toDate, LeaveRequest leaveRequest, Employee employee) throws AxelorException {
    BigDecimal leaveDays = BigDecimal.ZERO;
    WeeklyPlanning weeklyPlanning = employee.getWeeklyPlanning();
    LocalDate leaveFrom = leaveRequest.getFromDateT().toLocalDate();
    LocalDate leaveTo = leaveRequest.getToDateT().toLocalDate();
    LocalDate itDate = fromDate;
    if (fromDate.isBefore(leaveFrom) || fromDate.equals(leaveFrom)) {
        itDate = leaveFrom;
    }
    boolean morningHalf = false;
    boolean eveningHalf = false;
    BigDecimal daysToAdd = BigDecimal.ZERO;
    if (leaveTo.equals(leaveFrom) && leaveRequest.getStartOnSelect() == leaveRequest.getEndOnSelect()) {
        eveningHalf = leaveRequest.getStartOnSelect() == LeaveRequestRepository.SELECT_AFTERNOON;
        morningHalf = leaveRequest.getStartOnSelect() == LeaveRequestRepository.SELECT_MORNING;
    }
    while (!itDate.isEqual(leaveTo.plusDays(1)) && !itDate.isEqual(toDate.plusDays(1))) {
        if (itDate.equals(leaveFrom) && !morningHalf) {
            daysToAdd = BigDecimal.valueOf(computeStartDateWithSelect(itDate, leaveRequest.getStartOnSelect(), weeklyPlanning));
        } else if (itDate.equals(leaveTo) && !eveningHalf) {
            daysToAdd = BigDecimal.valueOf(computeEndDateWithSelect(itDate, leaveRequest.getEndOnSelect(), weeklyPlanning));
        } else {
            daysToAdd = BigDecimal.valueOf(weeklyPlanningService.getWorkingDayValueInDays(weeklyPlanning, itDate));
        }
        if (!publicHolidayHrService.checkPublicHolidayDay(itDate, employee)) {
            leaveDays = leaveDays.add(daysToAdd);
        }
        itDate = itDate.plusDays(1);
    }
    return leaveDays;
}
Also used : WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 4 with WeeklyPlanning

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

the class WeeklyPlanningController method initPlanning.

public void initPlanning(ActionRequest request, ActionResponse response) {
    WeeklyPlanning planning = request.getContext().asType(WeeklyPlanning.class);
    planning = Beans.get(WeeklyPlanningService.class).initPlanning(planning);
    if (request.getContext().containsKey("_typeSelect")) {
        response.setValue("typeSelect", request.getContext().get("_typeSelect"));
    }
    response.setValue("weekDays", planning.getWeekDays());
}
Also used : WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning)

Example 5 with WeeklyPlanning

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

the class TimesheetServiceImpl method generateLines.

@Override
public Timesheet generateLines(Timesheet timesheet, LocalDate fromGenerationDate, LocalDate toGenerationDate, BigDecimal logTime, Project project, Product product) throws AxelorException {
    User user = timesheet.getUser();
    Employee employee = user.getEmployee();
    if (fromGenerationDate == null) {
        throw new AxelorException(timesheet, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.TIMESHEET_FROM_DATE));
    }
    if (toGenerationDate == null) {
        throw new AxelorException(timesheet, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.TIMESHEET_TO_DATE));
    }
    if (product == null) {
        throw new AxelorException(timesheet, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.TIMESHEET_PRODUCT));
    }
    if (employee == null) {
        throw new AxelorException(timesheet, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_USER_EMPLOYEE), 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();
    LocalDate fromDate = fromGenerationDate;
    LocalDate toDate = toGenerationDate;
    if (employee.getPublicHolidayEventsPlanning() == null) {
        throw new AxelorException(timesheet, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.TIMESHEET_EMPLOYEE_PUBLIC_HOLIDAY_EVENTS_PLANNING), user.getName());
    }
    LeaveService leaveService = Beans.get(LeaveService.class);
    PublicHolidayHrService publicHolidayHrService = Beans.get(PublicHolidayHrService.class);
    while (!fromDate.isAfter(toDate)) {
        if (isWorkedDay(fromDate, correspMap, dayPlanningList) && !leaveService.isLeaveDay(user, fromDate) && !publicHolidayHrService.checkPublicHolidayDay(fromDate, employee)) {
            TimesheetLine timesheetLine = timesheetLineService.createTimesheetLine(project, product, user, fromDate, timesheet, timesheetLineService.computeHoursDuration(timesheet, logTime, true), "");
            timesheetLine.setDuration(logTime);
        }
        fromDate = fromDate.plusDays(1);
    }
    return timesheet;
}
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)

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