help 18 KB

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