Search in sources :

Example 1 with EmployeeBonusMgtLine

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

the class EmployeeBonusService method compute.

@Transactional(rollbackOn = { Exception.class })
public void compute(EmployeeBonusMgt bonus) throws AxelorException {
    Map<Employee, EmployeeBonusMgtLine> employeeStatus = new HashMap<>();
    for (EmployeeBonusMgtLine line : bonus.getEmployeeBonusMgtLineList()) {
        employeeStatus.put(line.getEmployee(), line);
    }
    List<Employee> allEmployee = Beans.get(EmployeeRepository.class).all().filter("self.mainEmploymentContract.payCompany = ?1", bonus.getCompany()).fetch();
    TemplateMaker maker = new TemplateMaker(bonus.getCompany().getTimezone(), AppFilter.getLocale(), TEMPLATE_DELIMITER, TEMPLATE_DELIMITER);
    String eval;
    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);
    Integer employeeBonusStatus = EmployeeBonusMgtRepository.STATUS_CALCULATED;
    for (Employee employee : allEmployee) {
        if (EmployeeHRRepository.isEmployeeFormerNewOrArchived(employee)) {
            continue;
        }
        // check if line is already calculated
        if (employeeStatus.get(employee) != null) {
            if (employeeStatus.get(employee).getStatusSelect().equals(EmployeeBonusMgtLineRepository.STATUS_CALCULATED)) {
                continue;
            } else {
                bonus.removeEmployeeBonusMgtLineListItem(employeeStatus.get(employee));
            }
        }
        maker.setContext(employee, "Employee");
        EmployeeBonusMgtLine line = new EmployeeBonusMgtLine();
        line.setEmployeeBonusMgt(bonus);
        line.setEmployee(employee);
        maker.addInContext("EmployeeBonusMgtLine", line);
        String formula = bonus.getEmployeeBonusType().getApplicationCondition();
        Integer lineStatus = EmployeeBonusMgtLineRepository.STATUS_CALCULATED;
        try {
            formula = replaceExpressionInFormula(formula, bonus.getCompany().getHrConfig(), employee, bonus.getPayPeriod());
        } catch (Exception e) {
            TraceBackService.trace(e);
            formula = "true";
            lineStatus = EmployeeBonusMgtLineRepository.STATUS_ANOMALY;
        }
        maker.setTemplate(formula);
        eval = maker.make();
        if (shell.evaluate(eval).toString().equals("true")) {
            try {
                formula = replaceExpressionInFormula(bonus.getEmployeeBonusType().getFormula(), bonus.getCompany().getHrConfig(), employee, bonus.getPayPeriod());
            } catch (Exception e) {
                lineStatus = EmployeeBonusMgtLineRepository.STATUS_ANOMALY;
            }
            line.setStatusSelect(lineStatus);
            if (lineStatus.equals(EmployeeBonusMgtLineRepository.STATUS_ANOMALY)) {
                employeeBonusStatus = EmployeeBonusMgtRepository.STATUS_ANOMALY;
                employeeBonusMgtLineRepo.save(line);
                continue;
            }
            line.setSeniorityDate(employee.getSeniorityDate());
            line.setCoef(employee.getBonusCoef());
            line.setWeeklyPlanning(employee.getWeeklyPlanning());
            maker.setTemplate(formula);
            eval = maker.make();
            line.setAmount(new BigDecimal(shell.evaluate(eval).toString()));
            employeeBonusMgtLineRepo.save(line);
        }
    }
    bonus.setStatusSelect(employeeBonusStatus);
    employeeBonusMgtRepo.save(bonus);
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) GroovyShell(groovy.lang.GroovyShell) AxelorException(com.axelor.exception.AxelorException) BigDecimal(java.math.BigDecimal) EmployeeRepository(com.axelor.apps.hr.db.repo.EmployeeRepository) Employee(com.axelor.apps.hr.db.Employee) EmployeeBonusMgtLine(com.axelor.apps.hr.db.EmployeeBonusMgtLine) TemplateMaker(com.axelor.tool.template.TemplateMaker) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) Transactional(com.google.inject.persist.Transactional)

Example 2 with EmployeeBonusMgtLine

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

the class PayrollPreparationService method computeEmployeeBonusAmount.

public BigDecimal computeEmployeeBonusAmount(PayrollPreparation payrollPreparation) {
    BigDecimal employeeBonusAmount = BigDecimal.ZERO;
    List<EmployeeBonusMgtLine> employeeBonusList = Beans.get(EmployeeBonusMgtLineRepository.class).all().filter("self.employee = ?1" + " AND self.employeeBonusMgt.statusSelect = ?4" + " AND (self.payrollPreparation = null" + " OR self.payrollPreparation.id = ?2)" + " AND self.employeeBonusMgt.payPeriod = ?3", payrollPreparation.getEmployee(), payrollPreparation.getId(), payrollPreparation.getPeriod(), EmployeeBonusMgtRepository.STATUS_CALCULATED).fetch();
    for (EmployeeBonusMgtLine employeeBonusMgtLine : employeeBonusList) {
        payrollPreparation.addEmployeeBonusMgtLineListItem(employeeBonusMgtLine);
        employeeBonusAmount = employeeBonusAmount.add(employeeBonusMgtLine.getAmount());
    }
    return employeeBonusAmount;
}
Also used : EmployeeBonusMgtLine(com.axelor.apps.hr.db.EmployeeBonusMgtLine) EmployeeBonusMgtLineRepository(com.axelor.apps.hr.db.repo.EmployeeBonusMgtLineRepository) BigDecimal(java.math.BigDecimal)

Example 3 with EmployeeBonusMgtLine

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

the class PayrollPreparationService method exportNibelis.

