Esempio n. 1
0
 public static Orders Map(Library.Models.Orders orders) => new Orders
 {
     OrderId      = orders.Id,
     CustomerId   = orders.cutomerID,
     OrdDate      = orders.orderDate,
     OrdCost      = orders.CostTotal,
     RestaurantId = orders.restaurantID,
     OrderProduct = Map(orders.Products).ToList()
 };
 public static Models.Orders Map(Library.Models.Orders orders)
 {
     return(new Models.Orders {
         Id = orders.Id,
         TotalPrice = orders.TotalPrice,
         PlaceDate = DateTime.Now,
         StoreId = orders.StoreId
     });
 }
Esempio n. 3
0
 public static Entities.Orders MapSingleOrder(Library.Models.Orders orderItems)
 {
     return(new Entities.Orders
     {
         OrderId = orderItems.OrderId,
         OrderDate = orderItems.OrderDate,
         CustomerId = orderItems.CustomerId,
         LocationId = orderItems.LocationId
     });
 }
Esempio n. 4
0
 public static Orders LibOrdersMap(Library.Models.Orders orders)
 {
     return(new Orders()
     {
         OrderId = orders.OrderId,
         CustomerId = orders.Customer.customerID,
         LocationId = orders.StoreLocation.Id,
         Date = orders.OrderTime,
         TotalCost = orders.totalCost,
         Customer = MapLibCustomer(orders.Customer),
         Location = MapLibLocation(orders.StoreLocation),
         OrderLines = orders.OrderLines.Select(LibOrderLinesMap).ToList()
     });
 }
