Search in sources :

Example 1 with KilometricAllowanceRate

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

the class KilometricService method computeKilometricExpense.

public BigDecimal computeKilometricExpense(ExpenseLine expenseLine, Employee employee) throws AxelorException {
    BigDecimal distance = expenseLine.getDistance();
    EmploymentContract mainEmploymentContract = employee.getMainEmploymentContract();
    if (mainEmploymentContract == null || mainEmploymentContract.getPayCompany() == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_CONTRACT_OF_EMPLOYMENT), employee.getName());
    }
    Company company = mainEmploymentContract.getPayCompany();
    KilometricLog log = getKilometricLog(employee, expenseLine.getExpenseDate());
    BigDecimal previousDistance = log == null ? BigDecimal.ZERO : log.getDistanceTravelled();
    KilometricAllowanceRate allowance = expenseLine.getKilometricAllowParam() != null ? Beans.get(KilometricAllowanceRateRepository.class).all().filter("self.kilometricAllowParam.id = :_kilometricAllowParamId " + "and self.hrConfig.id = :_hrConfigId").bind("_kilometricAllowParamId", expenseLine.getKilometricAllowParam().getId()).bind("_hrConfigId", Beans.get(HRConfigService.class).getHRConfig(company).getId()).fetchOne() : null;
    if (allowance == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.KILOMETRIC_ALLOWANCE_RATE_MISSING), expenseLine.getKilometricAllowParam() != null ? expenseLine.getKilometricAllowParam().getName() : "", company.getName());
    }
    List<KilometricAllowanceRule> ruleList = new ArrayList<>();
    List<KilometricAllowanceRule> allowanceRuleList = allowance.getKilometricAllowanceRuleList();
    if (ObjectUtils.notEmpty(allowanceRuleList)) {
        for (KilometricAllowanceRule rule : allowanceRuleList) {
            if (rule.getMinimumCondition().compareTo(previousDistance.add(distance)) <= 0 && rule.getMaximumCondition().compareTo(previousDistance) >= 0) {
                ruleList.add(rule);
            }
        }
    }
    if (ruleList.isEmpty()) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.KILOMETRIC_ALLOWANCE_NO_RULE), allowance.getKilometricAllowParam().getName());
    }
    BigDecimal price = BigDecimal.ZERO;
    if (ruleList.size() == 1) {
        price = distance.multiply(ruleList.get(0).getRate());
    } else {
        Collections.sort(ruleList, (object1, object2) -> object1.getMinimumCondition().compareTo(object2.getMinimumCondition()));
        for (KilometricAllowanceRule rule : ruleList) {
            BigDecimal min = rule.getMinimumCondition().max(previousDistance);
            BigDecimal max = rule.getMaximumCondition().min(previousDistance.add(distance));
            price = price.add(max.subtract(min).multiply(rule.getRate()));
        }
    }
    return price.setScale(2, RoundingMode.HALF_UP);
}
Also used : EmploymentContract(com.axelor.apps.hr.db.EmploymentContract) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) ArrayList(java.util.ArrayList) KilometricAllowanceRate(com.axelor.apps.hr.db.KilometricAllowanceRate) HRConfigService(com.axelor.apps.hr.service.config.HRConfigService) KilometricLog(com.axelor.apps.hr.db.KilometricLog) KilometricAllowanceRule(com.axelor.apps.hr.db.KilometricAllowanceRule) BigDecimal(java.math.BigDecimal)

Aggregations

Company (com.axelor.apps.base.db.Company)1 EmploymentContract (com.axelor.apps.hr.db.EmploymentContract)1 KilometricAllowanceRate (com.axelor.apps.hr.db.KilometricAllowanceRate)1 KilometricAllowanceRule (com.axelor.apps.hr.db.KilometricAllowanceRule)1 KilometricLog (com.axelor.apps.hr.db.KilometricLog)1 HRConfigService (com.axelor.apps.hr.service.config.HRConfigService)1 AxelorException (com.axelor.exception.AxelorException)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1