Interface ITaxCalculator
Tax calculator calculates tax totals
Namespace: EPiServer.Commerce.Order
Assembly: Mediachase.Commerce.dll
Version: 10.8.0Syntax
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
GetShippingTaxTotal(IShipment, IMarket, Currency)
Gets the shipping tax total.
Declaration
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 |
Examples
GetTaxTotal(IOrderForm, IMarket, Currency)
Gets the tax total for the order form.
Declaration
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 |
Examples
GetTaxTotal(IOrderGroup, IMarket, Currency)
Gets the tax total.
Declaration
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 |
Examples