Search in sources :

Example 11 with Reconcile

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

the class PaymentVoucherConfirmService method payMoveLine.

/**
 * @param paymentMove
 * @param moveLineSeq
 * @param payerPartner
 * @param moveLineToPay
 * @param amountToPay
 * @param payVoucherElementToPay
 * @return
 * @throws AxelorException
 */
public MoveLine payMoveLine(Move paymentMove, int moveLineSeq, Partner payerPartner, MoveLine moveLineToPay, BigDecimal amountToPay, PayVoucherElementToPay payVoucherElementToPay, boolean isDebitToPay, LocalDate paymentDate) throws AxelorException {
    String invoiceName = "";
    if (moveLineToPay.getMove().getInvoice() != null) {
        invoiceName = moveLineToPay.getMove().getInvoice().getInvoiceId();
    } else {
        invoiceName = payVoucherElementToPay.getPaymentVoucher().getRef();
    }
    MoveLine moveLine = moveLineService.createMoveLine(paymentMove, payerPartner, moveLineToPay.getAccount(), amountToPay, !isDebitToPay, paymentDate, moveLineSeq, invoiceName, null);
    paymentMove.addMoveLineListItem(moveLine);
    payVoucherElementToPay.setMoveLineGenerated(moveLine);
    BigDecimal amountInCompanyCurrency = moveLine.getDebit().add(moveLine.getCredit());
    Reconcile reconcile = reconcileService.createReconcile(moveLineToPay, moveLine, amountInCompanyCurrency, true);
    if (reconcile != null) {
        log.debug("Reconcile : : : {}", reconcile);
        reconcileService.confirmReconcile(reconcile, true);
    }
    return moveLine;
}
Also used : MoveLine(com.axelor.apps.account.db.MoveLine) BigDecimal(java.math.BigDecimal) Reconcile(com.axelor.apps.account.db.Reconcile)

Example 12 with Reconcile

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

the class BatchBankPaymentServiceImpl method createBankOrders.

@Transactional(rollbackOn = { Exception.class })
protected void createBankOrders(Batch batch, Collection<PaymentScheduleLine> paymentScheduleLines) throws AxelorException, JAXBException, IOException, DatatypeConfigurationException {
    for (PaymentScheduleLine paymentScheduleLine : paymentScheduleLines) {
        PaymentSchedule paymentSchedule = paymentScheduleLine.getPaymentSchedule();
        MoveLine creditMoveLine = paymentScheduleLine.getAdvanceMoveLine();
        for (Invoice invoice : paymentSchedule.getInvoiceSet()) {
            MoveLine debitMoveLine = moveService.getMoveLineService().getDebitCustomerMoveLine(invoice);
            Reconcile reconcile = reconcileRepo.findByMoveLines(debitMoveLine, creditMoveLine);
            if (reconcile == null) {
                continue;
            }
            createBankOrders(batch, reconcile);
        }
    }
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) PaymentScheduleLine(com.axelor.apps.account.db.PaymentScheduleLine) PaymentSchedule(com.axelor.apps.account.db.PaymentSchedule) MoveLine(com.axelor.apps.account.db.MoveLine) Reconcile(com.axelor.apps.account.db.Reconcile) Transactional(com.google.inject.persist.Transactional)

Example 13 with Reconcile

use of com.axelor.apps.account.db.Reconcile 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 14 with Reconcile

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

the class PaymentServiceImpl method useExcessPaymentWithAmountConsolidated.

@SuppressWarnings("unchecked")
@Override
public int useExcessPaymentWithAmountConsolidated(List<MoveLine> creditMoveLines, BigDecimal remainingPaidAmount, Move move, int moveLineNo, Partner partner, Company company, Account account, LocalDate date, LocalDate dueDate) throws AxelorException {
    log.debug("In useExcessPaymentWithAmount");
    int moveLineNo2 = moveLineNo;
    BigDecimal remainingPaidAmount2 = remainingPaidAmount;
    List<Reconcile> reconcileList = new ArrayList<Reconcile>();
    int i = creditMoveLines.size();
    if (i != 0) {
        Query q = JPA.em().createQuery("select new map(ml.account, SUM(ml.amountRemaining)) FROM MoveLine as ml " + "WHERE ml in ?1 group by ml.account");
        q.setParameter(1, creditMoveLines);
        List<Map<Account, BigDecimal>> allMap = new ArrayList<Map<Account, BigDecimal>>();
        allMap = q.getResultList();
        for (Map<Account, BigDecimal> map : allMap) {
            Account accountMap = (Account) map.values().toArray()[0];
            BigDecimal amountMap = (BigDecimal) map.values().toArray()[1];
            BigDecimal amountDebit = amountMap.min(remainingPaidAmount2);
            if (amountDebit.compareTo(BigDecimal.ZERO) > 0) {
                MoveLine debitMoveLine = moveLineService.createMoveLine(move, partner, accountMap, amountDebit, true, date, dueDate, moveLineNo2, null, null);
                move.getMoveLineList().add(debitMoveLine);
                moveLineNo2++;
                for (MoveLine creditMoveLine : creditMoveLines) {
                    if (creditMoveLine.getAccount().equals(accountMap)) {
                        Reconcile reconcile = null;
                        i--;
                        // Afin de pouvoir arrêter si il n'y a plus rien à payer
                        if (amountDebit.compareTo(BigDecimal.ZERO) <= 0) {
                            break;
                        }
                        BigDecimal amountToPay = amountDebit.min(creditMoveLine.getAmountRemaining());
                        // Gestion du passage en 580
                        if (i == 0) {
                            reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amountToPay, true);
                        } else {
                            reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amountToPay, false);
                        }
                        if (reconcile != null) {
                            remainingPaidAmount2 = remainingPaidAmount2.subtract(amountToPay);
                            amountDebit = amountDebit.subtract(amountToPay);
                            reconcileList.add(reconcile);
                        }
                    }
                }
            }
        }
        for (Reconcile reconcile : reconcileList) {
            reconcileService.confirmReconcile(reconcile, true);
        }
    }
    // Si il y a un restant à payer, alors on crée un dû.
    if (remainingPaidAmount2.compareTo(BigDecimal.ZERO) > 0) {
        MoveLine debitmoveLine = moveLineService.createMoveLine(move, partner, account, remainingPaidAmount2, true, date, dueDate, moveLineNo2, null, null);
        move.getMoveLineList().add(debitmoveLine);
        moveLineNo2++;
    }
    log.debug("End useExcessPaymentWithAmount");
    return moveLineNo2;
}
Also used : Account(com.axelor.apps.account.db.Account) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) MoveLine(com.axelor.apps.account.db.MoveLine) Map(java.util.Map) BigDecimal(java.math.BigDecimal) Reconcile(com.axelor.apps.account.db.Reconcile)

