Search in sources :

Example 1 with SaleOrderLineTax

use of com.axelor.apps.sale.db.SaleOrderLineTax in project axelor-open-suite by axelor.

the class SaleOrderInvoiceServiceImpl method generateAdvancePayment.

@Override
@Transactional(rollbackOn = { Exception.class })
public Invoice generateAdvancePayment(SaleOrder saleOrder, BigDecimal amountToInvoice, boolean isPercent) throws AxelorException {
    List<SaleOrderLineTax> taxLineList = saleOrder.getSaleOrderLineTaxList();
    AccountConfigService accountConfigService = Beans.get(AccountConfigService.class);
    BigDecimal percentToInvoice = computeAmountToInvoicePercent(saleOrder, amountToInvoice, isPercent);
    Product invoicingProduct = accountConfigService.getAccountConfig(saleOrder.getCompany()).getAdvancePaymentProduct();
    Account advancePaymentAccount = accountConfigService.getAccountConfig(saleOrder.getCompany()).getAdvancePaymentAccount();
    if (invoicingProduct == null) {
        throw new AxelorException(saleOrder, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.SO_INVOICE_MISSING_ADVANCE_PAYMENT_PRODUCT));
    }
    if (advancePaymentAccount == null) {
        throw new AxelorException(saleOrder, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.SO_INVOICE_MISSING_ADVANCE_PAYMENT_ACCOUNT), saleOrder.getCompany().getName());
    }
    Invoice invoice = createInvoiceAndLines(saleOrder, taxLineList, invoicingProduct, percentToInvoice, InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE, advancePaymentAccount);
    // no need for link to sale order lines for an advance payment
    if (invoice.getInvoiceLineList() != null) {
        invoice.getInvoiceLineList().forEach(invoiceLine -> invoiceLine.setSaleOrderLine(null));
    }
    return invoiceRepo.save(invoice);
}
Also used : Account(com.axelor.apps.account.db.Account) AxelorException(com.axelor.exception.AxelorException) Invoice(com.axelor.apps.account.db.Invoice) SaleOrderLineTax(com.axelor.apps.sale.db.SaleOrderLineTax) AccountConfigService(com.axelor.apps.account.service.config.AccountConfigService) Product(com.axelor.apps.base.db.Product) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 2 with SaleOrderLineTax

use of com.axelor.apps.sale.db.SaleOrderLineTax in project axelor-open-suite by axelor.

the class SaleOrderComputeServiceImpl method _computeSaleOrder.

/**
 * Compute the sale order total amounts
 *
 * @param saleOrder
 * @throws AxelorException
 */
@Override
public void _computeSaleOrder(SaleOrder saleOrder) throws AxelorException {
    saleOrder.setExTaxTotal(BigDecimal.ZERO);
    saleOrder.setCompanyExTaxTotal(BigDecimal.ZERO);
    saleOrder.setTaxTotal(BigDecimal.ZERO);
    saleOrder.setInTaxTotal(BigDecimal.ZERO);
    for (SaleOrderLine saleOrderLine : saleOrder.getSaleOrderLineList()) {
        // skip title lines in computing total amounts
        if (saleOrderLine.getTypeSelect() != SaleOrderLineRepository.TYPE_NORMAL) {
            continue;
        }
        saleOrder.setExTaxTotal(saleOrder.getExTaxTotal().add(saleOrderLine.getExTaxTotal()));
        // In the company accounting currency
        saleOrder.setCompanyExTaxTotal(saleOrder.getCompanyExTaxTotal().add(saleOrderLine.getCompanyExTaxTotal()));
    }
    for (SaleOrderLineTax saleOrderLineVat : saleOrder.getSaleOrderLineTaxList()) {
        // In the sale order currency
        saleOrder.setTaxTotal(saleOrder.getTaxTotal().add(saleOrderLineVat.getTaxTotal()));
    }
    saleOrder.setInTaxTotal(saleOrder.getExTaxTotal().add(saleOrder.getTaxTotal()));
    saleOrder.setAdvanceTotal(computeTotalAdvancePayment(saleOrder));
    logger.debug("Montant de la facture: HTT = {},  HT = {}, Taxe = {}, TTC = {}", new Object[] { saleOrder.getExTaxTotal(), saleOrder.getTaxTotal(), saleOrder.getInTaxTotal() });
}
Also used : SaleOrderLineTax(com.axelor.apps.sale.db.SaleOrderLineTax) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine)

