pgBasket.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using CalcS;
  2. using System;
  3. using System.Collections.Generic;
  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 ExamBoldin
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для pgBasket.xaml
  20. /// </summary>
  21. public partial class pgBasket : Page
  22. {
  23. List<BookShop> books;
  24. bool CanGoBack = true;
  25. public pgBasket(List<BookShop> books)
  26. {
  27. InitializeComponent();
  28. this.books = books;
  29. lbBooksBasket.ItemsSource = books;
  30. }
  31. private void btnDeleteBook_Click(object sender, RoutedEventArgs e)
  32. {
  33. Button btn = (Button)sender;
  34. int id = Convert.ToInt32(btn.Uid);
  35. BookShop delBook = books.FirstOrDefault(x => x.id == id);
  36. books.Remove(delBook);
  37. lbBooksBasket.Items.Refresh();
  38. }
  39. private void btnBuy_Click(object sender, RoutedEventArgs e)
  40. {
  41. int numOreder;
  42. if (BaseConnect.BaseModel.Orders.Count() != 0)
  43. numOreder = BaseConnect.BaseModel.Orders.Max(x => x.NOrder) + 1;
  44. else
  45. numOreder = 1;
  46. bool flag = false;
  47. int count = 0;
  48. int sale = 0;
  49. decimal CountCost = 0;
  50. foreach (BookShop book in books)
  51. {
  52. BookShop bk = BaseConnect.BaseModel.BookShop.FirstOrDefault(x => x.id == book.id);
  53. if (bk.CountStore >= book.AllCount)
  54. {
  55. flag = true;
  56. bk.CountStore -= book.AllCount;
  57. BaseConnect.BaseModel.SaveChanges();
  58. }
  59. else
  60. {
  61. int res = book.AllCount - bk.CountStore;
  62. bk.CountStore = 0;
  63. bk.CountStock -= res;
  64. BaseConnect.BaseModel.SaveChanges();
  65. }
  66. Orders or = new Orders();
  67. or.IdBook = book.id;
  68. or.NOrder = numOreder;
  69. or.Count = book.AllCount;
  70. count += book.AllCount;
  71. sale = book.Sale;
  72. CountCost += book.Cost * book.AllCount;
  73. or.DateOrder = DateTime.Now;
  74. BaseConnect.BaseModel.Orders.Add(or);
  75. }
  76. if (flag)
  77. MessageBox.Show("Заказ: " + numOreder + "\nПоловина заказа прибудет: " + DateTime.Now.ToShortDateString() + "\nОстальная часть заказа прибудет: " + DateTime.Now.AddDays(3).ToShortDateString() + "\nКниги зарезервированы до: " + DateTime.Now.AddDays(7).ToShortDateString() + "\nКоличество книг в заказе: " + count + "\nЦена:" + Math.Floor(CountCost - (CountCost * sale / 100)) + "\nСкидка:" + sale + "%");
  78. else
  79. {
  80. MessageBox.Show("Заказ: " + numOreder + "\nЗаказ можно забрать: " + DateTime.Now.ToShortDateString() + "\nЗарезервировано до: " + DateTime.Now.AddDays(7).ToShortDateString() + "\nКоличество книг в заказе: " + count + "\nЦена:" + Math.Floor(CountCost - (CountCost * sale / 100)) + "\nСкидка:" + sale + "%");
  81. }
  82. books.Clear();
  83. lbBooksBasket.Items.Refresh();
  84. CanGoBack = false;
  85. }
  86. private void btnDel_Click(object sender, RoutedEventArgs e)
  87. {
  88. books.Clear();
  89. lbBooksBasket.Items.Refresh();
  90. CanGoBack = false;
  91. }
  92. private void btnBack_Click(object sender, RoutedEventArgs e)
  93. {
  94. if (!CanGoBack)
  95. LoadPages.GoPage.Navigate(new pgListBooks());
  96. else
  97. LoadPages.GoPage.GoBack();
  98. }
  99. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  100. {
  101. TextBox tb = (TextBox)sender;
  102. int id = Convert.ToInt32(tb.Uid);
  103. BookShop book = books.FirstOrDefault(x => x.id == id);
  104. int tmpValue = book.AllCount ;
  105. try
  106. {
  107. BookShop checkCountBook = BaseConnect.BaseModel.BookShop.FirstOrDefault(x => x.id == id);
  108. int countBase = checkCountBook.CountStore + checkCountBook.CountStock;
  109. if (countBase >= Convert.ToInt32(tb.Text))
  110. {
  111. CalcSale cs = new CalcSale();
  112. book.AllCount = Convert.ToInt32(tb.Text);
  113. int count=0;
  114. decimal cost =0;
  115. foreach (BookShop booke in books)
  116. {
  117. cost += booke.Cost * booke.AllCount;
  118. count += booke.AllCount;
  119. }
  120. foreach (BookShop booke in books)
  121. {
  122. booke.Sale = cs.Calc(count, cost);
  123. }
  124. lbBooksBasket.ItemsSource = books;
  125. lbBooksBasket.Items.Refresh();
  126. }
  127. else
  128. {
  129. MessageBox.Show("Данного количества нет на скалде!");
  130. tb.Text = tmpValue.ToString();
  131. }
  132. }
  133. catch { }
  134. }
  135. }
  136. }