Search in sources :

Example 1 with LeaveService

use of com.axelor.apps.hr.service.leave.LeaveService 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 LeaveService

use of com.axelor.apps.hr.service.leave.LeaveService in project axelor-open-suite by axelor.

the class LeaveController method refuse.

/**
 * Refuses leave request and sends an email to the applicant.
 *
 * @param request
 * @param response
 * @throws AxelorException
 */
public void refuse(ActionRequest request, ActionResponse response) throws AxelorException {
    try {
        LeaveService leaveService = Beans.get(LeaveService.class);
        LeaveRequest leaveRequest = request.getContext().asType(LeaveRequest.class);
        leaveRequest = Beans.get(LeaveRequestRepository.class).find(leaveRequest.getId());
        leaveService.refuse(leaveRequest);
        Message message = leaveService.sendRefusalEmail(leaveRequest);
        if (message != null && message.getStatusSelect() == MessageRepository.STATUS_SENT) {
            response.setFlash(String.format(I18n.get("Email sent to %s"), Beans.get(MessageServiceBaseImpl.class).getToRecipients(message)));
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    } finally {
        response.setReload(true);
    }
}
Also used : IExceptionMessage(com.axelor.apps.hr.exception.IExceptionMessage) Message(com.axelor.apps.message.db.Message) LeaveService(com.axelor.apps.hr.service.leave.LeaveService) MessageServiceBaseImpl(com.axelor.apps.base.service.message.MessageServiceBaseImpl) LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) AxelorException(com.axelor.exception.AxelorException)

Example 3 with LeaveService

use of com.axelor.apps.hr.service.leave.LeaveService in project axelor-open-suite by axelor.

the class LeaveController method send.

// sending leave request and an email to the manager
public void send(ActionRequest request, ActionResponse response) {
    try {
        LeaveService leaveService = Beans.get(LeaveService.class);
        LeaveRequest leaveRequest = request.getContext().asType(LeaveRequest.class);
        leaveRequest = Beans.get(LeaveRequestRepository.class).find(leaveRequest.getId());
        if (leaveRequest.getUser().getEmployee().getWeeklyPlanning() == null) {
            response.setAlert(String.format(I18n.get(IExceptionMessage.EMPLOYEE_PLANNING), leaveRequest.getUser().getEmployee().getName()));
            return;
        }
        LeaveLine leaveLine = Beans.get(LeaveLineRepository.class).all().filter("self.leaveReason = :leaveReason AND self.employee = :employee").bind("leaveReason", leaveRequest.getLeaveReason()).bind("employee", leaveRequest.getUser().getEmployee()).fetchOne();
        if (leaveLine != null && leaveLine.getQuantity().subtract(leaveRequest.getDuration()).signum() < 0) {
            if (!leaveRequest.getLeaveReason().getAllowNegativeValue() && !leaveService.willHaveEnoughDays(leaveRequest)) {
                String instruction = leaveRequest.getLeaveReason().getInstruction();
                if (instruction == null) {
                    instruction = "";
                }
                response.setAlert(String.format(I18n.get(IExceptionMessage.LEAVE_ALLOW_NEGATIVE_VALUE_REASON), leaveRequest.getLeaveReason().getName()) + " " + instruction);
                return;
            } else {
                response.setNotify(String.format(I18n.get(IExceptionMessage.LEAVE_ALLOW_NEGATIVE_ALERT), leaveRequest.getLeaveReason().getName()));
            }
        }
        leaveService.confirm(leaveRequest);
        Message message = leaveService.sendConfirmationEmail(leaveRequest);
        if (message != null && message.getStatusSelect() == MessageRepository.STATUS_SENT) {
            response.setFlash(String.format(I18n.get("Email sent to %s"), Beans.get(MessageServiceBaseImpl.class).getToRecipients(message)));
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    } finally {
        response.setReload(true);
    }
}
Also used : IExceptionMessage(com.axelor.apps.hr.exception.IExceptionMessage) Message(com.axelor.apps.message.db.Message) LeaveService(com.axelor.apps.hr.service.leave.LeaveService) LeaveLine(com.axelor.apps.hr.db.LeaveLine) MessageServiceBaseImpl(com.axelor.apps.base.service.message.MessageServiceBaseImpl) LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) LeaveLineRepository(com.axelor.apps.hr.db.repo.LeaveLineRepository) AxelorException(com.axelor.exception.AxelorException)

Example 4 with LeaveService

use of com.axelor.apps.hr.service.leave.LeaveService 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)

Example 5 with LeaveService

use of com.axelor.apps.hr.service.leave.LeaveService 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)

Aggregations

LeaveService (com.axelor.apps.hr.service.leave.LeaveService)7 AxelorException (com.axelor.exception.AxelorException)6 LeaveRequest (com.axelor.apps.hr.db.LeaveRequest)5 MessageServiceBaseImpl (com.axelor.apps.base.service.message.MessageServiceBaseImpl)4 IExceptionMessage (com.axelor.apps.hr.exception.IExceptionMessage)4 Message (com.axelor.apps.message.db.Message)4 WeeklyPlanning (com.axelor.apps.base.db.WeeklyPlanning)3 Employee (com.axelor.apps.hr.db.Employee)3 User (com.axelor.auth.db.User)3 LocalDate (java.time.LocalDate)3 DayPlanning (com.axelor.apps.base.db.DayPlanning)2 TimesheetLine (com.axelor.apps.hr.db.TimesheetLine)2 PublicHolidayHrService (com.axelor.apps.hr.service.publicHoliday.PublicHolidayHrService)2 AppTimesheet (com.axelor.apps.base.db.AppTimesheet)1 EventsPlanning (com.axelor.apps.base.db.EventsPlanning)1 PriceList (com.axelor.apps.base.db.PriceList)1 PeriodService (com.axelor.apps.base.service.PeriodService)1 PublicHolidayService (com.axelor.apps.base.service.publicHoliday.PublicHolidayService)1 WeeklyPlanningService (com.axelor.apps.base.service.weeklyplanning.WeeklyPlanningService)1 HRConfig (com.axelor.apps.hr.db.HRConfig)1