Search in sources :

Example 71 with MoveLine

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

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

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

the class PaymentVoucherLoadService method initFromInvoice.

/**
 * Initialize a payment voucher from an invoice.
 *
 * @param paymentVoucher
 * @param invoice
 * @throws AxelorException
 */
public void initFromInvoice(PaymentVoucher paymentVoucher, Invoice invoice) throws AxelorException {
    paymentVoucher.setOperationTypeSelect(invoice.getOperationTypeSelect());
    paymentVoucher.setPartner(invoice.getPartner());
    paymentVoucher.setPaymentMode(invoice.getPaymentMode());
    paymentVoucher.setCurrency(invoice.getCurrency());
    paymentVoucher.clearPayVoucherDueElementList();
    paymentVoucher.clearPayVoucherElementToPayList();
    paymentVoucher.setCompany(invoice.getCompany());
    BankDetails companyBankDetails;
    if (invoice.getCompanyBankDetails() != null) {
        companyBankDetails = invoice.getCompanyBankDetails();
    } else {
        companyBankDetails = Beans.get(BankDetailsService.class).getDefaultCompanyBankDetails(invoice.getCompany(), invoice.getPaymentMode(), invoice.getPartner(), null);
    }
    paymentVoucher.setCompanyBankDetails(companyBankDetails);
    BigDecimal amount = BigDecimal.ZERO;
    List<MoveLine> moveLineList = getMoveLines(paymentVoucher);
    for (MoveLine moveLine : moveLineList) {
        PayVoucherDueElement payVoucherDueElement = createPayVoucherDueElement(moveLine);
        paymentVoucher.addPayVoucherDueElementListItem(payVoucherDueElement);
        if (invoice.equals(payVoucherDueElement.getMoveLine().getMove().getInvoice())) {
            amount = amount.add(payVoucherDueElement.getAmountRemaining());
        }
    }
    paymentVoucher.setPaidAmount(amount);
    paymentVoucher.clearPayVoucherDueElementList();
    for (MoveLine moveLine : moveLineList) {
        paymentVoucher.addPayVoucherDueElementListItem(createPayVoucherDueElement(moveLine));
    }
    if (paymentVoucher.getPayVoucherDueElementList() == null) {
        return;
    }
    int sequence = 0;
    for (Iterator<PayVoucherDueElement> it = paymentVoucher.getPayVoucherDueElementList().iterator(); it.hasNext(); ) {
        PayVoucherDueElement payVoucherDueElement = it.next();
        if (invoice.equals(payVoucherDueElement.getMoveLine().getMove().getInvoice()) && paymentVoucher.getCurrency().equals(payVoucherDueElement.getCurrency())) {
            paymentVoucher.addPayVoucherElementToPayListItem(createPayVoucherElementToPay(paymentVoucher, payVoucherDueElement, ++sequence));
            it.remove();
        }
    }
}
Also used : BankDetails(com.axelor.apps.base.db.BankDetails) MoveLine(com.axelor.apps.account.db.MoveLine) PayVoucherDueElement(com.axelor.apps.account.db.PayVoucherDueElement) BigDecimal(java.math.BigDecimal)

Example 74 with MoveLine

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

the class PaymentVoucherLoadService method assignMaxAmountToReconcile.

/**
 * @param moveLineInvoiceToPay Les lignes de factures récupérées depuis l'échéance
 * @param payVoucherElementToPay La Ligne de saisie paiement
 * @return
 */
public List<MoveLine> assignMaxAmountToReconcile(List<MoveLine> moveLineInvoiceToPay, BigDecimal amountToPay) {
    List<MoveLine> debitMoveLines = new ArrayList<MoveLine>();
    if (moveLineInvoiceToPay != null && moveLineInvoiceToPay.size() != 0) {
        // Récupération du montant imputé sur l'échéance, et assignation de la valeur
        // dans la moveLine (champ temporaire)
        BigDecimal maxAmountToPayRemaining = amountToPay;
        for (MoveLine moveLine : moveLineInvoiceToPay) {
            if (maxAmountToPayRemaining.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal amountPay = maxAmountToPayRemaining.min(moveLine.getAmountRemaining());
                moveLine.setMaxAmountToReconcile(amountPay);
                debitMoveLines.add(moveLine);
                maxAmountToPayRemaining = maxAmountToPayRemaining.subtract(amountPay);
            }
        }
    }
    return debitMoveLines;
}
Also used : MoveLine(com.axelor.apps.account.db.MoveLine) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal)

Example 75 with MoveLine

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

the class MoveController method isHiddenMoveLineListViewer.

public void isHiddenMoveLineListViewer(ActionRequest request, ActionResponse response) {
    Move move = request.getContext().asType(Move.class);
    boolean isHidden = true;
    try {
        if (move.getMoveLineList() != null && move.getStatusSelect() < MoveRepository.STATUS_VALIDATED) {
            for (MoveLine moveLine : move.getMoveLineList()) {
                if (moveLine.getAmountPaid().compareTo(BigDecimal.ZERO) > 0 || moveLine.getReconcileGroup() != null) {
                    isHidden = false;
                }
            }
        }
        response.setAttr("$reconcileTags", "hidden", isHidden);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) AxelorException(com.axelor.exception.AxelorException)

Aggregations

MoveLine (com.axelor.apps.account.db.MoveLine)135 BigDecimal (java.math.BigDecimal)60 Move (com.axelor.apps.account.db.Move)51 Transactional (com.google.inject.persist.Transactional)42 AxelorException (com.axelor.exception.AxelorException)40 Company (com.axelor.apps.base.db.Company)38 ArrayList (java.util.ArrayList)38 Partner (com.axelor.apps.base.db.Partner)33 Account (com.axelor.apps.account.db.Account)28 LocalDate (java.time.LocalDate)27 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)25 Journal (com.axelor.apps.account.db.Journal)22 Reconcile (com.axelor.apps.account.db.Reconcile)22 AccountConfig (com.axelor.apps.account.db.AccountConfig)17 Invoice (com.axelor.apps.account.db.Invoice)15 List (java.util.List)13 TaxPaymentMoveLine (com.axelor.apps.account.db.TaxPaymentMoveLine)8 Tax (com.axelor.apps.account.db.Tax)7 TaxLine (com.axelor.apps.account.db.TaxLine)7 MoveLineRepository (com.axelor.apps.account.db.repo.MoveLineRepository)7