WindowOrderView.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using WriteErase.ClassBase;
  18. using WriteErase.ClassData;
  19. namespace WriteErase.ClassWindows
  20. {
  21. /// <summary>
  22. /// Логика взаимодействия для WindowOrderView.xaml
  23. /// </summary>
  24. public partial class WindowOrderView : Window,INotifyPropertyChanged
  25. {
  26. public WindowOrderView()
  27. {
  28. InitializeComponent();
  29. init();
  30. }
  31. private void init()
  32. {
  33. if(UserSession.user == null)
  34. btnNewOrder.Visibility = Visibility.Collapsed;
  35. cbPoint.ItemsSource = BaseConnection.kotBase.PickupPoint.ToList().Select(tb => tb.PickupPointAdress);
  36. lvProduct.ItemsSource = UserBasket.basket;
  37. calculateCost();
  38. }
  39. private void calculateCost()
  40. {
  41. tbkCountProduct.Text = "Количество товаров в корзине: " + UserBasket.basket.Count;
  42. double oldCost = 0;
  43. double newCost = 0;
  44. foreach (Product product in UserBasket.basket)
  45. {
  46. oldCost += (double)product.ProductCost * product.CountProduct;
  47. newCost += (double)product.Cost * product.CountProduct;
  48. }
  49. int discount = (int)Math.Round((1 - (newCost / oldCost)) * 100);
  50. if (oldCost == newCost)
  51. {
  52. tbkOldCost.Visibility = Visibility.Collapsed;
  53. tbkDiscount.Visibility = Visibility.Collapsed;
  54. }
  55. tbkDiscount.Text = discount.ToString() + " %";
  56. tbkOldCost.Text = string.Format("{0:C2}", oldCost);
  57. tbkNowCost.Text = string.Format("{0:C2}", newCost);
  58. if (UserBasket.basket.Count == 0)
  59. btnNewOrder.Visibility = Visibility.Collapsed;
  60. }
  61. private void cbPointTextChanged(object sender, TextChangedEventArgs e)
  62. {
  63. cbPoint.IsDropDownOpen = true;
  64. TextBox tb = (TextBox)e.OriginalSource;
  65. tb.Select(tb.SelectionStart + tb.SelectionLength, 0);
  66. CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(cbPoint.ItemsSource);
  67. cv.Filter = s => ((string)s).IndexOf(cbPoint.Text, StringComparison.CurrentCultureIgnoreCase) >= 0;
  68. }
  69. private void ProductDeleteClick(object sender, RoutedEventArgs e)
  70. {
  71. Button btn = (Button)sender;
  72. Product product = BaseConnection.kotBase.Product.FirstOrDefault(tb => tb.ProductArticleNumber == btn.Uid);
  73. UserBasket.basket.Remove(product);
  74. calculateCost();
  75. }
  76. private void ChangeCountClick(object sender, RoutedEventArgs e)
  77. {
  78. Button btn = (Button)sender;
  79. Product product = BaseConnection.kotBase.Product.FirstOrDefault(tb => tb.ProductArticleNumber == btn.Uid);
  80. switch (btn.Content)
  81. {
  82. case "+":
  83. {
  84. product.CountProduct += 1;
  85. break;
  86. }
  87. case "+10":
  88. {
  89. product.CountProduct += 10;
  90. break;
  91. }
  92. case "-":
  93. {
  94. product.CountProduct -= 1;
  95. break;
  96. }
  97. case "-10":
  98. {
  99. product.CountProduct -= 10;
  100. break;
  101. }
  102. }
  103. if(product.CountProduct <= 0)
  104. {
  105. product.CountProduct = 0;
  106. UserBasket.basket.Remove(product);
  107. }
  108. calculateCost();
  109. }
  110. public event PropertyChangedEventHandler PropertyChanged;
  111. public void OnPropertyChanged([CallerMemberName] string prop = "")
  112. {
  113. if (PropertyChanged != null)
  114. PropertyChanged(this, new PropertyChangedEventArgs(prop));
  115. }
  116. private void btnNewOrderClick(object sender, RoutedEventArgs e)
  117. {
  118. try
  119. {
  120. if (cbPoint.SelectedIndex >= 0 && UserBasket.basket.Count > 0)
  121. {
  122. Random rnd = new Random();
  123. int code;
  124. while (true)//генерируем код
  125. {
  126. code = rnd.Next(100, 1000);
  127. if (BaseConnection.kotBase.Order.Where(tb => tb.OrderCode == code).Count() == 0)
  128. break;
  129. }
  130. PickupPoint point = BaseConnection.kotBase.PickupPoint.First(tb => tb.PickupPointAdress == cbPoint.SelectedValue);
  131. Order newOrder = new Order()
  132. {
  133. OrderStatus = "Новый",
  134. OrderDate = DateTime.Now,
  135. OrderCode = code,
  136. OrderPickupPoint = point.PickupPointId,
  137. OrderClienr = UserSession.user.UserSurname + " " + UserSession.user.UserName + " " + UserSession.user.UserPatronymic
  138. };
  139. BaseConnection.kotBase.Order.Add(newOrder);
  140. BaseConnection.kotBase.SaveChanges();
  141. int count = 0;
  142. foreach (Product item in UserBasket.basket)
  143. {
  144. if (item.ProductQuantityInStock >= item.CountProduct)
  145. count++;
  146. OrderProduct op = new OrderProduct()
  147. {
  148. OrderID = newOrder.OrderID,
  149. ProductArticleNumber = item.ProductArticleNumber,
  150. ProductCount = item.CountProduct
  151. };
  152. BaseConnection.kotBase.OrderProduct.Add(op);
  153. }
  154. if (count > 3)
  155. newOrder.OrderDeliveryDate = DateTime.Now.AddDays(3);
  156. else
  157. newOrder.OrderDeliveryDate = DateTime.Now.AddDays(6);
  158. BaseConnection.kotBase.SaveChanges();
  159. MessageBox.Show("Заказ успешно добавлен\nКод получения: " + newOrder.OrderCode);
  160. UserBasket.basket.Clear();
  161. }
  162. else
  163. {
  164. if(cbPoint.SelectedIndex < 0) MessageBox.Show("Выберите пункт выдачи");
  165. if(UserBasket.basket.Count <= 0) MessageBox.Show("Количество товаров в корзине должно быть больше 1");
  166. }
  167. }
  168. catch(Exception ex)
  169. {
  170. Debug.WriteLine(ex.Message);
  171. }
  172. }
  173. }
  174. }