OrderCard.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. Dictionary<string, int> keyValuePairs = (Dictionary<string, int>)(Application.Current.Properties["ListOrderCountUser"]);
  17. //{
  18. // {"djkdkla", 19 }
  19. //};
  20. User user = null;
  21. public OrderCard(User userSign)
  22. {
  23. InitializeComponent();
  24. user = userSign;
  25. LB_ProductsOrder.ItemsSource = products;
  26. if (keyValuePairs.Count == 0)
  27. {
  28. foreach (Product product in products)
  29. {
  30. keyValuePairs.Add($"{product.ProductArticleNumber}", 1);
  31. }
  32. }
  33. else
  34. {
  35. foreach (Product product in products)
  36. {
  37. try
  38. {
  39. keyValuePairs.Add($"{product.ProductArticleNumber}", 1);
  40. }
  41. catch { }
  42. }
  43. }
  44. ItogoSkidka();
  45. //keyValuePairs.Add("dawlk", 2718);
  46. //foreach(KeyValuePair<string, int> pair in keyValuePairs)
  47. //{
  48. // pair.Key; //1
  49. // pair.Value; //2
  50. //}
  51. }
  52. private void countPr_Loaded(object sender, RoutedEventArgs e)
  53. {
  54. TextBlock textBlock = (TextBlock)sender;
  55. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  56. if (product.ProductDiscountAmount != null)
  57. textBlock.TextDecorations = TextDecorations.Strikethrough;
  58. }
  59. private void countDPr_Loaded(object sender, RoutedEventArgs e)
  60. {
  61. TextBlock textBlock = (TextBlock)sender;
  62. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  63. if (product.ProductDiscountAmount != null)
  64. {
  65. textBlock.Visibility = Visibility.Visible;
  66. textBlock.Text = $" {(double)(product.ProductCost - product.ProductCost * product.ProductDiscountAmount / 100)} ₽";
  67. }
  68. }
  69. private void Discount_Loaded(object sender, RoutedEventArgs e)
  70. {
  71. TextBlock textBlock = (TextBlock)sender;
  72. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  73. if (product.ProductDiscountAmount != null)
  74. {
  75. textBlock.Visibility = Visibility.Visible;
  76. textBlock.Text = $"Скидка {product.ProductDiscountAmount}%";
  77. }
  78. }
  79. private void B_Back_Click(object sender, RoutedEventArgs e)
  80. {
  81. Application.Current.Properties["ListOrderCountUser"] = keyValuePairs;
  82. FrameClass.C_Frame.Navigate(new ProductsPage(user));
  83. }
  84. private void B_Save_Click(object sender, RoutedEventArgs e)
  85. {
  86. Application.Current.Properties["ListOrderUser"] = new List<Product>();
  87. Application.Current.Properties["ListOrderCountUser"] = new Dictionary<string, int>();
  88. MessageBoxResult mbr = MessageBox.Show("Заказ оформлен.", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
  89. if (mbr == MessageBoxResult.OK)
  90. FrameClass.C_Frame.Navigate(new ProductsPage(user));
  91. }
  92. private void Back_B_Click(object sender, RoutedEventArgs e)
  93. {
  94. Button bBack = (Button)sender;
  95. Grid grid = (Grid)bBack.Parent;
  96. TextBlock textBlock = (TextBlock)grid.FindName("CountProduct");
  97. Product pr = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == bBack.Uid);
  98. if (pr != null)
  99. {
  100. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  101. {
  102. if (pair.Key == bBack.Uid)
  103. {
  104. if (pair.Value > 1)
  105. {
  106. keyValuePairs[pair.Key]--;
  107. textBlock.Text = keyValuePairs[pair.Key].ToString();
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. ItogoSkidka();
  114. }
  115. private void Next_B_Click(object sender, RoutedEventArgs e)
  116. {
  117. Button bNext = (Button)sender;
  118. Grid grid = (Grid)bNext.Parent;
  119. TextBlock textBlock = (TextBlock)grid.FindName("CountProduct");
  120. Product pr = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == bNext.Uid);
  121. if (pr != null)
  122. {
  123. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  124. {
  125. if (pair.Key == bNext.Uid)
  126. {
  127. keyValuePairs[pair.Key]++;
  128. textBlock.Text = keyValuePairs[pair.Key].ToString();
  129. break;
  130. }
  131. }
  132. }
  133. ItogoSkidka();
  134. }
  135. private void CountProduct_Loaded(object sender, RoutedEventArgs e)
  136. {
  137. TextBlock tb = (TextBlock)sender;
  138. if (keyValuePairs.Count > 0)
  139. {
  140. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  141. {
  142. if (pair.Key == tb.Uid)
  143. {
  144. tb.Text = keyValuePairs[pair.Key].ToString();
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. public void ItogoSkidka()
  151. {
  152. decimal itogoBezSkidki = 0;
  153. foreach(Product item in products)
  154. {
  155. foreach(KeyValuePair<string, int> pair in keyValuePairs)
  156. {
  157. if(pair.Key == item.ProductArticleNumber)
  158. {
  159. itogoBezSkidki += item.ProductCost * pair.Value;
  160. }
  161. }
  162. }
  163. decimal itogoSoSkidkoy = 0;
  164. foreach (Product item in products)
  165. {
  166. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  167. {
  168. if (pair.Key == item.ProductArticleNumber)
  169. {
  170. if(item.ProductDiscountAmount != null)
  171. {
  172. itogoSoSkidkoy += (item.ProductCost - (item.ProductCost * ((decimal)item.ProductDiscountAmount / 100))) * pair.Value;
  173. }
  174. else
  175. itogoSoSkidkoy += item.ProductCost * pair.Value;
  176. }
  177. }
  178. }
  179. decimal ITOGO = Math.Round((1 - (itogoSoSkidkoy / itogoBezSkidki)) * 100, 2);
  180. TB_ItogoSkidka.Text = $"Скидка {ITOGO}%";
  181. TB_Itogo.Text = $"{(double)itogoSoSkidkoy} ₽";
  182. }
  183. }
  184. }