Search in sources :

Example 6 with InvoicePayment

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

the class InvoicePaymentCreateServiceImpl method createInvoicePayment.

/**
 * @param invoice
 * @param amount
 * @param paymentDate
 * @param paymentMove
 * @return
 */
public InvoicePayment createInvoicePayment(Invoice invoice, BigDecimal amount, Move paymentMove) throws AxelorException {
    LocalDate paymentDate = paymentMove.getDate();
    BigDecimal amountConverted = currencyService.getAmountCurrencyConvertedAtDate(paymentMove.getCompanyCurrency(), paymentMove.getCurrency(), amount, paymentDate);
    int typePaymentMove = this.determineType(paymentMove);
    Currency currency = paymentMove.getCurrency();
    if (currency == null) {
        currency = paymentMove.getCompanyCurrency();
    }
    PaymentMode paymentMode;
    InvoicePayment invoicePayment;
    if (typePaymentMove == InvoicePaymentRepository.TYPE_REFUND_INVOICE || typePaymentMove == InvoicePaymentRepository.TYPE_INVOICE) {
        paymentMode = null;
    } else {
        paymentMode = paymentMove.getPaymentMode();
    }
    invoicePayment = this.createInvoicePayment(invoice, amountConverted, paymentDate, currency, paymentMode, typePaymentMove);
    invoicePayment.setMove(paymentMove);
    invoicePayment.setStatusSelect(InvoicePaymentRepository.STATUS_VALIDATED);
    PaymentVoucher paymentVoucher = paymentMove.getPaymentVoucher();
    if (paymentVoucher != null) {
        invoicePayment.setCompanyBankDetails(paymentVoucher.getCompanyBankDetails());
    } else if (invoice.getSchedulePaymentOk() && invoice.getPaymentSchedule() != null) {
        BankDetails companyBankDetails = invoice.getPaymentSchedule().getCompanyBankDetails();
        invoicePayment.setCompanyBankDetails(companyBankDetails);
    }
    computeAdvancePaymentImputation(invoicePayment, paymentMove);
    invoice.addInvoicePaymentListItem(invoicePayment);
    invoicePaymentToolService.updateAmountPaid(invoice);
    invoicePaymentRepository.save(invoicePayment);
    return invoicePayment;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) BankDetails(com.axelor.apps.base.db.BankDetails) Currency(com.axelor.apps.base.db.Currency) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) PaymentVoucher(com.axelor.apps.account.db.PaymentVoucher) PaymentMode(com.axelor.apps.account.db.PaymentMode)

Example 7 with InvoicePayment

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

the class InvoicePaymentCreateServiceImpl method computeAdvancePaymentImputation.

