Search in sources :

Example 1 with InvoicePaymentCreateService

use of com.axelor.apps.account.service.payment.invoice.payment.InvoicePaymentCreateService in project axelor-open-suite by axelor.

the class BatchDirectDebitCustomerInvoice method processQuery.

private List<InvoicePayment> processQuery(List<String> filterList, List<Pair<String, Object>> bindingList) {
    List<InvoicePayment> doneList = new ArrayList<>();
    List<Long> anomalyList = Lists.newArrayList(0L);
    filterList.add("self.id NOT IN (:anomalyList)");
    bindingList.add(Pair.of("anomalyList", (Object) anomalyList));
    String filter = Joiner.on(" AND ").join(Lists.transform(filterList, new Function<String, String>() {

        @Override
        public String apply(String input) {
            return String.format("(%s)", input);
        }
    }));
    Query<Invoice> query = Beans.get(InvoiceRepository.class).all().filter(filter);
    for (Pair<String, Object> binding : bindingList) {
        query.bind(binding.getLeft(), binding.getRight());
    }
    Set<Long> treatedSet = new HashSet<>();
    List<Invoice> invoiceList;
    InvoicePaymentCreateService invoicePaymentCreateService = Beans.get(InvoicePaymentCreateService.class);
    BankDetailsRepository bankDetailsRepo = Beans.get(BankDetailsRepository.class);
    BankDetails companyBankDetails = getCompanyBankDetails(batch.getAccountingBatch());
    while (!(invoiceList = query.fetch(FETCH_LIMIT)).isEmpty()) {
        if (!JPA.em().contains(companyBankDetails)) {
            companyBankDetails = bankDetailsRepo.find(companyBankDetails.getId());
        }
        for (Invoice invoice : invoiceList) {
            if (treatedSet.contains(invoice.getId())) {
                throw new IllegalArgumentException("Invoice payment generation error");
            }
            treatedSet.add(invoice.getId());
            try {
                doneList.add(invoicePaymentCreateService.createInvoicePayment(invoice, companyBankDetails));
                incrementDone();
            } catch (Exception e) {
                incrementAnomaly();
                anomalyList.add(invoice.getId());
                query.bind("anomalyList", anomalyList);
                TraceBackService.trace(e, ExceptionOriginRepository.DIRECT_DEBIT, batch.getId());
                LOG.error(e.getMessage());
                break;
            }
        }
        JPA.clear();
    }
    return doneList;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Invoice(com.axelor.apps.account.db.Invoice) BankDetailsRepository(com.axelor.apps.base.db.repo.BankDetailsRepository) BankDetails(com.axelor.apps.base.db.BankDetails) ArrayList(java.util.ArrayList) AxelorException(com.axelor.exception.AxelorException) Function(com.google.common.base.Function) InvoicePaymentCreateService(com.axelor.apps.account.service.payment.invoice.payment.InvoicePaymentCreateService) HashSet(java.util.HashSet)

Aggregations

Invoice (com.axelor.apps.account.db.Invoice)1 InvoicePayment (com.axelor.apps.account.db.InvoicePayment)1 InvoicePaymentCreateService (com.axelor.apps.account.service.payment.invoice.payment.InvoicePaymentCreateService)1 BankDetails (com.axelor.apps.base.db.BankDetails)1 BankDetailsRepository (com.axelor.apps.base.db.repo.BankDetailsRepository)1 AxelorException (com.axelor.exception.AxelorException)1 Function (com.google.common.base.Function)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1