Search in sources :

Example 6 with LeaveRequest

use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.

the class LeaveServiceImpl method getLeaves.

public List<LeaveRequest> getLeaves(User user, LocalDate date) {
    List<LeaveRequest> leavesList = new ArrayList<>();
    List<LeaveRequest> leaves = leaveRequestRepo.all().filter("self.user = :userId AND self.statusSelect IN (:awaitingValidation,:validated)").bind("userId", user).bind("awaitingValidation", LeaveRequestRepository.STATUS_AWAITING_VALIDATION).bind("validated", LeaveRequestRepository.STATUS_VALIDATED).fetch();
    if (ObjectUtils.notEmpty(leaves)) {
        for (LeaveRequest leave : leaves) {
            LocalDate from = leave.getFromDateT().toLocalDate();
            LocalDate to = leave.getToDateT().toLocalDate();
            if ((from.isBefore(date) && to.isAfter(date)) || from.isEqual(date) || to.isEqual(date)) {
                leavesList.add(leave);
            }
        }
    }
    return leavesList;
}
Also used : ArrayList(java.util.ArrayList) LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) LocalDate(java.time.LocalDate)

Example 7 with LeaveRequest

use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.

the class HumanResourceMobileController method insertLeave.

/*
   * This method is used in mobile application.
   * It was in LeaveServiceImpl
   * @param request
   * @param response
   *
   * POST /open-suite-webapp/ws/action/com.axelor.apps.hr.mobile.HumanResourceMobileController:insertLeave
   * Content-Type: application/json
   *
   * URL: com.axelor.apps.hr.mobile.HumanResourceMobileController:insertLeave
   * fields: leaveReason, fromDateT, startOn, toDateT, endOn, comment
   *
   * payload:
   * { "data": {
   * 		"action": "com.axelor.apps.hr.mobile.HumanResourceMobileController:insertLeave",
   * 		"leaveReason": 10,
   * 		"fromDateT": "2018-02-22T10:30:00",
   * 		"startOn": 1,
   * 		"toDateT": "2018-02-24T:19:30:00",
   *	 	"endOn": 1,
   * 		"comment": "no"
   * } }
   */
@Transactional(rollbackOn = { Exception.class })
public void insertLeave(ActionRequest request, ActionResponse response) throws AxelorException {
    AppBaseService appBaseService = Beans.get(AppBaseService.class);
    User user = AuthUtils.getUser();
    Map<String, Object> requestData = request.getData();
    LeaveReason leaveReason = Beans.get(LeaveReasonRepository.class).find(Long.valueOf(requestData.get("leaveReason").toString()));
    Employee employee = user.getEmployee();
    if (employee == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_USER_EMPLOYEE), user.getName());
    }
    if (leaveReason != null) {
        LeaveRequest leave = new LeaveRequest();
        leave.setUser(user);
        Company company = null;
        if (employee.getMainEmploymentContract() != null) {
            company = employee.getMainEmploymentContract().getPayCompany();
        }
        leave.setCompany(company);
        LeaveLine leaveLine = Beans.get(LeaveLineRepository.class).all().filter("self.employee = ?1 AND self.leaveReason = ?2", employee, leaveReason).fetchOne();
        if (leaveLine == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_LINE), employee.getName(), leaveReason.getName());
        }
        leave.setLeaveReason(leaveReason);
        leave.setRequestDate(appBaseService.getTodayDate(company));
        if (requestData.get("fromDateT") != null) {
            leave.setFromDateT(LocalDateTime.parse(requestData.get("fromDateT").toString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        }
        leave.setStartOnSelect(new Integer(requestData.get("startOn").toString()));
        if (requestData.get("toDateT") != null) {
            leave.setToDateT(LocalDateTime.parse(requestData.get("toDateT").toString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        }
        leave.setEndOnSelect(new Integer(requestData.get("endOn").toString()));
        leave.setDuration(Beans.get(LeaveService.class).computeDuration(leave));
        leave.setStatusSelect(LeaveRequestRepository.STATUS_AWAITING_VALIDATION);
        if (requestData.get("comments") != null) {
            leave.setComments(requestData.get("comments").toString());
        }
        leave = Beans.get(LeaveRequestRepository.class).save(leave);
        response.setTotal(1);
        response.setValue("id", leave.getId());
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) LeaveReasonRepository(com.axelor.apps.hr.db.repo.LeaveReasonRepository) Company(com.axelor.apps.base.db.Company) User(com.axelor.auth.db.User) LeaveReason(com.axelor.apps.hr.db.LeaveReason) LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) Employee(com.axelor.apps.hr.db.Employee) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) LeaveLine(com.axelor.apps.hr.db.LeaveLine) LeaveLineRepository(com.axelor.apps.hr.db.repo.LeaveLineRepository) Transactional(com.google.inject.persist.Transactional)

Example 8 with LeaveRequest

use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.

the class PayrollPreparationService method fillInLeaves.

public List<PayrollLeave> fillInLeaves(PayrollPreparation payrollPreparation) throws AxelorException {
    List<PayrollLeave> payrollLeaveList = new ArrayList<>();
    LocalDate fromDate = payrollPreparation.getPeriod().getFromDate();
    LocalDate toDate = payrollPreparation.getPeriod().getToDate();
    Employee employee = payrollPreparation.getEmployee();
    if (employee.getWeeklyPlanning() == null) {
        throw new AxelorException(payrollPreparation, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_PLANNING), employee.getName());
    }
    List<LeaveRequest> leaveRequestList = leaveRequestRepo.all().filter("self.statusSelect = ?4 AND self.user.employee = ?3 AND ((self.fromDateT BETWEEN ?2 AND ?1 OR self.toDateT BETWEEN ?2 AND ?1) OR (?1 BETWEEN self.fromDateT AND self.toDateT OR ?2 BETWEEN self.fromDateT AND self.toDateT))", toDate, fromDate, employee, LeaveRequestRepository.STATUS_VALIDATED).fetch();
    for (LeaveRequest leaveRequest : leaveRequestList) {
        PayrollLeave payrollLeave = new PayrollLeave();
        if (leaveRequest.getFromDateT().toLocalDate().isBefore(fromDate)) {
            payrollLeave.setFromDate(fromDate);
        } else {
            payrollLeave.setFromDate(leaveRequest.getFromDateT().toLocalDate());
        }
        if (leaveRequest.getToDateT().toLocalDate().isAfter(toDate)) {
            payrollLeave.setToDate(toDate);
        } else {
            payrollLeave.setToDate(leaveRequest.getToDateT().toLocalDate());
        }
        payrollLeave.setDuration(leaveService.computeLeaveDaysByLeaveRequest(fromDate, toDate, leaveRequest, employee));
        payrollLeave.setLeaveReason(leaveRequest.getLeaveReason());
        payrollLeave.setLeaveRequest(leaveRequest);
        payrollLeaveList.add(payrollLeave);
    }
    return payrollLeaveList;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Employee(com.axelor.apps.hr.db.Employee) ArrayList(java.util.ArrayList) PayrollLeave(com.axelor.apps.hr.db.PayrollLeave) LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) LocalDate(java.time.LocalDate)

