Search in sources :

Example 11 with TaxLine

use of com.axelor.apps.account.db.TaxLine 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)

Example 12 with TaxLine

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

the class MoveTemplateService method generateMove.

@Transactional(rollbackOn = { Exception.class })
public List<Long> generateMove(MoveTemplate moveTemplate, List<HashMap<String, Object>> dataList) throws AxelorException {
    List<Long> moveList = new ArrayList<>();
    BigDecimal hundred = new BigDecimal(100);
    for (HashMap<String, Object> data : dataList) {
        LocalDate moveDate = LocalDate.parse(data.get("date").toString(), DateTimeFormatter.ISO_DATE);
        boolean isDebit = false;
        Partner debitPartner = null;
        Partner creditPartner = null;
        BigDecimal moveBalance = new BigDecimal(data.get("moveBalance").toString());
        Partner partner = null;
        if (data.get("debitPartner") != null) {
            debitPartner = partnerRepo.find(Long.parseLong(((HashMap<String, Object>) data.get("debitPartner")).get("id").toString()));
            partner = debitPartner;
        }
        if (data.get("creditPartner") != null) {
            creditPartner = partnerRepo.find(Long.parseLong(((HashMap<String, Object>) data.get("creditPartner")).get("id").toString()));
            partner = creditPartner;
        }
        if (moveTemplate.getJournal().getCompany() != null) {
            Move move = moveService.getMoveCreateService().createMove(moveTemplate.getJournal(), moveTemplate.getJournal().getCompany(), null, partner, moveDate, null, MoveRepository.TECHNICAL_ORIGIN_TEMPLATE, 0);
            int counter = 1;
            for (MoveTemplateLine moveTemplateLine : moveTemplate.getMoveTemplateLineList()) {
                partner = null;
                if (moveTemplateLine.getDebitCreditSelect().equals(MoveTemplateLineRepository.DEBIT)) {
                    isDebit = true;
                    if (moveTemplateLine.getHasPartnerToDebit()) {
                        partner = debitPartner;
                    }
                } else if (moveTemplateLine.getDebitCreditSelect().equals(MoveTemplateLineRepository.CREDIT)) {
                    isDebit = false;
                    if (moveTemplateLine.getHasPartnerToCredit()) {
                        partner = creditPartner;
                    }
                }
                BigDecimal amount = moveBalance.multiply(moveTemplateLine.getPercentage()).divide(hundred, RoundingMode.HALF_UP);
                MoveLine moveLine = moveLineService.createMoveLine(move, partner, moveTemplateLine.getAccount(), amount, isDebit, moveDate, moveDate, counter, moveTemplate.getFullName(), moveTemplateLine.getName());
                move.getMoveLineList().add(moveLine);
                Tax tax = moveTemplateLine.getTax();
                if (tax != null) {
                    TaxLine taxLine = taxService.getTaxLine(tax, moveDate);
                    if (taxLine != null) {
                        moveLine.setTaxLine(taxLine);
                        moveLine.setTaxRate(taxLine.getValue());
                        moveLine.setTaxCode(tax.getCode());
                    }
                }
                moveLine.setAnalyticDistributionTemplate(moveTemplateLine.getAnalyticDistributionTemplate());
                moveLineService.generateAnalyticMoveLines(moveLine);
                counter++;
            }
            if (moveTemplate.getAutomaticallyValidate()) {
                moveValidateService.validate(move);
            }
            moveRepo.save(move);
            moveList.add(move.getId());
        }
    }
    return moveList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Tax(com.axelor.apps.account.db.Tax) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine) Move(com.axelor.apps.account.db.Move) MoveTemplateLine(com.axelor.apps.account.db.MoveTemplateLine) MoveLine(com.axelor.apps.account.db.MoveLine) Partner(com.axelor.apps.base.db.Partner) Transactional(com.google.inject.persist.Transactional)

Example 13 with TaxLine

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

the class MoveTemplateService method generateMove.

