Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Interface ITaxCalculator

Tax calculator calculates tax totals

Namespace: EPiServer.Commerce.Order
Assembly: Mediachase.Commerce.dll
Version: 14.26.0
Syntax
public interface ITaxCalculator
Examples
    /// <summary>
/// Sample implementation of <see cref="ITaxCalculator"/>.
/// </summary>
public class TaxCalculatorSample : ITaxCalculator
{
private readonly IContentRepository _contentRepository;
private readonly ReferenceConverter _referenceConverter;

public TaxCalculatorSample(IContentRepository contentRepository, ReferenceConverter referenceConverter)
{
_contentRepository = contentRepository;
_referenceConverter = referenceConverter;
}

[Obsolete("This method is no longer used, use IOrderGroupCalculator.GetTaxTotal instead.")]
public Money GetTaxTotal(IOrderGroup orderGroup, IMarket market, Currency currency)
{
return orderGroup.GetTaxTotal();
}

[Obsolete("This method is no longer used, use IOrderFormCalculator.GetTaxTotal instead.")]
public Money GetTaxTotal(IOrderForm orderForm, IMarket market, Currency currency)
{
var orderFormCalculator = ServiceLocator.Current.GetInstance<IOrderFormCalculator>();
return orderFormCalculator.GetTaxTotal(orderForm, market, currency);
}

[Obsolete("This method is no longer used, use IReturnOrderFormCalculator.GetReturnTaxTotal instead.")]
public Money GetReturnTaxTotal(IReturnOrderForm returnOrderForm, IMarket market, Currency currency)
{
var returnOrderFormCalculator = ServiceLocator.Current.GetInstance<IReturnOrderFormCalculator>();
return returnOrderFormCalculator.GetReturnTaxTotal(returnOrderForm, market, currency);
}

[Obsolete("This method is no longer used, use IShippingCalculator.GetShippingTax instead.")]
public Money GetShippingTaxTotal(IShipment shipment, IMarket market, Currency currency)
{
var shippingCalculator = ServiceLocator.Current.GetInstance<IShippingCalculator>();
return shippingCalculator.GetShippingTax(shipment, market, currency);
}

[Obsolete("This method is no longer used, use IShippingCalculator.GetShippingReturnTax instead.")]
public Money GetShippingReturnTaxTotal(IShipment shipment, IMarket market, Currency currency)
{
var shippingCalculator = ServiceLocator.Current.GetInstance<IShippingCalculator>();
return shippingCalculator.GetReturnShippingTax(shipment, market, currency);
}

public Money GetSalesTax(ILineItem lineItem, IMarket market, IOrderAddress shippingAddress, Money basePrice)
{
var taxValues = GetTaxValues(lineItem, market, shippingAddress);
var taxPercentage = (decimal)taxValues.Where(x => x.TaxType == TaxType.SalesTax).Sum(x => x.Percentage);
var pricesIncludeTax = market.PricesIncludeTax;// you might want to get the PricesIncludeTax setting from the IOrderGroup where the line item belongs to.

var salesTaxAmount = basePrice.Amount * taxPercentage / (pricesIncludeTax ? taxPercentage + 100 : 100);

return new Money(salesTaxAmount, basePrice.Currency);
}

public Money GetSalesTax(IEnumerable<ILineItem> lineItems, IMarket market, IOrderAddress shippingAddress, Currency currency)
{
var salesTaxAmount = 0m;
foreach (var lineItem in lineItems)
{
    salesTaxAmount =+ GetSalesTax(lineItem, market, shippingAddress, lineItem.GetExtendedPrice(currency)).Amount;
}
return new Money(salesTaxAmount, currency);
}

public Money GetShippingTax(ILineItem lineItem, IMarket market, IOrderAddress shippingAddress, Money basePrice)
{
var taxValues = GetTaxValues(lineItem, market, shippingAddress);
var taxPercentage = (decimal)taxValues.Where(x => x.TaxType == TaxType.ShippingTax).Sum(x => x.Percentage);
var pricesIncludeTax = market.PricesIncludeTax;// you might want to get the PricesIncludeTax setting from the IOrderGroup where the line item belongs to.

var shippingTaxAmount = basePrice.Amount * taxPercentage / (pricesIncludeTax ? taxPercentage + 100 : 100);

return new Money(shippingTaxAmount, basePrice.Currency);
}

private IEnumerable<ITaxValue> GetTaxValues(ILineItem lineItem, IMarket market, IOrderAddress shippingAddress)
{
// The tax rate depends on the shipping address - where line items will be shipped to.
if (shippingAddress == null)
{
    return Enumerable.Empty<ITaxValue>();
}

if (!lineItem.TaxCategoryId.HasValue)
{
    var entry = lineItem.GetEntryContent(_referenceConverter, _contentRepository);
    var pricingContent = entry as IPricing;
    lineItem.TaxCategoryId = pricingContent != null ? pricingContent.TaxCategoryId : null;
}

var taxCategoryId = lineItem.TaxCategoryId ?? 0;
var taxCategory = CatalogTaxManager.GetTaxCategoryNameById(taxCategoryId);

return OrderContext.Current.GetTaxes(Guid.Empty, taxCategory, market.DefaultLanguage.DisplayName, shippingAddress);
}
}

