Search in sources :

Example 36 with MoveLine

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

the class ExpenseServiceImpl method createAndSetMove.

protected Move createAndSetMove(Expense expense) throws AxelorException {
    LocalDate moveDate = expense.getMoveDate();
    if (moveDate == null) {
        moveDate = appAccountService.getTodayDate(expense.getCompany());
        expense.setMoveDate(moveDate);
    }
    Company company = expense.getCompany();
    Partner partner = expense.getUser().getPartner();
    Account account = null;
    AccountConfig accountConfig = accountConfigService.getAccountConfig(company);
    if (partner == null) {
        throw new AxelorException(expense, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(com.axelor.apps.account.exception.IExceptionMessage.USER_PARTNER), expense.getUser().getName());
    }
    Move move = moveService.getMoveCreateService().createMove(accountConfigService.getExpenseJournal(accountConfig), company, null, partner, moveDate, partner.getInPaymentMode(), MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_PURCHASE);
    List<MoveLine> moveLines = new ArrayList<>();
    Set<AnalyticAccount> analyticAccounts = new HashSet<>();
    BigDecimal exTaxTotal = null;
    int moveLineId = 1;
    int expenseLineId = 1;
    Account employeeAccount = accountingSituationService.getEmployeeAccount(partner, company);
    moveLines.add(moveLineService.createMoveLine(move, partner, employeeAccount, expense.getInTaxTotal(), false, moveDate, moveDate, moveLineId++, expense.getExpenseSeq(), expense.getFullName()));
    for (ExpenseLine expenseLine : getExpenseLineList(expense)) {
        analyticAccounts.clear();
        Product product = expenseLine.getExpenseProduct();
        account = accountManagementService.getProductAccount(product, company, partner.getFiscalPosition(), true, false);
        if (account == null) {
            throw new AxelorException(expense, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(com.axelor.apps.account.exception.IExceptionMessage.MOVE_LINE_4), expenseLineId, company.getName());
        }
        exTaxTotal = expenseLine.getUntaxedAmount();
        MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, exTaxTotal, true, moveDate, moveDate, moveLineId++, expense.getExpenseSeq(), expenseLine.getComments() != null ? expenseLine.getComments().replaceAll("(\r\n|\n\r|\r|\n)", " ") : "");
        for (AnalyticMoveLine analyticDistributionLineIt : expenseLine.getAnalyticMoveLineList()) {
            AnalyticMoveLine analyticDistributionLine = Beans.get(AnalyticMoveLineRepository.class).copy(analyticDistributionLineIt, false);
            analyticDistributionLine.setExpenseLine(null);
            moveLine.addAnalyticMoveLineListItem(analyticDistributionLine);
        }
        moveLines.add(moveLine);
        expenseLineId++;
    }
    moveLineService.consolidateMoveLines(moveLines);
    account = accountConfigService.getExpenseTaxAccount(accountConfig);
    BigDecimal taxTotal = BigDecimal.ZERO;
    for (ExpenseLine expenseLine : getExpenseLineList(expense)) {
        exTaxTotal = expenseLine.getTotalTax();
        taxTotal = taxTotal.add(exTaxTotal);
    }
    if (taxTotal.signum() != 0) {
        MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, taxTotal, true, moveDate, moveDate, moveLineId++, expense.getExpenseSeq(), expense.getFullName());
        moveLines.add(moveLine);
    }
    move.getMoveLineList().addAll(moveLines);
    moveService.getMoveValidateService().validate(move);
    expense.setMove(move);
    return move;
}
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) ArrayList(java.util.ArrayList) Product(com.axelor.apps.base.db.Product) AnalyticAccount(com.axelor.apps.account.db.AnalyticAccount) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AccountConfig(com.axelor.apps.account.db.AccountConfig) AnalyticMoveLineRepository(com.axelor.apps.account.db.repo.AnalyticMoveLineRepository) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) ExpenseLine(com.axelor.apps.hr.db.ExpenseLine) Partner(com.axelor.apps.base.db.Partner) HashSet(java.util.HashSet) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

Example 37 with MoveLine

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

the class AccountingCutOffServiceImpl method reconcile.

protected void reconcile(Move move, Move reverseMove) throws AxelorException {
    List<MoveLine> moveLineSortedList = move.getMoveLineList();
    Collections.sort(moveLineSortedList, Comparator.comparing(MoveLine::getCounter));
    List<MoveLine> reverseMoveLineSortedList = reverseMove.getMoveLineList();
    Collections.sort(reverseMoveLineSortedList, Comparator.comparing(MoveLine::getCounter));
    Iterator<MoveLine> reverseMoveLinesIt = reverseMoveLineSortedList.iterator();
    for (MoveLine moveLine : moveLineSortedList) {
        MoveLine reverseMoveLine = reverseMoveLinesIt.next();
        reconcileService.reconcile(moveLine, reverseMoveLine, false, false);
    }
}
Also used : MoveLine(com.axelor.apps.account.db.MoveLine) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

