Search in sources :

Example 41 with MoveLine

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

the class MoveLineServiceImpl method populateMoveLineMap.

private void populateMoveLineMap(Map<List<Object>, Pair<List<MoveLine>, List<MoveLine>>> moveLineMap, List<MoveLine> reconciliableMoveLineList, boolean isCredit) {
    for (MoveLine moveLine : reconciliableMoveLineList) {
        Move move = moveLine.getMove();
        List<Object> keys = new ArrayList<Object>();
        keys.add(move.getCompany());
        keys.add(moveLine.getAccount());
        keys.add(moveLine.getPartner());
        Pair<List<MoveLine>, List<MoveLine>> moveLineLists = moveLineMap.get(keys);
        if (moveLineLists == null) {
            moveLineLists = Pair.of(new ArrayList<>(), new ArrayList<>());
            moveLineMap.put(keys, moveLineLists);
        }
        List<MoveLine> moveLineList = isCredit ? moveLineLists.getLeft() : moveLineLists.getRight();
        moveLineList.add(moveLine);
    }
}
Also used : Move(com.axelor.apps.account.db.Move) TaxPaymentMoveLine(com.axelor.apps.account.db.TaxPaymentMoveLine) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 42 with MoveLine

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

the class MoveLineServiceImpl method createMoveLines.

/**
 * Créer les lignes d'écritures comptables d'une facture.
 *
 * @param invoice
 * @param move
 * @param consolidate
 * @return
 */