@Transactional(rollbackOn = { Exception.class })
public List<Long> generateMove(LocalDate moveDate, List<HashMap<String, Object>> moveTemplateList) throws AxelorException {
    List<Long> moveList = new ArrayList<>();
    for (HashMap<String, Object> moveTemplateMap : moveTemplateList) {
        MoveTemplate moveTemplate = moveTemplateRepo.find(Long.valueOf((Integer) moveTemplateMap.get("id")));
        if (moveTemplate.getJournal().getCompany() != null) {
            Move move = moveService.getMoveCreateService().createMove(moveTemplate.getJournal(), moveTemplate.getJournal().getCompany(), null, null, moveDate, null, MoveRepository.TECHNICAL_ORIGIN_TEMPLATE, 0);
            int counter = 1;
            for (MoveTemplateLine moveTemplateLine : moveTemplate.getMoveTemplateLineList()) {
                BigDecimal amount = moveTemplateLine.getDebit().add(moveTemplateLine.getCredit());
                MoveLine moveLine = moveLineService.createMoveLine(move, moveTemplateLine.getPartner(), moveTemplateLine.getAccount(), amount, moveTemplateLine.getDebit().compareTo(BigDecimal.ZERO) > 0, moveDate, moveDate, counter, moveTemplate.getFullName(), moveTemplateLine.getName());
                move.getMoveLineList().add(moveLine);
                Tax tax = moveTemplateLine.getTax();
                if (tax != null) {
                    TaxLine taxLine = taxService.getTaxLine(tax, moveDate);
                    if (taxLine != null) {
                        moveLine.setTaxLine(taxLine);
                        moveLine.setTaxRate(taxLine.getValue());
                        moveLine.setTaxCode(tax.getCode());
                    }
                }
                moveLine.setAnalyticDistributionTemplate(moveTemplateLine.getAnalyticDistributionTemplate());
                moveLineService.generateAnalyticMoveLines(moveLine);
                counter++;
            }
            if (moveTemplate.getAutomaticallyValidate()) {
                moveValidateService.validate(move);
            }
            moveRepo.save(move);
            moveList.add(move.getId());
        }
    }
    return moveList;
}
Also used : ArrayList(java.util.ArrayList) Tax(com.axelor.apps.account.db.Tax) MoveTemplate(com.axelor.apps.account.db.MoveTemplate) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine) Move(com.axelor.apps.account.db.Move) MoveTemplateLine(com.axelor.apps.account.db.MoveTemplateLine) MoveLine(com.axelor.apps.account.db.MoveLine) Transactional(com.google.inject.persist.Transactional)

Example 14 with TaxLine

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

the class ContractLineServiceImpl method compute.

@Override
public ContractLine compute(ContractLine contractLine, Contract contract, Product product) throws AxelorException {
    Preconditions.checkNotNull(contract, I18n.get("Contract can't be " + "empty for compute contract line price."));
    Preconditions.checkNotNull(product, "Product can't be " + "empty for compute contract line price.");
    // TODO: maybe put tax computing in another method
    contractLine.setFiscalPosition(contract.getPartner().getFiscalPosition());
    TaxLine taxLine = accountManagementService.getTaxLine(appBaseService.getTodayDate(contract.getCompany()), product, contract.getCompany(), contractLine.getFiscalPosition(), false);
    contractLine.setTaxLine(taxLine);
    if (taxLine != null && (Boolean) productCompanyService.get(product, "inAti", contract.getCompany())) {
        BigDecimal price = contractLine.getPrice();
        price = price.divide(taxLine.getValue().add(BigDecimal.ONE), 2, BigDecimal.ROUND_HALF_UP);
        contractLine.setPrice(price);
    }
    BigDecimal price = contractLine.getPrice();
    BigDecimal convert = currencyService.getCurrencyConversionRate((Currency) productCompanyService.get(product, "saleCurrency", contract.getCompany()), contract.getCurrency(), appBaseService.getTodayDate(contract.getCompany()));
    contractLine.setPrice(price.multiply(convert));
    return contractLine;
}
Also used : BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine)

Example 15 with TaxLine

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

the class PurchaseOrderLineController method updateInTaxPrice.

/**
 * Update the in. tax unit price of an invoice line from its ex. tax unit price.
 *
 * @param request
 * @param response
 */
public void updateInTaxPrice(ActionRequest request, ActionResponse response) {
    Context context = request.getContext();
    PurchaseOrderLine purchaseOrderLine = context.asType(PurchaseOrderLine.class);
    try {
        BigDecimal exTaxPrice = purchaseOrderLine.getPrice();
        TaxLine taxLine = purchaseOrderLine.getTaxLine();
        response.setValue("inTaxPrice", Beans.get(PurchaseOrderLineService.class).convertUnitPrice(false, taxLine, exTaxPrice));
    } catch (Exception e) {
        response.setFlash(e.getMessage());
    }
}
Also used : Context(com.axelor.rpc.Context) PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine)

Aggregations

TaxLine (com.axelor.apps.account.db.TaxLine)28 BigDecimal (java.math.BigDecimal)25 AxelorException (com.axelor.exception.AxelorException)9 Account (com.axelor.apps.account.db.Account)8 MoveLine (com.axelor.apps.account.db.MoveLine)7 TaxEquiv (com.axelor.apps.account.db.TaxEquiv)7 ArrayList (java.util.ArrayList)7 Context (com.axelor.rpc.Context)6 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)5 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)5 Partner (com.axelor.apps.base.db.Partner)5 Product (com.axelor.apps.base.db.Product)5 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)5 Tax (com.axelor.apps.account.db.Tax)4 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)4 Transactional (com.google.inject.persist.Transactional)4 HashMap (java.util.HashMap)4 FiscalPosition (com.axelor.apps.account.db.FiscalPosition)3 TaxPaymentMoveLine (com.axelor.apps.account.db.TaxPaymentMoveLine)3 Company (com.axelor.apps.base.db.Company)3