Beispiel #1
0
        public async Task <ActionResult> Index(Models.CartDTO Model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(Model));
                }

                var ordersBLL      = new OrdersBLL();
                var restaurantsBLL = new RestaurantsBLL();

                // create order + rows
                var orderDetails = new Entities.OrderDetails {
                    ClientEmail   = Model.ClientEmail,
                    ClientName    = Model.ClientName,
                    ClientPhone   = Model.ClientPhone,
                    NameOnReceipt = Model.NameOnReceipt,
                    Notes         = Model.Notes,
                    OrderDate     = Model.OrderDate,
                    RestaurantId  = Model.RestaurantId,
                    Type          = Entities.eOrderType.TAKE_AWAY,
                    PaymentType   = Model.PaymentType,
                    Status        = Entities.eOrderStatus.NONE,
                    IsPaid        = false,
                    TableNumber   = Model.TableNumber
                };

                var menu = await restaurantsBLL.GetMenu(Model.RestaurantId);

                var menuIds   = Model.CartItemIds;
                var orderRows = menu.Items?.Join(menuIds, m => m.Id, idPair => idPair.ItemId, (m, idPair) => new { m, rowId = idPair.ItemRowId })?.Select(model =>
                {
                    var selectedProperties = Model.ItemPropertiesMap?.ContainsKey(model.rowId) ?? false ? Model.ItemPropertiesMap[model.rowId].Properties : "";
                    var isStarter          = Model.IsStarterMap?.ContainsKey(model.rowId) ?? false ? Model.IsStarterMap[model.rowId].IsStarter : false;

                    return(new Entities.OrderRow
                    {
                        IsDeal = false,
                        ItemName = model.m.Name,
                        ItemPrice = model.m.Price,
                        ItemId = model.m.Id,
                        Properties = selectedProperties,
                        IsStarter = isStarter
                    });
                })?.ToList();

                var sideDishesDB = new List <Entities.MenuItemSideDish>();
                if (Model.ItemSideDishMap != null)
                {
                    foreach (var sdm in Model.ItemSideDishMap)
                    {
                        var sd = await new MenuBLL().GetItemSideDishes(sdm.ItemId);
                        sideDishesDB.Add(sd.FirstOrDefault(x => x.SideDishId == sdm.SidedishId));
                    }
                    ;
                }

                var sideDishes = sideDishesDB?.Select(sd => {
                    return(new Entities.OrderRow
                    {
                        IsDeal = false,
                        ItemName = sd.SideDishName,
                        ItemPrice = sd.Price,
                        ItemId = sd.SideDishId,
                        Notes = "תוספת למנה עיקרית"
                    });
                })?.ToList();

                if ((sideDishes?.Count ?? 0) > 0)
                {
                    orderRows.AddRange(sideDishes);
                }

                var orderId = await ordersBLL.Create(new Entities.Order {
                    Details = orderDetails,
                    Rows    = orderRows
                });

                // send overload alert if needed
                await restaurantsBLL.NotifyOverload(Model.RestaurantId);

                if (Model.PaymentType == Entities.ePaymentType.CASH)
                {
                    // update order status
                    var orderUpdated = await ordersBLL.SaveStatus(orderId, Entities.eOrderStatus.APPROVED);

                    if (!orderUpdated)
                    {
                        LoggerSingleton.Instance.Info("Cardcom", "Save Order Status Failed", new List <string> {
                            $"#{orderId}", "Status: APPROVED"
                        });
                    }

                    return(RedirectToAction("PayThanks", new { Id = orderId }));
                }
                else
                {
                    return(RedirectToAction("Pay", new { Id = orderId }));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("CustomError", ex.Message);
                return(View(Model));
            }
        }
Beispiel #2
0
        public void InsertOrder()
        {
            var inOrder = new Order()
            {
                Address     = "test address",
                City        = "riga",
                Email       = "*****@*****.**",
                FirstName   = "maris",
                LastName    = "also maris",
                HouseNumber = "n1",
                Title       = "MR",
                ZipCode     = "1010",
            };

            inOrder.Products = new List <Product>()
            {
                new Product()
                {
                    Id    = "MD5something1",
                    Name  = "Test product1",
                    Price = 99m
                },

                new Product()
                {
                    Id    = "MD5something2",
                    Name  = "Test product2",
                    Price = 88m
                },

                new Product()
                {
                    Id    = "MD5something3",
                    Name  = "Test product3",
                    Price = 77m
                },
            };


            var bll  = new OrdersBLL();
            var guid = bll.Create(inOrder);

            var outOrder = bll.GetById(guid);


            Assert.IsNotNull(outOrder);
            Assert.IsNotNull(outOrder.Id);
            Assert.AreEqual(inOrder.Title, outOrder.Title);
            Assert.AreEqual(inOrder.FirstName, outOrder.FirstName);
            Assert.AreEqual(inOrder.LastName, outOrder.LastName);
            Assert.AreEqual(inOrder.Address, outOrder.Address);
            Assert.AreEqual(inOrder.City, outOrder.City);
            Assert.AreEqual(inOrder.ZipCode, outOrder.ZipCode);
            Assert.AreEqual(inOrder.HouseNumber, outOrder.HouseNumber);
            Assert.AreEqual(inOrder.Email, outOrder.Email);

            Assert.IsNotNull(outOrder.Products);
            Assert.IsTrue(outOrder.Products.Count() == 3);

            var inProduct  = inOrder.Products.OrderBy(p => p.Name).First();
            var outProduct = outOrder.Products.OrderBy(p => p.Name).First();

            Assert.IsNotNull(inProduct);
            Assert.AreEqual(inProduct.Id, outProduct.Id);
            Assert.AreEqual(inProduct.Name, outProduct.Name);
            Assert.AreEqual(inProduct.Price, outProduct.Price);
        }