November Happy Hour will be moved to Thursday December 5th.

Interface IOrderFormCalculator

Calculates totals on the order form.

Namespace: EPiServer.Commerce.Order
Assembly: Mediachase.Commerce.dll
Version: 12.17.2
Syntax
public interface IOrderFormCalculator
Examples
    /// <summary>
/// Sample implementation of <see cref="IOrderFormCalculator"/>.
/// </summary>
public class OrderFormCalculatorSample : IOrderFormCalculator
{
private readonly IShippingCalculator _shippingCalculator;

public OrderFormCalculatorSample(IShippingCalculator shippingCalculator)
{
_shippingCalculator = shippingCalculator;
}

public Money GetTotal(IOrderForm orderForm, IMarket market, Currency currency)
{
var total = GetSubTotal(orderForm, currency) +
    GetHandlingTotal(orderForm, currency) +
    GetShippingSubTotal(orderForm, market, currency);

//If the prices in the order where the order form belongs to already include tax, then it does not need to calculate tax.
if (orderForm.PricesIncludeTax)
{
    return total;
}

//otherwise; the prices in the order exclude tax, it should calculate the tax total and include it to the return value.
total += GetTaxTotal(orderForm, market, currency);

return total;
}

public Money GetSubTotal(IOrderForm orderForm, Currency currency)
{
//It's the total of prices of all line items in all shipments of the order form (so all line items of the order form).
var orderSubTotal = orderForm.Shipments.Sum(shipment => _shippingCalculator.GetShippingItemsTotal(shipment, currency));

return new Money(orderSubTotal, currency);
}

public Money GetHandlingTotal(IOrderForm orderForm, Currency currency)
{
return new Money(orderForm.HandlingTotal, currency);
}

public Money GetShippingSubTotal(IOrderForm orderForm, IMarket market, Currency currency)
{
var shippingSubTotal = orderForm.Shipments.Sum(shipment => _shippingCalculator.GetShippingCost(shipment, market, currency).Amount);

return new Money(shippingSubTotal, currency);
}

[Obsolete("This is no longer used. The order discount total should be calculated at IOrderGroup level. See IOrderGroupCalculator.GetOrderDiscountTotal(orderGroup).")]
public Money GetOrderDiscountTotal(IOrderForm orderForm, Currency currency)
{
return new Money(0, currency);
}

public Money GetDiscountTotal(IOrderForm orderForm, Currency currency)
{
var discountTotal = orderForm.Shipments.Sum(shipment =>
    shipment.GetShipmentDiscountPrice(currency) +
    shipment.LineItems.Sum(item => item.GetDiscountTotal(currency)));

return new Money(discountTotal, currency);
}

public OrderFormTotals GetOrderFormTotals(IOrderForm orderForm, IMarket market, Currency currency)
{
var formHandlingTotal = GetHandlingTotal(orderForm, currency);
var formShippingTotal = GetShippingSubTotal(orderForm, market, currency);
var formSubTotal = GetSubTotal(orderForm, currency);
var formTaxTotal = GetTaxTotal(orderForm, market, currency);
var formTotal = GetTotal(orderForm, market, currency);
var formDiscountTotal = GetDiscountTotal(orderForm, currency);
var shipmentTotalsDictionary = orderForm.Shipments.ToDictionary(shipment => shipment, shipment => _shippingCalculator.GetShippingTotals(shipment, market, currency));

return new OrderFormTotals(formHandlingTotal, formShippingTotal, formSubTotal, formTaxTotal, formTotal, formDiscountTotal, shipmentTotalsDictionary);
}

public Money GetTaxTotal(IOrderForm orderForm, IMarket market, Currency currency)
{
//It's the total of all shipping taxes and sales taxes.
var taxTotal = orderForm.Shipments.Sum(shipment => (
    _shippingCalculator.GetShippingTax(shipment, market, currency) +
    _shippingCalculator.GetSalesTax(shipment, market, currency)));

return new Money(taxTotal, currency);
}
}

Methods

GetDiscountTotal(IOrderForm, Currency)

Gets the total of all discounts applied on an IOrderForm.

Declaration
Money GetDiscountTotal(IOrderForm orderForm, Currency currency)
Parameters
Type Name Description
IOrderForm orderForm

The given order form.

Currency currency

The currency.

Returns
Type Description
Money

The discount total of the order form.

Remarks

There're three types of discount can be applied to an order: order level, shipment level (or shipping discount) and line item level discount.

Examples
        public void GetDiscountTotal(IOrderForm orderForm, Currency currency, IOrderFormCalculator orderFormCalculator)
{
var discountTotal = orderFormCalculator.GetDiscountTotal(orderForm, currency);
Debug.WriteLine("Discount total for order form '{0}': {1}", orderForm.OrderFormId, discountTotal);
}

GetHandlingTotal(IOrderForm, Currency)

Gets the handling total of an IOrderForm.

