use of com.axelor.apps.hr.db.LeaveManagement 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);
}
}
use of com.axelor.apps.hr.db.LeaveManagement in project axelor-open-suite by axelor.
the class LeaveManagementService method computeQuantityAvailable.
public LeaveLine computeQuantityAvailable(LeaveLine leaveLine) {
List<LeaveManagement> leaveManagementList = leaveLine.getLeaveManagementList();
leaveLine.setTotalQuantity(BigDecimal.ZERO);
if (leaveManagementList != null && !leaveManagementList.isEmpty()) {
for (LeaveManagement leaveManagement : leaveManagementList) {
leaveLine.setTotalQuantity(leaveLine.getTotalQuantity().add(leaveManagement.getValue()));
}
leaveLine.setQuantity(leaveLine.getTotalQuantity().subtract(leaveLine.getDaysValidated()));
}
return leaveLine;
}
use of com.axelor.apps.hr.db.LeaveManagement in project axelor-open-suite by axelor.
the class LeaveManagementService method createLeaveManagement.
@Transactional
public LeaveManagement createLeaveManagement(LeaveLine leaveLine, User user, String comments, LocalDate date, LocalDate fromDate, LocalDate toDate, BigDecimal value) {
LeaveManagement leaveManagement = new LeaveManagement();
leaveManagement.setLeaveLine(leaveLine);
leaveManagement.setUser(user);
leaveManagement.setComments(comments);
if (date == null) {
leaveManagement.setDate(appBaseService.getTodayDate(user.getActiveCompany()));
} else {
leaveManagement.setDate(date);
}
leaveManagement.setFromDate(fromDate);
leaveManagement.setToDate(toDate);
leaveManagement.setValue(value.setScale(4, RoundingMode.HALF_UP));
return leaveManagement;
}
use of com.axelor.apps.hr.db.LeaveManagement in project axelor-open-suite by axelor.
the class LeaveManagementService method reset.
/**
* Reset leave management list by adding a new leave management line with negative quantity.
*
* @param leaveLine
* @param user
* @param comments
* @param date
* @param fromDate
* @param toDate
*/
@Transactional
public void reset(LeaveLine leaveLine, User user, String comments, LocalDate date, LocalDate fromDate, LocalDate toDate) {
LeaveManagement leaveManagement = createLeaveManagement(leaveLine, user, comments, date, fromDate, toDate, leaveLine.getQuantity().negate());
leaveLine.addLeaveManagementListItem(leaveManagement);
leaveLine.setQuantity(BigDecimal.ZERO);
leaveLine.setTotalQuantity(BigDecimal.ZERO);
}
use of com.axelor.apps.hr.db.LeaveManagement 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);
}
Aggregations