Hide menu Last updated: Oct 24 2016
Area: Episerver Commerce Applies to versions: 10 and higher

Calculating orders

The following calculation services are available to calculate order totals in different levels.

Shipment required for correct calculation

When you create an order (cart, purchase order, or payment plan) through the IOrderRepository, a shipment is created. Order calculators calculate only line items that belong to a shipment. This is a changed behavior from the way it worked with workflow activities. 

Calculate all

The IOrderGroupTotalsCalculator makes calculations for the IOrderGroup, its IOrderForms, IShipments, and ILineItems.

  • The order group.
  • The order group's order forms.
  • Shipments on the order forms.
  • Line items on the shipments.

The method calls all calculators to calculate the values.

public void GetTotals(IOrderGroup orderGroup, IOrderGroupTotalsCalculator calculator)
{
    var calculatedTotals = calculator.GetTotals(orderGroup);

    Debug.WriteLine("Handling total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, calculatedTotals.HandlingTotal);
    Debug.WriteLine("Shipping total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, calculatedTotals.ShippingTotal);
    Debug.WriteLine("Sub total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, calculatedTotals.SubTotal);
    Debug.WriteLine("Tax total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, calculatedTotals.TaxTotal);
    Debug.WriteLine("Total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, calculatedTotals.Total);

    foreach (var form in orderGroup.Forms)
    {
        var orderFormTotals = calculatedTotals[form];

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

        foreach (var shipment in form.Shipments)
        {
            var shipmentTotals = orderFormTotals[shipment];

            Debug.WriteLine("Shipping cost for shipment '{0}': {1}", shipment.ShipmentId, shipmentTotals.ShippingCost);
            Debug.WriteLine("Shipping tax for shipment '{0}': {1}", shipment.ShipmentId, shipmentTotals.ShippingTax);
            Debug.WriteLine("Items total for shipment '{0}': {1}", shipment.ShipmentId, shipmentTotals.ItemsTotal);

            foreach (var item in shipment.LineItems)
            {
                Debug.WriteLine("Extended price for '{0}': {1}", item.Code, shipmentTotals[item]);
            }
        }
    }
}

Line item calculator

The LineItemCalculator calculates a line item's extended price and discounted price.

  • Extended price. Includes order-level discount amount (spread over all line items in the shipment) and any line item discount amount.
  • Discounted price. Only calculates the price with the "line item discount amount," that is, the discount for a specific item.
public void GetExtendedPrice(ILineItem lineItem, ILineItemCalculator lineItemCalculator)
{
    var extendedPrice = lineItemCalculator.GetExtendedPrice(lineItem, Currency.USD);
    Debug.WriteLine("Extended price for '{0}': {1}", lineItem.Code, extendedPrice);
}

public void GetDiscountedPrice(ILineItem lineItem, ILineItemCalculator lineItemCalculator)
{
    var extendedPrice = lineItemCalculator.GetDiscountedPrice(lineItem, Currency.USD);
    Debug.WriteLine("Discounted price for '{0}': {1}", lineItem.Code, extendedPrice);
}

Change the default calculation of extended price

By inheriting from the default implementation of the interface, DefaultLineItemCalculator, you can override the extended price calculation. Just override the CalculateExtendedPrice method.

public class LineItemCalculatorSample : DefaultLineItemCalculator
{
    protected override Money GetExtendedPrice(ILineItem lineItem, Currency currency)
    {
        throw new NotImplementedException();
    }

    protected override Money GetDiscountedPrice(ILineItem lineItem, Currency currency)
    {
        throw new NotImplementedException();
    }
}

Change the validation for extended price

The default implementation validates that the extended price is not negative after the calculation. To change the behavior, override the ValidateExtendedPrice method.

public class LineItemCalculatorSample : DefaultLineItemCalculator
{
    protected override void ValidateExtendedPrice(Money money)
    {
        if (money.Amount <= 0)
        {
            throw new ValidationException("Extended price must be greater than 0");
        }
    }
}
Shipping calculator

The ShippingCalculator calculates the shipping cost of an order, order form, and shipment. It also calculates the total amount of line items in the shipment.

public void GetShippingCost(IOrderGroup orderGroup, IShippingCalculator shipmentCalculator, ICurrentMarket currentMarket)
{
    var shipmentCost = shipmentCalculator.GetShippingCost(orderGroup, currentMarket.GetCurrentMarket(), Currency.USD);
    Debug.WriteLine("Shipping cost for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, shipmentCost);
}

public void GetShippingCost(IOrderForm orderForm, IShippingCalculator shipmentCalculator, ICurrentMarket currentMarket)
{
    var shipmentCost = shipmentCalculator.GetShippingCost(orderForm, currentMarket.GetCurrentMarket(), Currency.USD);
    Debug.WriteLine("Shipping cost for order form '{0}': {1}", orderForm.OrderFormId, shipmentCost);
}

public void GetShippingCost(IShipment shipment, IShippingCalculator shipmentCalculator, ICurrentMarket currentMarket)
{
    var shipmentCost = shipmentCalculator.GetShippingCost(shipment, currentMarket.GetCurrentMarket(), Currency.USD);
    Debug.WriteLine("Shipping cost for shipment '{0}': {1}", shipment.ShipmentId, shipmentCost);
}

public void GetShippingItemsTotal(IShipment shipment, IShippingCalculator shipmentCalculator)
{
    var shippingItemsTotal = shipmentCalculator.GetShippingItemsTotal(shipment, Currency.USD);
    Debug.WriteLine("Shipping items total for '{0}': {1}", shipment.ShipmentId, shippingItemsTotal);
}

public void GetDiscountedShippingAmount(IShipment shipment, IShippingCalculator shipmentCalculator, ICurrentMarket currentMarket)
{
    var discountedShippingAmount = shipmentCalculator.GetDiscountedShippingAmount(shipment, currentMarket.GetcurrentMarket(), Currency.USD);
    Debug.WriteLine("Discounted shipping amount for shipment '{0}': {1}", shipment.ShipmentId, discountedShippingAmount);
}

public void GetShipmentDiscountPrice(IShipment shipment, IShippingCalculator shippingCalculator)
{
    var shipmentDiscountedPrice = shippingCalculator.GetShipmentDiscountedPrice(shipment, Currency.USD);
    Debug.WriteLine("Shipment discounted price for '{0}': {1}", shipment.ShipmentId, shipmentDiscountedPrice);
}

Change the default calculation of shipping cost and shipping item total

By inheriting from the default implementation of the interface, DefaultShippingCalculator, you can override the shipping cost calculation. To change that calculation, override one or several CalculateShippingCost methods. To change the calculation of shipping items' total, override the CalculateShippingItemsTotal method.

public class ShippingCalculatorSample : DefaultShippingCalculator
{
    private readonly ILineItemCalculator _lineItemCalculator;

    public ShippingCalculatorSample(ILineItemCalculator lineItemCalculator)
        : base(lineItemCalculator)
    {
        _lineItemCalculator = lineItemCalculator;
    }

    protected override Money CalculateShippingCost(IOrderGroup orderGroup, IMarket market, Currency currency)
    {
        //iterate over all order forms in this order group and sum the results.
        var shippingTotal = orderGroup.Forms
            .Sum(form => GetShippingCost(form, market, currency).Amount);

        return new Money(shippingTotal, currency);
    }

    protected override Money CalculateShippingCost(IOrderForm orderForm, IMarket market, Currency currency)
    {
        //iterate over all shipments in this order form and sum the results.
        var result = orderForm.Shipments
            .Sum(shipment => GetShippingCost(shipment, market, currency).Amount - shipment.ShippingDiscountAmount);

        return new Money(result, currency);
    }

    protected override Money CalculateShipmentCost(IShipment shipment, IMarket market, Currency currency)
    {
        //find the shipping method assigned to the shipment
        var shippingMethods = ShippingManager.GetShippingMethods(String.Empty);
        var row = shippingMethods.ShippingMethod.FindByShippingMethodId(shipment.ShippingMethodId);

        //get the instance of the shipping provider.
        var type = Type.GetType(row.ShippingOptionRow.ClassName);
        var provider = (IShippingGateway)Activator.CreateInstance(type, market);

        //get the rate for this shipment
        var message = String.Empty;
        var rate = provider.GetRate(row.ShippingMethodId, shipment as Shipment, ref message);

        //convert result to correct currency and return.
        return rate != null ?
            CurrencyFormatter.ConvertCurrency(rate.Money, currency) :
            new Money(0, currency);
    }

    protected override Money CalculateShippingItemsTotal(IShipment shipment, Currency currency)
    {
        //calculate the total price of all line items
        var result = 0m;
        foreach (var lineItem in shipment.LineItems.Where(x => x.Quantity > 0))
        {
            var extemdedPrice = _lineItemCalculator.GetExtendedPrice(lineItem, currency);
            result += extemdedPrice.Amount;
        }

        return new Money(result, currency);
    }

    protected override Money GetDiscountedShippingAmount(IShipment shipment, IMarket market, Currency currency)
    {
        var result = GetShippingCost(shipment, market, currency).Amount - GetShipmentDiscountPrice(shipment, currency).Amount;
        return new Money(result, currency);
    }

    protected override Money GetShipmentDiscountPrice(IShipment shipment, Currency currency)
    {
        return new Money(0, currency);
    }
}

Change the validation

The default implementation validates that the shipment cost is not negative after the calculation. To change the behavior, override the method ValidateShipmentCostForOrderValidateShipmentCostForOrderForm, ValidateShipmentCostForShipment, or ValidateShippingItemTotal, depending on which validation you should override.

public class ShippingCalculatorSample : DefaultShippingCalculator
{
    private readonly ILineItemCalculator _lineItemCalculator;

    public ShippingCalculatorSample(ILineItemCalculator lineItemCalculator)
        : base(lineItemCalculator)
    {
        _lineItemCalculator = lineItemCalculator;
    }

    protected override void ValidateShipmentCostForOrder(Money money)
    {
        if (money.Amount <= 0)
        {
            throw new ValidationException("Shipment cost must be greater than 0");
        }
    }

    protected override void ValidateShipmentCostForOrderForm(Money money)
    {
        if (money.Amount <= 0)
        {
            throw new ValidationException("Shipment cost must be greater than 0");
        }
    }

    protected override void ValidateShipmentCostForShipment(Money money)
    {
        if (money.Amount <= 0)
        {
            throw new ValidationException("Shipment cost must be greater than 0");
        }
    }

    protected override void ValidateShippingItemTotal(Money money)
    {
        if (money.Amount <= 0)
        {
            throw new ValidationException("Shipping item total must be greater than 0");
        }
    }
}

Tax calculator

The TaxCalculator calculates the tax cost for the shipment, order, and order form.

public void GetTaxTotal(IOrderGroup orderGroup, ITaxCalculator taxCalculator, ICurrentMarket currentMarket)
{
    var taxTotal = taxCalculator.GetTaxTotal(orderGroup, currentMarket.GetCurrentMarket(), Currency.USD);
    Debug.WriteLine("Tax total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, taxTotal);
}

public void GetTaxTotal(IOrderForm orderForm, ITaxCalculator taxCalculator, ICurrentMarket currentMarket)
{
    var taxTotal = taxCalculator.GetTaxTotal(orderForm, currentMarket.GetCurrentMarket(), Currency.USD);
    Debug.WriteLine("Tax total for order form '{0}': {1}", orderForm.OrderFormId, taxTotal);
}

public void GetShippingTaxTotal(IShipment shipment, ITaxCalculator taxCalculator, ICurrentMarket currentMarket)
{
    var shippingTaxTotal = taxCalculator.GetShippingTaxTotal(shipment, currentMarket.GetCurrentMarket(), Currency.USD);
    Debug.WriteLine("Shipping tax total for shipment '{0}': {1}", shipment.ShipmentId, shippingTaxTotal);
}

Change the default calculation of tax total

By inheriting from the default implementation of the interface, DefaultTaxCalculator, you can override the calculation of tax total. Just override one or several CalculateTaxTotal methods.

public class TaxCalculatorOverridingDefault : DefaultTaxCalculator
{
    public TaxCalculatorOverridingDefault(IContentRepository contentRepository,
        ReferenceConverter referenceConverter,
        IShippingCalculator shippingCalculator)
        : base(contentRepository, referenceConverter, shippingCalculator)
    {
    }

    //override this to implement custom tax retrieval
    protected override IEnumerable GetTaxValues(string taxCategory, string languageCode, IOrderAddress orderAddress)
    {
        return new[] { new TaxValue() };
    }

    //override this to implement custom shipping tax calculation for a shipment
    protected override Money CalculateShippingTaxTotal(IShipment shipment, IMarket market, Currency currency)
    {
        return new Money(0, currency);
    }
}

Change the validation

The default implementation validates that the tax is not negative after the calculation. To change the behavior, override the ValidateTaxTotalForOrder method or the ValidateTaxTotalForOrderForm method, depending on which validation you should override.

public class TaxCalculatorOverridingDefault : DefaultTaxCalculator
{
    public TaxCalculatorOverridingDefault(IContentRepository contentRepository,
        ReferenceConverter referenceConverter,
        IShippingCalculator shippingCalculator)
        : base(contentRepository, referenceConverter, shippingCalculator)
    {
    }

    protected override void ValidateTaxTotalForOrder(Money money)
    {
        if (money.Amount <= 0)
        {
            throw new ValidationException("Shipping item total must be greater than 0");
        }
    }

    protected override void ValidateTaxTotalForOrderForm(Money money)
    {
        if (money.Amount <= 0)
        {
            throw new ValidationException("Shipping item total must be greater than 0");
        }
    }
}

Order form calculator

The OrderFormCalculator calculates the handling total, sub total, and total for an order form.

public void GetOrderGroupTotals(IOrderForm orderForm, IOrderFormCalculator orderFormCalculator, ICurrentMarket currentMarket)
{
    var handlingTotal = orderFormCalculator.GetHandlingTotal(orderForm, Currency.USD);
    var subTotal = orderFormCalculator.GetSubTotal(orderForm, Currency.USD);
    var total = orderFormCalculator.GetTotal(orderForm, currentMarket.GetCurrentMarket(), Currency.USD);

    Debug.WriteLine("Handling total for order form '{0}': {1}", orderForm.OrderFormId, handlingTotal);
    Debug.WriteLine("Sub total for order form '{0}': {1}", orderForm.OrderFormId, subTotal);
    Debug.WriteLine("Total for order form '{0}': {1}", orderForm.OrderFormId, total);
}

Change the default calculations

By inheriting from the default implementation of the interface, DefaultOrderFormCalculator, you can override the calculations. Just override one or several of the CalculateHandlingTotalCalculateSubtotal, or CalculateTotal methods.

Change the validation

The default implementation validates that the total is not negative after the calculation. To change the behavior, override the ValidateHandlingTotalValidateSubtotal, or ValidateTotal method, depending on which validation you should override.

Order calculator

The OrderGroupCalculator calculates an order's handling total, sub total, and total.

public void GetOrderGroupTotals(IOrderGroup orderGroup, IOrderGroupCalculator orderGroupCalculator, ICurrentMarket currentMarket)
{
    var handlingTotal = orderGroupCalculator.GetHandlingTotal(orderGroup, Currency.USD);
    var subTotal = orderGroupCalculator.GetSubTotal(orderGroup, Currency.USD);
    var total = orderGroupCalculator.GetTotal(orderGroup, currentMarket.GetCurrentMarket(), Currency.USD);

    Debug.WriteLine("Handling total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, handlingTotal);
    Debug.WriteLine("Sub total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, subTotal);
    Debug.WriteLine("Total for order '{0}': {1}", orderGroup.OrderLink.OrderGroupId, total);
}

Change the default calculations

By inheriting from the default implementation of the interface, DefaultOrderGroupCalculator, you override the calculations. Just override one or several CalculateHandlingTotal, CalculateSubtotal, or CalculateTotal methods.

Change the validation

The default implementation validates that the total is not negative after the calculation. To change the behavior, override the ValidateHandlingTotal, ValidateSubtotal, or ValidateTotal method, depending on which validation you should override.

Comments