Search in sources :

Example 6 with PayVoucherElementToPay

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

the class PaymentVoucherConfirmService method createMoveAndConfirm.

/**
 * Confirm payment voucher and create move.
 *
 * @param paymentVoucher
 * @throws AxelorException
 */
@Transactional(rollbackOn = { Exception.class })
public void createMoveAndConfirm(PaymentVoucher paymentVoucher) throws AxelorException {
    Partner payerPartner = paymentVoucher.getPartner();
    PaymentMode paymentMode = paymentVoucher.getPaymentMode();
    Company company = paymentVoucher.getCompany();
    BankDetails companyBankDetails = paymentVoucher.getCompanyBankDetails();
    Journal journal = paymentModeService.getPaymentModeJournal(paymentMode, company, companyBankDetails);
    LocalDate paymentDate = paymentVoucher.getPaymentDate();
    boolean scheduleToBePaid = false;
    Account paymentModeAccount = paymentModeService.getPaymentModeAccount(paymentMode, company, companyBankDetails);
    // If paid by a moveline check if all the lines selected have the same account + company
    // Excess payment
    boolean allRight = paymentVoucherControlService.checkIfSameAccount(paymentVoucher.getPayVoucherElementToPayList(), paymentVoucher.getMoveLine());
    // Check if allright=true (means companies and accounts in lines are all the same and same as in
    // move line selected for paying
    log.debug("allRight : {}", allRight);
    if (allRight) {
        scheduleToBePaid = this.toPayWithExcessPayment(paymentVoucher.getPayVoucherElementToPayList(), paymentVoucher.getMoveLine(), scheduleToBePaid, paymentDate);
    }
    if (paymentVoucher.getMoveLine() == null || (paymentVoucher.getMoveLine() != null && !allRight) || (scheduleToBePaid && !allRight && paymentVoucher.getMoveLine() != null)) {
        // Manage all the cases in the same way. As if a move line (Excess payment) is selected, we
        // cancel it first
        Move move = moveService.getMoveCreateService().createMoveWithPaymentVoucher(journal, company, paymentVoucher, payerPartner, paymentDate, paymentMode, MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_PAYMENT);
        move.setPaymentVoucher(paymentVoucher);
        move.setTradingName(paymentVoucher.getTradingName());
        paymentVoucher.setGeneratedMove(move);
        // Create move lines for payment lines
        BigDecimal paidLineTotal = BigDecimal.ZERO;
        int moveLineNo = 1;
        boolean isDebitToPay = paymentVoucherToolService.isDebitToPay(paymentVoucher);
        for (PayVoucherElementToPay payVoucherElementToPay : this.getPayVoucherElementToPayList(paymentVoucher)) {
            MoveLine moveLineToPay = payVoucherElementToPay.getMoveLine();
            log.debug("PV moveLineToPay debit : {}", moveLineToPay.getDebit());
            log.debug("PV moveLineToPay amountPaid : {}", moveLineToPay.getAmountPaid());
            BigDecimal amountToPay = payVoucherElementToPay.getAmountToPayCurrency();
            if (amountToPay.compareTo(BigDecimal.ZERO) > 0) {
                paidLineTotal = paidLineTotal.add(amountToPay);
                this.payMoveLine(move, moveLineNo++, payerPartner, moveLineToPay, amountToPay, payVoucherElementToPay, isDebitToPay, paymentDate);
            }
        }
        // Create move line for the payment amount
        MoveLine moveLine = null;
        // in the else case we create a classical balance on the bank account of the payment mode
        if (paymentVoucher.getMoveLine() != null) {
            moveLine = moveLineService.createMoveLine(move, paymentVoucher.getPartner(), paymentVoucher.getMoveLine().getAccount(), paymentVoucher.getPaidAmount(), isDebitToPay, paymentDate, moveLineNo++, paymentVoucher.getRef(), null);
            Reconcile reconcile = reconcileService.createReconcile(moveLine, paymentVoucher.getMoveLine(), moveLine.getDebit(), !isDebitToPay);
            if (reconcile != null) {
                reconcileService.confirmReconcile(reconcile, true);
            }
        } else {
            moveLine = moveLineService.createMoveLine(move, payerPartner, paymentModeAccount, paymentVoucher.getPaidAmount(), isDebitToPay, paymentDate, moveLineNo++, paymentVoucher.getRef(), null);
        }
        move.getMoveLineList().add(moveLine);
        // Then Use Excess payment on old invoices / moveLines
        if (paymentVoucher.getPaidAmount().compareTo(paidLineTotal) > 0) {
            BigDecimal remainingPaidAmount = paymentVoucher.getRemainingAmount();
            // TODO rajouter le process d'imputation automatique
            // if(paymentVoucher.getHasAutoInput())  {
            // 
            // List<MoveLine> debitMoveLines =
            // Lists.newArrayList(pas.getDebitLinesToPay(contractLine,
            // paymentVoucher.getPaymentScheduleToPay()));
            // pas.createExcessPaymentWithAmount(debitMoveLines, remainingPaidAmount,
            // move, moveLineNo,
            // paymentVoucher.getPayerPartner(), company, contractLine, null,
            // paymentDate, updateCustomerAccount);
            // }
            // else  {
            Account partnerAccount = Beans.get(AccountCustomerService.class).getPartnerAccount(payerPartner, company, paymentVoucherToolService.isPurchase(paymentVoucher));
            moveLine = moveLineService.createMoveLine(move, paymentVoucher.getPartner(), partnerAccount, remainingPaidAmount, !isDebitToPay, paymentDate, moveLineNo++, paymentVoucher.getRef(), null);
            move.getMoveLineList().add(moveLine);
            if (isDebitToPay) {
                reconcileService.balanceCredit(moveLine);
            }
        }
        moveService.getMoveValidateService().validate(move);
        paymentVoucher.setGeneratedMove(move);
    }
    paymentVoucher.setStatusSelect(PaymentVoucherRepository.STATUS_CONFIRMED);
    deleteUnPaidLines(paymentVoucher);
}
Also used : Account(com.axelor.apps.account.db.Account) Company(com.axelor.apps.base.db.Company) AccountCustomerService(com.axelor.apps.account.service.AccountCustomerService) BankDetails(com.axelor.apps.base.db.BankDetails) Journal(com.axelor.apps.account.db.Journal) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) PayVoucherElementToPay(com.axelor.apps.account.db.PayVoucherElementToPay) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) Partner(com.axelor.apps.base.db.Partner) PaymentMode(com.axelor.apps.account.db.PaymentMode) Reconcile(com.axelor.apps.account.db.Reconcile) Transactional(com.google.inject.persist.Transactional)