Example 9 with LeaveRequest

use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.

the class TimesheetReportServiceImpl method getLeaveHours.

private BigDecimal getLeaveHours(User user, LocalDate date, BigDecimal dailyWorkingHours) throws AxelorException {
    List<LeaveRequest> leavesList = leaveService.getLeaves(user, date);
    BigDecimal totalLeaveHours = BigDecimal.ZERO;
    for (LeaveRequest leave : leavesList) {
        BigDecimal leaveHours = leaveService.computeDuration(leave, date, date);
        if (leave.getLeaveReason().getUnitSelect() == LeaveReasonRepository.UNIT_SELECT_DAYS) {
            leaveHours = leaveHours.multiply(dailyWorkingHours);
        }
        totalLeaveHours = totalLeaveHours.add(leaveHours);
    }
    return totalLeaveHours;
}
Also used : LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) BigDecimal(java.math.BigDecimal)

Example 10 with LeaveRequest

use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.

the class LeaveController method cancel.

public void cancel(ActionRequest request, ActionResponse response) {
    try {
        LeaveRequest leave = request.getContext().asType(LeaveRequest.class);
        leave = Beans.get(LeaveRequestRepository.class).find(leave.getId());
        LeaveService leaveService = Beans.get(LeaveService.class);
        leaveService.cancel(leave);
        Message message = leaveService.sendCancellationEmail(leave);
        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)

Aggregations

LeaveRequest (com.axelor.apps.hr.db.LeaveRequest)14 AxelorException (com.axelor.exception.AxelorException)10 LeaveService (com.axelor.apps.hr.service.leave.LeaveService)6 MessageServiceBaseImpl (com.axelor.apps.base.service.message.MessageServiceBaseImpl)4 Employee (com.axelor.apps.hr.db.Employee)4 IExceptionMessage (com.axelor.apps.hr.exception.IExceptionMessage)4 Message (com.axelor.apps.message.db.Message)4 LeaveLine (com.axelor.apps.hr.db.LeaveLine)3 User (com.axelor.auth.db.User)3 BigDecimal (java.math.BigDecimal)3 LocalDate (java.time.LocalDate)3 ArrayList (java.util.ArrayList)3 Company (com.axelor.apps.base.db.Company)2 LeaveReason (com.axelor.apps.hr.db.LeaveReason)2 LeaveLineRepository (com.axelor.apps.hr.db.repo.LeaveLineRepository)2 LeaveReasonRepository (com.axelor.apps.hr.db.repo.LeaveReasonRepository)2 LeaveRequestRepository (com.axelor.apps.hr.db.repo.LeaveRequestRepository)2 Transactional (com.google.inject.persist.Transactional)2 AppTimesheet (com.axelor.apps.base.db.AppTimesheet)1 EventsPlanning (com.axelor.apps.base.db.EventsPlanning)1