Search in sources :

Example 1 with EmployeeAdvance

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()));
    }
}
Also used : EmployeeRepository(com.axelor.apps.hr.db.repo.EmployeeRepository) Employee(com.axelor.apps.hr.db.Employee) EmployeeAdvance(com.axelor.apps.hr.db.EmployeeAdvance) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Aggregations

Employee (com.axelor.apps.hr.db.Employee)1 EmployeeAdvance (com.axelor.apps.hr.db.EmployeeAdvance)1 EmployeeRepository (com.axelor.apps.hr.db.repo.EmployeeRepository)1 Transactional (com.google.inject.persist.Transactional)1 BigDecimal (java.math.BigDecimal)1