Try our conversational search powered by Generative AI!

Interface ITaxCalculator

Tax calculator calculates tax totals

Namespace: EPiServer.Commerce.Order
Assembly: Mediachase.Commerce.dll
Version: 12.17.2
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

GetReturnTaxTotal(IReturnOrderForm, IMarket, Currency)

Gets the tax total for an IReturnOrderForm.

Declaration
[Obsolete("This method is no longer used, use IReturnOrderFormCalculator.GetReturnTaxTotal instead. Will remain at least until May 2019.")]
Money GetReturnTaxTotal(IReturnOrderForm returnOrderForm, IMarket market, Currency currency)
Parameters
Type Name Description
IReturnOrderForm returnOrderForm

The return order form.

IMarket market

The market to be used in the calculation.

Currency currency

The currency to be used in the calculations.

Returns
Type Description
Money

The total tax amount for the return order form.

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);
}

GetShippingReturnTaxTotal(IShipment, IMarket, Currency)

Gets the tax total for a return shipment that contains IReturnLineItem.

Declaration
[Obsolete("This method is no longer used, use IShippingCalculator.GetShippingReturnTax instead. Will remain at least until May 2019.")]
Money GetShippingReturnTaxTotal(IShipment shipment, IMarket market, Currency currency)
Parameters
Type Name Description
IShipment shipment

The return shipment.

IMarket market

The market to be used in the calculation.

Currency currency

The currency to be used in the calculations.

Returns
Type Description
Money

The shipping tax amount for the return shipment.

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);
}

GetShippingTaxTotal(IShipment, IMarket, Currency)

Gets the shipping tax total.

Declaration
[Obsolete("This method is no longer used, use IShippingCalculator.GetShippingTax instead. Will remain at least until May 2019.")]
Money GetShippingTaxTotal(IShipment shipment, IMarket market, Currency currency)
Parameters
Type Name Description
IShipment shipment

The shipment.

IMarket market

The market to be used in the calculation.

Currency currency

The currency to be used in the calculations.

Returns
Type Description
Money

The shipping tax amount for the shipment.

GetTaxTotal(IOrderForm, IMarket, Currency)

Gets the tax total for the order form.

Declaration
[Obsolete("This method is no longer used, use IOrderFormCalculator.GetTaxTotal instead. Will remain at least until May 2019.")]
Money GetTaxTotal(IOrderForm orderForm, IMarket market, Currency currency)
Parameters
Type Name Description
IOrderForm orderForm

The order form.

IMarket market

The market to be used in the calculation.

Currency currency

The currency to be used in the calculations

Returns
Type Description
Money

The total tax amount for the order form

GetTaxTotal(IOrderGroup, IMarket, Currency)

Gets the tax total.

Declaration
[Obsolete("This method is no longer used, use IOrderGroupCalculator.GetTaxTotal instead. Will remain at least until May 2019.")]
Money GetTaxTotal(IOrderGroup orderGroup, IMarket market, Currency currency)
Parameters
Type Name Description
IOrderGroup orderGroup

The order group.

IMarket market

The market to be used in the calculation.

Currency currency

The currency to be used in the calculations.

Returns
Type Description
Money

The total tax amount for the order.