123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- //Работа с заглушкой картинки:
- <Page.Resources>
- <BitmapImage x:Key="defaultImage" UriSource="/image/picture.png"/>
- </Page.Resources>
- <Image Width="100" Height="100" Source="{Binding ProductPhoto, TargetNullValue={StaticResource defaultImage}}"/>
- //Поиск, фильтрация, сортировка:
- void Filter()
- {
- List<Product> product = new List<Product>();
- product = ClassDBase.DB.Product.ToList();
- //Поиск по названию
- if (!string.IsNullOrWhiteSpace(SearchName.Text)) // Проверка пустую запись и запись состоящую из пробелов
- {
- product = product.Where(x => x.ProductName.ToLower().Contains(SearchName.Text.ToLower())).ToList();
- if (product.Count == 0)
- {
- MessageBox.Show("Записей с таким названием нет");
- SearchName.Text = "";
- }
- }
- //Фильтрация по размеру скидки
- switch (Filtering.SelectedIndex)
- {
- case 0:
- {
- product = product.ToList();
- }
- break;
- case 1:
- {
- product = product.Where(x => ((x.ProductDiscountAmount >= 0) && (x.ProductDiscountAmount < 10))).ToList();
- }
- break;
- case 2:
- {
- product = product.Where(x => ((x.ProductDiscountAmount >= 10) && (x.ProductDiscountAmount < 15))).ToList();
- }
- break;
- case 3:
- {
- product = product.Where(x => (x.ProductDiscountAmount>= 15)).ToList();
- }
- break;
-
- }
- //сортировка
- switch (Sorting.SelectedIndex)
- {
- case 0:
- {
- product.Sort((x, y) => x.ProductCost.CompareTo(y.ProductCost));
- }
- break;
- case 1:
- {
- product.Sort((x, y) => x.ProductCost.CompareTo(y.ProductCost));
- product.Reverse();
- }
- break;
- }
- ListProduct.ItemsSource = product;
-
- //количество записей на странице
- if (product.Count == 0)
- {
- MessageBox.Show("нет записей");
- CountProduct.Text = ClassDBase.DB.Product.ToList().Count + "/" + ClassDBase.DB.Product.ToList().Count;
- SearchName.Text = "";
- Sorting.SelectedIndex = 0;
- Filtering.SelectedIndex = 0;
- }
- CountProduct.Text = product.Count + "/" + ClassDBase.DB.Product.ToList().Count;
- }
- // добавление в корзину
- // в классе с создание объекта бд
- public static List<Product> products = new List<Product>();
- // событие на правую кнопку мыши
- <Grid.ContextMenu>
- <ContextMenu>
- <MenuItem Header="Добавить к заказу" Click="MenuItem_Click" Uid="{Binding ProductArticleNumber}"/>
- </ContextMenu>
- </Grid.ContextMenu>
- private void MenuItem_Click(object sender, RoutedEventArgs e)
- {
- MenuItem mi = (MenuItem)sender;
- string index = mi.Uid;
- Product product = ClassDBase.DB.Product.FirstOrDefault(x => x.ProductArticleNumber == index);
- ClassDBase.products.Add(product);
- ShowOrders.Visibility = Visibility.Visible;
- }
- // обновление суммы товаров корзины
- private void ButonDelete_Click(object sender, RoutedEventArgs e)
- {
- Button Buton = (Button)sender;
- int index = Convert.ToInt32(Buton.Uid);
- ProductBasket productBasket = bascet.FirstOrDefault(x => x.product.ID == index);
- _ = bascet.Remove(productBasket);
- if (bascet.Count == 0)
- {
- Close();
- }
- lvProduct.Items.Refresh();
- calculateSummaAndDiscount();
- }
- /// <summary>
- /// Подсчёт суммы заказа и скидок
- /// </summary>
- private void calculateSummaAndDiscount()
- {
- summa = 0;
- summaDiscount = 0;
- foreach (ProductBasket productBasket in bascet)
- {
- summa += productBasket.count * productBasket.product.costWithDiscount;
- summaDiscount += productBasket.count * ((double)productBasket.product.ProductCost - productBasket.product.costWithDiscount);
- }
- TBSumma.Text = "Сумма заказа: " + summa.ToString("0.00") + " руб.";
- TBSummaDiscount.Text = "Сумма скидки: " + summaDiscount.ToString("0.00") + " руб.";
- }
- private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- TextBox TB = (TextBox)sender;
- int index = Convert.ToInt32(TB.Uid);
- ProductBasket productBasket = bascet.FirstOrDefault(x => x.product.ID == index);
- productBasket.count = TB.Text.Replace(" ", "") == "" ? 0 : Convert.ToInt32(TB.Text);
- if (productBasket.count == 0) // Если колличество 0, то продукт из корзины удаляется
- {
- _ = bascet.Remove(productBasket);
- }
- if (bascet.Count == 0) // Если в корзине нет товаров, то окно закрывается
- {
- Close();
- }
- lvProduct.Items.Refresh();
- calculateSummaAndDiscount();
- }
- // вывод на всякий
- <ListView Grid.Row="3" Name="ListProduct" Height="300" ScrollViewer.HorizontalScrollBarVisibility="Hidden" HorizontalContentAlignment="Stretch">
- <ListView.ItemTemplate>
- <DataTemplate>
- <Border Style="{StaticResource BorderStyle}">
- <Grid Height="150" VerticalAlignment="Center" Background="{Binding Brush}">
- <Grid.ContextMenu>
- <ContextMenu>
- <MenuItem Header="Добавить к заказу" Click="MenuItem_Click" Uid="{Binding ProductArticleNumber}"/>
- </ContextMenu>
- </Grid.ContextMenu>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="300"/>
- <ColumnDefinition/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Grid Grid.Column="0" >
- <Image Width="100" Height="100" Source="{Binding ProductPhoto, TargetNullValue={StaticResource defaultImage}}"/>
- </Grid>
- <Grid Grid.Column="1">
- <Grid>
- <StackPanel>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="Название:"/>
- <TextBlock Name="product" Text="{Binding ProductName}" FontWeight="Bold" Style="{StaticResource TextList}"></TextBlock>
- </StackPanel>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="Описание:"/>
- <TextBlock Name="Description" Text="{Binding ProductDescription}" Style="{StaticResource TextList}"></TextBlock>
- </StackPanel>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="Производитель:"/>
- <TextBlock Name="Manufacturer" Text="{Binding Manufacturer.NameProductManufacturer}" Style="{StaticResource TextList}"></TextBlock>
- </StackPanel>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="Цена:"/>
- <TextBlock Name="Price" Text="{Binding Price}" Style="{StaticResource TextList}"></TextBlock>
- <TextBlock Name="Cost" Text="{Binding Cost}" TextDecorations="Strikethrough" Style="{StaticResource TextList}"></TextBlock>
- </StackPanel>
- </StackPanel>
- </Grid>
- </Grid>
- <Grid Grid.Column="2">
- <Grid.RowDefinitions>
- <RowDefinition Height="1*"/>
- <RowDefinition Height="1*"/>
- </Grid.RowDefinitions>
- <StackPanel Orientation="Horizontal" Grid.Row="0">
- <TextBlock Name="Discount" Text="{Binding Discount}" Style="{StaticResource TextList}"></TextBlock>
- </StackPanel>
- <Button Grid.Row="1" Name="Delete" Visibility="Collapsed" Content="Удалить" Click="Delete_Click" Uid="{Binding ProductArticleNumber}" Loaded="Delete_Loaded" Template="{StaticResource ButtonInElement}"/>
- </Grid>
- </Grid>
- </Border>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- // частичный класс
- public partial class Order
- {
- public string FullName
- {
- get
- {
- return User.UserSurname + " " + User.UserName + " " + User.UserPatronymic;
- }
- }
-
- public string OrderComposition
- {
- get
- {
- string str = "Состав заказа: ";
- List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
- foreach (OrderProduct prod in orderProducts)
- {
- Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
- str += product.ProductName + " (" + product.ProductArticleNumber + "), ";
- }
- str = str.Substring(0, str.Length - 2);
- return str;
- }
- }
- public string OrderAmountPrint
- {
- get
- {
- double resultCost = 0;
- List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
- foreach (OrderProduct prod in orderProducts)
- {
- Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
- resultCost += product.CostOrder;
- }
- return "Стоимость заказа: " + resultCost;
- }
- }
- public double OrderAmount
- {
- get
- {
- double resultCost = 0;
- List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
- foreach (OrderProduct orderProduct in orderProducts)
- {
- Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == orderProduct.ProductArticleNumber);
- resultCost += product.CostOrder;
- }
- return resultCost;
- }
- }
- public string DiscountOrderPrint
- {
- get
- {
- double discount = 0;
- double withoutDiscount = 0;
- double newAmount = 0;
- List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
- foreach (OrderProduct prod in orderProducts)
- {
- Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
- withoutDiscount += (double)product.ProductCost;
- newAmount += product.CostOrder;
- }
- discount = 100 - 100 * newAmount / withoutDiscount;
- return "Общая скидка: " + discount + "%";
- }
- }
- public double DiscountOrder
- {
- get
- {
- double discount = 0;
- double withoutDiscount = 0;
- double newAmount = 0;
- List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
- foreach (OrderProduct prod in orderProducts)
- {
- Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
- withoutDiscount += (double)product.ProductCost;
- newAmount += product.CostOrder;
- }
- discount = 100 - 100 * newAmount / withoutDiscount;
- return discount;
- }
- }
- public SolidColorBrush ColorOrder
- {
- get
- {
- bool moreThree = false;
- bool zero = false;
- List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
- foreach (OrderProduct prod in orderProducts)
- {
- Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
- if (product.ProductQuantityInStock > 3)
- {
- moreThree = true;
- }
- else
- {
- moreThree = false;
- if (product.ProductQuantityInStock == 0)
- {
- moreThree = false;
- zero = true;
- goto met;
- }
- }
- }
- met: if (moreThree == true)
- {
- return (SolidColorBrush)new BrushConverter().ConvertFrom("#20b2aa");
- }
- else if (zero == true)
- {
- return (SolidColorBrush)new BrushConverter().ConvertFrom("#ff8c00");
- }
- else
- {
- return Brushes.White;
- }
- }
- }
- }
-
- // и еще один
- public partial class Product
- {
- public SolidColorBrush Brush
- {
- get
- {
- var brushConverter = new BrushConverter();
- if (ProductDiscountAmount < 15 || ProductDiscountAmount == null)
- {
- return Brushes.White;
- }
- else
- {
- return (SolidColorBrush)(Brush)brushConverter.ConvertFrom("#7fff00");
- }
- }
- }
- public string Price
- {
- get
- {
- if (ProductDiscountAmount != null)
- {
- double b = (Convert.ToDouble(ProductCost) / 100) * (100 - ProductDiscountAmount.Value);
- return b + "";
- }
- else
- {
- double a = Convert.ToDouble(ProductCost);
- return a + "";
- }
- }
- }
- public string Cost
- {
- get
- {
- if (ProductDiscountAmount != null)
- {
-
- return Math.Round(Convert.ToDouble(ProductCost)) + " ";
- }
- return "";
- }
- }
- public double PriceOrder
- {
- get
- {
- if (ProductDiscountAmount != null)
- {
- double b = (Convert.ToDouble(ProductCost) / 100) * (100 - ProductDiscountAmount.Value);
- return b;
- }
- else
- {
- double a = Convert.ToDouble(ProductCost);
- return a;
- }
- }
- }
- public double CostOrder
- {
- get
- {
- if (ProductDiscountAmount != null)
- {
- return Math.Round(Convert.ToDouble(ProductCost));
- }
- return 0;
- }
- }
- public string Discount
- {
- get
- {
- return ProductDiscountAmount + "%";
- }
- }
- }
|