WinCart.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace TestDemo
  7. {
  8. /// <summary>
  9. /// Логика взаимодействия для WinCart.xaml
  10. /// </summary>
  11. public partial class WinCart : Window
  12. {
  13. List<books> Knigi = new List<books>();
  14. public decimal Sale { get; set; }
  15. decimal sale;
  16. public WinCart(List<books> books, decimal sale)
  17. {
  18. try
  19. {
  20. InitializeComponent();
  21. this.sale = sale;
  22. Knigi = books;
  23. foreach (books books1 in Knigi)
  24. {
  25. if (books1.CountInCart >= 2)
  26. {
  27. books1.VisibleMainPrice = "Visible";
  28. }
  29. if (sale != 0)
  30. {
  31. books1.TextDecor = "Strikethrough";
  32. books1.OldPrice = (decimal)books1.CountInCart * (decimal)books1.price;
  33. books1.NewPrice = books1.OldPrice - (books1.OldPrice * sale / 100);
  34. books1.VisibleNewPrice = "Visible";
  35. }
  36. else
  37. {
  38. books1.OldPrice = (decimal)books1.CountInCart * (decimal)books1.price;
  39. }
  40. }
  41. ListCart.ItemsSource = Knigi;
  42. ListCart.Items.Refresh(); ;
  43. }
  44. catch
  45. {
  46. }
  47. }
  48. private void BtnClearAllList_Click(object sender, RoutedEventArgs e)
  49. {
  50. try
  51. {
  52. ListCart.ItemsSource = null;
  53. this.Close();
  54. LoadPages.SwitchPages.Navigate(new PgBooks());
  55. }
  56. catch
  57. {
  58. }
  59. }
  60. private void BtnPlus_Click(object sender, RoutedEventArgs e)
  61. {
  62. try
  63. {
  64. Button button = (Button)sender;
  65. int id = Convert.ToInt32(button.Uid);
  66. books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
  67. book.CountInCart++;
  68. if (book.CountInCart > (book.count_in_shop + book.count_in_stock))
  69. {
  70. MessageBox.Show("Превышен лимит товара");
  71. book.CountInCart--;
  72. }
  73. ListCart.Items.Refresh();
  74. }
  75. catch
  76. {
  77. }
  78. }
  79. private void BtnMinus_Click(object sender, RoutedEventArgs e)
  80. {
  81. try
  82. {
  83. Button button = (Button)sender;
  84. int id = Convert.ToInt32(button.Uid);
  85. books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
  86. book.CountInCart--;
  87. if (book.CountInCart < 1)
  88. {
  89. MessageBox.Show("Количество товаров не может быть меньше 0");
  90. book.CountInCart++;
  91. }
  92. ListCart.Items.Refresh();
  93. if (ListCart.Items == null)
  94. BtnZakazat.IsEnabled = false;
  95. }
  96. catch
  97. {
  98. }
  99. }
  100. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  101. {
  102. try
  103. {
  104. Button button = (Button)sender;
  105. int id = Convert.ToInt32(button.Uid);
  106. books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
  107. Knigi.Remove(book);
  108. ListCart.Items.Refresh();
  109. }
  110. catch
  111. {
  112. }
  113. }
  114. private void BtnZakazat_Click(object sender, RoutedEventArgs e)
  115. {
  116. try
  117. {
  118. List<books> books = (List<books>)ListCart.ItemsSource;
  119. decimal totalprice = 0;
  120. decimal totalpricenonsale = 0;
  121. int count = 0;
  122. string ZabratTovar = "";
  123. foreach (books books1 in books)
  124. {
  125. if (sale == 0)
  126. {
  127. totalprice += (decimal)books1.price * books1.CountInCart;
  128. }
  129. else
  130. {
  131. totalprice += books1.NewPrice;
  132. }
  133. totalpricenonsale += (decimal)books1.price * books1.CountInCart;
  134. count += books1.CountInCart;
  135. if (books1.count_in_shop >= books1.CountInCart)
  136. {
  137. ZabratTovar = "Сейчас";
  138. }
  139. else
  140. ZabratTovar = DateTime.Now.AddDays(3).AddHours(9).ToUniversalTime().ToString();
  141. }
  142. foreach (books books1 in books)
  143. {
  144. if (books1.count_in_shop > books1.CountInCart)
  145. {
  146. books1.count_in_shop -= books1.CountInCart;
  147. }
  148. else
  149. {
  150. int minus = books1.count_in_shop - books1.CountInCart;
  151. books1.count_in_shop -= books1.CountInCart;
  152. if (minus < 0)
  153. {
  154. books1.count_in_shop = 0;
  155. books1.count_in_stock -= minus * -1;
  156. }
  157. }
  158. }
  159. BaseModel.BaseConnect.SaveChanges();
  160. orders order = new orders() { sale = (int)sale, total_price = totalprice, count_book = count, date_reserve = DateTime.Now.AddDays(7) };
  161. BaseModel.BaseConnect.orders.Add(order);
  162. BaseModel.BaseConnect.SaveChanges();
  163. orders ordercur = BaseModel.BaseConnect.orders.FirstOrDefault(x => x.total_price == totalprice && x.sale == (int)sale && x.count_book == count);
  164. ListCart.Visibility = Visibility.Collapsed;
  165. StackInfo.Visibility = Visibility.Visible;
  166. TxtCount.Text = order.count_book.ToString();
  167. TxtDateZakaza.Text = ZabratTovar;
  168. TxtTotalPrice.Text = order.total_price.ToString("0.00");
  169. TxtSale.Text = order.sale.ToString();
  170. TxtNumber.Text = order.id_zakaza.ToString();
  171. TxtDateReserv.Text = order.date_reserve.ToShortDateString();
  172. TxtTotalPriceNonSale.Text = totalpricenonsale.ToString("0.00");
  173. Knigi.Clear();
  174. LoadPages.SwitchPages.Navigate(new PgBooks());
  175. BtnZakazat.IsEnabled = false;
  176. ListCart.ItemsSource = null;
  177. ListCart.Items.Refresh();
  178. }
  179. catch
  180. {
  181. }
  182. }
  183. private void BtnGoBack_Click(object sender, RoutedEventArgs e)
  184. {
  185. try
  186. {
  187. this.Close();
  188. LoadPages.SwitchPages.Navigate(new PgBooks());
  189. }
  190. catch
  191. {
  192. }
  193. }
  194. }
  195. }