Class DefaultTaxCalculator
Calculates tax on an order.
Inheritance
Implements
Inherited Members
Namespace: EPiServer.Commerce.Order.Calculator
Assembly: EPiServer.Business.Commerce.dll
Version: 10.8.0Syntax
public class DefaultTaxCalculator : 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);
}
}
Constructors
DefaultTaxCalculator(IContentRepository, ReferenceConverter, IShippingCalculator, ILineItemCalculator)
Initializes a new instance of the DefaultTaxCalculator class.
Declaration
public DefaultTaxCalculator(IContentRepository contentRepository, ReferenceConverter referenceConverter, IShippingCalculator shippingCalculator, ILineItemCalculator lineItemCalculator)
Parameters
Type | Name | Description |
---|---|---|
EPiServer.IContentRepository | contentRepository | The content Repository. |
ReferenceConverter | referenceConverter | The reference converter. |
IShippingCalculator | shippingCalculator | The shipping calculator. |
ILineItemCalculator | lineItemCalculator | The line item calculator. |
Methods
CalculateShippingTaxTotal(IShipment, IMarket, Currency)
Gets the shipping tax total for the shipment.
Declaration
protected virtual Money CalculateShippingTaxTotal(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 calculation. |
Returns
Type | Description |
---|---|
Money | The shipping tax amount for the shipment. |
CalculateTaxTotal(IOrderForm, IMarket, Currency)
Gets the tax total for the order form.
Declaration
protected virtual Money CalculateTaxTotal(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 calculation. |
Returns
Type | Description |
---|---|
Money | The total tax amount for the order form. |
Examples
CalculateTaxTotal(IOrderGroup, IMarket, Currency)
Gets the tax total.
Declaration
protected virtual Money CalculateTaxTotal(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 calculation. |
Returns
Type | Description |
---|---|
Money | The total tax amount for the order. |
Remarks
Gets the result by looping over all order forms and calling GetTaxTotal.
Examples
GetShippingTaxTotal(IShipment, IMarket, Currency)
Gets the shipping tax total for the shipment.
Declaration
public 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 calculation. |
Returns
Type | Description |
---|---|
Money | The shipping tax amount for the shipment. |
Examples
Exceptions
Type | Condition |
---|---|
System.ComponentModel.DataAnnotations.ValidationException | If validation fails. |
GetTaxCategoryNameById(Int32)
Gets the tax category name by its id
Declaration
protected virtual string GetTaxCategoryNameById(int taxCategoryId)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | taxCategoryId | The tax category id. |
Returns
Type | Description |
---|---|
System.String | The tax category name |
GetTaxTotal(IOrderForm, IMarket, Currency)
Gets the tax total for the order form.
Declaration
public 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 calculation. |
Returns
Type | Description |
---|---|
Money | The total tax amount for the order form |
Examples
Exceptions
Type | Condition |
---|---|
System.ComponentModel.DataAnnotations.ValidationException | If validation fails. |
GetTaxTotal(IOrderGroup, IMarket, Currency)
Gets the tax total.
Declaration
public 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 calculation. |
Returns
Type | Description |
---|---|
Money | The total tax amount for the order. |
Remarks
Gets the result by looping over all order forms and calling GetTaxTotal.
Examples
Exceptions
Type | Condition |
---|---|
System.ComponentModel.DataAnnotations.ValidationException | If validation fails. |
GetTaxValues(String, String, IOrderAddress)
Gets the tax values.
Declaration
protected virtual IEnumerable<ITaxValue> GetTaxValues(string taxCategory, string languageCode, IOrderAddress orderAddress)
Parameters
Type | Name | Description |
---|---|---|
System.String | taxCategory | The tax category. |
System.String | languageCode | The language code. |
IOrderAddress | orderAddress | The order address. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<ITaxValue> | The tax values |
ValidateTaxTotalForOrder(Money)
Validates the shipping item total.
Declaration
protected virtual void ValidateTaxTotalForOrder(Money money)
Parameters
Type | Name | Description |
---|---|---|
Money | money | The calculated value. |
Exceptions
Type | Condition |
---|---|
System.ComponentModel.DataAnnotations.ValidationException | If validation fails. |
ValidateTaxTotalForOrderForm(Money)
Validates the shipping item total.
Declaration
protected virtual void ValidateTaxTotalForOrderForm(Money money)
Parameters
Type | Name | Description |
---|---|---|
Money | money | The calculated value. |
Exceptions
Type | Condition |
---|---|
System.ComponentModel.DataAnnotations.ValidationException | If validation fails. |