private TestOrderBuilder(OrderGroup order)
		{
			_order = order;

			var orderForm = new OrderForm { Name = "default", OrderGroupId = _order.OrderGroupId, OrderGroup = _order };
			_order.OrderForms.Add(orderForm);
		}
		private void SplitForm(OrderForm form)
		{
			foreach (var item in form.LineItems)
			{
				Shipment itemShipment = null;

				// Find appropriate shipment for item
				foreach (var shipment in form.Shipments)
				{
					if (shipment.ShippingMethodId == item.ShippingMethodId
						&& string.CompareOrdinal(shipment.ShippingAddressId, item.ShippingAddressId) == 0
						&& string.Compare(shipment.FulfillmentCenterId, item.FulfillmentCenterId, StringComparison.OrdinalIgnoreCase) == 0)
					{
						// we found out match, exit
						itemShipment = shipment;
						//break;
					}
					else
					{
						// if shipment contains current LineItem, remove it from the shipment
						RemoveLineItemFromShipment(shipment, item.LineItemId);
					}
				}

				// did we find any shipment?
				if (itemShipment == null)
				{
					itemShipment = new Shipment
						{
							ShippingAddressId = item.ShippingAddressId,
							ShippingMethodId = item.ShippingMethodId,
							ShippingMethodName = item.ShippingMethodName,
							FulfillmentCenterId = item.FulfillmentCenterId,
							OrderForm = form
						};
					form.Shipments.Add(itemShipment);
				}

				// Add item to the shipment
				//if (item.LineItemId == 0)
				//    throw new ArgumentNullException("LineItemId = 0");

				RemoveLineItemFromShipment(itemShipment, item.LineItemId);

				var link = new ShipmentItem
					{
						LineItemId = item.LineItemId,
						LineItem = item,
						Quantity = item.Quantity,
						ShipmentId = itemShipment.ShipmentId
					};
				itemShipment.ShipmentItems.Add(link);
			}
		}
        /// <summary>
        /// This method is called before the order is completed. This method should check all the parameters
        /// and validate the credit card or other parameters accepted.
        /// </summary>
        /// <param name="form">The form.</param>
        /// <param name="model">The model.</param>
        /// <returns>payment</returns>
        public Payment PreProcess(OrderForm form, PaymentModel model)
        {
            var cardPayment = new CreditCardPayment
                {
                    BillingAddressId = form.BillingAddressId,
                    CreditCardCustomerName = model.CustomerName,
                    CreditCardNumber = model.CardNumber,
                    CreditCardExpirationMonth = model.ExpirationMonth,
                    CreditCardExpirationYear = model.ExpirationYear,
                    CreditCardSecurityCode = model.CardVerificationNumber,
                    CreditCardType = model.CardType,
                    PaymentType = PaymentType.CreditCard.GetHashCode(),
                    PaymentTypeId = 0,
                    PaymentMethodName = model.DisplayName,
                    PaymentMethodId = model.Id,
					Amount = form.Total
                };

            return cardPayment;
        }
		/// <summary>
		/// Calculates the totals order forms.
		/// </summary>
		/// <param name="form">The form.</param>
		private void CalculateTotalsOrderForms(OrderForm form)
		{
			decimal subTotal = 0;
			decimal shippingCostTotal = 0;
		    decimal shipmentDiscountAmount = 0;
		    decimal lineItemDiscountAmount = 0;

			foreach (var item in form.LineItems)
			{
				// calculate discounts
				item.LineItemDiscountAmount = item.Discounts.Sum(discount => discount.DiscountAmount);
                lineItemDiscountAmount += item.LineItemDiscountAmount;
				item.ExtendedPrice = item.PlacedPrice * item.Quantity - item.LineItemDiscountAmount;
				subTotal += item.PlacedPrice * item.Quantity;
			}

			// calculate shipment totals
			form.DiscountAmount = form.Discounts.Sum(discount => discount.DiscountAmount);

			foreach (var shipment in form.Shipments)
			{
				shipment.ShippingDiscountAmount = shipment.Discounts.Sum(discount => discount.DiscountAmount);
			    shipmentDiscountAmount += shipment.ShippingDiscountAmount;
				shipment.ItemSubtotal = CalculateShipmentItemSubtotal(form, shipment);
				shipment.Subtotal = shipment.ItemSubtotal + shipment.ShippingCost + shipment.ItemTaxTotal + shipment.ShippingTaxTotal;
				shipment.TotalBeforeTax = shipment.ItemSubtotal + shipment.ShippingCost - shipment.ShippingDiscountAmount;
				shipment.ShipmentTotal = shipment.Subtotal - shipment.ShippingDiscountAmount;
				shippingCostTotal += shipment.ShippingCost;
			}

			form.ShippingTotal = shippingCostTotal;
			form.Subtotal = subTotal;
            form.ShipmentDiscountAmount = shipmentDiscountAmount;
            form.LineItemDiscountAmount = lineItemDiscountAmount;
            form.Total = form.Subtotal + form.ShippingTotal + form.TaxTotal - (form.LineItemDiscountAmount + form.ShipmentDiscountAmount + form.DiscountAmount);

		}
        /// <summary>
        /// Adds the specified form.
        /// </summary>
        /// <param name="form">The form.</param>
        /// <param name="value">The value.</param>
        /// <param name="lineItemRollup">if set to <c>true</c> [line item rollup].</param>
        /// <returns>System.Int32.</returns>
        /// <exception cref="System.ArgumentNullException">value</exception>
        public virtual int Add(OrderForm form, LineItem value, bool lineItemRollup)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            LineItem existingItem = null;

            foreach (var item in form.LineItems)
            {
                if (ReferenceEquals(value, item))
                {
                    return form.LineItems.IndexOf(value);
                }

                if (item.CatalogItemId == value.CatalogItemId)
                {
                    existingItem = item;
                    break;
                }
            }

            if (lineItemRollup && (existingItem != null))
            {
                existingItem.Quantity += value.Quantity;
                return form.LineItems.IndexOf(existingItem);
            }
            value.OrderFormId = form.OrderFormId;
            value.OrderForm = form;
            form.LineItems.Add(value);
            return form.LineItems.IndexOf(value);
        }
        /// <summary>
        /// Adds the specified order group to the existing cart.
        /// </summary>
        /// <param name="orderGroup">The order group.</param>
        /// <param name="lineItemRollup">if set to <c>true</c> [line item rollup].</param>
        /// <exception cref="System.ArgumentNullException">orderGroup</exception>
        public virtual void Add(OrderGroup orderGroup, bool lineItemRollup)
        {
            if (orderGroup == null)
            {
                throw new ArgumentNullException("orderGroup");
            }

            if ((orderGroup.OrderForms != null) && (orderGroup.OrderForms.Count != 0))
            {
                // need to set meta data context before cloning
                foreach (var form in orderGroup.OrderForms)
                {
                    var orderForm = Cart.OrderForms.FirstOrDefault(f => f.Name.Equals(form.Name, StringComparison.OrdinalIgnoreCase));
                    if (orderForm == null)
                    {
                        orderForm = new OrderForm { Name = form.Name };
                        Cart.OrderForms.Add(orderForm);
                    }

                    orderForm.OrderGroupId = Cart.OrderGroupId;
                    orderForm.OrderGroup = Cart;

                    var count = form.LineItems.Count;
                    for (var i = 0; i < count; i++)
                    {
                        var newLineItem = new LineItem();

                        // preserve the new id
                        var id = newLineItem.LineItemId;
                        newLineItem.InjectFrom(form.LineItems[i]);
                        newLineItem.LineItemId = id;

                        Add(orderForm, newLineItem, lineItemRollup);
                    }
                }
            }
        }
        /// <summary>
        /// Adds the entry.
        /// </summary>
        /// <param name="item">The entry.</param>
        /// <param name="parent">Parent item</param>
        /// <param name="quantity">The quantity.</param>
        /// <param name="fixedQuantity">If true, line item's qty will be set to <paramref name="quantity" /> value. Otherwise, <paramref name="quantity" /> will be added to the current line item's qty value.</param>
        /// <param name="helpersToRemove">CartHelper(s) from which the item needs to be removed simultaneously with adding it to the current CartHelper.</param>
        /// <returns>LineItem.</returns>
        public virtual LineItem AddItem(Item item, Item parent, decimal quantity, bool fixedQuantity, params CartHelper[] helpersToRemove)
        {
            OrderForm orderForm;
            if (Cart.OrderForms.Count == 0) // create a new one
            {
                orderForm = new OrderForm { Name = Cart.Name };
                Cart.OrderForms.Add(orderForm);
            }
            else // use first one
            {
                orderForm = Cart.OrderForms[0];
            }

            // Add line items
            var lineItem = CreateLineItem(item, parent, quantity);
            lineItem.OrderFormId = orderForm.OrderFormId;

            // check if items already exist
            var litem = orderForm.LineItems.FirstOrDefault(li => li.CatalogItemId == lineItem.CatalogItemId);
            if (litem != null)
            {
                litem.Quantity = fixedQuantity ? lineItem.Quantity : litem.Quantity + lineItem.Quantity;
                litem.ExtendedPrice = lineItem.PlacedPrice * litem.Quantity;
                lineItem = litem;
            }
            else
            {
                orderForm.LineItems.Add(lineItem);
            }

            // remove entry from other helpers if needed
            if (helpersToRemove != null && helpersToRemove.Length > 0)
            {
                foreach (var ch in helpersToRemove)
                {
                    // if entry is in the helper, remove it
                    var li = ch.LineItems.FirstOrDefault(i => i.CatalogItemId == item.ItemId);
                    if (li != null)
                    {
                        //li.Delete();
                        ch.Remove(li);

                        // If helper is empty, remove it from the database
                        if (ch.IsEmpty)
                        {
                            ch.Delete();
                        }
                    }
                }
            }

            return lineItem;
        }
        /// <summary>
        /// Gets the named OrderForm.
        /// </summary>
        /// <param name="orderFormName">The name of the OrderForm object to retrieve.</param>
        /// <returns>The named OrderForm.</returns>
        public virtual OrderForm GetOrderForm(string orderFormName)
        {
            if (String.IsNullOrEmpty(orderFormName))
            {
                orderFormName = CartName;
            }

            OrderForm orderForm = null;
            foreach (var form in Cart.OrderForms
                                     .Where(form => form.Name.Equals(orderFormName, StringComparison.OrdinalIgnoreCase))
                )
            {
                orderForm = form;
            }

            if (orderForm == null)
            {
                orderForm = new OrderForm { Name = orderFormName };
                Cart.OrderForms.Add(orderForm);
            }
            return orderForm;
        }
 public OrderFormBuilder()
 {
     _form = new OrderForm();
 }
		private PromotionEntrySet GetPromotionEntrySetFromOrderForm(OrderForm form)
		{
			var set = new PromotionEntrySet();

			foreach (var lineItem in form.LineItems)
			{
				var entry = GetPromotionEntryFromLineItem(lineItem);
				set.Entries.Add(entry);
			}

			return set;
		}
		private OrderAddress GetAddressByName(OrderForm form, string shipmentAddressId)
		{
			return form.OrderGroup.OrderAddresses
				.SingleOrDefault(a => a.OrderAddressId == shipmentAddressId);
		}
        /// <summary>
        /// Creates the payment.
        /// </summary>
        /// <param name="form">The order form.</param>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="model">The payment  model.</param>
        /// <returns>Payment.</returns>
        private Payment CreatePayment(OrderForm form, string methodName, PaymentModel model)
        {
            var paymentOption =
                (from o in _paymentOptions where o.Key.Equals(methodName, StringComparison.OrdinalIgnoreCase) select o)
                    .SingleOrDefault();

            Payment payment;

            if (paymentOption != null)
            {
                payment = paymentOption.PreProcess(form, model);
            }
            else
            {
                payment = new OtherPayment
                    {
                        PaymentType = PaymentType.Other.GetHashCode(),
                        BillingAddressId = form.BillingAddressId,
                        PaymentMethodName = model.DisplayName,
                        PaymentMethodId = model.Id,
                    };
            }
            //Common Properties
            payment.Status = PaymentStatus.Pending.ToString();
            payment.OrderForm = form;
            payment.TransactionType = TransactionType.Sale.ToString();
            payment.Amount = form.OrderGroup.Total;

            return payment;
        }
		private static decimal CalculateShipmentItemSubtotal(OrderForm form, Shipment shipment)
		{
			var retVal = (from shipItem in shipment.ShipmentItems where shipItem.Quantity > 0 
						  from lineItem in form.LineItems where lineItem.LineItemId == shipItem.LineItemId 
						  select lineItem.ExtendedPrice/lineItem.Quantity*shipItem.Quantity).Sum();

			return Math.Floor(retVal * 100) * 0.01m;
		}
Beispiel #14
0
 /// <summary>
 /// This method is called after the order is placed. This method should be used by the gateways that want to
 /// redirect customer to their site.
 /// </summary>
 /// <param name="orderForm">The order form.</param>
 /// <param name="model">The model.</param>
 /// <returns><c>true</c></returns>
 public bool PostProcess(OrderForm orderForm, PaymentModel model)
 {
     return true;
 }