Example #1
0
        public OperationResult <StallProductViewModel> GetProductById(string productId)
        {
            var result = new OperationResult <StallProductViewModel>(true);

            //get stall
            StallProductViewModel productVM = null;
            Product product = null;

            using (var db = new StallEntities())
            {
                product = Product.FindById(productId, db);
                if (product == null)
                {
                    result.Succeeded = false;
                    result.Message   = "Can not load products " + productId;
                    return(result);
                }

                productVM = new StallProductViewModel()
                {
                    Id             = product.Id,
                    Name           = product.BaseName,
                    Image          = product.ImageLarge,
                    Price          = product.PriceIncTax,
                    StallId        = product.StallId,
                    StallName      = product.Stall.StallName,
                    Description    = product.Description,
                    Stock          = product.Stock,
                    TrackInventory = product.TrackInventory
                };
            }

            result.Data = productVM;
            return(result);
        }
        public async void InventoryUpdate()
        {
            string data = null;

            try
            {
                data = await Request.Content.ReadAsStringAsync();

                var payload = data.Substring(data.IndexOf("&payload=") + 9);
                var jsonStr = System.Web.HttpUtility.UrlDecode(payload);

                //parse
                var vPI = JsonConvert.DeserializeObject <VendInventory>(jsonStr);
                using (var db = new StallEntities())
                {
                    Models.Product.SetInventoryById(vPI.ProductId, Convert.ToInt32(vPI.Count), db);
                }

                _sysLogger.Info(data);
            }
            catch (Exception ex)
            {
                _sysLogger.Info("failed to parse Inventory update date", ex);
                _sysLogger.Info(string.Format("Inventory update data: {0}", data));
            }
        }
Example #3
0
        public OperationResult <StallViewModel> Get(int id)
        {
            var result = new OperationResult <StallViewModel>(true);

            //get stall
            StallViewModel stallVM = null;

            Models.Stall stall = null;
            using (var db = new StallEntities())
            {
                stall = Models.Stall.FindById(id, db);
                if (stall == null)
                {
                    result.Succeeded = false;
                    result.Message   = "Can not load products for stall " + id;
                    return(result);
                }

                stallVM = new StallViewModel()
                {
                    Id              = stall.Id,
                    Name            = stall.StallName,
                    Categories      = stall.Categories,
                    ShowCategory    = stall.ShowCategory,
                    InitialProducts = new List <StallProductViewModel>()
                };
            }

            //initial products
            var initProducts = stall.SellingProducts;

            if (stall.ShowCategory)
            {
                //recommend
                initProducts = initProducts.Take(stall.RecommendNumber).ToList();
            }

            foreach (var p in initProducts)
            {
                if (p.Active == true && p.Stock > 0 && p.Price != null)
                {
                    stallVM.InitialProducts.Add(new StallProductViewModel()
                    {
                        Id             = p.Id,
                        Name           = p.BaseName,
                        Image          = p.Image,
                        Price          = p.PriceIncTax,
                        StallId        = p.StallId,
                        StallName      = p.Stall.StallName,
                        Description    = p.Description,
                        Stock          = p.Stock,
                        TrackInventory = p.TrackInventory
                    });
                }
            }

            result.Data = stallVM;
            return(result);
        }
Example #4
0
        static StallApplication()
        {
            var vendApp = new VendApplication(GreenspotConfiguration.AccessAccounts["vend"].Id,
                                              GreenspotConfiguration.AccessAccounts["vend"].Secret,
                                              GreenspotConfiguration.AccessAccounts["vend"].RedirectUri);

            var tokenManager = new AccessTokenManager(new VendAccessTokenStore(), vendApp);

            _instance = new StallApplication();
            _instance._vendApplication        = vendApp;
            _instance._vendAccessTokenManager = tokenManager;

            using (var db = new StallEntities())
            {
                _instance._areas = new Dictionary <string, Area>();
                foreach (var a in db.Areas.ToList())
                {
                    _instance._areas.Add(a.ID, a);
                }
            }
        }
        public async void ProductUpdate()
        {
            string data = null;

            try
            {
                data = await Request.Content.ReadAsStringAsync();

                var payload = data.Substring(data.IndexOf("&payload=") + 9);
                var jsonStr = System.Web.HttpUtility.UrlDecode(payload);

                //parse
                var vP = JsonConvert.DeserializeObject <VendProduct>(jsonStr);
                using (var db = new StallEntities())
                {
                    if (vP.DeletedAt != null)
                    {
                        //delete
                        Models.Product.DeleteById(vP.Id, db);
                    }
                    else
                    {
                        //new or update
                        var stall = Models.Stall.FindByRetailerIdAndSuppilerName(vP.RetailerId, vP.Supplier?.Name ?? vP.SupplierName, db);
                        if (stall != null)
                        {
                            await stall.UpdateProductById(vP.Id, db);
                        }
                    }
                }

                _sysLogger.Info(data);
            }
            catch (Exception ex)
            {
                _sysLogger.Info("failed to parse product update date", ex);
                _sysLogger.Info(string.Format("product update data: {0}", data));
            }
        }