Declaration
Money GetHandlingTotal(IOrderForm orderForm, Currency currency)
Parameters
Type Name Description
IOrderForm orderForm

The given order form.

Currency currency

The currency to be used in the calculation.

Returns
Type Description
Money

The handling total for the order form.

Examples
        public void GetHandlingTotal(IOrderForm orderForm, Currency currency, IOrderFormCalculator orderFormCalculator)
{
var handlingTotal = orderFormCalculator.GetHandlingTotal(orderForm, currency);
Debug.WriteLine("Handling total for order form '{0}': {1}", orderForm.OrderFormId, handlingTotal);
}

GetOrderDiscountTotal(IOrderForm, Currency)

Gets the order discount price of the orderForm.

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

The order form.

Currency currency

The currency.

Returns
Type Description
Money

The order discount price for the order form.

Examples
        public void GetOrderDiscountTotal(IOrderForm orderForm, Currency currency, IOrderFormCalculator orderFormCalculator)
{
var orderDiscountTotal = orderFormCalculator.GetOrderDiscountTotal(orderForm, currency);
Debug.WriteLine("Order discount total for order form '{0}': {1}", orderForm.OrderFormId, orderDiscountTotal);
}

GetOrderFormTotals(IOrderForm, IMarket, Currency)

Gets the order form totals of an IOrderForm.

Declaration
OrderFormTotals GetOrderFormTotals(IOrderForm orderForm, IMarket market, Currency currency)
Parameters
Type Name Description
IOrderForm orderForm

The given order form.

IMarket market

The market to be used in the calculation.

Currency currency

The currency.

Returns
Type Description
OrderFormTotals

An order form totals.

Examples
        public void GetOrderFormTotals(IOrderForm orderForm, IMarket market, Currency currency, IOrderFormCalculator orderFormCalculator)
{
var orderFormTotals = orderFormCalculator.GetOrderFormTotals(orderForm, market, currency);

Debug.WriteLine("Handling total for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.HandlingTotal);
Debug.WriteLine("Shipping subtotal for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.ShippingTotal);
Debug.WriteLine("Tax total for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.TaxTotal);
Debug.WriteLine("Subtotal for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.SubTotal);
Debug.WriteLine("Discount total for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.DiscountTotal);
Debug.WriteLine("Total for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.Total);
}

GetShippingSubTotal(IOrderForm, IMarket, Currency)

Gets the total of shipping cost of all shipments in an IOrderForm.

Declaration
Money GetShippingSubTotal(IOrderForm orderForm, IMarket market, Currency currency)
Parameters
Type Name Description
IOrderForm orderForm

The given 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 shipping subtotal of the order form.

Examples
        public void GetShippingSubTotal(IOrderForm orderForm, IMarket market, Currency currency, IOrderFormCalculator orderFormCalculator)
{
var shippingSubTotal = orderFormCalculator.GetShippingSubTotal(orderForm, market, currency);
Debug.WriteLine("Shipping subtotal for order form '{0}': {1}", orderForm.OrderFormId, shippingSubTotal);
}

GetSubTotal(IOrderForm, Currency)

Gets the subtotal of an IOrderForm.

Declaration
Money GetSubTotal(IOrderForm orderForm, Currency currency)
Parameters
Type Name Description
IOrderForm orderForm

The given order form.

Currency currency

The currency to be used in the calculation.

Returns
Type Description
Money

The subtotal of the order form.

Examples
        public void GetSubTotal(IOrderForm orderForm, Currency currency, IOrderFormCalculator orderFormCalculator)
{
var subTotal = orderFormCalculator.GetSubTotal(orderForm, currency);
Debug.WriteLine("Subtotal for order form '{0}': {1}", orderForm.OrderFormId, subTotal);
}

GetTaxTotal(IOrderForm, IMarket, Currency)

Gets the tax total of of an IOrderForm.

Declaration
Money GetTaxTotal(IOrderForm orderForm, IMarket market, Currency currency)
Parameters
Type Name Description
IOrderForm orderForm

The given 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 of the order form.

Examples
        public void GetTaxTotal(IOrderForm orderForm, IMarket market, Currency currency, IOrderFormCalculator orderFormCalculator)
{
var taxTotal = orderFormCalculator.GetTaxTotal(orderForm, market, currency);
Debug.WriteLine("Tax total for order form '{0}': {1}", orderForm.OrderFormId, taxTotal);
}

GetTotal(IOrderForm, IMarket, Currency)

Gets the total of an IOrderForm.

Declaration
Money GetTotal(IOrderForm orderForm, IMarket market, Currency currency)
Parameters
Type Name Description
IOrderForm orderForm

The given 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 of the order form.

Examples
        public void GetTotal(IOrderForm orderForm, IMarket market, Currency currency, IOrderFormCalculator orderFormCalculator)
{
var total = orderFormCalculator.GetTotal(orderForm, market, currency);
Debug.WriteLine("Total for order form '{0}': {1}", orderForm.OrderFormId, total);
}