protected void computeAdvancePaymentImputation(InvoicePayment invoicePayment, Move paymentMove) {
    // check if the payment is an advance payment imputation
    Invoice advanceInvoice = determineIfReconcileFromInvoice(paymentMove);
    if (advanceInvoice != null) {
        List<InvoicePayment> invoicePaymentList = advanceInvoice.getInvoicePaymentList();
        if (invoicePaymentList != null && !invoicePaymentList.isEmpty()) {
            // set right type
            invoicePayment.setTypeSelect(InvoicePaymentRepository.TYPE_ADV_PAYMENT_IMPUTATION);
            // create link between advance payment and its imputation
            InvoicePayment advancePayment = advanceInvoice.getInvoicePaymentList().get(0);
            advancePayment.setImputedBy(invoicePayment);
            invoicePaymentRepository.save(advancePayment);
            // set the imputed payment currency
            invoicePayment.setCurrency(advancePayment.getCurrency());
            BigDecimal currentImputedAmount = invoicePayment.getAmount();
            // we force the payment amount to be equal to the advance
            // invoice amount, so we get the right amount in the
            // right currency.
            BigDecimal totalAmountInAdvanceInvoice = advancePayment.getInvoice().getCompanyInTaxTotal();
            BigDecimal convertedImputedAmount = currentImputedAmount.multiply(advancePayment.getAmount()).divide(totalAmountInAdvanceInvoice, 2, RoundingMode.HALF_UP);
            invoicePayment.setAmount(convertedImputedAmount);
        }
    }
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Invoice(com.axelor.apps.account.db.Invoice) BigDecimal(java.math.BigDecimal)

Example 8 with InvoicePayment

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

the class InvoicePaymentCreateServiceImpl method determineIfReconcileFromInvoice.

/**
 * We try to get to the status of the invoice from the reconcile to see if this move was created
 * from a payment for an advance payment invoice.
 *
 * @param move
 * @return the found advance invoice if the move is from a payment that comes from this invoice.
 *     null in other cases
 */
protected Invoice determineIfReconcileFromInvoice(Move move) {
    List<MoveLine> moveLineList = move.getMoveLineList();
    if (moveLineList == null || moveLineList.size() != 2) {
        return null;
    }
    InvoicePaymentRepository invoicePaymentRepo = Beans.get(InvoicePaymentRepository.class);
    for (MoveLine moveLine : moveLineList) {
        // search for the reconcile between the debit line
        if (moveLine.getDebit().compareTo(BigDecimal.ZERO) > 0) {
            Reconcile reconcile = Beans.get(ReconcileRepository.class).all().filter("self.debitMoveLine = ?", moveLine).fetchOne();
            if (reconcile == null) {
                return null;
            }
            // associated payment
            if (reconcile.getCreditMoveLine() == null || reconcile.getCreditMoveLine().getMove() == null) {
                continue;
            }
            Move candidatePaymentMove = reconcile.getCreditMoveLine().getMove();
            InvoicePayment invoicePayment = invoicePaymentRepo.all().filter("self.move = :_move").bind("_move", candidatePaymentMove).fetchOne();
            // payment, then return true.
            if (invoicePayment != null && invoicePayment.getInvoice() != null && invoicePayment.getInvoice().getOperationSubTypeSelect() == InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE) {
                return invoicePayment.getInvoice();
            }
        }
    }
    return null;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) ReconcileRepository(com.axelor.apps.account.db.repo.ReconcileRepository) InvoicePaymentRepository(com.axelor.apps.account.db.repo.InvoicePaymentRepository) Reconcile(com.axelor.apps.account.db.Reconcile)

Example 9 with InvoicePayment

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

the class InvoicePaymentCreateServiceImpl method createInvoicePayment.

@Transactional
public InvoicePayment createInvoicePayment(Invoice invoice, PaymentMode paymentMode, BankDetails companyBankDetails, LocalDate paymentDate, LocalDate bankDepositDate, String chequeNumber) {
    InvoicePayment invoicePayment = createInvoicePayment(invoice, invoice.getInTaxTotal().subtract(invoice.getAmountPaid()), paymentDate, invoice.getCurrency(), paymentMode, InvoicePaymentRepository.TYPE_PAYMENT);
    invoicePayment.setCompanyBankDetails(companyBankDetails);
    invoicePayment.setBankDepositDate(bankDepositDate);
    invoicePayment.setChequeNumber(chequeNumber);
    return invoicePaymentRepository.save(invoicePayment);
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Transactional(com.google.inject.persist.Transactional)

Example 10 with InvoicePayment

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

the class InvoicePaymentToolServiceImpl method getCreditMoveLinesFromPayments.

@Override
public List<MoveLine> getCreditMoveLinesFromPayments(List<InvoicePayment> payments) {
    List<MoveLine> moveLines = new ArrayList<>();
    for (InvoicePayment payment : payments) {
        Move move = payment.getMove();
        if (move == null || move.getMoveLineList() == null || move.getMoveLineList().isEmpty()) {
            continue;
        }
        moveLines.addAll(moveToolService.getToReconcileCreditMoveLines(move));
    }
    return moveLines;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) ArrayList(java.util.ArrayList)

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