Search in sources :

Example 81 with MoveLine

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

the class MoveValidateService method freezeAccountAndPartnerFieldsOnMoveLines.

/**
 * Method that freeze the account and partner fields on move lines
 *
 * @param move
 */
public void freezeAccountAndPartnerFieldsOnMoveLines(Move move) {
    for (MoveLine moveLine : move.getMoveLineList()) {
        Account account = moveLine.getAccount();
        moveLine.setAccountId(account.getId());
        moveLine.setAccountCode(account.getCode());
        moveLine.setAccountName(account.getName());
        Partner partner = moveLine.getPartner();
        if (partner != null) {
            moveLine.setPartnerId(partner.getId());
            moveLine.setPartnerFullName(partner.getFullName());
            moveLine.setPartnerSeq(partner.getPartnerSeq());
        }
        if (moveLine.getTaxLine() != null) {
            moveLine.setTaxRate(moveLine.getTaxLine().getValue());
            moveLine.setTaxCode(moveLine.getTaxLine().getTax().getCode());
        }
    }
}
Also used : Account(com.axelor.apps.account.db.Account) MoveLine(com.axelor.apps.account.db.MoveLine) Partner(com.axelor.apps.base.db.Partner)

Example 82 with MoveLine

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

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

the class PaymentServiceImpl method createExcessPaymentWithAmount.

/**
 * Il crée des écritures de trop percu avec des montants exacts pour chaque débitMoveLines avec le
 * compte du débitMoveLines. A la fin, si il reste un trop-percu alors créer un trop-perçu
 * classique.
 *
 * @param debitMoveLines Les lignes d'écriture à payer
 * @param remainingPaidAmount Le montant restant à payer
 * @param move Une écriture
 * @param moveLineNo Un numéro de ligne d'écriture
 * @return
 * @throws AxelorException
 */
@Override
public int createExcessPaymentWithAmount(List<MoveLine> debitMoveLines, BigDecimal remainingPaidAmount, Move move, int moveLineNo, Partner partner, Company company, PayVoucherElementToPay payVoucherElementToPay, Account account, LocalDate paymentDate) throws AxelorException {
    log.debug("In createExcessPaymentWithAmount");
    int moveLineNo2 = moveLineNo;
    BigDecimal remainingPaidAmount2 = remainingPaidAmount;
    List<Reconcile> reconcileList = new ArrayList<Reconcile>();
    int i = debitMoveLines.size();
    for (MoveLine debitMoveLine : debitMoveLines) {
        i--;
        BigDecimal amountRemaining = debitMoveLine.getAmountRemaining();
        // Afin de pouvoir arrêter si il n'y a plus rien pour payer
        if (remainingPaidAmount2.compareTo(BigDecimal.ZERO) <= 0) {
            break;
        }
        BigDecimal amountToPay = remainingPaidAmount2.min(amountRemaining);
        String invoiceName = "";
        if (debitMoveLine.getMove().getInvoice() != null) {
            invoiceName = debitMoveLine.getMove().getInvoice().getInvoiceId();
        } else {
            invoiceName = payVoucherElementToPay.getPaymentVoucher().getRef();
        }
        MoveLine creditMoveLine = moveLineService.createMoveLine(move, debitMoveLine.getPartner(), debitMoveLine.getAccount(), amountToPay, false, appAccountService.getTodayDate(company), moveLineNo2, invoiceName, null);
        move.getMoveLineList().add(creditMoveLine);
        // Utiliser uniquement dans le cas du paiemnt des échéances lors d'une saisie paiement
        if (payVoucherElementToPay != null) {
            creditMoveLine.setPaymentScheduleLine(payVoucherElementToPay.getMoveLine().getPaymentScheduleLine());
            payVoucherElementToPay.setMoveLineGenerated(creditMoveLine);
        }
        moveLineNo2++;
        Reconcile reconcile = null;
        // Gestion du passage en 580
        if (i == 0) {
            log.debug("last loop");
            reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amountToPay, true);
        } else {
            reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amountToPay, false);
        }
        if (reconcile != null) {
            reconcileList.add(reconcile);
            remainingPaidAmount2 = remainingPaidAmount2.subtract(amountRemaining);
        }
    }
    for (Reconcile reconcile : reconcileList) {
        reconcileService.confirmReconcile(reconcile, true);
    }
    // Si il y a un restant à payer, alors on crée un trop-perçu.
    if (remainingPaidAmount2.compareTo(BigDecimal.ZERO) > 0) {
        MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, remainingPaidAmount2, false, appAccountService.getTodayDate(company), moveLineNo2, null, null);
        move.getMoveLineList().add(moveLine);
        moveLineNo2++;
        // Gestion du passage en 580
        reconcileService.balanceCredit(moveLine);
    }
    log.debug("End createExcessPaymentWithAmount");
    return moveLineNo2;
}
Also used : ArrayList(java.util.ArrayList) MoveLine(com.axelor.apps.account.db.MoveLine) BigDecimal(java.math.BigDecimal) Reconcile(com.axelor.apps.account.db.Reconcile)