Example 15 with Reconcile

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

the class PaymentServiceImpl method createReconcile.

/**
 * Private method called by useExcessPaymentOnMoveLines used to lighten it and to create a
 * reconcile
 *
 * @param debitMoveLine
 * @param creditMoveLine
 * @param debitTotalRemaining
 * @param creditTotalRemaining
 * @throws AxelorException
 */
@Transactional
private void createReconcile(MoveLine debitMoveLine, MoveLine creditMoveLine, BigDecimal debitTotalRemaining, BigDecimal creditTotalRemaining) throws AxelorException {
    BigDecimal amount;
    Reconcile reconcile;
    if (debitMoveLine.getMaxAmountToReconcile() != null && debitMoveLine.getMaxAmountToReconcile().compareTo(BigDecimal.ZERO) > 0) {
        amount = debitMoveLine.getMaxAmountToReconcile().min(creditMoveLine.getAmountRemaining());
        debitMoveLine.setMaxAmountToReconcile(null);
    } else {
        amount = creditMoveLine.getAmountRemaining().min(debitMoveLine.getAmountRemaining());
    }
    log.debug("amount : {}", amount);
    log.debug("debitTotalRemaining : {}", debitTotalRemaining);
    log.debug("creditTotalRemaining : {}", creditTotalRemaining);
    BigDecimal nextDebitTotalRemaining = debitTotalRemaining.subtract(amount);
    BigDecimal nextCreditTotalRemaining = creditTotalRemaining.subtract(amount);
    // Gestion du passage en 580
    if (nextDebitTotalRemaining.compareTo(BigDecimal.ZERO) <= 0 || nextCreditTotalRemaining.compareTo(BigDecimal.ZERO) <= 0) {
        log.debug("last loop");
        reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amount, true);
    } else {
        reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amount, false);
    }
    if (reconcile != null) {
        reconcileService.confirmReconcile(reconcile, true);
        debitTotalRemaining = debitTotalRemaining.subtract(amount);
        creditTotalRemaining = creditTotalRemaining.subtract(amount);
        log.debug("Réconciliation : {}", reconcile);
    }
}
Also used : BigDecimal(java.math.BigDecimal) Reconcile(com.axelor.apps.account.db.Reconcile) Transactional(com.google.inject.persist.Transactional)

Aggregations

Reconcile (com.axelor.apps.account.db.Reconcile)30 MoveLine (com.axelor.apps.account.db.MoveLine)21 BigDecimal (java.math.BigDecimal)18 Transactional (com.google.inject.persist.Transactional)14 Move (com.axelor.apps.account.db.Move)13 Company (com.axelor.apps.base.db.Company)11 Partner (com.axelor.apps.base.db.Partner)10 Account (com.axelor.apps.account.db.Account)7 AccountConfig (com.axelor.apps.account.db.AccountConfig)7 Invoice (com.axelor.apps.account.db.Invoice)4 Journal (com.axelor.apps.account.db.Journal)4 LocalDate (java.time.LocalDate)4 ArrayList (java.util.ArrayList)4 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)3 ReconcileGroup (com.axelor.apps.account.db.ReconcileGroup)3 ReconcileRepository (com.axelor.apps.account.db.repo.ReconcileRepository)3 InvoiceLineTax (com.axelor.apps.account.db.InvoiceLineTax)2 InvoicePayment (com.axelor.apps.account.db.InvoicePayment)2 PaymentMode (com.axelor.apps.account.db.PaymentMode)2 PaymentSchedule (com.axelor.apps.account.db.PaymentSchedule)2