Order form calculator
Describes OrderFormCalculator, which calculates the following for an order form: total, subtotal, handling total, shipping subtotal, order discount total, discount total, order form totals, and tax total in Optimizely Commerce Connect.
total
Calculates the total for an order form.
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);
}subTotal
Calculates the total of all shipments in an order form.
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);
}handlingTotal
Calculates the handling total for an order form.
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);
}shippingSubTotal
Calculates the shipping subtotal for an order form.
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);
}orderDiscountTotal
Calculates the order form discount amount, which is the sum of all order-level discounts applied to line items across every shipment in the order form.
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);
}discountTotal
Calculates the order form discount total, which is the sum of:
- Order-level discount amounts.
- Line item discount amounts for all line items in all shipments.
- Shipment discount amounts for all shipments in the order form.
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);
}orderFormTotals
Calculates all totals for the order form (handling total, shipping subtotal, tax total, subtotal, discount total, and total).
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);
}taxTotal
Calculates an order form's tax total.
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);
}Change the default calculation
Inherit from DefaultOrderFormCalculator, the default implementation of IOrderFormCalculator, and override one or more of its methods to change the calculation behavior.
public class OrderFormCalculatorOverridingDefault: DefaultOrderFormCalculator {
public OrderFormCalculatorOverridingDefault(IShippingCalculator shippingCalculator, ITaxCalculator taxCalculator): base(shippingCalculator, taxCalculator) {}
protected override Money CalculateTotal(IOrderForm orderForm, IMarket market, Currency currency) {
return new Money(0, currency);
}
protected override Money CalculateSubtotal(IOrderForm orderForm, Currency currency) {
return new Money(0, currency);
}
protected override Money CalculateHandlingTotal(IOrderForm orderForm, Currency currency) {
return new Money(0, currency);
}
protected override Money CalculateShippingSubTotal(IOrderForm orderForm, IMarket market, Currency currency) {
return new Money(0, currency);
}
protected override Money CalculateTaxTotal(IOrderForm orderForm, IMarket market, Currency currency) {
return new Money(0, currency);
}
}Change the default validation
The default implementation validates that each total is non-negative after calculation. To change the behavior, override the appropriate method: ValidateTotal, ValidateSubtotal, ValidateHandlingTotal, ValidateShippingSubtotal, or ValidateTaxTotal.
public class OrderFormCalculatorOverridingDefault: DefaultOrderFormCalculator {
public OrderFormCalculatorOverridingDefault(IShippingCalculator shippingCalculator, ITaxCalculator taxCalculator): base(shippingCalculator, taxCalculator) {}
protected override void ValidateTotal(Money money) {
if (money.Amount < 0) {
throw new ValidationException("Order total must not be negative.");
}
}
protected override void ValidateSubtotal(Money money) {
if (money.Amount < 0) {
throw new ValidationException("Subtotal must not be negative.");
}
}
protected override void ValidateHandlingTotal(Money money) {
if (money.Amount < 0) {
throw new ValidationException("Handling total must not be negative.");
}
}
protected override void ValidateShippingSubTotal(Money money) {
if (money.Amount < 0) {
throw new ValidationException("Shipping subtotal must not be negative.");
}
}
protected override void ValidateTaxTotal(Money money) {
if (money.Amount < 0) {
throw new ValidationException("Tax total must not be negative.");
}
}
}Updated 21 days ago
