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);
}
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);
}
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;
}
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);
}
Aggregations