Search in sources :

Example 1 with LeaveReason

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

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

the class LeaveController method leaveReasonToJustify.

/* Count Tags displayed on the menu items */
@Transactional
public void leaveReasonToJustify(ActionRequest request, ActionResponse response) {
    try {
        LeaveRequest leave = request.getContext().asType(LeaveRequest.class);
        Boolean leaveToJustify = leave.getToJustifyLeaveReason();
        LeaveLine leaveLine = null;
        if (!leaveToJustify) {
            return;
        }
        Company company = leave.getCompany();
        if (leave.getUser() == null) {
            return;
        }
        if (company == null) {
            company = leave.getUser().getActiveCompany();
        }
        if (company == null) {
            return;
        }
        Beans.get(HRConfigService.class).getLeaveReason(company.getHrConfig());
        Employee employee = leave.getUser().getEmployee();
        LeaveReason leaveReason = Beans.get(LeaveReasonRepository.class).find(company.getHrConfig().getToJustifyLeaveReason().getId());
        if (employee != null) {
            employee = Beans.get(EmployeeRepository.class).find(leave.getUser().getEmployee().getId());
            leaveLine = Beans.get(LeaveService.class).addLeaveReasonOrCreateIt(employee, leaveReason);
            response.setValue("leaveLine", leaveLine);
        }
    } catch (AxelorException e) {
        TraceBackService.trace(response, e);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) LeaveReasonRepository(com.axelor.apps.hr.db.repo.LeaveReasonRepository) Employee(com.axelor.apps.hr.db.Employee) LeaveLine(com.axelor.apps.hr.db.LeaveLine) HRConfigService(com.axelor.apps.hr.service.config.HRConfigService) LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) LeaveReason(com.axelor.apps.hr.db.LeaveReason) Transactional(com.google.inject.persist.Transactional)

Example 3 with LeaveReason

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

the class HumanResourceMobileController method getLeaveReason.

/*
   * 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:getLeaveReason
   * Content-Type: application/json
   *
   * URL: com.axelor.apps.hr.mobile.HumanResourceMobileController:getLeaveReason
   * fields: no field
   *
   * payload:
   * { "data": {
   * 		"action": "com.axelor.apps.hr.mobile.HumanResourceMobileController:getLeaveReason"
   * } }
   */
