public OrderTransactionBuilder UpdatePricing() { var orderPricing = OrderPricing.Compute(m_order); foreach (var dbOrderItem in m_order.OrderItems) { var orderItemPricing = orderPricing.OrderItemPricings.Where(r => r.OrderItemId == dbOrderItem.OrderItemId).Single(); dbOrderItem.NetQuantity = orderItemPricing.NetQuantity; dbOrderItem.UnitPrice = orderItemPricing.UnitPrice; dbOrderItem.TotalPrice = orderItemPricing.TotalPrice; } _ = AddItemSubtotal(orderPricing.ItemSubtotal - m_order.ItemSubtotal); _ = AddShipping(orderPricing.Shipping - m_order.Shipping); _ = AddSalesTax(orderPricing.SalesTax - m_order.SalesTax); m_order.PretaxAmount = orderPricing.PreTaxAmount; m_order.TaxableAmount = orderPricing.TaxableAmount; m_order.TotalAmount = orderPricing.TotalAmount; return(this); }
public static OrderPricing Compute(Order dbOrder) { var orderPricing = new OrderPricing() { OrderItemPricings = new List <OrderItemPricing>() }; foreach (var dbOrderItem in dbOrder.OrderItems) { orderPricing.OrderItemPricings.Add(ComputeOrderItemPricing(dbOrderItem)); } var shipping = dbOrder.OrderItems.Sum(r => r.NetQuantity) * 5.00m; orderPricing.Shipping = shipping; // Compute item subtotal. // { var itemSubtotal = 0m; foreach (var orderItemPricing in orderPricing.OrderItemPricings) { itemSubtotal += orderItemPricing.TotalPrice; } orderPricing.ItemSubtotal = itemSubtotal; } // Compute pre-tax amount. // { var pretaxAmount = orderPricing.ItemSubtotal + orderPricing.Shipping + dbOrder.Discount; orderPricing.PreTaxAmount = pretaxAmount; } // Compute taxable amount. // { // HACK: Assuming shipping is taxable. // var shippingTaxable = true; decimal taxableAmount; taxableAmount = shippingTaxable ? orderPricing.ItemSubtotal + orderPricing.Shipping + dbOrder.Discount : orderPricing.ItemSubtotal + dbOrder.Discount; orderPricing.TaxableAmount = taxableAmount; } // Compute sales tax. // { var salesTax = Math.Round(orderPricing.TaxableAmount * dbOrder.SalesTaxRate, 2, MidpointRounding.AwayFromZero); orderPricing.SalesTax = salesTax; } // Update total amount. { var totalAmount = orderPricing.PreTaxAmount + orderPricing.SalesTax; orderPricing.TotalAmount = totalAmount; } return(orderPricing); }