Example 38 with MoveLine

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

the class AccountingCutOffServiceImpl method generateProductMoveLine.

protected MoveLine generateProductMoveLine(Move move, StockMoveLine stockMoveLine, String origin, boolean isPurchase, boolean recoveredTax, boolean ati, String moveDescription, boolean isReverse, LocalDate originDate) throws AxelorException {
    SaleOrderLine saleOrderLine = stockMoveLine.getSaleOrderLine();
    PurchaseOrderLine purchaseOrderLine = stockMoveLine.getPurchaseOrderLine();
    Company company = move.getCompany();
    LocalDate moveDate = move.getDate();
    Partner partner = move.getPartner();
    boolean isFixedAssets = false;
    BigDecimal amountInCurrency = null;
    BigDecimal totalQty = null;
    BigDecimal notInvoicedQty = null;
    if (isPurchase && purchaseOrderLine != null) {
        totalQty = purchaseOrderLine.getQty();
        notInvoicedQty = unitConversionService.convert(stockMoveLine.getUnit(), purchaseOrderLine.getUnit(), stockMoveLine.getRealQty().subtract(stockMoveLine.getQtyInvoiced()), stockMoveLine.getRealQty().scale(), purchaseOrderLine.getProduct());
        isFixedAssets = purchaseOrderLine.getFixedAssets();
        if (ati && !recoveredTax) {
            amountInCurrency = purchaseOrderLine.getInTaxTotal();
        } else {
            amountInCurrency = purchaseOrderLine.getExTaxTotal();
        }
    }
    if (!isPurchase && saleOrderLine != null) {
        totalQty = saleOrderLine.getQty();
        notInvoicedQty = unitConversionService.convert(stockMoveLine.getUnit(), saleOrderLine.getUnit(), stockMoveLine.getRealQty().subtract(stockMoveLine.getQtyInvoiced()), stockMoveLine.getRealQty().scale(), saleOrderLine.getProduct());
        if (ati) {
            amountInCurrency = saleOrderLine.getInTaxTotal();
        } else {
            amountInCurrency = saleOrderLine.getExTaxTotal();
        }
    }
    if (totalQty == null || BigDecimal.ZERO.compareTo(totalQty) == 0) {
        return null;
    }
    BigDecimal qtyRate = notInvoicedQty.divide(totalQty, 10, RoundingMode.HALF_UP);
    amountInCurrency = amountInCurrency.multiply(qtyRate).setScale(2, RoundingMode.HALF_UP);
    if (amountInCurrency == null || amountInCurrency.compareTo(BigDecimal.ZERO) == 0) {
        return null;
    }
    Product product = stockMoveLine.getProduct();
    Account account = accountManagementAccountService.getProductAccount(product, company, partner.getFiscalPosition(), isPurchase, isFixedAssets);
    boolean isDebit = false;
    if ((isPurchase && amountInCurrency.compareTo(BigDecimal.ZERO) == 1) || !isPurchase && amountInCurrency.compareTo(BigDecimal.ZERO) == -1) {
        isDebit = true;
    }
    if (isReverse) {
        isDebit = !isDebit;
    }
    MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, amountInCurrency, isDebit, originDate, ++counter, origin, moveDescription);
    moveLine.setDate(moveDate);
    moveLine.setDueDate(moveDate);
    getAndComputeAnalyticDistribution(product, move, moveLine);
    move.addMoveLineListItem(moveLine);
    if (recoveredTax) {
        TaxLine taxLine = accountManagementAccountService.getTaxLine(originDate, product, company, partner.getFiscalPosition(), isPurchase);
        if (taxLine != null) {
            moveLine.setTaxLine(taxLine);
            moveLine.setTaxRate(taxLine.getValue());
            moveLine.setTaxCode(taxLine.getTax().getCode());
            if (taxLine.getValue().compareTo(BigDecimal.ZERO) != 0) {
                generateTaxMoveLine(move, moveLine, origin, isPurchase, isFixedAssets, moveDescription);
            }
        }
    }
    return moveLine;
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) Account(com.axelor.apps.account.db.Account) Company(com.axelor.apps.base.db.Company) MoveLine(com.axelor.apps.account.db.MoveLine) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) Product(com.axelor.apps.base.db.Product) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) LocalDate(java.time.LocalDate) Partner(com.axelor.apps.base.db.Partner) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine)

Example 39 with MoveLine

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

the class AccountingCutOffServiceImpl method generatePartnerMoveLine.

