Search in sources :

Example 1 with LeaveLine

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

the class LeaveManagementController method computeQuantityAvailable.

public void computeQuantityAvailable(ActionRequest request, ActionResponse response) {
    LeaveLine leaveLine = request.getContext().asType(LeaveLine.class);
    leaveLine = Beans.get(LeaveManagementService.class).computeQuantityAvailable(leaveLine);
    response.setValue("quantity", leaveLine.getQuantity());
    response.setValue("totalQuantity", leaveLine.getTotalQuantity());
    response.setValue("leaveManagementList", leaveLine.getLeaveManagementList());
}
Also used : LeaveLine(com.axelor.apps.hr.db.LeaveLine)

Example 2 with LeaveLine

use of com.axelor.apps.hr.db.LeaveLine 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 3 with LeaveLine

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

the class LeaveServiceImpl method manageCancelLeaves.

@Override
@Transactional(rollbackOn = { Exception.class })
public void manageCancelLeaves(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());
    }
    LeaveLine leaveLine = leaveLineRepo.all().filter("self.employee = ?1 AND self.leaveReason = ?2", employee, leave.getLeaveReason()).fetchOne();
    if (leaveLine == null) {
        throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_LINE), employee.getName(), leave.getLeaveReason().getName());
    }
    if (leave.getStatusSelect() == LeaveRequestRepository.STATUS_VALIDATED) {
        if (leave.getInjectConsumeSelect() == LeaveRequestRepository.SELECT_CONSUME) {
            leaveLine.setQuantity(leaveLine.getQuantity().add(leave.getDuration()));
        } else {
            leaveLine.setQuantity(leaveLine.getQuantity().subtract(leave.getDuration()));
        }
        leaveLine.setDaysValidated(leaveLine.getDaysValidated().subtract(leave.getDuration()));
    } else if (leave.getStatusSelect() == LeaveRequestRepository.STATUS_AWAITING_VALIDATION) {
        if (leave.getInjectConsumeSelect() == LeaveRequestRepository.SELECT_CONSUME) {
            leaveLine.setDaysToValidate(leaveLine.getDaysToValidate().subtract(leave.getDuration()));
        } else {
            leaveLine.setDaysToValidate(leaveLine.getDaysToValidate().add(leave.getDuration()));
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) Employee(com.axelor.apps.hr.db.Employee) LeaveLine(com.axelor.apps.hr.db.LeaveLine) Transactional(com.google.inject.persist.Transactional)

Example 4 with LeaveLine

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

the class LeaveServiceImpl method manageValidateLeaves.

@Override
@Transactional(rollbackOn = { Exception.class })
public void manageValidateLeaves(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());
    }
    LeaveLine leaveLine = leaveLineRepo.all().filter("self.employee = ?1 AND self.leaveReason = ?2", employee, leave.getLeaveReason()).fetchOne();
    if (leaveLine == null) {
        throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_LINE), employee.getName(), leave.getLeaveReason().getName());
    }
    if (leave.getInjectConsumeSelect() == LeaveRequestRepository.SELECT_CONSUME) {
        leaveLine.setQuantity(leaveLine.getQuantity().subtract(leave.getDuration()));
        if (leaveLine.getQuantity().signum() == -1 && !employee.getNegativeValueLeave()) {
            throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_ALLOW_NEGATIVE_VALUE_EMPLOYEE), employee.getName());
        }
        if (leaveLine.getQuantity().signum() == -1 && !leave.getLeaveReason().getAllowNegativeValue()) {
            throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_ALLOW_NEGATIVE_VALUE_REASON), leave.getLeaveReason().getName());
        }
        leaveLine.setDaysToValidate(leaveLine.getDaysToValidate().subtract(leave.getDuration()));
        leaveLine.setDaysValidated(leaveLine.getDaysValidated().add(leave.getDuration()));
    } else {
        leaveLine.setQuantity(leaveLine.getQuantity().add(leave.getDuration()));
        leaveLine.setDaysToValidate(leaveLine.getDaysToValidate().add(leave.getDuration()));
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) Employee(com.axelor.apps.hr.db.Employee) LeaveLine(com.axelor.apps.hr.db.LeaveLine) Transactional(com.google.inject.persist.Transactional)

Example 5 with LeaveLine

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

Aggregations

LeaveLine (com.axelor.apps.hr.db.LeaveLine)14 AxelorException (com.axelor.exception.AxelorException)10 Transactional (com.google.inject.persist.Transactional)10 Employee (com.axelor.apps.hr.db.Employee)6 LeaveLineRepository (com.axelor.apps.hr.db.repo.LeaveLineRepository)5 LeaveReason (com.axelor.apps.hr.db.LeaveReason)4 LeaveRequest (com.axelor.apps.hr.db.LeaveRequest)3 BigDecimal (java.math.BigDecimal)3 Company (com.axelor.apps.base.db.Company)2 LeaveManagement (com.axelor.apps.hr.db.LeaveManagement)2 LeaveReasonRepository (com.axelor.apps.hr.db.repo.LeaveReasonRepository)2 User (com.axelor.auth.db.User)2 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)1 MessageServiceBaseImpl (com.axelor.apps.base.service.message.MessageServiceBaseImpl)1 EmploymentContract (com.axelor.apps.hr.db.EmploymentContract)1 LeaveManagementBatchRule (com.axelor.apps.hr.db.LeaveManagementBatchRule)1 IExceptionMessage (com.axelor.apps.hr.exception.IExceptionMessage)1 HRConfigService (com.axelor.apps.hr.service.config.HRConfigService)1 LeaveService (com.axelor.apps.hr.service.leave.LeaveService)1 Message (com.axelor.apps.message.db.Message)1