Search in sources :

Example 1 with LeaveManagementBatchRule

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

Aggregations

EmploymentContract (com.axelor.apps.hr.db.EmploymentContract)1 LeaveLine (com.axelor.apps.hr.db.LeaveLine)1 LeaveManagement (com.axelor.apps.hr.db.LeaveManagement)1 LeaveManagementBatchRule (com.axelor.apps.hr.db.LeaveManagementBatchRule)1 AxelorException (com.axelor.exception.AxelorException)1 Transactional (com.google.inject.persist.Transactional)1 Binding (groovy.lang.Binding)1 GroovyShell (groovy.lang.GroovyShell)1 BigDecimal (java.math.BigDecimal)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)1