Methods

GetSalesTax(ILineItem, IMarket, IOrderAddress, Money)

Gets the sales tax of an ILineItem.

Declaration
Money GetSalesTax(ILineItem lineItem, IMarket market, IOrderAddress shippingAddress, Money basePrice)
Parameters
Type Name Description
ILineItem lineItem

The line item.

IMarket market

The market to be used in the calculation.

IOrderAddress shippingAddress

The shipping address.

Money basePrice

The base price.

Returns
Type Description
Money

The sales tax.

Examples
        public void GetSalesTax(ILineItem lineItem, IMarket market, IOrderAddress shippingAddress, Money basePrice, ITaxCalculator taxCalculator)
{
var salesTaxAmount = taxCalculator.GetSalesTax(lineItem, market, shippingAddress, basePrice);
Debug.WriteLine("Sales tax amount: ", salesTaxAmount);
}

GetSalesTax(IEnumerable<ILineItem>, IMarket, IOrderAddress, Currency)

Gets the sales tax of a collection of ILineItems.

Declaration
Money GetSalesTax(IEnumerable<ILineItem> lineItems, IMarket market, IOrderAddress shippingAddress, Currency currency)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<ILineItem> lineItems

The line items.

IMarket market

The market to be used in the calculation.

IOrderAddress shippingAddress

The shipping address.

Currency currency

The currency.

Returns
Type Description
Money

The sales tax.

Examples
        public void GetSalesTax(IEnumerable<ILineItem> lineItems, IMarket market, IOrderAddress shippingAddress, Currency currency, ITaxCalculator taxCalculator)
{
var salesTax = taxCalculator.GetSalesTax(lineItems, market, shippingAddress, currency);
Debug.WriteLine("Sales tax amount: ", salesTax.Amount);

}

GetShippingTax(ILineItem, IMarket, IOrderAddress, Money)

Gets the shipping tax of an ILineItem.

Declaration
Money GetShippingTax(ILineItem lineItem, IMarket market, IOrderAddress shippingAddress, Money basePrice)
Parameters
Type Name Description
ILineItem lineItem

The line item.

IMarket market

The market to be used in the calculation.

IOrderAddress shippingAddress

The shipping address.

Money basePrice

The base price.

Returns
Type Description
Money

The shipping tax.

Examples
        public void GetShippingTax(ILineItem lineItem, IMarket market, IOrderAddress shippingAddress, Money basePrice, ITaxCalculator taxCalculator)
{
var shippingTaxAmount = taxCalculator.GetShippingTax(lineItem, market, shippingAddress, basePrice);
Debug.WriteLine("Shipping tax amount: ", shippingTaxAmount);
}