Example 3 with SaleOrderLineTax

use of com.axelor.apps.sale.db.SaleOrderLineTax in project axelor-open-suite by axelor.

the class SaleOrderInvoiceServiceImpl method createInvoiceLinesFromTax.

@Override
public List<InvoiceLine> createInvoiceLinesFromTax(Invoice invoice, List<SaleOrderLineTax> taxLineList, Product invoicingProduct, BigDecimal percentToInvoice) throws AxelorException {
    List<InvoiceLine> createdInvoiceLineList = new ArrayList<>();
    if (taxLineList != null) {
        for (SaleOrderLineTax saleOrderLineTax : taxLineList) {
            BigDecimal lineAmountToInvoice = percentToInvoice.multiply(saleOrderLineTax.getExTaxBase()).divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP);
            TaxLine taxLine = saleOrderLineTax.getTaxLine();
            BigDecimal lineAmountToInvoiceInclTax = (taxLine != null) ? lineAmountToInvoice.add(lineAmountToInvoice.multiply(taxLine.getValue())) : lineAmountToInvoice;
            InvoiceLineGenerator invoiceLineGenerator = new InvoiceLineGenerator(invoice, invoicingProduct, invoicingProduct.getName(), lineAmountToInvoice, lineAmountToInvoiceInclTax, invoice.getInAti() ? lineAmountToInvoiceInclTax : lineAmountToInvoice, invoicingProduct.getDescription(), BigDecimal.ONE, invoicingProduct.getUnit(), taxLine, InvoiceLineGenerator.DEFAULT_SEQUENCE, BigDecimal.ZERO, PriceListLineRepository.AMOUNT_TYPE_NONE, lineAmountToInvoice, null, false) {

                @Override
                public List<InvoiceLine> creates() throws AxelorException {
                    InvoiceLine invoiceLine = this.createInvoiceLine();
                    List<InvoiceLine> invoiceLines = new ArrayList<>();
                    invoiceLines.add(invoiceLine);
                    return invoiceLines;
                }
            };
            List<InvoiceLine> invoiceOneLineList = invoiceLineGenerator.creates();
            // link to the created invoice line the first line of the sale order.
            for (InvoiceLine invoiceLine : invoiceOneLineList) {
                SaleOrderLine saleOrderLine = saleOrderLineTax.getSaleOrder().getSaleOrderLineList().get(0);
                invoiceLine.setSaleOrderLine(saleOrderLine);
            }
            createdInvoiceLineList.addAll(invoiceOneLineList);
        }
    }
    return createdInvoiceLineList;
}
Also used : SaleOrderLineTax(com.axelor.apps.sale.db.SaleOrderLineTax) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) ArrayList(java.util.ArrayList) InvoiceLineGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine)

Example 4 with SaleOrderLineTax

use of com.axelor.apps.sale.db.SaleOrderLineTax in project axelor-open-suite by axelor.

the class SaleOrderLineTaxService method createsSaleOrderLineTax.

/**
 * Créer les lignes de TVA du devis. La création des lignes de TVA se basent sur les lignes de
 * devis ainsi que les sous-lignes de devis de celles-ci. Si une ligne de devis comporte des
 * sous-lignes de devis, alors on se base uniquement sur celles-ci.
 *
 * @param invoice La facture.
 * @param invoiceLines Les lignes de facture.
 * @param invoiceLineTaxes Les lignes des taxes de la facture.
 * @return La liste des lignes de taxe de la facture.
 */