protected MoveLine generatePartnerMoveLine(Move move, String origin, Account account, String moveDescription, LocalDate originDate) throws AxelorException {
    LocalDate moveDate = move.getDate();
    BigDecimal currencyBalance = moveToolService.getBalanceCurrencyAmount(move.getMoveLineList());
    BigDecimal balance = moveToolService.getBalanceAmount(move.getMoveLineList());
    if (balance.compareTo(BigDecimal.ZERO) == 0) {
        return null;
    }
    MoveLine moveLine = moveLineService.createMoveLine(move, move.getPartner(), account, currencyBalance.abs(), balance.abs(), null, balance.compareTo(BigDecimal.ZERO) == -1, moveDate, moveDate, originDate, ++counter, origin, moveDescription);
    move.addMoveLineListItem(moveLine);
    return moveLine;
}
Also used : MoveLine(com.axelor.apps.account.db.MoveLine) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 40 with MoveLine

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

the class MoveLineServiceImpl method balanceCreditDebit.

@Override
public MoveLine balanceCreditDebit(MoveLine moveLine, Move move) {
    if (move.getMoveLineList() != null) {
        BigDecimal totalCredit = move.getMoveLineList().stream().map(it -> it.getCredit()).reduce((a, b) -> a.add(b)).orElse(BigDecimal.ZERO);
        BigDecimal totalDebit = move.getMoveLineList().stream().map(it -> it.getDebit()).reduce((a, b) -> a.add(b)).orElse(BigDecimal.ZERO);
        if (totalCredit.compareTo(totalDebit) < 0) {
            moveLine.setCredit(totalDebit.subtract(totalCredit));
        } else if (totalCredit.compareTo(totalDebit) > 0) {
            moveLine.setDebit(totalCredit.subtract(totalDebit));
        }
    }
    return moveLine;
}
Also used : TaxAccountService(com.axelor.apps.account.service.TaxAccountService) AppAccountService(com.axelor.apps.account.service.app.AppAccountService) Move(com.axelor.apps.account.db.Move) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) TaxPaymentMoveLineService(com.axelor.apps.account.service.TaxPaymentMoveLineService) CurrencyService(com.axelor.apps.base.service.CurrencyService) Transactional(com.google.inject.persist.Transactional) BigDecimal(java.math.BigDecimal) CompanyConfigService(com.axelor.apps.base.service.config.CompanyConfigService) AccountTypeRepository(com.axelor.apps.account.db.repo.AccountTypeRepository) Pair(org.apache.commons.lang3.tuple.Pair) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) TaxLine(com.axelor.apps.account.db.TaxLine) AnalyticAccount(com.axelor.apps.account.db.AnalyticAccount) Map(java.util.Map) FiscalPositionAccountService(com.axelor.apps.account.service.FiscalPositionAccountService) RoundingMode(java.math.RoundingMode) AnalyticMoveLineRepository(com.axelor.apps.account.db.repo.AnalyticMoveLineRepository) AccountManagementAccountService(com.axelor.apps.account.service.AccountManagementAccountService) Journal(com.axelor.apps.account.db.Journal) MethodHandles(java.lang.invoke.MethodHandles) Reconcile(com.axelor.apps.account.db.Reconcile) Set(java.util.Set) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) Collectors(java.util.stream.Collectors) Currency(com.axelor.apps.base.db.Currency) InvoiceToolService(com.axelor.apps.account.service.invoice.InvoiceToolService) List(java.util.List) LocalDate(java.time.LocalDate) ObjectUtils(com.axelor.common.ObjectUtils) Partner(com.axelor.apps.base.db.Partner) Company(com.axelor.apps.base.db.Company) PaymentService(com.axelor.apps.account.service.payment.PaymentService) HashMap(java.util.HashMap) TaxPaymentMoveLine(com.axelor.apps.account.db.TaxPaymentMoveLine) AnalyticMoveLineService(com.axelor.apps.account.service.AnalyticMoveLineService) ArrayList(java.util.ArrayList) InvoiceLineTax(com.axelor.apps.account.db.InvoiceLineTax) IExceptionMessage(com.axelor.apps.account.exception.IExceptionMessage) HashSet(java.util.HashSet) AxelorException(com.axelor.exception.AxelorException) MoveLine(com.axelor.apps.account.db.MoveLine) I18n(com.axelor.i18n.I18n) StringTool(com.axelor.apps.tool.StringTool) JPA(com.axelor.db.JPA) Logger(org.slf4j.Logger) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) Iterator(java.util.Iterator) TraceBackService(com.axelor.exception.service.TraceBackService) Invoice(com.axelor.apps.account.db.Invoice) Account(com.axelor.apps.account.db.Account) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Beans(com.axelor.inject.Beans) Comparator(java.util.Comparator) MoveLineRepository(com.axelor.apps.account.db.repo.MoveLineRepository) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) Tax(com.axelor.apps.account.db.Tax) BigDecimal(java.math.BigDecimal)

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