Example 84 with MoveLine

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

the class PaymentServiceImpl method useExcessPaymentOnMoveLines.

/**
 * Use excess payment between a list of debit move lines and a list of credit move lines. The
 * lists needs to be ordered by date in order to pay the invoices chronologically.
 *
 * @param debitMoveLines
 * @param creditMoveLines
 * @param dontThrow
 * @throws AxelorException
 */
protected void useExcessPaymentOnMoveLines(List<MoveLine> debitMoveLines, List<MoveLine> creditMoveLines, boolean dontThrow) throws AxelorException {
    if (debitMoveLines != null && creditMoveLines != null) {
        log.debug("Emploie du trop perçu (nombre de lignes en débit : {}, nombre de ligne en crédit : {})", new Object[] { debitMoveLines.size(), creditMoveLines.size() });
        BigDecimal debitTotalRemaining = BigDecimal.ZERO;
        BigDecimal creditTotalRemaining = BigDecimal.ZERO;
        for (MoveLine creditMoveLine : creditMoveLines) {
            log.debug("Emploie du trop perçu : ligne en crédit : {})", creditMoveLine);
            log.debug("Emploie du trop perçu : ligne en crédit (restant à payer): {})", creditMoveLine.getAmountRemaining());
            creditTotalRemaining = creditTotalRemaining.add(creditMoveLine.getAmountRemaining());
        }
        for (MoveLine debitMoveLine : debitMoveLines) {
            log.debug("Emploie du trop perçu : ligne en débit : {})", debitMoveLine);
            log.debug("Emploie du trop perçu : ligne en débit (restant à payer): {})", debitMoveLine.getAmountRemaining());
            debitTotalRemaining = debitTotalRemaining.add(debitMoveLine.getAmountRemaining());
        }
        for (MoveLine creditMoveLine : creditMoveLines) {
            if (creditMoveLine.getAmountRemaining().compareTo(BigDecimal.ZERO) == 1) {
                for (MoveLine debitMoveLine : debitMoveLines) {
                    if ((debitMoveLine.getAmountRemaining().compareTo(BigDecimal.ZERO) == 1) && (creditMoveLine.getAmountRemaining().compareTo(BigDecimal.ZERO) == 1)) {
                        try {
                            createReconcile(debitMoveLine, creditMoveLine, debitTotalRemaining, creditTotalRemaining);
                        } catch (Exception e) {
                            if (dontThrow) {
                                TraceBackService.trace(e);
                                log.debug(e.getMessage());
                            } else {
                                throw e;
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : MoveLine(com.axelor.apps.account.db.MoveLine) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException)

Example 85 with MoveLine

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

the class InvoicePaymentValidateServiceImpl method createMoveForInvoicePayment.

/**
 * Method to create a payment move for an invoice Payment
 *
 * <p>Create a move and reconcile it with the invoice move
 *
 * @param invoicePayment An invoice payment
 * @throws AxelorException
 */
@Transactional(rollbackOn = { Exception.class })
public InvoicePayment createMoveForInvoicePayment(InvoicePayment invoicePayment) throws AxelorException {
    Invoice invoice = invoicePayment.getInvoice();
    Company company = invoice.getCompany();
    PaymentMode paymentMode = invoicePayment.getPaymentMode();
    Partner partner = invoice.getPartner();
    LocalDate paymentDate = invoicePayment.getPaymentDate();
    BigDecimal paymentAmount = invoicePayment.getAmount();
    BankDetails companyBankDetails = invoicePayment.getCompanyBankDetails();
    Account customerAccount;
    Journal journal = paymentModeService.getPaymentModeJournal(paymentMode, company, companyBankDetails);
    boolean isDebitInvoice = moveService.getMoveToolService().isDebitCustomer(invoice, true);
    MoveLine invoiceMoveLine = moveService.getMoveToolService().getInvoiceCustomerMoveLineByLoop(invoice);
    if (invoice.getOperationSubTypeSelect() == InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE) {
        AccountConfig accountConfig = accountConfigService.getAccountConfig(company);
        customerAccount = accountConfigService.getAdvancePaymentAccount(accountConfig);
    } else {
        if (invoiceMoveLine == null) {
            return null;
        }
        customerAccount = invoiceMoveLine.getAccount();
    }
    String origin = invoicePayment.getInvoice().getInvoiceId();
    if (invoicePayment.getPaymentMode().getTypeSelect() == PaymentModeRepository.TYPE_CHEQUE || invoicePayment.getPaymentMode().getTypeSelect() == PaymentModeRepository.TYPE_IPO_CHEQUE) {
        origin = invoicePayment.getChequeNumber() != null ? invoicePayment.getChequeNumber() : origin;
    } else if (invoicePayment.getPaymentMode().getTypeSelect() == PaymentModeRepository.TYPE_BANK_CARD) {
        origin = invoicePayment.getInvoicePaymentRef() != null ? invoicePayment.getInvoicePaymentRef() : origin;
    }
    if (invoicePayment.getInvoice().getOperationTypeSelect() == InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE || invoicePayment.getInvoice().getOperationTypeSelect() == InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND) {
        origin = invoicePayment.getInvoice().getSupplierInvoiceNb();
    }
    Move move = moveService.getMoveCreateService().createMove(journal, company, invoicePayment.getCurrency(), partner, paymentDate, paymentMode, MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_PAYMENT);
    move.setTradingName(invoice.getTradingName());
    move.addMoveLineListItem(moveLineService.createMoveLine(move, partner, paymentModeService.getPaymentModeAccount(paymentMode, company, companyBankDetails), paymentAmount, isDebitInvoice, paymentDate, null, 1, origin, invoicePayment.getDescription()));
    MoveLine customerMoveLine = moveLineService.createMoveLine(move, partner, customerAccount, paymentAmount, !isDebitInvoice, paymentDate, null, 2, origin, invoicePayment.getDescription());
    move.addMoveLineListItem(customerMoveLine);
    moveService.getMoveValidateService().validate(move);
    if (invoice.getOperationSubTypeSelect() != InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE) {
        Reconcile reconcile = reconcileService.reconcile(invoiceMoveLine, customerMoveLine, true, false);
        invoicePayment.setReconcile(reconcile);
    }
    invoicePayment.setMove(move);
    invoicePaymentRepository.save(invoicePayment);
    return invoicePayment;
}
Also used : Account(com.axelor.apps.account.db.Account) Company(com.axelor.apps.base.db.Company) Invoice(com.axelor.apps.account.db.Invoice) BankDetails(com.axelor.apps.base.db.BankDetails) Journal(com.axelor.apps.account.db.Journal) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AccountConfig(com.axelor.apps.account.db.AccountConfig) 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)

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