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);
}
}
Aggregations