Search in sources :

Example 21 with Expense

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

the class ExpenseController method ventilate.

public void ventilate(ActionRequest request, ActionResponse response) throws AxelorException {
    try {
        Expense expense = request.getContext().asType(Expense.class);
        expense = Beans.get(ExpenseRepository.class).find(expense.getId());
        Move move = Beans.get(ExpenseService.class).ventilate(expense);
        response.setReload(true);
        if (move != null) {
            response.setView(ActionView.define(I18n.get("Move")).model(Move.class.getName()).add("grid", "move-grid").add("form", "move-form").param("search-filters", "move-filters").context("_showRecord", String.valueOf(move.getId())).map());
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Expense(com.axelor.apps.hr.db.Expense) Move(com.axelor.apps.account.db.Move) ExpenseService(com.axelor.apps.hr.service.expense.ExpenseService) AxelorException(com.axelor.exception.AxelorException)

Example 22 with Expense

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

the class HumanResourceMobileController method removeLines.

/**
 * 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:removeLines
 *     Content-Type: application/json
 *     <p>URL: com.axelor.apps.hr.mobile.HumanResourceMobileController:removeLines no field
 *     <p>payload: { "data": { "action":
 *     "com.axelor.apps.hr.mobile.HumanResourceMobileController:removeLines" } }
 */
@Transactional
public void removeLines(ActionRequest request, ActionResponse response) {
    User user = AuthUtils.getUser();
    try {
        if (user == null) {
            return;
        }
        Expense expense = Beans.get(ExpenseRepository.class).all().filter("self.statusSelect = ?1 AND self.user.id = ?2", ExpenseRepository.STATUS_DRAFT, user.getId()).order("-id").fetchOne();
        if (expense == null) {
            return;
        }
        List<ExpenseLine> expenseLineList = Beans.get(ExpenseService.class).getExpenseLineList(expense);
        if (expenseLineList != null && !expenseLineList.isEmpty()) {
            Iterator<ExpenseLine> expenseLineIter = expenseLineList.iterator();
            while (expenseLineIter.hasNext()) {
                ExpenseLine generalExpenseLine = expenseLineIter.next();
                if (generalExpenseLine.getKilometricExpense() != null && (expense.getKilometricExpenseLineList() != null && !expense.getKilometricExpenseLineList().contains(generalExpenseLine) || expense.getKilometricExpenseLineList() == null)) {
                    expenseLineIter.remove();
                }
            }
        }
        response.setValue("expenseLineList", expenseLineList);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : User(com.axelor.auth.db.User) Expense(com.axelor.apps.hr.db.Expense) ExpenseService(com.axelor.apps.hr.service.expense.ExpenseService) ExpenseLine(com.axelor.apps.hr.db.ExpenseLine) AxelorException(com.axelor.exception.AxelorException) Transactional(com.google.inject.persist.Transactional)

Example 23 with Expense

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

Example 24 with Expense

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

the class HumanResourceMobileController method insertOrUpdateExpenseLine.

/*
   * This method is used in mobile application.
   * It was in ExpenseServiceImpl
   * @param request
   * @param response
   *
   * POST /open-suite-webapp/ws/action/com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine
   * Content-Type: application/json
   *
   * URL: com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine
   * fields: (id,) project, expenseType, date, comments, toInvoice, unTaxTotal, taxTotal, justification
   *
   * payload:
   * { "data": {
   * 		"action": "com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine",
   *        "id": 1,
   * 		"project": 2,
   * 		"expenseType": 10,
   * 		"date": "2018-02-22",
   * 		"comments": "No",
   * 		"toInvoice": "no",
   * 		"unTaxTotal": 100,
   *	 	"taxTotal": 2,
   *		"justification": "no"
   * } }
   */
@Transactional
public void insertOrUpdateExpenseLine(ActionRequest request, ActionResponse response) {
    try {
        User user = AuthUtils.getUser();
        Map<String, Object> requestData = request.getData();
        Project project = Beans.get(ProjectRepository.class).find(Long.valueOf(requestData.get("project").toString()));
        Product product = Beans.get(ProductRepository.class).find(Long.valueOf(requestData.get("expenseType").toString()));
        if (user != null) {
            ExpenseService expenseService = Beans.get(ExpenseService.class);
            Expense expense = expenseService.getOrCreateExpense(user);
            ExpenseLine expenseLine;
            Object idO = requestData.get("id");
            if (idO != null) {
                expenseLine = Beans.get(ExpenseLineRepository.class).find(Long.valueOf(idO.toString()));
            } else {
                expenseLine = new ExpenseLine();
            }
            expenseLine.setExpenseDate(LocalDate.parse(requestData.get("date").toString(), DateTimeFormatter.ISO_DATE));
            expenseLine.setComments(requestData.get("comments").toString());
            expenseLine.setExpenseProduct(product);
            expenseLine.setProject(project);
            expenseLine.setUser(user);
            expenseLine.setTotalAmount(new BigDecimal(requestData.get("unTaxTotal").toString()));
            expenseLine.setTotalTax(new BigDecimal(requestData.get("taxTotal").toString()));
            expenseLine.setUntaxedAmount(expenseLine.getTotalAmount().subtract(expenseLine.getTotalTax()));
            expenseLine.setToInvoice(new Boolean(requestData.get("toInvoice").toString()));
            String justification = (String) requestData.get("justification");
            if (!Strings.isNullOrEmpty(justification)) {
                String MIME_IMAGE_X_ICON = "image/x-icon";
                String MIME_IMAGE_SVG_XML = "image/svg+xml";
                String MIME_IMAGE_BMP = "image/bmp";
                String MIME_IMAGE_GIF = "image/gif";
                String MIME_IMAGE_JPEG = "image/jpeg";
                String MIME_IMAGE_TIFF = "image/tiff";
                String MIME_IMAGE_PNG = "image/png";
                Map<String, String> mimeTypeMapping = new HashMap<>(200);
                mimeTypeMapping.put(MIME_IMAGE_X_ICON, "ico");
                mimeTypeMapping.put(MIME_IMAGE_SVG_XML, "svg");
                mimeTypeMapping.put(MIME_IMAGE_BMP, "bmp");
                mimeTypeMapping.put(MIME_IMAGE_GIF, "gif");
                mimeTypeMapping.put(MIME_IMAGE_JPEG, "jpg");
                mimeTypeMapping.put(MIME_IMAGE_TIFF, "tif");
                mimeTypeMapping.put(MIME_IMAGE_PNG, "png");
                byte[] decodedFile = Base64.getDecoder().decode(justification);
                String formatName = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(decodedFile));
                String extension = "";
                if (mimeTypeMapping.containsKey(formatName)) {
                    extension = "." + mimeTypeMapping.get(formatName);
                    File file = MetaFiles.createTempFile("justification", extension).toFile();
                    Files.write(decodedFile, file);
                    MetaFile metaFile = Beans.get(MetaFiles.class).upload(file);
                    expenseLine.setJustificationMetaFile(metaFile);
                }
            }
            expense.addGeneralExpenseLineListItem(expenseLine);
            expense = expenseService.compute(expense);
            Beans.get(ExpenseRepository.class).save(expense);
            HashMap<String, Object> data = new HashMap<>();
            data.put("id", expenseLine.getId());
            response.setData(data);
            response.setTotal(1);
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : MetaFiles(com.axelor.meta.MetaFiles) User(com.axelor.auth.db.User) ProjectRepository(com.axelor.apps.project.db.repo.ProjectRepository) HashMap(java.util.HashMap) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) Product(com.axelor.apps.base.db.Product) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) Project(com.axelor.apps.project.db.Project) Expense(com.axelor.apps.hr.db.Expense) ByteArrayInputStream(java.io.ByteArrayInputStream) ExpenseRepository(com.axelor.apps.hr.db.repo.ExpenseRepository) ExpenseService(com.axelor.apps.hr.service.expense.ExpenseService) ExpenseLine(com.axelor.apps.hr.db.ExpenseLine) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Transactional(com.google.inject.persist.Transactional)

Example 25 with Expense

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

the class BankOrderMergeHRServiceImpl method mergeBankOrders.

@Transactional(rollbackOn = { AxelorException.class, Exception.class })
@Override
public BankOrder mergeBankOrders(Collection<BankOrder> bankOrders) throws AxelorException {
    if (!Beans.get(AppService.class).isApp("employee")) {
        return super.mergeBankOrders(bankOrders);
    }
    List<Expense> expenseList = expenseHRRepository.all().filter("self.bankOrder.id IN (?)", bankOrders.stream().map(BankOrder::getId).collect(Collectors.toList())).fetch();
    for (Expense expense : expenseList) {
        expense.setBankOrder(null);
        expenseHRRepository.save(expense);
    }
    BankOrder bankOrder = super.mergeBankOrders(bankOrders);
    for (Expense expense : expenseList) {
        expense.setBankOrder(bankOrder);
        expenseHRRepository.save(expense);
    }
    return bankOrder;
}
Also used : Expense(com.axelor.apps.hr.db.Expense) BankOrder(com.axelor.apps.bankpayment.db.BankOrder) Transactional(com.google.inject.persist.Transactional)

Aggregations

Expense (com.axelor.apps.hr.db.Expense)27 AxelorException (com.axelor.exception.AxelorException)14 ExpenseService (com.axelor.apps.hr.service.expense.ExpenseService)12 ExpenseLine (com.axelor.apps.hr.db.ExpenseLine)7 Transactional (com.google.inject.persist.Transactional)7 ExpenseRepository (com.axelor.apps.hr.db.repo.ExpenseRepository)6 BigDecimal (java.math.BigDecimal)5 MessageServiceBaseImpl (com.axelor.apps.base.service.message.MessageServiceBaseImpl)4 IExceptionMessage (com.axelor.apps.hr.exception.IExceptionMessage)4 Message (com.axelor.apps.message.db.Message)4 User (com.axelor.auth.db.User)4 ArrayList (java.util.ArrayList)4 Product (com.axelor.apps.base.db.Product)3 Employee (com.axelor.apps.hr.db.Employee)3 BankOrder (com.axelor.apps.bankpayment.db.BankOrder)2 Company (com.axelor.apps.base.db.Company)2 AccountingBatch (com.axelor.apps.account.db.AccountingBatch)1 Move (com.axelor.apps.account.db.Move)1 BankDetails (com.axelor.apps.base.db.BankDetails)1 Period (com.axelor.apps.base.db.Period)1