Example #1
0
 public int InitiateOrder(int? carId, DateTime orderDate, DateTime appointmentDate, List<OrderItem> orderItems)
 {
     Order newOrder = new Order
     {
         CarId = carId,
         OrderDate = orderDate,
         AppointmentDate = appointmentDate,
     };
     foreach (var orderItem in orderItems)
     {
         OrderItem newOrderItem = new OrderItem
         {
             ProductId = orderItem.ProductId,
             Quantity = orderItem.Quantity,
             PriceId = orderItem.PriceId,
             TotalPrice = orderItem.Price.Amount * orderItem.Quantity
         };
         if (orderItem.Product.IsService)
         {
             foreach(var productComponent in orderItem.Product.ProductComponents)
             {
                 List<TempComponentStockQuantity> tempComponentStocks = stockRepository.SubtractComponentStock(productComponent.ComponentId, orderItem.Quantity);
                 foreach (var tempComponentStock in tempComponentStocks)
                 {
                     newOrderItem.OrderItemsStocks.Add(new OrderItemsStock
                     {
                         ComponentStockId = tempComponentStock.ComponentStockId,
                         Quantity = tempComponentStock.Quantity
                     });
                 }
             }
         }
         else
         {
             List<TempProductStockQuantity> tempProductStocks = stockRepository.SubtractProductStock(newOrderItem.ProductId, newOrderItem.Quantity);
             foreach (var tempProductStock in tempProductStocks)
             {
                 newOrderItem.OrderItemsStocks.Add(new OrderItemsStock
                 {
                     ProductStockId = tempProductStock.ProductStockId,
                     Quantity = tempProductStock.Quantity
                 });
             }
         }
         newOrder.OrderItems.Add(newOrderItem);
     }
     db.Orders.InsertOnSubmit(newOrder);
     Save();
     return newOrder.Id;
 }
Example #2
0
        public ActionResult AddItemToShoppingCart(int productId, int? carId)
        {
            Product product = productRepository.Get(productId);

            if ((Session["cart"] == null)||(((List<OrderItem>)Session["cart"]).Count ==0))
            {
                Price price = new Price();
                if (product.IsService)
                {
                    Car car = customerRepository.GetCar(carId.Value);
                    price = product.CurrentServicePriceEntity(car.CarModelId.Value);
                }
                else
                {
                    price = product.CurrentPriceEntity;
                }
                OrderItem orderItem = new OrderItem
                {
                    Product = product,
                    Price = price,
                    Quantity = 1,
                    TotalPrice = price.Amount * 1
                };
                List<OrderItem> orderItems = new List<OrderItem>();
                orderItems.Add(orderItem);
                Session.Add("cart", orderItems);
            }
            else
            {
                if (((List<OrderItem>)Session["cart"]).Any(x => x.ProductId == productId))
                {
                    List<OrderItem> orderItems = (List<OrderItem>)Session["cart"];
                    OrderItem orderItem = orderItems.FirstOrDefault(x => x.ProductId == productId);
                    //orderItems.Remove(orderItem);
                    Price price = new Price();
                    if (product.IsService)
                    {
                        Car car = customerRepository.GetCar(carId.Value);
                        price = product.CurrentServicePriceEntity(car.CarModelId.Value);
                    }
                    else
                    {
                        price = product.CurrentPriceEntity;
                    }
                    orderItem.Quantity += 1;
                    orderItem.TotalPrice += price.Amount;

                    //orderItems.Add(orderItem);
                    Session.Remove("cart");
                    Session.Add("cart", orderItems);
                }
                else
                {
                    List<OrderItem> orderItems = (List<OrderItem>)Session["cart"];
                    Price price = new Price();
                    if (product.IsService)
                    {
                        Car car = customerRepository.GetCar(carId.Value);
                        price = product.CurrentServicePriceEntity(car.CarModelId.Value);
                    }
                    else
                    {
                        price = product.CurrentPriceEntity;
                    }
                    orderItems.Add(new OrderItem
                    {
                        Product = product,
                        Price = price,
                        Quantity = 1,
                        TotalPrice = price.Amount
                    });
                    Session.Remove("cart");
                    Session.Add("cart", orderItems);
                }

            }
            if (carId.HasValue)
            {
                ViewData["carId"] = carId;
                ViewData["carRegistrationNumber"] = customerRepository.GetCar(carId.Value).RegistrationNumber;
            }
            return PartialView("_ShoppingCart", (IEnumerable<OrderItem>)Session["cart"]);
        }
 partial void DeleteOrderItem(OrderItem instance);
 partial void UpdateOrderItem(OrderItem instance);
 partial void InsertOrderItem(OrderItem instance);
		private void detach_OrderItems(OrderItem entity)
		{
			this.SendPropertyChanging();
			entity.Order = null;
		}
		private void attach_OrderItems(OrderItem entity)
		{
			this.SendPropertyChanging();
			entity.Order = this;
		}