Cart.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace modulekz
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для Cart.xaml
  19. /// </summary>
  20. public partial class Cart : Page
  21. {
  22. List<Book> bb = new List<Book>();
  23. public Cart(List<Book> books, double sale)
  24. {
  25. InitializeComponent();
  26. foreach (Book b in books) //пересчет скидки
  27. {
  28. if (sale > 0)
  29. {
  30. b.sale = sale;
  31. b.decor = "";
  32. }
  33. else
  34. { }
  35. }
  36. bb = books;
  37. ListBook.ItemsSource = books;
  38. }
  39. /// <summary>
  40. /// Покупка книг
  41. /// </summary>
  42. /// <param name="sender"></param>
  43. /// <param name="e"></param>
  44. private void Buy_Click(object sender, RoutedEventArgs e)
  45. {
  46. int o = BaseConnect.BaseModel.BuyOrder.Max(x => x.id) + 1;
  47. int allBook = 0, sale = 0, thrfl = 0;
  48. double allSum = 0, ss = 0;
  49. int flag = 0, firstfl = 0, secfl = 0;
  50. foreach (Book b in bb)
  51. {
  52. BuyOrder order = new BuyOrder() { IdBook = b.Id, Count = b.inc, DateOrder = DateTime.Now };
  53. BaseConnect.BaseModel.BuyOrder.Add(order);
  54. allBook += b.inc;
  55. if (b.inc < b.Store)
  56. {
  57. b.Store -= b.inc;
  58. flag = 0;
  59. }
  60. else
  61. {
  62. if (b.Store > 0)
  63. flag = 1;
  64. else
  65. flag = 2;
  66. b.inc -= b.Store;
  67. b.Store = 0;
  68. b.Stock -= b.inc;
  69. }
  70. if (flag == 0)
  71. {
  72. firstfl = 1;
  73. }
  74. else if (flag == 1)
  75. {
  76. secfl = 1;
  77. }
  78. else
  79. {
  80. thrfl = 1;
  81. }
  82. allSum += Convert.ToDouble(b.inc) * Convert.ToDouble(b.Cost);
  83. sale = Convert.ToInt32(b.sale);
  84. if (sale > 0)
  85. ss = Convert.ToDouble(sale) / 100;
  86. b.inc = 0;
  87. }
  88. allSum = Math.Floor(allSum - (allSum * ss));
  89. string mesto = "";
  90. if (firstfl == 1 && secfl == 0 && thrfl == 0)
  91. mesto = "на кассе";
  92. else if (secfl == 1 || (thrfl == 1 && firstfl == 1))
  93. mesto = "частично на кассе, вторую часть заказа можно получить через 72 часа (" + DateTime.Now.AddDays(3) + ")";
  94. else
  95. mesto = "через 72 часа (" + DateTime.Now.AddDays(3) + ")";
  96. BaseConnect.BaseModel.SaveChanges();
  97. bb.Clear();
  98. ListBook.Items.Refresh();
  99. MessageBox.Show("Заказ №" +
  100. o + " оформлен!\n" +
  101. "Забрать его можно " + mesto +
  102. "\nОбщее количество книг в заказе: " + allBook +
  103. "\nОбщая цена: " + allSum +
  104. "\nСкидка: " + sale + "%" +
  105. "\nЗаказ зарезервирован до: " + DateTime.Now.AddDays(7));
  106. LoadPage.MainFrame.Navigate(new ListBook(bb));
  107. }
  108. /// <summary>
  109. /// Очищение корзины
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. private void Clear_Click(object sender, RoutedEventArgs e)
  114. {
  115. foreach (Book b in bb)
  116. {
  117. b.inc = 0;
  118. }
  119. bb.Clear();
  120. ListBook.Items.Refresh();
  121. }
  122. /// <summary>
  123. /// Переход назад
  124. /// </summary>
  125. /// <param name="sender"></param>
  126. /// <param name="e"></param>
  127. private void Back_Click(object sender, RoutedEventArgs e)
  128. {
  129. LoadPage.MainFrame.Navigate(new ListBook(bb));
  130. }
  131. private void del_Click(object sender, RoutedEventArgs e)
  132. {
  133. }
  134. private void add_Click(object sender, RoutedEventArgs e)
  135. {
  136. }
  137. /// <summary>
  138. /// Удаление позиции
  139. /// </summary>
  140. /// <param name="sender"></param>
  141. /// <param name="e"></param>
  142. private void Delet_Click(object sender, RoutedEventArgs e)
  143. {
  144. Button button = (Button)sender;
  145. int id = Convert.ToInt32(button.Uid);
  146. Book book = bb.FirstOrDefault(x => x.Id == id);
  147. book.inc = 0;
  148. bb.Remove(book);
  149. ListBook.Items.Refresh();
  150. }
  151. }
  152. }