Example #6
0
 public void InitTest()
 {
     _db = new StallEntities();
 }
Example #7
0
 public void InitTest()
 {
     _db = new StallEntities();
     AccessTokenContainer.Register(GreenspotConfiguration.AccessAccounts["wechat"].Id, GreenspotConfiguration.AccessAccounts["wechat"].Secret);
 }
Example #8
0
        public OperationResult <IList <DeliveryOptionCollectionViewModel> > GetDeliveryOptions(int id,
                                                                                               [FromUri] string country, [FromUri] string city, [FromUri] string suburb, [FromUri] string area, [FromUri] decimal orderAmount)
        {
            string areaStr = $"{country}:{city}:{area.Replace(' ', '-')}".ToLower();
            var    result  = new OperationResult <IList <DeliveryOptionCollectionViewModel> >(true)
            {
                Data = new List <DeliveryOptionCollectionViewModel>()
            };

            Models.Stall stall = null;

            //get stall
            using (var db = new StallEntities())
            {
                stall = Models.Stall.FindById(id, db);
                if (stall == null)
                {
                    result.Succeeded = false;
                    result.Message   = "Can not load delivery schedule for stall " + id;
                    return(result);
                }
            }

            //minimum delivery order amount
            if (stall.Setting.Delivery.MinOrderAmount != null &&
                orderAmount < stall.Setting.Delivery.MinOrderAmount)
            {
                return(result);
            }

            //
            var advDays = stall.Setting.MaxAdvancedOrderDays > StallApplication.Setting.MaxAdvancedOrderDays ?
                          StallApplication.Setting.MaxAdvancedOrderDays : stall.Setting.MaxAdvancedOrderDays;

            var advMins = stall.Setting.MinDeliveryAdvancedMinutes < StallApplication.Setting.MinDeliveryAdvancedMinutes ?
                          StallApplication.Setting.MinDeliveryAdvancedMinutes : stall.Setting.MinDeliveryAdvancedMinutes;


            //get distance
            var distance = stall.GetDistance(country, city, suburb);


            IList <DeliveryOrPickupOption> deliveryOpts;

            if (stall.Setting.Delivery.DeliveryType == Models.Settings.DeliveryTypes.StoreOnly)
            {
                //get store delivery
                deliveryOpts = stall.GetDeliveryOptions(DateTime.Now.AddMinutes(advMins), advDays, areaStr, distance, orderAmount);
            }
            else
            {
                //get platform
                deliveryOpts = StallApplication.GetDeliveryOptions(stall, DateTime.Now.AddMinutes(advMins), advDays, areaStr, distance, orderAmount);
            }


            DeliveryOptionCollectionViewModel collection = null;
            DateTime currDate = DateTime.MinValue.Date;

            foreach (var opt in deliveryOpts)
            {
                //free delivery
                if (stall.Setting.Delivery.FreeDeliveryOrderAmount != null &&
                    orderAmount >= stall.Setting.Delivery.FreeDeliveryOrderAmount)
                {
                    opt.Fee = 0;
                }

                var optObjs = opt.Divide();

                foreach (var optObj in optObjs)
                {
                    if (!optObj.IsApplicableToArea(areaStr))
                    {
                        //has suitable area = available
                        continue;
                    }

                    if (opt.From.Date != currDate)
                    {
                        //new date group
                        collection = new DeliveryOptionCollectionViewModel();
                        result.Data.Add(collection);
                        currDate = opt.From.Date;
                    }

                    //add
                    collection.Options.Add(optObj);
                }
            }

            return(result);
        }
