YourBasket.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace WpfApp1
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для korzinka.xaml
  20. /// </summary>
  21. public partial class YourBasket : Page, INotifyPropertyChanged
  22. {
  23. List<Books> changeBooksList = new List<Books>();
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. int Sale;
  26. public YourBasket(List<Books> books, int sale)
  27. {
  28. InitializeComponent();
  29. ListBasket.ItemsSource = books;
  30. ListBasket.Items.Refresh();
  31. Sale = sale;
  32. changeBooksList = books;
  33. foreach (Books book in books)
  34. {
  35. if (book.CountInBasket == 1)
  36. {
  37. book.Visible = "Collapsed";
  38. }
  39. if (sale == 0)
  40. {
  41. book.Visible2 = "Collapsed";
  42. }
  43. book.TotalPriseOrder = (book.Cost * book.CountInBasket);
  44. book.TotalPriseOrderWithSale = book.TotalPriseOrder - (book.TotalPriseOrder * sale / 100);
  45. book.newDecoration = "Strikethrough";
  46. }
  47. }
  48. int bufCount = 0, buffCost = 0, buffCostNotSale = 0, itogCount = 0, itogCost = 0, itogCostNotSale = 0;
  49. DateTime dateOr;
  50. private void Order_Click(object sender, RoutedEventArgs e)
  51. {
  52. foreach (Books books1 in changeBooksList)
  53. {
  54. if (books1.CountInStore > books1.CountInBasket)
  55. {
  56. books1.CountInStore -= books1.CountInBasket;
  57. }
  58. else
  59. {
  60. int minus = books1.CountInStore - books1.CountInBasket;
  61. books1.CountInStore -= books1.CountInBasket;
  62. if (minus < 0)
  63. {
  64. books1.CountInStore = 0;
  65. books1.CountInStock -= minus * -1;
  66. }
  67. }
  68. }
  69. DataBAse.BaseModel.SaveChanges();
  70. DateTime today = DateTime.Now;
  71. foreach (Books books in changeBooksList)
  72. {
  73. bufCount = books.CountInBasket;
  74. buffCost = Convert.ToInt32(books.TotalPriseOrderWithSale);
  75. buffCostNotSale = Convert.ToInt32(books.TotalPriseOrder);
  76. itogCount += bufCount;
  77. itogCost += buffCost;
  78. itogCostNotSale += buffCostNotSale;
  79. if (books.CountInStore >= books.CountInBasket)
  80. {
  81. dateOr = today;
  82. }
  83. else
  84. {
  85. dateOr = today.AddHours(72);
  86. }
  87. }
  88. Order order = new Order()
  89. {
  90. date = dateOr,
  91. count = itogCount,
  92. totalcost = itogCost,
  93. sale = Sale,
  94. reserve = today.AddDays(7),
  95. };
  96. DataBAse.BaseModel.Order.Add(order);
  97. DataBAse.BaseModel.SaveChanges();
  98. DataBAse.BaseModel = new Entities();
  99. ListBasket.ItemsSource = null;
  100. ListBasket.Items.Refresh();
  101. MessageBox.Show("Данные о заказе добавлены в БД");
  102. ListBasket.Visibility = Visibility.Collapsed;
  103. Info.Visibility = Visibility.Visible;
  104. number.Text = "Номер заказа " + order.ID.ToString();
  105. dateOrder.Text = "Можно забирать " + dateOr.ToString();
  106. DateRezerv.Text = "Конец срока резервирования продукции " + today.AddDays(7).ToString();
  107. totalCostOrder.Text = "Цена заказа " + itogCostNotSale.ToString();
  108. totalCostSaleOrder.Text = "Процент скидки " + Sale.ToString();
  109. totalCostWithSaleOrder.Text = "Цена заказа со скидкой" + itogCost.ToString();
  110. totalCount.Text = "Итоговое количество " + itogCount.ToString();
  111. }
  112. private void Plus_Click(object sender, RoutedEventArgs e)
  113. {
  114. Button button = (Button)sender;
  115. int id = Convert.ToInt32(button.Uid);
  116. Books prov = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == id);
  117. prov.CountInBasket += 1;
  118. if (prov.CountInBasket == 1)
  119. {
  120. prov.Visible = "Collapsed";
  121. }
  122. if (Sale == 0)
  123. {
  124. prov.Visible2 = "Collapsed";
  125. }
  126. prov.TotalPriseOrder = (prov.Cost * prov.CountInBasket);
  127. prov.TotalPriseOrderWithSale = prov.TotalPriseOrder - (prov.TotalPriseOrder * Sale / 100);
  128. prov.newDecoration = "Strikethrough";
  129. ListBasket.Items.Refresh();
  130. }
  131. private void Minus_Click(object sender, RoutedEventArgs e)
  132. {
  133. try
  134. {
  135. Button button = (Button)sender;
  136. int id = Convert.ToInt32(button.Uid);
  137. Books minus = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == id);
  138. Books prov = new Books();
  139. minus.CountInBasket -= 1;
  140. if ((minus.CountInBasket > 0) && (minus.CountInBasket < (minus.CountInStock + minus.CountInStore)))
  141. {
  142. if (minus.CountInBasket == 1)
  143. {
  144. minus.Visible = "Collapsed";
  145. }
  146. if (Sale == 0)
  147. {
  148. minus.Visible2 = "Collapsed";
  149. }
  150. minus.TotalPriseOrder = (minus.Cost * minus.CountInBasket);
  151. minus.TotalPriseOrderWithSale = minus.TotalPriseOrder - (minus.TotalPriseOrder * Sale / 100);
  152. minus.newDecoration = "Strikethrough";
  153. ListBasket.Items.Refresh();
  154. }
  155. else if (minus.CountInBasket == 0)
  156. {
  157. prov = minus;
  158. }
  159. else
  160. {
  161. throw new Exception("Количество не может быть");
  162. }
  163. changeBooksList.Remove(prov);
  164. ListBasket.Items.Refresh();
  165. }
  166. catch (Exception ex)
  167. {
  168. MessageBox.Show(ex.Message);
  169. }
  170. }
  171. private void MainPage_Click(object sender, RoutedEventArgs e)
  172. {
  173. ChangePages.changed.Navigate(new MainPage());
  174. }
  175. private void Clear_Click(object sender, RoutedEventArgs e)
  176. {
  177. ListBasket.ItemsSource = null;
  178. ListBasket.Items.Refresh();
  179. }
  180. private void Delit_Click(object sender, RoutedEventArgs e)
  181. {
  182. Button button = (Button)sender;
  183. int id = Convert.ToInt32(button.Uid);
  184. Books prov = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == id);
  185. changeBooksList.Remove(prov);
  186. ListBasket.Items.Refresh();
  187. }
  188. }
  189. }