Search in sources :

Example 1 with KilometricService

use of com.axelor.apps.hr.service.KilometricService in project axelor-open-suite by axelor.

the class ExpenseController method computeDistanceAndKilometricExpense.

public void computeDistanceAndKilometricExpense(ActionRequest request, ActionResponse response) throws AxelorException {
    // Compute distance.
    try {
        if (!Beans.get(AppHumanResourceService.class).getAppExpense().getComputeDistanceWithWebService()) {
            return;
        }
        Context context = request.getContext();
        ExpenseLine expenseLine = context.asType(ExpenseLine.class);
        if (Strings.isNullOrEmpty(expenseLine.getFromCity()) || Strings.isNullOrEmpty(expenseLine.getToCity())) {
            return;
        }
        KilometricService kilometricService = Beans.get(KilometricService.class);
        BigDecimal distance = kilometricService.computeDistance(expenseLine);
        expenseLine.setDistance(distance);
        response.setValue("distance", distance);
        if (expenseLine.getKilometricAllowParam() == null || expenseLine.getExpenseDate() == null || expenseLine.getKilometricTypeSelect() == 0) {
            return;
        }
        Expense expense = expenseLine.getExpense();
        if (expense == null) {
            expense = context.getParent().asType(Expense.class);
        }
        Employee employee = expense.getUser().getEmployee();
        if (employee == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_USER_EMPLOYEE), expense.getUser().getName());
        }
        BigDecimal amount = kilometricService.computeKilometricExpense(expenseLine, employee);
        response.setValue("totalAmount", amount);
        response.setValue("untaxedAmount", amount);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Context(com.axelor.rpc.Context) AxelorException(com.axelor.exception.AxelorException) Employee(com.axelor.apps.hr.db.Employee) AppHumanResourceService(com.axelor.apps.hr.service.app.AppHumanResourceService) Expense(com.axelor.apps.hr.db.Expense) ExpenseLine(com.axelor.apps.hr.db.ExpenseLine) KilometricService(com.axelor.apps.hr.service.KilometricService) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException)

Example 2 with KilometricService

use of com.axelor.apps.hr.service.KilometricService in project axelor-open-suite by axelor.

the class ExpenseServiceImpl method validate.

@Override
@Transactional(rollbackOn = { Exception.class })
public void validate(Expense expense) throws AxelorException {
    Employee employee = expense.getUser().getEmployee();
    if (employee == null) {
        throw new AxelorException(expense, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_USER_EMPLOYEE), expense.getUser().getFullName());
    }
    if (expense.getPeriod() == null) {
        throw new AxelorException(expense, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.EXPENSE_MISSING_PERIOD));
    }
    List<ExpenseLine> kilometricExpenseLineList = expense.getKilometricExpenseLineList();
    KilometricService kilometricService = Beans.get(KilometricService.class);
    if (ObjectUtils.notEmpty(kilometricExpenseLineList)) {
        for (ExpenseLine line : kilometricExpenseLineList) {
            BigDecimal amount = kilometricService.computeKilometricExpense(line, employee);
            line.setTotalAmount(amount);
            line.setUntaxedAmount(amount);
            kilometricService.updateKilometricLog(line, employee);
        }
        compute(expense);
    }
    Beans.get(EmployeeAdvanceService.class).fillExpenseWithAdvances(expense);
    expense.setStatusSelect(ExpenseRepository.STATUS_VALIDATED);
    expense.setValidatedBy(AuthUtils.getUser());
    expense.setValidationDate(appAccountService.getTodayDate(expense.getCompany()));
    if (expense.getUser().getPartner() != null) {
        PaymentMode paymentMode = expense.getUser().getPartner().getOutPaymentMode();
        expense.setPaymentMode(paymentMode);
    }
    expenseRepository.save(expense);
}
Also used : AxelorException(com.axelor.exception.AxelorException) Employee(com.axelor.apps.hr.db.Employee) EmployeeAdvanceService(com.axelor.apps.hr.service.EmployeeAdvanceService) ExpenseLine(com.axelor.apps.hr.db.ExpenseLine) KilometricService(com.axelor.apps.hr.service.KilometricService) BigDecimal(java.math.BigDecimal) PaymentMode(com.axelor.apps.account.db.PaymentMode) Transactional(com.google.inject.persist.Transactional)

Aggregations

Employee (com.axelor.apps.hr.db.Employee)2 ExpenseLine (com.axelor.apps.hr.db.ExpenseLine)2 KilometricService (com.axelor.apps.hr.service.KilometricService)2 AxelorException (com.axelor.exception.AxelorException)2 BigDecimal (java.math.BigDecimal)2 PaymentMode (com.axelor.apps.account.db.PaymentMode)1 Expense (com.axelor.apps.hr.db.Expense)1 EmployeeAdvanceService (com.axelor.apps.hr.service.EmployeeAdvanceService)1 AppHumanResourceService (com.axelor.apps.hr.service.app.AppHumanResourceService)1 Context (com.axelor.rpc.Context)1 Transactional (com.google.inject.persist.Transactional)1