示例#1
0
        public static int GetProductCount(Product product, int selectSizeId, Dictionary <int, string> sizeDictionary)
        {
            if (product.productsAttributes.Count > 0 && selectSizeId == -1)
            {
                //OnePage.mainPage.DisplayMessage (MessageSize);
                return(-1);
            }
            int productCount;

            if (product.productsAttributes.Count > 0)
            {
                KeyValuePair <int, string> attributeKey = sizeDictionary.SingleOrDefault(g => g.Key == selectSizeId);
                if (attributeKey.Key == 0 && attributeKey.Value == null)
                {
                    return(-1);
                }
                ProductsAttributes attr = product.productsAttributes.SingleOrDefault(g => g.OptionValue.ID == attributeKey.Key);
                productCount = attr.Quantity;
            }
            else
            {
                productCount = product.Quantity;
            }

            return(productCount);
        }
示例#2
0
        public async void ShowBasketAsync()
        {
            basketList = new List <Basket>();
            if (User.Singleton != null)
            {
                try {
                    if (BasketDB.GetItems().Count > 0)
                    {
                        await User.LoadBasket();
                    }
                    basketList = await Basket.GetProductInBasketAsync(true);
                } catch (Exception) {
                    eventRefresh  = null;
                    eventRefresh += (sender, e) => {
                        Button content = sender as Button;
                        content.IsEnabled = false;
                        ShowBasketAsync();
                    };
                    Content = OnePage.mainPage.ShowMessageError(eventRefresh);
                    return;
                }

                try {                /// Нужно для определения минимальной суммы заказа. Для этого узнать были ли сделаны до этого заказы
                    User.Singleton.OrderStatusList = await OrderStatus.GetOrderStatusListAsync();
                } catch (Exception) {
                    if (eventRefresh == null)
                    {
                        eventRefresh += (sender, e) => {
                            Button content = sender as Button;
                            content.IsEnabled = false;
                            ShowBasketAsync();
                        }
                    }
                    ;
                    Content = OnePage.mainPage.ShowMessageError(eventRefresh);
                    return;
                }
            }
            List <Basket> basketFromDB = BasketDB.GetItemsToBasketList();

            if (basketFromDB != null)
            {
                int[]          idsProduct = basketFromDB.Select(g => g.ProductID).ToArray();
                List <Product> productsList;
                try {
                    productsList = await Product.GetProductsByIDsListAsync(idsProduct);
                } catch (Exception) {
                    if (eventRefresh == null)
                    {
                        eventRefresh += (sender, e) => {
                            Button content = sender as Button;
                            content.IsEnabled = false;
                            ShowBasketAsync();
                        }
                    }
                    ;
                    Content = OnePage.mainPage.ShowMessageError(eventRefresh);
                    return;
                }
                foreach (Basket basetBD in basketFromDB)
                {
                    Product product = productsList.FirstOrDefault(g => g.ProductsID == basetBD.ProductID);
                    if (product != null)
                    {
                        if (product.productsAttributes?.Count > 0)
                        {
                            ProductsAttributes productsAttribute = product.productsAttributes.FirstOrDefault(g => g.OptionValue.ID == basetBD.SizeValueId);
                            if (productsAttribute != null)
                            {
                                basetBD.ProductActualQuantity = productsAttribute.Quantity;
                            }
                        }
                        else
                        {
                            basetBD.ProductActualQuantity = product.Quantity;
                        }
                        basetBD.ProductsQuantityOrderMin   = product.ProductsQuantityOrderMin;
                        basetBD.ProductsQuantityOrderUnits = product.ProductsQuantityOrderUnits;
                        basetBD.ProductAvailable           = true;
                    }
                    else
                    {
                        basetBD.ProductAvailable = false;
                    }
                }
                basketList.AddRange(basketFromDB);
            }

            if (basketList == null || basketList.Count == 0)
            {
                Content = layoutProductZero;
                return;
            }

            countPage = (int)Math.Ceiling((double)basketList.Count / countProductPage);
            SetProductList();
            total = 0;
            notValidBasketList = new List <Basket>();
            foreach (var item in basketList)
            {
                if (!item.ProductAvailable)
                {
                    notValidBasketList.Add(item);
                    continue;
                }

                if (item.Quantity > item.ProductActualQuantity)
                {
                    if (!item.IsLocalBasket)
                    {
                        notValidBasketList.Add(item);
                    }
                    total += item.ProductActualQuantity * item.Price;
                }
                else
                {
                    total += item.Quantity * item.Price;
                }
            }
            //total = basketList.Sum (g => Math.Ceiling(g.Price) * g.Quantity);

            lblTotal = new Label {
                FontSize                = 24,
                TextColor               = ApplicationStyle.GreenColor,
                VerticalOptions         = LayoutOptions.CenterAndExpand,
                HorizontalOptions       = LayoutOptions.FillAndExpand,
                HorizontalTextAlignment = TextAlignment.Center,
                FontAttributes          = FontAttributes.Bold,
            };
            Button btnOrder = new Button {
                Text              = "Оформить",
                FontSize          = Utils.GetSize(18),
                TextColor         = Color.White,
                BackgroundColor   = ApplicationStyle.RedColor,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                BorderRadius      = 0,
            };

            btnOrder.Clicked += ClickCheckOut;

            Grid gridBottom = new Grid {
                HeightRequest     = Utils.GetSize(49),
                VerticalOptions   = LayoutOptions.FillAndExpand,
                ColumnDefinitions =
                {
                    new ColumnDefinition {
                        Width = new GridLength(50, GridUnitType.Star)
                    },
                    new ColumnDefinition {
                        Width = new GridLength(50, GridUnitType.Star)
                    },
                },
                BackgroundColor = Color.White,
            };

            MyPicker pickerAuthorization = new MyPicker {
                TextColor       = Color.Transparent,
                BackgroundColor = Color.Transparent,
                Items           =
                {
                    login,
                    registration,
                    "Отмена"
                }
            };

            pickerAuthorization.Unfocused += OnLoginClick;

            Grid gridPicker = new Grid();

            gridPicker.Children.Add(btnOrder, 0, 0);
            if (User.Singleton == null)
            {
                gridPicker.Children.Add(pickerAuthorization, 0, 0);
            }

            gridBottom.Children.Add(lblTotal, 0, 0);
            gridBottom.Children.Add(gridPicker, 1, 0);

            StackLayout layoutBottom = new StackLayout {
                VerticalOptions = LayoutOptions.FillAndExpand,
                Spacing         = 0,
                Children        =
                {
                    new BoxView(),
                    gridBottom
                }
            };

            mainGrid = new Grid {
                VerticalOptions = LayoutOptions.FillAndExpand,
                RowSpacing      = 0,
                RowDefinitions  =
                {
                    new RowDefinition {
                        Height = new GridLength(100, GridUnitType.Star)
                    },
                    new RowDefinition {
                        Height = Utils.GetSize(50)
                    },
                }
            };
            mainGrid.Children.Add(scrollProduct, 0, 0);
            mainGrid.Children.Add(layoutBottom, 0, 1);
            Content = mainGrid;

            if (total > 0)
            {
                lblTotal.Text = total + "р.";
            }
        }

        void OnGoToProduct(object sender, EventArgs e)
        {
            OnePage.redirectApp.RedirectToPage(PageName.Catalog, false, false);
        }

        void OnLoginClick(object sender, FocusEventArgs e)
        {
            Picker picker = sender as Picker;

            if (picker.SelectedIndex == -1)
            {
                return;
            }

            string doUser = picker.Items [picker.SelectedIndex];

            switch (doUser)
            {
            case login:
                Task.Run(() => {
                    Task.Delay(20);
                    Device.BeginInvokeOnMainThread(() => {
                        OnePage.redirectApp.RedirectToPage(PageName.Login, false, true);
                    });
                });
                break;

            case registration:
                Task.Run(() => {
                    Task.Delay(20);
                    Device.BeginInvokeOnMainThread(() => {
                        OnePage.redirectApp.RedirectToPage(PageName.Login, true, false);
                        OnePage.redirectApp.loginView.GoToPersonalData(true);
                    });
                });
                break;

            default:
                break;
            }
        }

        async void OnClickProduct(object sender, SelectedItemChangedEventArgs e)
        {
            if (e.SelectedItem == null)
            {
                return;
            }
            Basket basket = e.SelectedItem as Basket;

            if (!basket.ProductAvailable)
            {
                ListView entity = sender as ListView;
                entity.SelectedItem = null;
                return;
            }
            //await Task.Delay(500);
            Product product = await Product.GetProductsByIDAsync(basket.ProductID);

            OnePage.redirectApp.RedirectToPage(PageName.Catalog, true, true);
            OnePage.redirectApp.catalogView.GoToProductAsync(basket, product);
        }