@Override
public List<MoveLine> createMoveLines(Invoice invoice, Move move, Company company, Partner partner, Account partnerAccount, boolean consolidate, boolean isPurchase, boolean isDebitCustomer) throws AxelorException {
    log.debug("Création des lignes d'écriture comptable de la facture/l'avoir {}", invoice.getInvoiceId());
    List<MoveLine> moveLines = new ArrayList<MoveLine>();
    Set<AnalyticAccount> analyticAccounts = new HashSet<AnalyticAccount>();
    int moveLineId = 1;
    if (partner == null) {
        throw new AxelorException(invoice, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.MOVE_LINE_1), invoice.getInvoiceId());
    }
    if (partnerAccount == null) {
        throw new AxelorException(invoice, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.MOVE_LINE_2), invoice.getInvoiceId());
    }
    String origin = invoice.getInvoiceId();
    if (InvoiceToolService.isPurchase(invoice)) {
        origin = invoice.getSupplierInvoiceNb();
    }
    // Creation of partner move line
    MoveLine moveLine1 = this.createMoveLine(move, partner, partnerAccount, invoice.getInTaxTotal(), invoice.getCompanyInTaxTotal(), null, isDebitCustomer, invoice.getInvoiceDate(), invoice.getDueDate(), invoice.getOriginDate(), moveLineId++, origin, null);
    moveLines.add(moveLine1);
    AnalyticMoveLineRepository analyticMoveLineRepository = Beans.get(AnalyticMoveLineRepository.class);
    // Creation of product move lines for each invoice line
    for (InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
        BigDecimal companyExTaxTotal = invoiceLine.getCompanyExTaxTotal();
        if (companyExTaxTotal.compareTo(BigDecimal.ZERO) != 0) {
            analyticAccounts.clear();
            Account account = invoiceLine.getAccount();
            if (account == null) {
                throw new AxelorException(move, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MOVE_LINE_4), invoiceLine.getName(), company.getName());
            }
            companyExTaxTotal = invoiceLine.getCompanyExTaxTotal();
            log.debug("Traitement de la ligne de facture : compte comptable = {}, montant = {}", new Object[] { account.getName(), companyExTaxTotal });
            if (invoiceLine.getAnalyticDistributionTemplate() == null && (invoiceLine.getAnalyticMoveLineList() == null || invoiceLine.getAnalyticMoveLineList().isEmpty()) && account.getAnalyticDistributionAuthorized() && account.getAnalyticDistributionRequiredOnInvoiceLines()) {
                throw new AxelorException(move, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.ANALYTIC_DISTRIBUTION_MISSING), invoiceLine.getName(), company.getName());
            }
            MoveLine moveLine = this.createMoveLine(move, partner, account, invoiceLine.getExTaxTotal(), companyExTaxTotal, null, !isDebitCustomer, invoice.getInvoiceDate(), null, invoice.getOriginDate(), moveLineId++, origin, invoiceLine.getProductName());
            moveLine.setAnalyticDistributionTemplate(invoiceLine.getAnalyticDistributionTemplate());
            if (invoiceLine.getAnalyticMoveLineList() != null && !invoiceLine.getAnalyticMoveLineList().isEmpty()) {
                for (AnalyticMoveLine invoiceAnalyticMoveLine : invoiceLine.getAnalyticMoveLineList()) {
                    AnalyticMoveLine analyticMoveLine = analyticMoveLineRepository.copy(invoiceAnalyticMoveLine, false);
                    analyticMoveLine.setTypeSelect(AnalyticMoveLineRepository.STATUS_REAL_ACCOUNTING);
                    analyticMoveLine.setInvoiceLine(null);
                    analyticMoveLine.setAccount(moveLine.getAccount());
                    analyticMoveLine.setAccountType(moveLine.getAccount().getAccountType());
                    analyticMoveLineService.updateAnalyticMoveLine(analyticMoveLine, moveLine.getDebit().add(moveLine.getCredit()), moveLine.getDate());
                    moveLine.addAnalyticMoveLineListItem(analyticMoveLine);
                }
            } else {
                generateAnalyticMoveLines(moveLine);
            }
            TaxLine taxLine = invoiceLine.getTaxLine();
            if (taxLine != null) {
                moveLine.setTaxLine(taxLine);
                moveLine.setTaxRate(taxLine.getValue());
                moveLine.setTaxCode(taxLine.getTax().getCode());
            }
            moveLines.add(moveLine);
        }
    }
    // Creation of tax move lines for each invoice line tax
    for (InvoiceLineTax invoiceLineTax : invoice.getInvoiceLineTaxList()) {
        BigDecimal companyTaxTotal = invoiceLineTax.getCompanyTaxTotal();
        if (companyTaxTotal.compareTo(BigDecimal.ZERO) != 0) {
            Tax tax = invoiceLineTax.getTaxLine().getTax();
            boolean hasFixedAssets = !invoiceLineTax.getSubTotalOfFixedAssets().equals(BigDecimal.ZERO);
            boolean hasOtherAssets = !invoiceLineTax.getSubTotalExcludingFixedAssets().equals(BigDecimal.ZERO);
            Account account;
            MoveLine moveLine;
            if (hasFixedAssets && invoiceLineTax.getCompanySubTotalOfFixedAssets().compareTo(BigDecimal.ZERO) != 0) {
                account = taxAccountService.getAccount(tax, company, isPurchase, true);
                if (account == null) {
                    throw new AxelorException(move, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MOVE_LINE_6), tax.getName(), company.getName());
                }
                moveLine = this.createMoveLine(move, partner, account, invoiceLineTax.getSubTotalOfFixedAssets(), invoiceLineTax.getCompanySubTotalOfFixedAssets(), null, !isDebitCustomer, invoice.getInvoiceDate(), null, invoice.getOriginDate(), moveLineId++, origin, null);
                moveLine.setTaxLine(invoiceLineTax.getTaxLine());
                moveLine.setTaxRate(invoiceLineTax.getTaxLine().getValue());
                moveLine.setTaxCode(tax.getCode());
                moveLines.add(moveLine);
            }
            if (hasOtherAssets && invoiceLineTax.getCompanySubTotalExcludingFixedAssets().compareTo(BigDecimal.ZERO) != 0) {
                account = taxAccountService.getAccount(tax, company, isPurchase, false);
                if (account == null) {
                    throw new AxelorException(move, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MOVE_LINE_6), tax.getName(), company.getName());
                }
                moveLine = this.createMoveLine(move, partner, account, invoiceLineTax.getSubTotalExcludingFixedAssets(), invoiceLineTax.getCompanySubTotalExcludingFixedAssets(), null, !isDebitCustomer, invoice.getInvoiceDate(), null, invoice.getOriginDate(), moveLineId++, origin, null);
                moveLine.setTaxLine(invoiceLineTax.getTaxLine());
                moveLine.setTaxRate(invoiceLineTax.getTaxLine().getValue());
                moveLine.setTaxCode(tax.getCode());
                moveLines.add(moveLine);
            }
        }
    }
    if (consolidate) {
        this.consolidateMoveLines(moveLines);
    }
    return moveLines;
}
Also used : AxelorException(com.axelor.exception.AxelorException) AnalyticAccount(com.axelor.apps.account.db.AnalyticAccount) Account(com.axelor.apps.account.db.Account) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) ArrayList(java.util.ArrayList) InvoiceLineTax(com.axelor.apps.account.db.InvoiceLineTax) Tax(com.axelor.apps.account.db.Tax) AnalyticAccount(com.axelor.apps.account.db.AnalyticAccount) InvoiceLineTax(com.axelor.apps.account.db.InvoiceLineTax) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine) AnalyticMoveLineRepository(com.axelor.apps.account.db.repo.AnalyticMoveLineRepository) TaxPaymentMoveLine(com.axelor.apps.account.db.TaxPaymentMoveLine) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) HashSet(java.util.HashSet) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

