Exemple #1
0
        private IList <Order> ConvertToOrders(string jsonString, out IList <Product> outOtStocks)
        {
            var orders = new List <Order>();

            outOtStocks = new List <Product>();
            var orderVMs = JsonConvert.DeserializeObject <IList <OrderViewModel> >(jsonString);

            foreach (var orderVM in orderVMs)
            {
                var order = new Order();
                order.StallId   = orderVM.StallId;
                order.StallName = orderVM.StallName;
                order.Stall     = Models.Stall.FindById(orderVM.StallId, _db);
                order.UserId    = CurrentUser.Id;

                //product
                foreach (var i in orderVM.Items)
                {
                    var product = Product.FindById(i.Id, _db);
                    if (product.Stock < i.Quantity)
                    {
                        //check stock
                        outOtStocks.Add(product);
                    }
                    order.Items.Add(new OrderItem(product)
                    {
                        Quantity = i.Quantity
                    });
                }

                //delivery
                order.DeliveryTimeStart = orderVM.DeliveryOrPickupOption.From;
                order.DeliveryTimeEnd   = orderVM.DeliveryOrPickupOption.To;
                if (!orderVM.DeliveryOrPickupOption.IsPickUp)
                {
                    var devAddr = DeliveryAddress.FindByCode(CurrentUser.Id, orderVM.DeliveryAddress.Code, _db);
                    order.Receiver        = string.Format("{0} {1}", devAddr.Name, devAddr.Mobile);
                    order.DeliveryAddress = devAddr.FullAddress;
                }
                else
                {
                    order.Receiver        = "PICK-UP";
                    order.DeliveryAddress = orderVM.DeliveryOrPickupOption.PickUpAddresses;
                }

                if (orderVM.DeliveryOrPickupOption.IsStoreDelivery)
                {
                    //owner delivery
                    var deliveryProduct = Product.FindById(order.Stall.DeliveryProductId, _db);

                    deliveryProduct.LineNote = orderVM.DeliveryOrPickupOption.ToString() + "\n" + order.Receiver + "\n" + order.DeliveryAddress;
                    deliveryProduct.Price    = orderVM.DeliveryOrPickupOption.Fee;

                    order.Items.Add(new OrderItem(deliveryProduct)
                    {
                        Quantity = 1
                    });
                }
                else
                {
                    //platform delivery
                    order.PlatformDelivery = orderVM.DeliveryOrPickupOption.Fee;
                }

                //note
                order.Note = orderVM.Note;

                //amount
                order.StallAmount = order.CalcTotal();

                //discount
                order.StallDiscount    = 0;
                order.PlatformDiscount = 0;

                //charge amount
                order.ChargeAmount = order.CalcTotalCharge();

                orders.Add(order);
            }


            return(orders);
        }