Search in sources :

Example 26 with InvoicePayment

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

Example 27 with InvoicePayment

use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.

the class InvoicePaymentBankPayController method validateMassPayment.

@Override
public void validateMassPayment(ActionRequest request, ActionResponse response) {
    try {
        InvoicePayment invoicePayment = request.getContext().asType(InvoicePayment.class);
        if (!ObjectUtils.isEmpty(request.getContext().get("_selectedInvoices"))) {
            List<Long> invoiceIdList = Lists.transform((List) request.getContext().get("_selectedInvoices"), new Function<Object, Long>() {

                @Nullable
                @Override
                public Long apply(@Nullable Object input) {
                    return Long.parseLong(input.toString());
                }
            });
            List<InvoicePayment> invoicePaymentList = Beans.get(InvoicePaymentCreateService.class).createMassInvoicePayment(invoiceIdList, invoicePayment.getPaymentMode(), invoicePayment.getCompanyBankDetails(), invoicePayment.getPaymentDate(), invoicePayment.getBankDepositDate(), invoicePayment.getChequeNumber());
            if (!invoicePaymentList.isEmpty() && invoicePaymentList.get(0).getBankOrder() != null) {
                response.setView(ActionView.define(I18n.get("Bank order")).model(BankOrder.class.getName()).add("form", "bank-order-form").add("grid", "bank-order-grid").param("search-filters", "bank-order-filters").param("forceEdit", "true").context("_showRecord", String.valueOf(invoicePaymentList.get(0).getBankOrder().getId())).map());
            }
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Nullable(javax.annotation.Nullable) InvoicePaymentCreateService(com.axelor.apps.account.service.payment.invoice.payment.InvoicePaymentCreateService)

Example 28 with InvoicePayment

use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.

the class BatchCreditTransferInvoice method processInvoices.

/**
 * Process invoices of the specified document type.
 *
 * @param operationTypeSelect
 * @return
 */
protected List<InvoicePayment> processInvoices(int operationTypeSelect) {
    List<InvoicePayment> doneList = new ArrayList<>();
    // Can't pass an empty collection to the query
    List<Long> anomalyList = Lists.newArrayList(0L);
    AccountingBatch accountingBatch = batch.getAccountingBatch();
    boolean manageMultiBanks = appAccountService.getAppBase().getManageMultiBanks();
    StringBuilder filter = new StringBuilder();
    filter.append("self.operationTypeSelect = :operationTypeSelect " + "AND self.statusSelect = :statusSelect " + "AND self.amountRemaining > 0 " + "AND self.hasPendingPayments = FALSE " + "AND self.company = :company " + "AND self.dueDate <= :dueDate " + "AND self.paymentMode = :paymentMode " + "AND self.id NOT IN (:anomalyList)" + "AND self.pfpValidateStatusSelect != :pfpValidateStatusSelect");
    if (manageMultiBanks) {
        filter.append(" AND self.companyBankDetails IN (:bankDetailsSet)");
    }
    if (accountingBatch.getCurrency() != null) {
        filter.append(" AND self.currency = :currency");
    }
    Query<Invoice> query = invoiceRepo.all().filter(filter.toString()).bind("operationTypeSelect", operationTypeSelect).bind("statusSelect", InvoiceRepository.STATUS_VENTILATED).bind("company", accountingBatch.getCompany()).bind("dueDate", accountingBatch.getDueDate()).bind("paymentMode", accountingBatch.getPaymentMode()).bind("anomalyList", anomalyList).bind("pfpValidateStatusSelect", InvoiceRepository.PFP_STATUS_LITIGATION);
    if (manageMultiBanks) {
        Set<BankDetails> bankDetailsSet = Sets.newHashSet(accountingBatch.getBankDetails());
        if (accountingBatch.getIncludeOtherBankAccounts()) {
            bankDetailsSet.addAll(accountingBatch.getCompany().getBankDetailsList());
        }
        query.bind("bankDetailsSet", bankDetailsSet);
    }
    if (accountingBatch.getCurrency() != null) {
        query.bind("currency", accountingBatch.getCurrency());
    }
    BankDetailsRepository bankDetailsRepo = Beans.get(BankDetailsRepository.class);
    BankDetails companyBankDetails = accountingBatch.getBankDetails();
    for (List<Invoice> invoiceList; !(invoiceList = query.fetch(FETCH_LIMIT)).isEmpty(); JPA.clear()) {
        if (!JPA.em().contains(companyBankDetails)) {
            companyBankDetails = bankDetailsRepo.find(companyBankDetails.getId());
        }
        for (Invoice invoice : invoiceList) {
            try {
                doneList.add(invoicePaymentCreateService.createInvoicePayment(invoice, companyBankDetails));
                incrementDone();
            } catch (Exception ex) {
                incrementAnomaly();
                anomalyList.add(invoice.getId());
                query.bind("anomalyList", anomalyList);
                TraceBackService.trace(ex, ExceptionOriginRepository.CREDIT_TRANSFER, batch.getId());
                ex.printStackTrace();
                log.error(String.format("Credit transfer batch for invoices: anomaly for invoice %s", invoice.getInvoiceId()));
                break;
            }
        }
    }
    return doneList;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Invoice(com.axelor.apps.account.db.Invoice) BankDetails(com.axelor.apps.base.db.BankDetails) BankDetailsRepository(com.axelor.apps.base.db.repo.BankDetailsRepository) ArrayList(java.util.ArrayList) AccountingBatch(com.axelor.apps.account.db.AccountingBatch)

Example 29 with InvoicePayment

use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.

the class InvoiceServiceImpl method getMoveLinesFromInvoiceAdvancePayments.

@Override
public List<MoveLine> getMoveLinesFromInvoiceAdvancePayments(Invoice invoice) {
    List<MoveLine> advancePaymentMoveLines = new ArrayList<>();
    Set<Invoice> advancePayments = invoice.getAdvancePaymentInvoiceSet();
    List<InvoicePayment> invoicePayments;
    if (advancePayments == null || advancePayments.isEmpty()) {
        return advancePaymentMoveLines;
    }
    InvoicePaymentToolService invoicePaymentToolService = Beans.get(InvoicePaymentToolService.class);
    for (Invoice advancePayment : advancePayments) {
        invoicePayments = advancePayment.getInvoicePaymentList();
        List<MoveLine> creditMoveLines = invoicePaymentToolService.getCreditMoveLinesFromPayments(invoicePayments);
        advancePaymentMoveLines.addAll(creditMoveLines);
    }
    return advancePaymentMoveLines;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) RefundInvoice(com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice) Invoice(com.axelor.apps.account.db.Invoice) MoveLine(com.axelor.apps.account.db.MoveLine) ArrayList(java.util.ArrayList) InvoicePaymentToolService(com.axelor.apps.account.service.payment.invoice.payment.InvoicePaymentToolService)

Example 30 with InvoicePayment

use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.

the class InvoiceServiceImpl method removeBecauseOfTotalAmount.

protected boolean removeBecauseOfTotalAmount(Invoice invoice, Invoice candidateAdvancePayment) throws AxelorException {
    if (accountConfigService.getAccountConfig(invoice.getCompany()).getGenerateMoveForInvoicePayment()) {
        return false;
    }
    BigDecimal invoiceTotal = invoice.getInTaxTotal();
    List<InvoicePayment> invoicePayments = candidateAdvancePayment.getInvoicePaymentList();
    if (invoicePayments == null) {
        return false;
    }
    BigDecimal totalAmount = invoicePayments.stream().map(InvoicePayment::getAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
    return totalAmount.compareTo(invoiceTotal) > 0;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) BigDecimal(java.math.BigDecimal)

Aggregations

InvoicePayment (com.axelor.apps.account.db.InvoicePayment)31 Invoice (com.axelor.apps.account.db.Invoice)11 AxelorException (com.axelor.exception.AxelorException)9 Transactional (com.google.inject.persist.Transactional)9 BigDecimal (java.math.BigDecimal)8 ArrayList (java.util.ArrayList)7 MoveLine (com.axelor.apps.account.db.MoveLine)6 BankDetails (com.axelor.apps.base.db.BankDetails)6 Move (com.axelor.apps.account.db.Move)5 InvoicePaymentToolService (com.axelor.apps.account.service.payment.invoice.payment.InvoicePaymentToolService)4 LocalDate (java.time.LocalDate)4 PaymentMode (com.axelor.apps.account.db.PaymentMode)3 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)3 InvoicePaymentCreateService (com.axelor.apps.account.service.payment.invoice.payment.InvoicePaymentCreateService)3 BankOrder (com.axelor.apps.bankpayment.db.BankOrder)3 Map (java.util.Map)3 AccountingBatch (com.axelor.apps.account.db.AccountingBatch)2 Reconcile (com.axelor.apps.account.db.Reconcile)2 InvoicePaymentRepository (com.axelor.apps.account.db.repo.InvoicePaymentRepository)2 Company (com.axelor.apps.base.db.Company)2