public void exportNibelis(PayrollPreparation payrollPreparation, List<String[]> list) throws AxelorException {
    HRConfig hrConfig = hrConfigService.getHRConfig(payrollPreparation.getCompany());
    // LEAVES
    if (payrollPreparation.getLeaveDuration().compareTo(BigDecimal.ZERO) > 0) {
        List<PayrollLeave> payrollLeaveList = fillInLeaves(payrollPreparation);
        for (PayrollLeave payrollLeave : payrollLeaveList) {
            if (payrollLeave.getLeaveReason().getPayrollPreprationExport()) {
                String[] leaveLine = createExportFileLine(payrollPreparation);
                leaveLine[3] = payrollLeave.getLeaveReason().getExportCode();
                leaveLine[4] = payrollLeave.getFromDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
                leaveLine[5] = payrollLeave.getToDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
                leaveLine[6] = payrollLeave.getDuration().toString();
                list.add(leaveLine);
            }
        }
    }
    // LUNCH VOUCHER MANAGEMENT
    if (payrollPreparation.getLunchVoucherNumber().compareTo(BigDecimal.ZERO) > 0) {
        String[] lunchVoucherLine = createExportFileLine(payrollPreparation);
        lunchVoucherLine[3] = hrConfig.getExportCodeForLunchVoucherManagement();
        lunchVoucherLine[6] = payrollPreparation.getLunchVoucherNumber().toString();
        list.add(lunchVoucherLine);
    }
    // EMPLOYEE BONUS MANAGEMENT
    if (payrollPreparation.getEmployeeBonusAmount().compareTo(BigDecimal.ZERO) > 0) {
        Map<String, BigDecimal> map = new HashMap<>();
        for (EmployeeBonusMgtLine bonus : payrollPreparation.getEmployeeBonusMgtLineList()) {
            if (bonus.getEmployeeBonusMgt().getEmployeeBonusType().getPayrollPreparationExport()) {
                if (map.containsKey(bonus.getEmployeeBonusMgt().getEmployeeBonusType().getExportCode())) {
                    map.put(bonus.getEmployeeBonusMgt().getEmployeeBonusType().getExportCode(), bonus.getAmount().add(map.get(bonus.getEmployeeBonusMgt().getEmployeeBonusType().getExportCode())));
                } else {
                    map.put(bonus.getEmployeeBonusMgt().getEmployeeBonusType().getExportCode(), bonus.getAmount());
                }
            }
        }
        for (Map.Entry<String, BigDecimal> entry : map.entrySet()) {
            String[] employeeBonusLine = createExportFileLine(payrollPreparation);
            employeeBonusLine[3] = entry.getKey();
            employeeBonusLine[6] = entry.getValue().toString();
            list.add(employeeBonusLine);
        }
    }
    // EXTRA HOURS
    if (payrollPreparation.getExtraHoursNumber().compareTo(BigDecimal.ZERO) > 0) {
        List<ExtraHoursLine> extraHourLineList = Beans.get(ExtraHoursLineRepository.class).all().filter("self.payrollPreparation.id = ?1" + " AND self.extraHoursType.payrollPreprationExport = ?2", payrollPreparation.getId(), true).fetch();
        Map<ExtraHoursType, BigDecimal> extraHourLineExportMap = extraHourLineList.stream().collect(Collectors.groupingBy(ExtraHoursLine::getExtraHoursType, Collectors.reducing(BigDecimal.ZERO, ExtraHoursLine::getQty, BigDecimal::add)));
        extraHourLineExportMap.forEach((extraHoursTypeGroup, totalHours) -> {
            String[] extraHourLine = createExportFileLine(payrollPreparation);
            extraHourLine[3] = extraHoursTypeGroup.getExportCode();
            extraHourLine[6] = totalHours.toString();
            list.add(extraHourLine);
        });
    }
}
Also used : HashMap(java.util.HashMap) ExtraHoursType(com.axelor.apps.hr.db.ExtraHoursType) ExtraHoursLine(com.axelor.apps.hr.db.ExtraHoursLine) BigDecimal(java.math.BigDecimal) HRConfig(com.axelor.apps.hr.db.HRConfig) EmployeeBonusMgtLine(com.axelor.apps.hr.db.EmployeeBonusMgtLine) ExtraHoursLineRepository(com.axelor.apps.hr.db.repo.ExtraHoursLineRepository) PayrollLeave(com.axelor.apps.hr.db.PayrollLeave) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EmployeeBonusMgtLine (com.axelor.apps.hr.db.EmployeeBonusMgtLine)3 BigDecimal (java.math.BigDecimal)3 HashMap (java.util.HashMap)2 Employee (com.axelor.apps.hr.db.Employee)1 ExtraHoursLine (com.axelor.apps.hr.db.ExtraHoursLine)1 ExtraHoursType (com.axelor.apps.hr.db.ExtraHoursType)1 HRConfig (com.axelor.apps.hr.db.HRConfig)1 PayrollLeave (com.axelor.apps.hr.db.PayrollLeave)1 EmployeeBonusMgtLineRepository (com.axelor.apps.hr.db.repo.EmployeeBonusMgtLineRepository)1 EmployeeRepository (com.axelor.apps.hr.db.repo.EmployeeRepository)1 ExtraHoursLineRepository (com.axelor.apps.hr.db.repo.ExtraHoursLineRepository)1 AxelorException (com.axelor.exception.AxelorException)1 TemplateMaker (com.axelor.tool.template.TemplateMaker)1 Transactional (com.google.inject.persist.Transactional)1 Binding (groovy.lang.Binding)1 GroovyShell (groovy.lang.GroovyShell)1 Map (java.util.Map)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)1