HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Return purchase order calculator

This topic describes ReturnPurchaseOrderCalculator, which calculates the total, subtotal, and shipping subtotal for a purchase order.

  • Total. Calculates the total of all return order forms in a purchase order.
public void GetTotal(IPurchaseOrder purchaseOrder, IReturnPurchaseOrderCalculator returnPurchaseOrderCalculator)
          {
            var total = returnPurchaseOrderCalculator.GetTotal(purchaseOrder);
            Debug.WriteLine("Total for return order '{0}': {1}", purchaseOrder.OrderLink.OrderGroupId, total);
          }
  • SubTotal. Calculates the subtotal of all return order forms in a purchase order.
public void GetSubTotal(IPurchaseOrder purchaseOrder, IReturnPurchaseOrderCalculator returnPurchaseOrderCalculator)
          {
            var subTotal = returnPurchaseOrderCalculator.GetSubTotal(purchaseOrder);
            Debug.WriteLine("Subtotal for return order '{0}': {1}", purchaseOrder.OrderLink.OrderGroupId, subTotal);
          }
  • ShippingSubTotal. Calculates the shipping subtotal of all return order forms in a purchase order.
public void GetShippingSubTotal(IPurchaseOrder purchaseOrder, IReturnPurchaseOrderCalculator returnPurchaseOrderCalculator)
          {
            var shippingSubTotal = returnPurchaseOrderCalculator.GetShippingSubTotal(purchaseOrder);
            Debug.WriteLine("Shipping subtotal for return order '{0}': {1}", purchaseOrder.OrderLink.OrderGroupId, shippingSubTotal);
          }

Changing the default calculation

By inheriting from the default implementation of the interface, DefaultReturnPurchaseOrderCalculator, you can override the calculations. Just override one or more methods in DefaultReturnPurchaseOrderCalculator.

public class ReturnPurchaseOrderCalculatorOverridingDefault : DefaultReturnPurchaseOrderCalculator
      {
        public ReturnPurchaseOrderCalculatorOverridingDefault(IReturnOrderFormCalculator returnOrderFormCalculator)
          : base(returnOrderFormCalculator)
          { }
        protected override Money GetOrderDiscountTotal(IPurchaseOrder purchaseOrder)
          {
            return new Money(0, purchaseOrder.Currency);
          }
        protected override Money CalculateTotal(IPurchaseOrder purchaseOrder)
          {
            return new Money(0, purchaseOrder.Currency);
          }
        protected override Money CalculateSubTotal(IPurchaseOrder purchaseOrder)
          {
            return new Money(0, purchaseOrder.Currency);
          }
        protected override Money CalculateShippingSubTotal(IPurchaseOrder purchaseOrder)
          {
            return new Money(0, purchaseOrder.Currency);
          }
        protected override IEnumerable<IReturnOrderForm> GetActiveReturnForms(IPurchaseOrder purchaseOrder)
          {
            return new List<IReturnOrderForm>();
          }
      }

Changing the default validation

The default implementation validates that the totals are not negative after the calculation. To change the behavior, override the ValidateTotal, ValidateSubTotal and ValidateShippingSubTotal methods.

public class ReturnPurchaseOrderCalculatorOverridingDefault : DefaultReturnPurchaseOrderCalculator
      {
        public ReturnPurchaseOrderCalculatorOverridingDefault(IReturnOrderFormCalculator returnOrderFormCalculator)
          : base(returnOrderFormCalculator)
          { }
        protected override void ValidateTotal(Money money)
          {
            if (money.Amount <= 0)
              {
                throw new ValidationException("Order total must be greater than 0");
              }
          }
        protected override void ValidateSubTotal(Money money)
          {
            if (money.Amount <= 0)
              {
                throw new ValidationException("Order subtotal must be greater than 0");
              }
          }
        protected override void ValidateShippingSubTotal(Money money)
          {
            if (money.Amount <= 0)
              {
                throw new ValidationException("Shipping subtotal must be greater than 0");
              }
          }
      }

Did this page help you?