use of com.axelor.apps.hr.db.EmployeeAdvance in project axelor-open-suite by axelor.
the class EmployeeAdvanceService method fillExpenseWithAdvances.
@Transactional
public void fillExpenseWithAdvances(Expense expense) {
Employee employee = Beans.get(EmployeeRepository.class).find(expense.getUser().getEmployee().getId());
if (EmployeeHRRepository.isEmployeeFormerNewOrArchived(employee)) {
return;
}
List<EmployeeAdvance> advanceList = employeeAdvanceRepository.all().filter("self.employee.id = ?1 AND self.remainingAmount > 0 AND self.date < ?2 AND self.statusSelect = ?3 AND self.typeSelect = ?4", employee.getId(), expense.getPeriod().getToDate(), EmployeeAdvanceRepository.STATUS_VALIDATED, EmployeeAdvanceRepository.TYPE_OCCASIONAL).fetch();
if (advanceList != null && !advanceList.isEmpty()) {
BigDecimal currentAmountToRefund = expense.getInTaxTotal().subtract(expense.getPersonalExpenseAmount()).subtract(expense.getWithdrawnCash());
for (EmployeeAdvance advance : advanceList) {
if (currentAmountToRefund.signum() == 0) {
break;
}
currentAmountToRefund = withdrawFromAdvance(advance, expense, currentAmountToRefund);
employeeAdvanceRepository.save(advance);
}
expense.setAdvanceAmount(expense.getInTaxTotal().subtract(currentAmountToRefund).subtract(expense.getPersonalExpenseAmount()).subtract(expense.getWithdrawnCash()));
}
}
Aggregations