help 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. ListProduct.ItemsSource = ClassDBase.DB.Product.ToList()
  2. //Работа с заглушкой картинки:
  3. <Page.Resources>
  4. <BitmapImage x:Key="defaultImage" UriSource="/image/picture.png"/>
  5. </Page.Resources>
  6. <Image Width="100" Height="100" Source="{Binding ProductPhoto, TargetNullValue={StaticResource defaultImage}}"/>
  7. //Поиск, фильтрация, сортировка:
  8. void Filter()
  9. {
  10. List<Product> product = new List<Product>();
  11. product = ClassDBase.DB.Product.ToList();
  12. //Поиск по названию
  13. if (!string.IsNullOrWhiteSpace(SearchName.Text)) // Проверка пустую запись и запись состоящую из пробелов
  14. {
  15. product = product.Where(x => x.ProductName.ToLower().Contains(SearchName.Text.ToLower())).ToList();
  16. if (product.Count == 0)
  17. {
  18. MessageBox.Show("Записей с таким названием нет");
  19. SearchName.Text = "";
  20. }
  21. }
  22. //Фильтрация по размеру скидки
  23. switch (Filtering.SelectedIndex)
  24. {
  25. case 0:
  26. {
  27. product = product.ToList();
  28. }
  29. break;
  30. case 1:
  31. {
  32. product = product.Where(x => ((x.ProductDiscountAmount >= 0) && (x.ProductDiscountAmount < 10))).ToList();
  33. }
  34. break;
  35. case 2:
  36. {
  37. product = product.Where(x => ((x.ProductDiscountAmount >= 10) && (x.ProductDiscountAmount < 15))).ToList();
  38. }
  39. break;
  40. case 3:
  41. {
  42. product = product.Where(x => (x.ProductDiscountAmount>= 15)).ToList();
  43. }
  44. break;
  45. }
  46. //сортировка
  47. switch (Sorting.SelectedIndex)
  48. {
  49. case 0:
  50. {
  51. product.Sort((x, y) => x.ProductCost.CompareTo(y.ProductCost));
  52. }
  53. break;
  54. case 1:
  55. {
  56. product.Sort((x, y) => x.ProductCost.CompareTo(y.ProductCost));
  57. product.Reverse();
  58. }
  59. break;
  60. }
  61. ListProduct.ItemsSource = product;
  62. //количество записей на странице
  63. if (product.Count == 0)
  64. {
  65. MessageBox.Show("нет записей");
  66. CountProduct.Text = ClassDBase.DB.Product.ToList().Count + "/" + ClassDBase.DB.Product.ToList().Count;
  67. SearchName.Text = "";
  68. Sorting.SelectedIndex = 0;
  69. Filtering.SelectedIndex = 0;
  70. }
  71. CountProduct.Text = product.Count + "/" + ClassDBase.DB.Product.ToList().Count;
  72. }
  73. // добавление в корзину
  74. // в классе с создание объекта бд
  75. public static List<Product> products = new List<Product>();
  76. // событие на правую кнопку мыши
  77. <Grid.ContextMenu>
  78. <ContextMenu>
  79. <MenuItem Header="Добавить к заказу" Click="MenuItem_Click" Uid="{Binding ProductArticleNumber}"/>
  80. </ContextMenu>
  81. </Grid.ContextMenu>
  82. private void MenuItem_Click(object sender, RoutedEventArgs e)
  83. {
  84. MenuItem mi = (MenuItem)sender;
  85. string index = mi.Uid;
  86. Product product = ClassDBase.DB.Product.FirstOrDefault(x => x.ProductArticleNumber == index);
  87. ClassDBase.products.Add(product);
  88. ShowOrders.Visibility = Visibility.Visible;
  89. }
  90. // обновление суммы товаров корзины
  91. private void ButonDelete_Click(object sender, RoutedEventArgs e)
  92. {
  93. Button Buton = (Button)sender;
  94. int index = Convert.ToInt32(Buton.Uid);
  95. ProductBasket productBasket = bascet.FirstOrDefault(x => x.product.ID == index);
  96. _ = bascet.Remove(productBasket);
  97. if (bascet.Count == 0)
  98. {
  99. Close();
  100. }
  101. lvProduct.Items.Refresh();
  102. calculateSummaAndDiscount();
  103. }
  104. /// <summary>
  105. /// Подсчёт суммы заказа и скидок
  106. /// </summary>
  107. private void calculateSummaAndDiscount()
  108. {
  109. summa = 0;
  110. summaDiscount = 0;
  111. foreach (ProductBasket productBasket in bascet)
  112. {
  113. summa += productBasket.count * productBasket.product.costWithDiscount;
  114. summaDiscount += productBasket.count * ((double)productBasket.product.ProductCost - productBasket.product.costWithDiscount);
  115. }
  116. TBSumma.Text = "Сумма заказа: " + summa.ToString("0.00") + " руб.";
  117. TBSummaDiscount.Text = "Сумма скидки: " + summaDiscount.ToString("0.00") + " руб.";
  118. }
  119. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  120. {
  121. TextBox TB = (TextBox)sender;
  122. int index = Convert.ToInt32(TB.Uid);
  123. ProductBasket productBasket = bascet.FirstOrDefault(x => x.product.ID == index);
  124. productBasket.count = TB.Text.Replace(" ", "") == "" ? 0 : Convert.ToInt32(TB.Text);
  125. if (productBasket.count == 0) // Если колличество 0, то продукт из корзины удаляется
  126. {
  127. _ = bascet.Remove(productBasket);
  128. }
  129. if (bascet.Count == 0) // Если в корзине нет товаров, то окно закрывается
  130. {
  131. Close();
  132. }
  133. lvProduct.Items.Refresh();
  134. calculateSummaAndDiscount();
  135. }
  136. // вывод на всякий
  137. <ListView Grid.Row="3" Name="ListProduct" Height="300" ScrollViewer.HorizontalScrollBarVisibility="Hidden" HorizontalContentAlignment="Stretch">
  138. <ListView.ItemTemplate>
  139. <DataTemplate>
  140. <Border Style="{StaticResource BorderStyle}">
  141. <Grid Height="150" VerticalAlignment="Center" Background="{Binding Brush}">
  142. <Grid.ContextMenu>
  143. <ContextMenu>
  144. <MenuItem Header="Добавить к заказу" Click="MenuItem_Click" Uid="{Binding ProductArticleNumber}"/>
  145. </ContextMenu>
  146. </Grid.ContextMenu>
  147. <Grid.ColumnDefinitions>
  148. <ColumnDefinition Width="300"/>
  149. <ColumnDefinition/>
  150. <ColumnDefinition/>
  151. </Grid.ColumnDefinitions>
  152. <Grid Grid.Column="0" >
  153. <Image Width="100" Height="100" Source="{Binding ProductPhoto, TargetNullValue={StaticResource defaultImage}}"/>
  154. </Grid>
  155. <Grid Grid.Column="1">
  156. <Grid>
  157. <StackPanel>
  158. <StackPanel Orientation="Horizontal">
  159. <TextBlock Text="Название:"/>
  160. <TextBlock Name="product" Text="{Binding ProductName}" FontWeight="Bold" Style="{StaticResource TextList}"></TextBlock>
  161. </StackPanel>
  162. <StackPanel Orientation="Horizontal">
  163. <TextBlock Text="Описание:"/>
  164. <TextBlock Name="Description" Text="{Binding ProductDescription}" Style="{StaticResource TextList}"></TextBlock>
  165. </StackPanel>
  166. <StackPanel Orientation="Horizontal">
  167. <TextBlock Text="Производитель:"/>
  168. <TextBlock Name="Manufacturer" Text="{Binding Manufacturer.NameProductManufacturer}" Style="{StaticResource TextList}"></TextBlock>
  169. </StackPanel>
  170. <StackPanel Orientation="Horizontal">
  171. <TextBlock Text="Цена:"/>
  172. <TextBlock Name="Price" Text="{Binding Price}" Style="{StaticResource TextList}"></TextBlock>
  173. <TextBlock Name="Cost" Text="{Binding Cost}" TextDecorations="Strikethrough" Style="{StaticResource TextList}"></TextBlock>
  174. </StackPanel>
  175. </StackPanel>
  176. </Grid>
  177. </Grid>
  178. <Grid Grid.Column="2">
  179. <Grid.RowDefinitions>
  180. <RowDefinition Height="1*"/>
  181. <RowDefinition Height="1*"/>
  182. </Grid.RowDefinitions>
  183. <StackPanel Orientation="Horizontal" Grid.Row="0">
  184. <TextBlock Name="Discount" Text="{Binding Discount}" Style="{StaticResource TextList}"></TextBlock>
  185. </StackPanel>
  186. <Button Grid.Row="1" Name="Delete" Visibility="Collapsed" Content="Удалить" Click="Delete_Click" Uid="{Binding ProductArticleNumber}" Loaded="Delete_Loaded" Template="{StaticResource ButtonInElement}"/>
  187. </Grid>
  188. </Grid>
  189. </Border>
  190. </DataTemplate>
  191. </ListView.ItemTemplate>
  192. </ListView>
  193. // частичный класс
  194. public partial class Order
  195. {
  196. public string FullName
  197. {
  198. get
  199. {
  200. return User.UserSurname + " " + User.UserName + " " + User.UserPatronymic;
  201. }
  202. }
  203. public string OrderComposition
  204. {
  205. get
  206. {
  207. string str = "Состав заказа: ";
  208. List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
  209. foreach (OrderProduct prod in orderProducts)
  210. {
  211. Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
  212. str += product.ProductName + " (" + product.ProductArticleNumber + "), ";
  213. }
  214. str = str.Substring(0, str.Length - 2);
  215. return str;
  216. }
  217. }
  218. public string OrderAmountPrint
  219. {
  220. get
  221. {
  222. double resultCost = 0;
  223. List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
  224. foreach (OrderProduct prod in orderProducts)
  225. {
  226. Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
  227. resultCost += product.CostOrder;
  228. }
  229. return "Стоимость заказа: " + resultCost;
  230. }
  231. }
  232. public double OrderAmount
  233. {
  234. get
  235. {
  236. double resultCost = 0;
  237. List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
  238. foreach (OrderProduct orderProduct in orderProducts)
  239. {
  240. Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == orderProduct.ProductArticleNumber);
  241. resultCost += product.CostOrder;
  242. }
  243. return resultCost;
  244. }
  245. }
  246. public string DiscountOrderPrint
  247. {
  248. get
  249. {
  250. double discount = 0;
  251. double withoutDiscount = 0;
  252. double newAmount = 0;
  253. List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
  254. foreach (OrderProduct prod in orderProducts)
  255. {
  256. Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
  257. withoutDiscount += (double)product.ProductCost;
  258. newAmount += product.CostOrder;
  259. }
  260. discount = 100 - 100 * newAmount / withoutDiscount;
  261. return "Общая скидка: " + discount + "%";
  262. }
  263. }
  264. public double DiscountOrder
  265. {
  266. get
  267. {
  268. double discount = 0;
  269. double withoutDiscount = 0;
  270. double newAmount = 0;
  271. List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
  272. foreach (OrderProduct prod in orderProducts)
  273. {
  274. Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
  275. withoutDiscount += (double)product.ProductCost;
  276. newAmount += product.CostOrder;
  277. }
  278. discount = 100 - 100 * newAmount / withoutDiscount;
  279. return discount;
  280. }
  281. }
  282. public SolidColorBrush ColorOrder
  283. {
  284. get
  285. {
  286. bool moreThree = false;
  287. bool zero = false;
  288. List<OrderProduct> orderProducts = ClassDBase.DB.OrderProduct.Where(x => x.OrderID == OrderID).ToList();
  289. foreach (OrderProduct prod in orderProducts)
  290. {
  291. Product product = ClassDBase.DB.Product.FirstOrDefault(z => z.ProductArticleNumber == prod.ProductArticleNumber);
  292. if (product.ProductQuantityInStock > 3)
  293. {
  294. moreThree = true;
  295. }
  296. else
  297. {
  298. moreThree = false;
  299. if (product.ProductQuantityInStock == 0)
  300. {
  301. moreThree = false;
  302. zero = true;
  303. goto met;
  304. }
  305. }
  306. }
  307. met: if (moreThree == true)
  308. {
  309. return (SolidColorBrush)new BrushConverter().ConvertFrom("#20b2aa");
  310. }
  311. else if (zero == true)
  312. {
  313. return (SolidColorBrush)new BrushConverter().ConvertFrom("#ff8c00");
  314. }
  315. else
  316. {
  317. return Brushes.White;
  318. }
  319. }
  320. }
  321. }
  322. // и еще один
  323. public partial class Product
  324. {
  325. public SolidColorBrush Brush
  326. {
  327. get
  328. {
  329. var brushConverter = new BrushConverter();
  330. if (ProductDiscountAmount < 15 || ProductDiscountAmount == null)
  331. {
  332. return Brushes.White;
  333. }
  334. else
  335. {
  336. return (SolidColorBrush)(Brush)brushConverter.ConvertFrom("#7fff00");
  337. }
  338. }
  339. }
  340. public string Price
  341. {
  342. get
  343. {
  344. if (ProductDiscountAmount != null)
  345. {
  346. double b = (Convert.ToDouble(ProductCost) / 100) * (100 - ProductDiscountAmount.Value);
  347. return b + "";
  348. }
  349. else
  350. {
  351. double a = Convert.ToDouble(ProductCost);
  352. return a + "";
  353. }
  354. }
  355. }
  356. public string Cost
  357. {
  358. get
  359. {
  360. if (ProductDiscountAmount != null)
  361. {
  362. return Math.Round(Convert.ToDouble(ProductCost)) + " ";
  363. }
  364. return "";
  365. }
  366. }
  367. public double PriceOrder
  368. {
  369. get
  370. {
  371. if (ProductDiscountAmount != null)
  372. {
  373. double b = (Convert.ToDouble(ProductCost) / 100) * (100 - ProductDiscountAmount.Value);
  374. return b;
  375. }
  376. else
  377. {
  378. double a = Convert.ToDouble(ProductCost);
  379. return a;
  380. }
  381. }
  382. }
  383. public double CostOrder
  384. {
  385. get
  386. {
  387. if (ProductDiscountAmount != null)
  388. {
  389. return Math.Round(Convert.ToDouble(ProductCost));
  390. }
  391. return 0;
  392. }
  393. }
  394. public string Discount
  395. {
  396. get
  397. {
  398. return ProductDiscountAmount + "%";
  399. }
  400. }
  401. }