Example 7 with PayVoucherElementToPay

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

the class PaymentVoucherConfirmService method toPayWithExcessPayment.

/**
 * If paid by a moveline check if all the lines selected have the same account + company Excess
 * payment Check if allright=true (means companies and accounts in lines are all the same and same
 * as in move line selected for paying
 *
 * @param payVoucherElementToPayList Liste des paiement a réaliser
 * @param creditMoveLine Le trop-perçu
 * @param scheduleToBePaid
 * @return Une échéance doit-elle être payée?
 * @throws AxelorException
 */
public boolean toPayWithExcessPayment(List<PayVoucherElementToPay> payVoucherElementToPayList, MoveLine creditMoveLine, boolean scheduleToBePaid, LocalDate paymentDate) throws AxelorException {
    boolean scheduleToBePaid2 = scheduleToBePaid;
    List<MoveLine> debitMoveLines = new ArrayList<MoveLine>();
    for (PayVoucherElementToPay payVoucherElementToPay : payVoucherElementToPayList) {
        debitMoveLines.add(payVoucherElementToPay.getMoveLine());
    }
    List<MoveLine> creditMoveLines = new ArrayList<MoveLine>();
    creditMoveLines.add(creditMoveLine);
    paymentService.useExcessPaymentOnMoveLines(debitMoveLines, creditMoveLines);
    return scheduleToBePaid2;
}
Also used : PayVoucherElementToPay(com.axelor.apps.account.db.PayVoucherElementToPay) MoveLine(com.axelor.apps.account.db.MoveLine) ArrayList(java.util.ArrayList)

Example 8 with PayVoucherElementToPay

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

the class PaymentVoucherLoadService method createPayVoucherElementToPay.