public List<SaleOrderLineTax> createsSaleOrderLineTax(SaleOrder saleOrder, List<SaleOrderLine> saleOrderLineList) {
    List<SaleOrderLineTax> saleOrderLineTaxList = new ArrayList<SaleOrderLineTax>();
    Map<TaxLine, SaleOrderLineTax> map = new HashMap<TaxLine, SaleOrderLineTax>();
    Set<String> specificNotes = new HashSet<String>();
    boolean customerSpecificNote = false;
    if (saleOrder.getClientPartner().getFiscalPosition() != null) {
        customerSpecificNote = saleOrder.getClientPartner().getFiscalPosition().getCustomerSpecificNote();
    }
    if (saleOrderLineList != null && !saleOrderLineList.isEmpty()) {
        LOG.debug("Création des lignes de tva pour les lignes de factures.");
        for (SaleOrderLine saleOrderLine : saleOrderLineList) {
            TaxLine taxLine = saleOrderLine.getTaxLine();
            if (taxLine != null) {
                LOG.debug("Tax {}", taxLine);
                if (map.containsKey(taxLine)) {
                    SaleOrderLineTax saleOrderLineTax = map.get(taxLine);
                    saleOrderLineTax.setExTaxBase(saleOrderLineTax.getExTaxBase().add(saleOrderLine.getExTaxTotal()));
                } else {
                    SaleOrderLineTax saleOrderLineTax = new SaleOrderLineTax();
                    saleOrderLineTax.setSaleOrder(saleOrder);
                    saleOrderLineTax.setExTaxBase(saleOrderLine.getExTaxTotal());
                    saleOrderLineTax.setTaxLine(taxLine);
                    map.put(taxLine, saleOrderLineTax);
                }
            }
            if (!customerSpecificNote) {
                TaxEquiv taxEquiv = saleOrderLine.getTaxEquiv();
                if (taxEquiv != null && taxEquiv.getSpecificNote() != null) {
                    specificNotes.add(taxEquiv.getSpecificNote());
                }
            }
        }
    }
    for (SaleOrderLineTax saleOrderLineTax : map.values()) {
        // Dans la devise de la facture
        BigDecimal exTaxBase = saleOrderLineTax.getExTaxBase();
        BigDecimal taxTotal = BigDecimal.ZERO;
        if (saleOrderLineTax.getTaxLine() != null) {
            taxTotal = saleOrderToolService.computeAmount(exTaxBase, saleOrderLineTax.getTaxLine().getValue());
            saleOrderLineTax.setTaxTotal(taxTotal);
        }
        saleOrderLineTax.setInTaxTotal(exTaxBase.add(taxTotal));
        saleOrderLineTaxList.add(saleOrderLineTax);
        LOG.debug("Ligne de TVA : Total TVA => {}, Total HT => {}", new Object[] { saleOrderLineTax.getTaxTotal(), saleOrderLineTax.getInTaxTotal() });
    }
    if (!customerSpecificNote) {
        saleOrder.setSpecificNotes(Joiner.on('\n').join(specificNotes));
    } else {
        saleOrder.setSpecificNotes(saleOrder.getClientPartner().getSpecificTaxNote());
    }
    return saleOrderLineTaxList;
}
Also used : SaleOrderLineTax(com.axelor.apps.sale.db.SaleOrderLineTax) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) TaxEquiv(com.axelor.apps.account.db.TaxEquiv) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine) HashSet(java.util.HashSet)

Aggregations

SaleOrderLineTax (com.axelor.apps.sale.db.SaleOrderLineTax)4 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)3 BigDecimal (java.math.BigDecimal)3 TaxLine (com.axelor.apps.account.db.TaxLine)2 ArrayList (java.util.ArrayList)2 Account (com.axelor.apps.account.db.Account)1 Invoice (com.axelor.apps.account.db.Invoice)1 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)1 TaxEquiv (com.axelor.apps.account.db.TaxEquiv)1 AccountConfigService (com.axelor.apps.account.service.config.AccountConfigService)1 InvoiceLineGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator)1 Product (com.axelor.apps.base.db.Product)1 AxelorException (com.axelor.exception.AxelorException)1 Transactional (com.google.inject.persist.Transactional)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1