Example 43 with MoveLine

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

the class MoveLineServiceImpl method reconcileMoveLinesWithCacheManagement.

/**
 * Method used to reconcile the move line list passed as a parameter
 *
 * @param moveLineList
 */
@Override
public void reconcileMoveLinesWithCacheManagement(List<MoveLine> moveLineList) {
    List<MoveLine> reconciliableCreditMoveLineList = getReconciliableCreditMoveLines(moveLineList);
    List<MoveLine> reconciliableDebitMoveLineList = getReconciliableDebitMoveLines(moveLineList);
    Map<List<Object>, Pair<List<MoveLine>, List<MoveLine>>> moveLineMap = new HashMap<>();
    populateCredit(moveLineMap, reconciliableCreditMoveLineList);
    populateDebit(moveLineMap, reconciliableDebitMoveLineList);
    Comparator<MoveLine> byDate = Comparator.comparing(MoveLine::getDate);
    PaymentService paymentService = Beans.get(PaymentService.class);
    for (Pair<List<MoveLine>, List<MoveLine>> moveLineLists : moveLineMap.values()) {
        try {
            moveLineLists = this.findMoveLineLists(moveLineLists);
            this.useExcessPaymentOnMoveLinesDontThrow(byDate, paymentService, moveLineLists);
        } catch (Exception e) {
            TraceBackService.trace(e);
            log.debug(e.getMessage());
        } finally {
            JPA.clear();
        }
    }
}
Also used : HashMap(java.util.HashMap) TaxPaymentMoveLine(com.axelor.apps.account.db.TaxPaymentMoveLine) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) List(java.util.List) ArrayList(java.util.ArrayList) PaymentService(com.axelor.apps.account.service.payment.PaymentService) AxelorException(com.axelor.exception.AxelorException) Pair(org.apache.commons.lang3.tuple.Pair)

Example 44 with MoveLine

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

the class MoveLineServiceImpl method reconcileMoveLines.

@Override
@Transactional
public void reconcileMoveLines(List<MoveLine> moveLineList) {
    List<MoveLine> reconciliableCreditMoveLineList = getReconciliableCreditMoveLines(moveLineList);
    List<MoveLine> reconciliableDebitMoveLineList = getReconciliableDebitMoveLines(moveLineList);
    Map<List<Object>, Pair<List<MoveLine>, List<MoveLine>>> moveLineMap = new HashMap<>();
    populateCredit(moveLineMap, reconciliableCreditMoveLineList);
    populateDebit(moveLineMap, reconciliableDebitMoveLineList);
    Comparator<MoveLine> byDate = Comparator.comparing(MoveLine::getDate);
    PaymentService paymentService = Beans.get(PaymentService.class);
    for (Pair<List<MoveLine>, List<MoveLine>> moveLineLists : moveLineMap.values()) {
        List<MoveLine> companyPartnerCreditMoveLineList = moveLineLists.getLeft();
        List<MoveLine> companyPartnerDebitMoveLineList = moveLineLists.getRight();
        companyPartnerCreditMoveLineList.sort(byDate);
        companyPartnerDebitMoveLineList.sort(byDate);
        paymentService.useExcessPaymentOnMoveLinesDontThrow(companyPartnerDebitMoveLineList, companyPartnerCreditMoveLineList);
    }
}
Also used : HashMap(java.util.HashMap) TaxPaymentMoveLine(com.axelor.apps.account.db.TaxPaymentMoveLine) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) List(java.util.List) ArrayList(java.util.ArrayList) PaymentService(com.axelor.apps.account.service.payment.PaymentService) Pair(org.apache.commons.lang3.tuple.Pair) Transactional(com.google.inject.persist.Transactional)

Example 45 with MoveLine

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

