OrderCard.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using Sessioa.BaseModel;
  2. using Sessioa.Classes;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. namespace Sessioa.Pages
  9. {
  10. /// <summary>
  11. /// Логика взаимодействия для OrderCard.xaml
  12. /// </summary>
  13. public partial class OrderCard : Page
  14. {
  15. List<Product> products = (List<Product>)Application.Current.Properties["ListOrderUser"];
  16. List<PointsOfIssue> points = BaseClass.C_Base.PointsOfIssue.ToList();
  17. List<StatusOrder> statesOrder = BaseClass.C_Base.StatusOrder.ToList();
  18. Dictionary<string, int> keyValuePairs = (Dictionary<string, int>)(Application.Current.Properties["ListOrderCountUser"]);
  19. User user = null;
  20. Order orderEdit = null;
  21. bool state = false;
  22. public OrderCard(User userSign)
  23. {
  24. InitializeComponent();
  25. user = userSign;
  26. LB_ProductsOrder.ItemsSource = products;
  27. OrderTitle.Text = $"Заказ #{BaseClass.C_Base.Order.OrderByDescending(x => x.OrderID).First().OrderID + 1}";
  28. if (keyValuePairs != null)
  29. {
  30. if (keyValuePairs.Count == 0)
  31. {
  32. foreach (Product product in products)
  33. {
  34. keyValuePairs.Add($"{product.ProductArticleNumber}", 1);
  35. }
  36. }
  37. }
  38. else
  39. {
  40. foreach (Product product in products)
  41. {
  42. try
  43. {
  44. keyValuePairs.Add($"{product.ProductArticleNumber}", 1);
  45. }
  46. catch { }
  47. }
  48. }
  49. CB_Punkt.ItemsSource = points;
  50. CB_Punkt.DisplayMemberPath = "PointName";
  51. CB_Punkt.SelectedValuePath = "PointID";
  52. ItogoSkidka();
  53. }
  54. public OrderCard(User userSign, int order_ID)
  55. {
  56. InitializeComponent();
  57. state = true;
  58. user = userSign;
  59. if (user.UserRole < 2)
  60. {
  61. B_Save.Visibility = Visibility.Collapsed;
  62. }
  63. orderEdit = BaseClass.C_Base.Order.FirstOrDefault(x => x.OrderID == order_ID);
  64. OrderTitle.Text = $"Заказ #{orderEdit.OrderID}";
  65. if (orderEdit != null)
  66. {
  67. foreach (OrderProduct op in BaseClass.C_Base.OrderProduct.Where(x => x.OrderID == orderEdit.OrderID).ToList())
  68. {
  69. products.Add(op.Product);
  70. try
  71. {
  72. keyValuePairs.Add($"{op.Product.ProductArticleNumber}", op.ProductCount);
  73. }
  74. catch { }
  75. }
  76. }
  77. LB_ProductsOrder.ItemsSource = products;
  78. B_Save.Content = "Сохранить";
  79. CB_Punkt.Visibility = Visibility.Collapsed;
  80. TB_Punkt.Visibility = Visibility.Visible;
  81. TB_Punkt.Text = $"{points[orderEdit.OrderPickupPoint - 1].PointName}";
  82. StateOrder.ItemsSource = statesOrder;
  83. StateOrder.DisplayMemberPath = "StatusTitle";
  84. StateOrder.SelectedValuePath = "StatusId";
  85. StateOrder.SelectedItem = statesOrder[BaseClass.C_Base.Order.FirstOrDefault(x => x.OrderID == order_ID).OrderStatus - 1];
  86. if (orderEdit.OrderDeliveryDate != null)
  87. DP_Order.SelectedDate = orderEdit.OrderDeliveryDate;
  88. OrderCode.Text = $"Код выдачи: {orderEdit.IssueCode}";
  89. ItogoSkidka();
  90. }
  91. private void countPr_Loaded(object sender, RoutedEventArgs e)
  92. {
  93. TextBlock textBlock = (TextBlock)sender;
  94. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  95. if (product.ProductDiscountAmount != null)
  96. textBlock.TextDecorations = TextDecorations.Strikethrough;
  97. }
  98. private void countDPr_Loaded(object sender, RoutedEventArgs e)
  99. {
  100. TextBlock textBlock = (TextBlock)sender;
  101. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  102. if (product.ProductDiscountAmount != null)
  103. {
  104. textBlock.Visibility = Visibility.Visible;
  105. textBlock.Text = $" {(double)(product.ProductCost - product.ProductCost * product.ProductDiscountAmount / 100)} ₽";
  106. }
  107. }
  108. private void Discount_Loaded(object sender, RoutedEventArgs e)
  109. {
  110. TextBlock textBlock = (TextBlock)sender;
  111. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  112. if (product.ProductDiscountAmount != null)
  113. {
  114. textBlock.Visibility = Visibility.Visible;
  115. textBlock.Text = $"Скидка {product.ProductDiscountAmount}%";
  116. }
  117. }
  118. private void B_Back_Click(object sender, RoutedEventArgs e)
  119. {
  120. if (state == false)
  121. {
  122. Application.Current.Properties["ListOrderCountUser"] = keyValuePairs;
  123. FrameClass.C_Frame.Navigate(new ProductsPage(user));
  124. }
  125. else
  126. {
  127. Application.Current.Properties["ListOrderCountUser"] = new Dictionary<string, int>();
  128. Application.Current.Properties["ListOrderUser"] = new List<Product>();
  129. FrameClass.C_Frame.Navigate(new OrderNew(user));
  130. }
  131. }
  132. private void B_Save_Click(object sender, RoutedEventArgs e)
  133. {
  134. if (state == false)
  135. {
  136. Application.Current.Properties["ListOrderUser"] = new List<Product>();
  137. Application.Current.Properties["ListOrderCountUser"] = new Dictionary<string, int>();
  138. if (CB_Punkt.SelectedIndex == -1)
  139. MessageBox.Show("Выберите пункт выдачи.", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Warning);
  140. else
  141. {
  142. try
  143. {
  144. Random rnd = new Random();
  145. int code = rnd.Next(100, 999);
  146. Order newOrder = new Order()
  147. {
  148. //OrderID = BaseClass.C_Base.Order.OrderByDescending(x => x.OrderID).FirstOrDefault().OrderID + 1,
  149. OrderStatus = 2,
  150. OrderDate = DateTime.Now,
  151. OrderPickupPoint = ((PointsOfIssue)CB_Punkt.SelectedItem).PointID,
  152. UserID = user.UserID,
  153. IssueCode = code
  154. };
  155. BaseClass.C_Base.Order.Add(newOrder);
  156. BaseClass.C_Base.SaveChanges();
  157. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  158. {
  159. OrderProduct op = new OrderProduct()
  160. {
  161. OrderID = newOrder.OrderID,
  162. ProductArticleNumber = pair.Key,
  163. ProductCount = pair.Value
  164. };
  165. BaseClass.C_Base.OrderProduct.Add(op);
  166. }
  167. BaseClass.C_Base.SaveChanges();
  168. MessageBoxResult res = MessageBox.Show($"Заказ оформлен. Код для выдачи: {code}. Отследить заказ можно в разделе \"Заказы\" на странице товаров.", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
  169. if (res == MessageBoxResult.OK)
  170. FrameClass.C_Frame.Navigate(new ProductsPage(user));
  171. }
  172. catch
  173. {
  174. MessageBoxResult res = MessageBox.Show("что-то сломалось.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  175. }
  176. }
  177. }
  178. else
  179. {
  180. orderEdit.OrderStatus = ((StatusOrder)StateOrder.SelectedItem).StatusId;
  181. if (DP_Order.SelectedDate != null)
  182. orderEdit.OrderDeliveryDate = DP_Order.SelectedDate;
  183. BaseClass.C_Base.SaveChanges();
  184. MessageBoxResult res = MessageBox.Show("Изменения внесены.", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
  185. FrameClass.C_Frame.Navigate(new OrderNew(user));
  186. }
  187. }
  188. private void Back_B_Click(object sender, RoutedEventArgs e)
  189. {
  190. Button bBack = (Button)sender;
  191. Grid grid = (Grid)bBack.Parent;
  192. TextBlock textBlock = (TextBlock)grid.FindName("CountProduct");
  193. Product pr = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == bBack.Uid);
  194. if (pr != null)
  195. {
  196. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  197. {
  198. if (pair.Key == bBack.Uid)
  199. {
  200. if (pair.Value > 1)
  201. {
  202. keyValuePairs[pair.Key]--;
  203. textBlock.Text = keyValuePairs[pair.Key].ToString();
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. ItogoSkidka();
  210. }
  211. private void Next_B_Click(object sender, RoutedEventArgs e)
  212. {
  213. Button bNext = (Button)sender;
  214. Grid grid = (Grid)bNext.Parent;
  215. TextBlock textBlock = (TextBlock)grid.FindName("CountProduct");
  216. Product pr = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == bNext.Uid);
  217. if (pr != null)
  218. {
  219. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  220. {
  221. if (pair.Key == bNext.Uid)
  222. {
  223. keyValuePairs[pair.Key]++;
  224. textBlock.Text = keyValuePairs[pair.Key].ToString();
  225. break;
  226. }
  227. }
  228. }
  229. ItogoSkidka();
  230. }
  231. private void CountProduct_Loaded(object sender, RoutedEventArgs e)
  232. {
  233. TextBlock tb = (TextBlock)sender;
  234. if (keyValuePairs.Count > 0)
  235. {
  236. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  237. {
  238. if (pair.Key == tb.Uid)
  239. {
  240. tb.Text = keyValuePairs[pair.Key].ToString();
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. public void ItogoSkidka()
  247. {
  248. decimal itogoBezSkidki = 0;
  249. foreach (Product item in products)
  250. {
  251. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  252. {
  253. if (pair.Key == item.ProductArticleNumber)
  254. {
  255. itogoBezSkidki += item.ProductCost * pair.Value;
  256. }
  257. }
  258. }
  259. decimal itogoSoSkidkoy = 0;
  260. foreach (Product item in products)
  261. {
  262. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  263. {
  264. if (pair.Key == item.ProductArticleNumber)
  265. {
  266. if (item.ProductDiscountAmount != null)
  267. {
  268. itogoSoSkidkoy += (item.ProductCost - (item.ProductCost * ((decimal)item.ProductDiscountAmount / 100))) * pair.Value;
  269. }
  270. else
  271. itogoSoSkidkoy += item.ProductCost * pair.Value;
  272. }
  273. }
  274. }
  275. decimal ITOGO = Math.Round((1 - (itogoSoSkidkoy / itogoBezSkidki)) * 100, 2);
  276. TB_ItogoSkidka.Text = $"Скидка {ITOGO}%";
  277. TB_Itogo.Text = $"{(double)itogoSoSkidkoy} ₽";
  278. }
  279. private void SP_EditOrder_Loaded(object sender, RoutedEventArgs e)
  280. {
  281. StackPanel sp = (StackPanel)sender;
  282. if (state == false || user.UserRole < 2)
  283. sp.Visibility = Visibility.Collapsed;
  284. }
  285. private void Back_B_Loaded(object sender, RoutedEventArgs e)
  286. {
  287. Button btn = (Button)sender;
  288. if (state == true)
  289. btn.Visibility = Visibility.Collapsed;
  290. }
  291. private void Next_B_Loaded(object sender, RoutedEventArgs e)
  292. {
  293. Button btn = (Button)sender;
  294. if (state == true)
  295. btn.Visibility = Visibility.Collapsed;
  296. }
  297. private void OrderCode_Loaded(object sender, RoutedEventArgs e)
  298. {
  299. TextBlock tb = (TextBlock)sender;
  300. if (state == false)
  301. tb.Visibility = Visibility.Collapsed;
  302. }
  303. private void DelProduct_Click(object sender, RoutedEventArgs e)
  304. {
  305. foreach(KeyValuePair<string, int> item in keyValuePairs)
  306. {
  307. Product tmp = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == ((Product)LB_ProductsOrder.SelectedItem).ProductArticleNumber);
  308. if(item.Key == tmp.ProductArticleNumber)
  309. {
  310. keyValuePairs.Remove(item.Key);
  311. products.Remove(tmp);
  312. break;
  313. }
  314. }
  315. LB_ProductsOrder.ItemsSource = null;
  316. LB_ProductsOrder.ItemsSource = products;
  317. }
  318. }
  319. }