Search in sources :

Example 1 with KilometricLog

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

the class KilometricService method updateKilometricLog.

@Transactional(rollbackOn = { Exception.class })
public void updateKilometricLog(ExpenseLine expenseLine, Employee employee) throws AxelorException {
    KilometricLog log = getOrCreateKilometricLog(employee, expenseLine.getExpenseDate());
    if (log.getExpenseLineList() == null || !log.getExpenseLineList().contains(expenseLine)) {
        log.addExpenseLineListItem(expenseLine);
        log.setDistanceTravelled(log.getDistanceTravelled().add(expenseLine.getDistance()));
    }
    kilometricLogRepo.save(log);
}
Also used : KilometricLog(com.axelor.apps.hr.db.KilometricLog) Transactional(com.google.inject.persist.Transactional)

Example 2 with KilometricLog

use of com.axelor.apps.hr.db.KilometricLog 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)

Example 3 with KilometricLog

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

the class KilometricService method createKilometricLog.

public KilometricLog createKilometricLog(Employee employee, BigDecimal distance, Year year) {
    KilometricLog log = new KilometricLog();
    log.setDistanceTravelled(distance);
    log.setYear(year);
    employee.addKilometricLogListItem(log);
    return log;
}
Also used : KilometricLog(com.axelor.apps.hr.db.KilometricLog)

Example 4 with KilometricLog

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

the class KilometricService method getOrCreateKilometricLog.

public KilometricLog getOrCreateKilometricLog(Employee employee, LocalDate date) throws AxelorException {
    KilometricLog log = getKilometricLog(employee, date);
    if (log != null) {
        return log;
    }
    if (employee.getMainEmploymentContract() == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_CONTRACT_OF_EMPLOYMENT), employee.getName());
    }
    Year year = Beans.get(YearServiceImpl.class).getYear(date, employee.getMainEmploymentContract().getPayCompany(), YearRepository.TYPE_CIVIL);
    if (year == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.KILOMETRIC_LOG_NO_CIVIL_YEAR), employee.getMainEmploymentContract().getPayCompany(), date);
    }
    return createKilometricLog(employee, new BigDecimal("0.00"), year);
}
Also used : AxelorException(com.axelor.exception.AxelorException) Year(com.axelor.apps.base.db.Year) YearServiceImpl(com.axelor.apps.base.service.YearServiceImpl) KilometricLog(com.axelor.apps.hr.db.KilometricLog) BigDecimal(java.math.BigDecimal)

Aggregations

KilometricLog (com.axelor.apps.hr.db.KilometricLog)4 AxelorException (com.axelor.exception.AxelorException)2 BigDecimal (java.math.BigDecimal)2 Company (com.axelor.apps.base.db.Company)1 Year (com.axelor.apps.base.db.Year)1 YearServiceImpl (com.axelor.apps.base.service.YearServiceImpl)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 HRConfigService (com.axelor.apps.hr.service.config.HRConfigService)1 Transactional (com.google.inject.persist.Transactional)1 ArrayList (java.util.ArrayList)1