Esempio n. 5
0
 void OrderPizza(Library.Models.Pizza pizza, Library.Models.Orders ord)
 {
     Context.Orders.Add(Mapper.Map(ord));
     Context.SaveChanges();
 }
        /// <summary>
        /// Places order in database across 3 Tables:
        /// Orders, OrderItems and LocationStock.
        /// Adds order info to the Orders table.
        /// Add all OrderItems info to the OrderITems table.
        /// Updates LocationStock as needed (Depletes LocationStock quantities as needed)
        /// </summary>
        /// <param name="orders"></param>
        /// <param name="orderItems"></param>
        /// <returns>Boolean - true if everything was successful, false if there was an exception.</returns>
        bool IOrdersRepository.PlaceOrder(Library.Models.Orders orders, List <Library.Models.OrderItems> orderItems)
        {
            // Check stock
            var locationStock = from ls in _dbContext.LocationStock
                                where ls.LocationId == orders.LocationId
                                select ls;

            foreach (var item in orderItems)
            {
                foreach (var stockitem in locationStock)
                {
                    if (item.ProductId == stockitem.ProductId)
                    {
                        if (stockitem.Quantity < item.Quantity)
                        {
                            return(false);
                        }
                    }
                }
            }


            // Update stock
            foreach (var item in orderItems)
            {
                var CurrentItem = from ls in _dbContext.LocationStock
                                  where ((ls.LocationId == orders.LocationId) && (ls.ProductId == item.ProductId))
                                  select ls;
                if (CurrentItem.Count() != 1)
                {
                    return(false);
                }

                CurrentItem.First().Quantity -= (int)item.Quantity;
                try
                {
                    _dbContext.SaveChanges();
                    logger.Debug($"Stock updated successfully. | LocationId : {CurrentItem.First().LocationId} | ProductId : {item.ProductId}.");
                } catch (DbUpdateException ex)
                {
                    logger.Error($"Database Update Exception. | LocationId : {CurrentItem.First().LocationId}  | ProductId : {item.ProductId}: {ex}.");
                    return(false);
                }
            }

            // put order in db (Orders Table)
            var FinalOrder = Mapper.MapSingleOrder(orders);

            _dbContext.Orders.Add(FinalOrder);
            try
            {
                _dbContext.SaveChanges();
                logger.Debug($"Order created successfully. | OrderId : {FinalOrder.OrderId}.");
            }
            catch (DbUpdateException ex)
            {
                logger.Error($"Database Update Exception | Orders Tabe Order Id : {orders.OrderId}: {ex}.");
                return(false);
            }

            // put orderitems in db (OrderItems Table)
            var FinalOrderItems = orderItems.Select(Mapper.MapSingleOrderItems);

            foreach (var finalOrderItem in FinalOrderItems)
            {
                _dbContext.OrderItems.Add(finalOrderItem);
                try
                {
                    _dbContext.SaveChanges();
                    logger.Debug($"OrderItem created successfully. | OrderId : {FinalOrder.OrderId} | ProductId : {FinalOrder.OrderId}.");
                } catch (DbUpdateException ex)
                {
                    logger.Error($"Database Update Exception | Orders Tabe Order Id : {orders.OrderId}: {ex}.");
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 7
0
        public ActionResult PlaceOrder([FromQuery] string OrderId,
                                       string CustomerId,
                                       string FirstName,
                                       string LastName,
                                       string LocationId,
                                       string LocationName,
                                       string OrderDate,
                                       string PN2676,
                                       string PN4c99,
                                       string PN1765,
                                       string PNe7dc,
                                       string PN5526
                                       )
        {
            int?intLocationId        = (int?)(int.Parse(LocationId));
            var locationDetails      = _repository.GetLocationDetails(intLocationId);
            var customerDetails      = _repository.GetCustomerDetails(Guid.Parse(CustomerId));
            var locationStockDetails = _repository.GetLocationStockDetails(intLocationId);

            Library.Models.LocationOrderForm CurrentOrderDetails = new Library.Models.LocationOrderForm()
            {
                OrderId      = Guid.Parse(OrderId),
                OrderDate    = DateTime.Now,
                CustomerId   = Guid.Parse(CustomerId),
                LocationId   = intLocationId,
                FirstName    = customerDetails.First().FirstName,
                LastName     = customerDetails.First().LastName,
                LocationName = locationDetails.First().LocationName,

                LocationStock = new List <LocationStockDetails>(),
                Purchased     = new List <OrderDetailsItems>()
            };

            foreach (var item in locationStockDetails)
            {
                CurrentOrderDetails.LocationStock.Add(new LocationStockDetails()
                {
                    LocationId = item.LocationId,
                    ProductId  = item.ProductId,
                    Quantity   = item.Quantity,

                    ProductName = item.ProductName,
                    ProductDesc = item.ProductDesc
                });
            }

            Library.Models.Orders FinalOrder = new Library.Models.Orders()
            {
                OrderId    = Guid.Parse(OrderId),
                OrderDate  = DateTime.Now,
                CustomerId = Guid.Parse(CustomerId),
                LocationId = intLocationId,
            };

            foreach (var item in locationStockDetails)
            {
                string MappedQuantity = "";
                switch (item.ProductId.ToString().Substring(0, 4))
                {
                case "2676":
                    MappedQuantity = PN2676;
                    break;

                case "4c99":
                    MappedQuantity = PN4c99;
                    break;

                case "1765":
                    MappedQuantity = PN1765;
                    break;

                case "e7dc":
                    MappedQuantity = PNe7dc;
                    break;

                case "5526":
                    MappedQuantity = PN5526;
                    break;

                default:
                    MappedQuantity = "0";
                    break;
                }
                if (!(String.IsNullOrEmpty(MappedQuantity) || MappedQuantity == "0"))
                {
                    CurrentOrderDetails.Purchased.Add(new OrderDetailsItems()
                    {
                        OrderId   = Guid.Parse(OrderId),
                        ProductId = item.ProductId,
                        Quantity  = (int?)int.Parse(MappedQuantity)
                    });
                }
            }

            List <OrderItems> FinalOrderItems = CurrentOrderDetails.Purchased.Select(x => new OrderItems()
            {
                OrderId   = x.OrderId,
                ProductId = x.ProductId,
                Quantity  = x.Quantity
            }).ToList();



            Models.LocationOrderFormViewModel CurrentOrderDetailsViewModel = new Models.LocationOrderFormViewModel()
            {
                OrderId      = Guid.Parse(OrderId),
                OrderDate    = DateTime.Now,
                CustomerId   = Guid.Parse(CustomerId),
                LocationId   = intLocationId,
                FirstName    = customerDetails.First().FirstName,
                LastName     = customerDetails.First().LastName,
                LocationName = locationDetails.First().LocationName,

                LocationStock = new List <LocationStockDetailsViewModel>(),

                Purchased = new List <OrderDetailsItemsViewModel>()
            };

            foreach (var item in CurrentOrderDetails.LocationStock)
            {
                CurrentOrderDetailsViewModel.LocationStock.Add(new LocationStockDetailsViewModel()
                {
                    LocationId = item.LocationId,
                    ProductId  = item.ProductId,
                    Quantity   = item.Quantity,

                    ProductName = item.ProductName,
                    ProductDesc = item.ProductDesc
                });
            }

            foreach (var item in locationStockDetails)
            {
                string MappedQuantity = "";
                switch (item.ProductId.ToString().Substring(0, 4))
                {
                case "2676":
                    MappedQuantity = PN2676;
                    break;

                case "4c99":
                    MappedQuantity = PN4c99;
                    break;

                case "1765":
                    MappedQuantity = PN1765;
                    break;

                case "e7dc":
                    MappedQuantity = PNe7dc;
                    break;

                case "5526":
                    MappedQuantity = PN5526;
                    break;

                default:
                    MappedQuantity = "0";
                    break;
                }
                if (!(String.IsNullOrEmpty(MappedQuantity) || MappedQuantity == "0"))
                {
                    CurrentOrderDetailsViewModel.Purchased.Add(new OrderDetailsItemsViewModel()
                    {
                        OrderId   = Guid.Parse(OrderId),
                        ProductId = item.ProductId,
                        Quantity  = (int?)int.Parse(MappedQuantity),

                        ProductName = item.ProductName,
                        ProductDesc = item.ProductDesc
                    });
                }
            }

            if (_repository.PlaceOrder(FinalOrder, FinalOrderItems))
            {
                TempData["OrderCompleted"] = true;
            }
            else
            {
                TempData["OrderCompleted"] = false;
            }
            return(View(CurrentOrderDetailsViewModel));
        }