Example #9
0
        public OperationResult <IList <StallProductViewModel> > ProductsByCategory(int id, int cateIdx = 0)
        {
            var result = new OperationResult <IList <StallProductViewModel> >(true)
            {
                Data = new List <StallProductViewModel>()
            };

            //get stall
            Models.Stall stall = null;
            using (var db = new StallEntities())
            {
                stall = Models.Stall.FindById(id, db);
                if (stall == null)
                {
                    result.Succeeded = false;
                    result.Message   = "Can not load products for stall " + id;
                    return(result);
                }
            }



            IList <Product> products;

            if (Category.Recommend.Index == cateIdx)
            {
                products = stall.SellingProducts.Take(stall.RecommendNumber).ToList();
            }
            else
            {
                string category = null;
                if (Category.Others.Index == cateIdx)
                {
                    category = Category.Others.Identifier;
                }
                else
                {
                    foreach (var c in stall.Categories)
                    {
                        if (c.Index == cateIdx)
                        {
                            category = c.Identifier;
                            break;
                        }
                    }
                }

                products = stall.GetProductsByCategory(category);
            }
            foreach (var p in products)
            {
                if (p.Active == true && p.Stock > 0 && p.Price != null)
                {
                    result.Data.Add(new StallProductViewModel()
                    {
                        Id             = p.Id,
                        Name           = p.BaseName,
                        Image          = p.Image,
                        Price          = p.PriceIncTax,
                        StallId        = p.StallId,
                        StallName      = p.Stall.StallName,
                        Description    = p.Description,
                        Stock          = p.Stock,
                        TrackInventory = p.TrackInventory
                    });
                }
            }

            return(result);
        }
Example #10
0
        public OperationResult <IList <PickupOptionGroupViewModel> > GetPickUpOptions(int id)
        {
            var result = new OperationResult <IList <PickupOptionGroupViewModel> >(true)
            {
                Data = new List <PickupOptionGroupViewModel>()
            };

            Models.Stall stall = null;

            //get stall
            using (var db = new StallEntities())
            {
                stall = Models.Stall.FindById(id, db);
                if (stall == null)
                {
                    result.Succeeded = false;
                    result.Message   = "Can not load delivery schedule for stall " + id;
                    return(result);
                }
            }



            var advDays = stall.Setting.MaxAdvancedOrderDays > StallApplication.Setting.MaxAdvancedOrderDays ?
                          StallApplication.Setting.MaxAdvancedOrderDays : stall.Setting.MaxAdvancedOrderDays;

            var advMins = stall.Setting.MinPickupAdvancedMinutes < StallApplication.Setting.MinPickupAdvancedMinutes ?
                          StallApplication.Setting.MinPickupAdvancedMinutes : stall.Setting.MinPickupAdvancedMinutes;

            //get pickup delivery
            var pickupOptions = stall.GetPickupOptions(DateTime.Now.AddMinutes(advMins), advDays);

            PickupOptionGroupViewModel addrGrp = null;
            PickupOptionGroupViewModel dateGrp = null;
            string   currAddr = null;
            DateTime currDate = DateTime.MinValue.Date;

            foreach (var opt in pickupOptions)
            {
                if (!opt.PickUpAddress.Equals(currAddr))
                {
                    //new address
                    addrGrp = new PickupOptionGroupViewModel()
                    {
                        Groups = new List <PickupOptionGroupViewModel>()
                    };
                    result.Data.Add(addrGrp);
                    currAddr = opt.PickUpAddress;
                    currDate = DateTime.MinValue.Date;
                }

                if (opt.From.Date != currDate)
                {
                    //new date group
                    dateGrp = new PickupOptionGroupViewModel();
                    addrGrp.Groups.Add(dateGrp);
                    currDate = opt.From.Date;
                }

                //add options
                if (opt.DivisionType == Models.Settings.TimeDivisionTypes.Undivisible)
                {
                    dateGrp.Options.Add(opt);
                }
                else
                {
                    ((List <DeliveryOrPickupOption>)dateGrp.Options).AddRange(opt.Divide());
                }
            }

            return(result);
        }