the class MoveLineServiceImpl method autoTaxLineGenerate.

@Override
public void autoTaxLineGenerate(Move move) throws AxelorException {
    List<MoveLine> moveLineList = move.getMoveLineList();
    moveLineList.sort(new Comparator<MoveLine>() {

        @Override
        public int compare(MoveLine o1, MoveLine o2) {
            if (o2.getSourceTaxLine() != null) {
                return 0;
            }
            return -1;
        }
    });
    Iterator<MoveLine> moveLineItr = moveLineList.iterator();
    Map<String, MoveLine> map = new HashMap<>();
    Map<String, MoveLine> newMap = new HashMap<>();
    while (moveLineItr.hasNext()) {
        MoveLine moveLine = moveLineItr.next();
        TaxLine taxLine = moveLine.getTaxLine();
        TaxLine sourceTaxLine = moveLine.getSourceTaxLine();
        if (sourceTaxLine != null) {
            String sourceTaxLineKey = moveLine.getAccount().getCode() + sourceTaxLine.getId();
            moveLine.setCredit(BigDecimal.ZERO);
            moveLine.setDebit(BigDecimal.ZERO);
            map.put(sourceTaxLineKey, moveLine);
            moveLineItr.remove();
            continue;
        }
        if (taxLine != null) {
            String accountType = moveLine.getAccount().getAccountType().getTechnicalTypeSelect();
            if (accountType.equals(AccountTypeRepository.TYPE_DEBT) || accountType.equals(AccountTypeRepository.TYPE_CHARGE) || accountType.equals(AccountTypeRepository.TYPE_INCOME) || accountType.equals(AccountTypeRepository.TYPE_ASSET)) {
                BigDecimal debit = moveLine.getDebit();
                BigDecimal credit = moveLine.getCredit();
                LocalDate date = moveLine.getDate();
                Company company = move.getCompany();
                MoveLine newOrUpdatedMoveLine = new MoveLine();
                if (accountType.equals(AccountTypeRepository.TYPE_DEBT) || accountType.equals(AccountTypeRepository.TYPE_CHARGE)) {
                    newOrUpdatedMoveLine.setAccount(taxAccountService.getAccount(taxLine.getTax(), company, true, false));
                } else if (accountType.equals(AccountTypeRepository.TYPE_INCOME)) {
                    newOrUpdatedMoveLine.setAccount(taxAccountService.getAccount(taxLine.getTax(), company, false, false));
                } else if (accountType.equals(AccountTypeRepository.TYPE_ASSET)) {
                    newOrUpdatedMoveLine.setAccount(taxAccountService.getAccount(taxLine.getTax(), company, true, true));
                }
                Account newAccount = newOrUpdatedMoveLine.getAccount();
                if (newAccount == null) {
                    throw new AxelorException(move, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MOVE_LINE_6), taxLine.getName(), company.getName());
                }
                String newSourceTaxLineKey = newAccount.getCode() + taxLine.getId();
                if (!map.containsKey(newSourceTaxLineKey) && !newMap.containsKey(newSourceTaxLineKey)) {
                    newOrUpdatedMoveLine = this.createNewMoveLine(debit, credit, date, accountType, taxLine, newOrUpdatedMoveLine);
                } else {
                    if (newMap.containsKey(newSourceTaxLineKey)) {
                        newOrUpdatedMoveLine = newMap.get(newSourceTaxLineKey);
                    } else if (!newMap.containsKey(newSourceTaxLineKey) && map.containsKey(newSourceTaxLineKey)) {
                        newOrUpdatedMoveLine = map.get(newSourceTaxLineKey);
                    }
                    newOrUpdatedMoveLine.setDebit(newOrUpdatedMoveLine.getDebit().add(debit.multiply(taxLine.getValue())));
                    newOrUpdatedMoveLine.setCredit(newOrUpdatedMoveLine.getCredit().add(credit.multiply(taxLine.getValue())));
                }
                newMap.put(newSourceTaxLineKey, newOrUpdatedMoveLine);
            }
        }
    }
    moveLineList.addAll(newMap.values());
}
Also used : AnalyticAccount(com.axelor.apps.account.db.AnalyticAccount) Account(com.axelor.apps.account.db.Account) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) HashMap(java.util.HashMap) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine) TaxPaymentMoveLine(com.axelor.apps.account.db.TaxPaymentMoveLine) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

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