public PayVoucherElementToPay createPayVoucherElementToPay(PaymentVoucher paymentVoucher, PayVoucherDueElement payVoucherDueElement, int sequence) throws AxelorException {
    BigDecimal amountRemaining = paymentVoucher.getRemainingAmount();
    LocalDate paymentDate = paymentVoucher.getPaymentDate();
    PayVoucherElementToPay payVoucherElementToPay = new PayVoucherElementToPay();
    payVoucherElementToPay.setSequence(sequence);
    payVoucherElementToPay.setMoveLine(payVoucherDueElement.getMoveLine());
    payVoucherElementToPay.setTotalAmount(payVoucherDueElement.getDueAmount());
    payVoucherElementToPay.setRemainingAmount(payVoucherDueElement.getAmountRemaining());
    payVoucherElementToPay.setCurrency(payVoucherDueElement.getCurrency());
    BigDecimal amountRemainingInElementCurrency = currencyService.getAmountCurrencyConvertedAtDate(paymentVoucher.getCurrency(), payVoucherElementToPay.getCurrency(), amountRemaining, paymentDate).setScale(2, RoundingMode.HALF_UP);
    BigDecimal amountImputedInElementCurrency = amountRemainingInElementCurrency.min(payVoucherElementToPay.getRemainingAmount());
    BigDecimal amountImputedInPayVouchCurrency = currencyService.getAmountCurrencyConvertedAtDate(payVoucherElementToPay.getCurrency(), paymentVoucher.getCurrency(), amountImputedInElementCurrency, paymentDate).setScale(2, RoundingMode.HALF_UP);
    payVoucherElementToPay.setAmountToPay(amountImputedInElementCurrency);
    payVoucherElementToPay.setAmountToPayCurrency(amountImputedInPayVouchCurrency);
    payVoucherElementToPay.setRemainingAmountAfterPayment(payVoucherElementToPay.getRemainingAmount().subtract(amountImputedInElementCurrency));
    return payVoucherElementToPay;
}
Also used : PayVoucherElementToPay(com.axelor.apps.account.db.PayVoucherElementToPay) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 9 with PayVoucherElementToPay

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

the class PayVoucherElementToPayController method updateAmountToPayCurrency.

public void updateAmountToPayCurrency(ActionRequest request, ActionResponse response) {
    try {
        PayVoucherElementToPay elementToPayContext = request.getContext().asType(PayVoucherElementToPay.class);
        PayVoucherElementToPay elementToPay = Beans.get(PayVoucherElementToPayRepository.class).find(elementToPayContext.getId());
        Beans.get(PayVoucherElementToPayService.class).updateAmountToPayCurrency(elementToPay);
        response.setReload(true);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : PayVoucherElementToPay(com.axelor.apps.account.db.PayVoucherElementToPay) PayVoucherElementToPayRepository(com.axelor.apps.account.db.repo.PayVoucherElementToPayRepository) PayVoucherElementToPayService(com.axelor.apps.account.service.payment.paymentvoucher.PayVoucherElementToPayService)

Aggregations

PayVoucherElementToPay (com.axelor.apps.account.db.PayVoucherElementToPay)9 ArrayList (java.util.ArrayList)4 MoveLine (com.axelor.apps.account.db.MoveLine)3 Transactional (com.google.inject.persist.Transactional)2 BigDecimal (java.math.BigDecimal)2 LocalDate (java.time.LocalDate)2 Account (com.axelor.apps.account.db.Account)1 Invoice (com.axelor.apps.account.db.Invoice)1 Journal (com.axelor.apps.account.db.Journal)1 Move (com.axelor.apps.account.db.Move)1 PayVoucherDueElement (com.axelor.apps.account.db.PayVoucherDueElement)1 PaymentMode (com.axelor.apps.account.db.PaymentMode)1 PaymentVoucher (com.axelor.apps.account.db.PaymentVoucher)1 Reconcile (com.axelor.apps.account.db.Reconcile)1 PayVoucherElementToPayRepository (com.axelor.apps.account.db.repo.PayVoucherElementToPayRepository)1 AccountCustomerService (com.axelor.apps.account.service.AccountCustomerService)1 PayVoucherElementToPayService (com.axelor.apps.account.service.payment.paymentvoucher.PayVoucherElementToPayService)1 BankDetails (com.axelor.apps.base.db.BankDetails)1 Company (com.axelor.apps.base.db.Company)1 Partner (com.axelor.apps.base.db.Partner)1