Search in sources :

Example 1 with KilometricAllowParamRepository

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

the class HumanResourceMobileController method insertKMExpenses.

/**
 * This method is used in mobile application. It was in ExpenseController
 *
 * @param request
 * @param response
 * @throws AxelorException
 *     <p>POST
 *     /open-suite-webapp/ws/action/com.axelor.apps.hr.mobile.HumanResourceMobileController:insertKMExpenses
 *     Content-Type: application/json
 *     <p>URL: com.axelor.apps.hr.mobile.HumanResourceMobileController:insertKMExpenses fields:
 *     kmNumber, locationFrom, locationTo, allowanceTypeSelect, comments, date, projectTask,
 *     kilometricAllowParam
 *     <p>payload: { "data": { "action":
 *     "com.axelor.apps.hr.mobile.HumanResourceMobileController:insertKMExpenses", "kmNumber":
 *     350.00, "locationFrom": "Paris", "locationTo": "Marseille", "allowanceTypeSelect": 1,
 *     "comments": "no", "date": "2018-02-22", "expenseProduct": 43 } }
 */
@Transactional(rollbackOn = { Exception.class })
public void insertKMExpenses(ActionRequest request, ActionResponse response) throws AxelorException {
    User user = AuthUtils.getUser();
    if (user != null) {
        ExpenseService expenseService = Beans.get(ExpenseService.class);
        Expense expense = expenseService.getOrCreateExpense(user);
        ExpenseLine expenseLine = new ExpenseLine();
        expenseLine.setDistance(new BigDecimal(request.getData().get("kmNumber").toString()));
        expenseLine.setFromCity(request.getData().get("locationFrom").toString());
        expenseLine.setToCity(request.getData().get("locationTo").toString());
        expenseLine.setKilometricTypeSelect(new Integer(request.getData().get("allowanceTypeSelect").toString()));
        expenseLine.setComments(request.getData().get("comments").toString());
        expenseLine.setExpenseDate(LocalDate.parse(request.getData().get("date").toString()));
        expenseLine.setProject(Beans.get(ProjectRepository.class).find(Long.valueOf(request.getData().get("projectTask").toString())));
        HRConfigService hrConfigService = Beans.get(HRConfigService.class);
        HRConfig hrConfig = hrConfigService.getHRConfig(expense.getCompany());
        Product expenseProduct = hrConfigService.getKilometricExpenseProduct(hrConfig);
        expenseLine.setExpenseProduct(expenseProduct);
        Employee employee = user.getEmployee();
        if (employee != null && !EmployeeHRRepository.isEmployeeFormerNewOrArchived(employee)) {
            KilometricAllowParamRepository kilometricAllowParamRepo = Beans.get(KilometricAllowParamRepository.class);
            expenseLine.setKilometricAllowParam(kilometricAllowParamRepo.find(Long.valueOf(request.getData().get("kilometricAllowParam").toString())));
            expenseLine.setTotalAmount(Beans.get(KilometricService.class).computeKilometricExpense(expenseLine, employee));
            expenseLine.setUntaxedAmount(expenseLine.getTotalAmount());
        }
        expense.addKilometricExpenseLineListItem(expenseLine);
        Beans.get(ExpenseRepository.class).save(expense);
        response.setValue("id", expenseLine.getId());
    }
}
Also used : User(com.axelor.auth.db.User) Employee(com.axelor.apps.hr.db.Employee) HRConfig(com.axelor.apps.hr.db.HRConfig) Expense(com.axelor.apps.hr.db.Expense) KilometricAllowParamRepository(com.axelor.apps.hr.db.repo.KilometricAllowParamRepository) ExpenseRepository(com.axelor.apps.hr.db.repo.ExpenseRepository) ExpenseService(com.axelor.apps.hr.service.expense.ExpenseService) HRConfigService(com.axelor.apps.hr.service.config.HRConfigService) Product(com.axelor.apps.base.db.Product) ExpenseLine(com.axelor.apps.hr.db.ExpenseLine) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Aggregations

Product (com.axelor.apps.base.db.Product)1 Employee (com.axelor.apps.hr.db.Employee)1 Expense (com.axelor.apps.hr.db.Expense)1 ExpenseLine (com.axelor.apps.hr.db.ExpenseLine)1 HRConfig (com.axelor.apps.hr.db.HRConfig)1 ExpenseRepository (com.axelor.apps.hr.db.repo.ExpenseRepository)1 KilometricAllowParamRepository (com.axelor.apps.hr.db.repo.KilometricAllowParamRepository)1 HRConfigService (com.axelor.apps.hr.service.config.HRConfigService)1 ExpenseService (com.axelor.apps.hr.service.expense.ExpenseService)1 User (com.axelor.auth.db.User)1 Transactional (com.google.inject.persist.Transactional)1 BigDecimal (java.math.BigDecimal)1