@Transactional
public void getLeaveReason(ActionRequest request, ActionResponse response) {
    try {
        User user = AuthUtils.getUser();
        List<Map<String, String>> dataList = new ArrayList<>();
        if (user == null || user.getEmployee() == null) {
            List<LeaveReason> leaveReasonList = Beans.get(LeaveReasonRepository.class).all().fetch();
            for (LeaveReason leaveReason : leaveReasonList) {
                if (leaveReason.getUnitSelect() == LeaveReasonRepository.UNIT_SELECT_DAYS) {
                    Map<String, String> map = new HashMap<>();
                    map.put("name", leaveReason.getName());
                    map.put("id", leaveReason.getId().toString());
                    dataList.add(map);
                }
            }
        } else if (user.getEmployee() != null) {
            List<LeaveLine> leaveLineList = Beans.get(LeaveLineRepository.class).all().filter("self.employee = ?1", user.getEmployee()).order("name").fetch();
            String tmpName = "";
            for (LeaveLine leaveLine : leaveLineList) {
                String name = leaveLine.getName();
                if (tmpName != name) {
                    Map<String, String> map = new HashMap<>();
                    map.put("name", leaveLine.getName());
                    map.put("id", leaveLine.getLeaveReason().getId().toString());
                    map.put("quantity", leaveLine.getQuantity().toString());
                    dataList.add(map);
                }
                tmpName = name;
            }
        }
        response.setData(dataList);
        response.setTotal(dataList.size());
    } catch (Exception e) {
        response.setStatus(-1);
        response.setError(e.getMessage());
    }
}
Also used : User(com.axelor.auth.db.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LeaveReason(com.axelor.apps.hr.db.LeaveReason) AxelorException(com.axelor.exception.AxelorException) LeaveLine(com.axelor.apps.hr.db.LeaveLine) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LeaveLineRepository(com.axelor.apps.hr.db.repo.LeaveLineRepository) Transactional(com.google.inject.persist.Transactional)

Example 4 with LeaveReason

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

the class BatchLeaveManagement method createLeaveManagement.

@Transactional(rollbackOn = { Exception.class })
public void createLeaveManagement(Employee employee) throws AxelorException {
    if (employee == null || EmployeeHRRepository.isEmployeeFormerNewOrArchived(employee)) {
        return;
    }
    batch = batchRepo.find(batch.getId());
    LeaveLine leaveLine = null;
    LeaveReason leaveReason = batch.getHrBatch().getLeaveReason();
    leaveLine = leaveServiceProvider.get().addLeaveReasonOrCreateIt(employee, leaveReason);
    BigDecimal dayNumber = batch.getHrBatch().getUseWeeklyPlanningCoef() ? batch.getHrBatch().getDayNumber().multiply(employee.getWeeklyPlanning().getLeaveCoef()) : batch.getHrBatch().getDayNumber();
    dayNumber = dayNumber.subtract(new BigDecimal(publicHolidayService.getImposedDayNumber(employee, batch.getHrBatch().getStartDate(), batch.getHrBatch().getEndDate())));
    LeaveManagement leaveManagement = leaveManagementService.createLeaveManagement(leaveLine, AuthUtils.getUser(), batch.getHrBatch().getComments(), null, batch.getHrBatch().getStartDate(), batch.getHrBatch().getEndDate(), dayNumber);
    BigDecimal qty = leaveLine.getQuantity().add(dayNumber);
    BigDecimal totalQty = leaveLine.getTotalQuantity().add(dayNumber);
    try {
        int integer = LeaveLine.class.getDeclaredField("quantity").getAnnotation(Digits.class).integer();
        BigDecimal limit = new BigDecimal((long) Math.pow(10, integer));
        if (qty.compareTo(limit) >= 0 || totalQty.compareTo(limit) >= 0) {
            throw new AxelorException(employee, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.BATCH_LEAVE_MANAGEMENT_QTY_OUT_OF_BOUNDS), limit.longValue());
        }
    } catch (NoSuchFieldException | SecurityException e) {
        throw new AxelorException(e, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR);
    }
    leaveLine.setQuantity(qty.setScale(4, RoundingMode.HALF_UP));
    leaveLine.setTotalQuantity(totalQty.setScale(4, RoundingMode.HALF_UP));
    leaveManagementRepository.save(leaveManagement);
    leaveLineRepository.save(leaveLine);
    updateEmployee(employee);
}
Also used : AxelorException(com.axelor.exception.AxelorException) LeaveManagement(com.axelor.apps.hr.db.LeaveManagement) Digits(javax.validation.constraints.Digits) LeaveLine(com.axelor.apps.hr.db.LeaveLine) LeaveReason(com.axelor.apps.hr.db.LeaveReason) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Aggregations

LeaveLine (com.axelor.apps.hr.db.LeaveLine)4 LeaveReason (com.axelor.apps.hr.db.LeaveReason)4 AxelorException (com.axelor.exception.AxelorException)4 Transactional (com.google.inject.persist.Transactional)4 Company (com.axelor.apps.base.db.Company)2 Employee (com.axelor.apps.hr.db.Employee)2 LeaveRequest (com.axelor.apps.hr.db.LeaveRequest)2 LeaveLineRepository (com.axelor.apps.hr.db.repo.LeaveLineRepository)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 LeaveManagement (com.axelor.apps.hr.db.LeaveManagement)1 HRConfigService (com.axelor.apps.hr.service.config.HRConfigService)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Digits (javax.validation.constraints.Digits)1