Search in sources :

Example 6 with LeaveLine

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

the class BatchSeniorityLeaveManagement 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());
    int count = 0;
    String eval = null;
    LeaveLine leaveLine = null;
    BigDecimal quantity = BigDecimal.ZERO;
    if (!employee.getLeaveLineList().isEmpty()) {
        for (LeaveLine line : employee.getLeaveLineList()) {
            if (line.getLeaveReason().equals(batch.getHrBatch().getLeaveReason())) {
                count++;
                leaveLine = line;
            }
        }
    }
    if (count == 0) {
        throw new AxelorException(employee, TraceBackRepository.CATEGORY_NO_VALUE, I18n.get(IExceptionMessage.EMPLOYEE_NO_LEAVE_MANAGEMENT), employee.getName(), batch.getHrBatch().getLeaveReason().getName());
    }
    if (count > 1) {
        throw new AxelorException(employee, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_DOUBLE_LEAVE_MANAGEMENT), employee.getName(), batch.getHrBatch().getLeaveReason().getName());
    }
    if (count == 1) {
        EmploymentContract contract = employee.getMainEmploymentContract();
        if (contract == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_NO_VALUE, IExceptionMessage.EMPLOYEE_CONTRACT_OF_EMPLOYMENT);
        }
        Integer executiveStatusSelect = contract.getExecutiveStatusSelect();
        for (LeaveManagementBatchRule rule : Beans.get(HRConfigRepository.class).all().filter("self.company.id = ?1", batch.getHrBatch().getCompany().getId()).fetchOne().getLeaveManagementBatchRuleList()) {
            if (rule.getExecutiveStatusSelect().equals(executiveStatusSelect)) {
                maker.setContext(employee, "Employee");
                String formula = rule.getFormula();
                formula = formula.replace(hrConfig.getSeniorityVariableName(), String.valueOf(Beans.get(EmployeeService.class).getLengthOfService(employee, batch.getHrBatch().getReferentialDate())));
                formula = formula.replace(hrConfig.getAgeVariableName(), String.valueOf(Beans.get(EmployeeService.class).getAge(employee, batch.getHrBatch().getReferentialDate())));
                maker.setTemplate(formula);
                eval = maker.make();
                CompilerConfiguration conf = new CompilerConfiguration();
                ImportCustomizer customizer = new ImportCustomizer();
                customizer.addStaticStars("java.lang.Math");
                conf.addCompilationCustomizers(customizer);
                Binding binding = new Binding();
                GroovyShell shell = new GroovyShell(binding, conf);
                if (shell.evaluate(eval).toString().equals("true")) {
                    quantity = rule.getLeaveDayNumber();
                    break;
                }
            }
        }
        if (quantity.signum() == 0) {
            // If the quantity equals 0, no need to create a leaveManagement and to update the employee,
            // since we won't give them any leaves
            incrementDone();
            return;
        }
        LeaveManagement leaveManagement = leaveManagementService.createLeaveManagement(leaveLine, AuthUtils.getUser(), batch.getHrBatch().getComments(), null, batch.getHrBatch().getStartDate(), batch.getHrBatch().getEndDate(), quantity);
        leaveLine.setQuantity(leaveLine.getQuantity().add(quantity));
        leaveLine.setTotalQuantity(leaveLine.getTotalQuantity().add(quantity));
        leaveManagementRepository.save(leaveManagement);
        leaveLineRepository.save(leaveLine);
        updateEmployee(employee);
    }
}
Also used : EmploymentContract(com.axelor.apps.hr.db.EmploymentContract) Binding(groovy.lang.Binding) AxelorException(com.axelor.exception.AxelorException) BigDecimal(java.math.BigDecimal) GroovyShell(groovy.lang.GroovyShell) LeaveManagement(com.axelor.apps.hr.db.LeaveManagement) LeaveLine(com.axelor.apps.hr.db.LeaveLine) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) LeaveManagementBatchRule(com.axelor.apps.hr.db.LeaveManagementBatchRule) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) Transactional(com.google.inject.persist.Transactional)

Example 7 with LeaveLine

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

the class LeaveServiceImpl method manageRefuseLeaves.

@Override
@Transactional(rollbackOn = { Exception.class })
public void manageRefuseLeaves(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.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 8 with LeaveLine

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

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

the class LeaveServiceImpl method willHaveEnoughDays.

@Override
public boolean willHaveEnoughDays(LeaveRequest leaveRequest) {
    LocalDateTime todayDate = appBaseService.getTodayDateTime().toLocalDateTime();
    LocalDateTime beginDate = leaveRequest.getFromDateT();
    int interval = (beginDate.getYear() - todayDate.getYear()) * 12 + beginDate.getMonthValue() - todayDate.getMonthValue();
    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) {
        if (leaveRequest.getLeaveReason() != null && !leaveRequest.getLeaveReason().getManageAccumulation()) {
            return true;
        }
        return false;
    }
    BigDecimal num = leaveLine.getQuantity().add(leaveRequest.getUser().getEmployee().getWeeklyPlanning().getLeaveCoef().multiply(leaveRequest.getLeaveReason().getDefaultDayNumberGain()).multiply(BigDecimal.valueOf(interval)));
    return leaveRequest.getDuration().compareTo(num) <= 0;
}
Also used : LocalDateTime(java.time.LocalDateTime) LeaveLine(com.axelor.apps.hr.db.LeaveLine) LeaveLineRepository(com.axelor.apps.hr.db.repo.LeaveLineRepository) BigDecimal(java.math.BigDecimal)

Example 10 with LeaveLine

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

the class LeaveServiceImpl method createLeaveReasonToJustify.

protected LeaveLine createLeaveReasonToJustify(Employee employee, LeaveReason leaveReason) {
    LeaveLine leaveLineEmployee = new LeaveLine();
    leaveLineEmployee.setLeaveReason(leaveReason);
    leaveLineEmployee.setEmployee(employee);
    if (leaveReason != null) {
        leaveLineEmployee.setName(leaveReason.getName());
    }
    leaveLineRepo.save(leaveLineEmployee);
    return leaveLineEmployee;
}
Also used : LeaveLine(com.axelor.apps.hr.db.LeaveLine)

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