pgListBooks.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /// Логика взаимодействия для pgListBooks.xaml
  20. /// </summary>
  21. public partial class pgListBooks : Page
  22. {
  23. int count = 0;
  24. decimal price = 0;
  25. List<BookShop> books;
  26. int Sale = 0;
  27. public pgListBooks()
  28. {
  29. InitializeComponent();
  30. BaseConnect.BaseModel = new Entities();
  31. lbBooks.ItemsSource = BaseConnect.BaseModel.BookShop.ToList();
  32. books = new List<BookShop>();
  33. tbPer.Visibility = Visibility.Collapsed;
  34. tbRub.Visibility = Visibility.Collapsed;
  35. tbSalePrice.Visibility = Visibility.Collapsed;
  36. }
  37. private void btnAddBok_Click(object sender, RoutedEventArgs e)
  38. {
  39. Button btn = (Button)sender;
  40. int id = Convert.ToInt32(btn.Uid);
  41. BookShop book = BaseConnect.BaseModel.BookShop.FirstOrDefault(x => x.id == id);
  42. BookShop findBook = books.FirstOrDefault(x => x.id == id);
  43. int summ = BaseConnect.BaseModel.BookShop.Where(x => x.id == id).Sum(x => x.CountStock) + BaseConnect.BaseModel.BookShop.Where(x => x.id == id).Sum(x => x.CountStore);
  44. int summList = 0;
  45. if (book.CountStock > 0 || book.CountStore > 0)
  46. {
  47. if (findBook == null)
  48. {
  49. findBook = new BookShop();
  50. findBook.id = book.id;
  51. findBook.Cover = book.Cover;
  52. findBook.Name = book.Name;
  53. findBook.Description = book.Description;
  54. findBook.Author = book.Author;
  55. findBook.Cost = book.Cost;
  56. findBook.Genre = book.Genre;
  57. books.Add(findBook);
  58. }
  59. if (book.CountStock + book.CountStore > findBook.AllCount)
  60. {
  61. summList = books.Where(x => x.id == id).Sum(x => x.CountStock) + books.Where(x => x.id == id).Sum(x => x.CountStore);
  62. findBook.AllCount++;
  63. if (summList < summ)
  64. {
  65. count++;
  66. tbCount.Text = count.ToString();
  67. price += book.Cost;
  68. tbPrice.Text = price.ToString();
  69. if (tbPrice.Text != "" || tbSalePrice.Text != "")
  70. {
  71. tbRub.Visibility = Visibility.Visible;
  72. }
  73. lbBooks.Items.Refresh();
  74. tbPrice.Text = tbPrice.Text.Replace(".", ",");
  75. CalcSale calc = new CalcSale();
  76. int countB = Convert.ToInt32(tbCount.Text);
  77. decimal costB = Convert.ToDecimal(tbPrice.Text);
  78. Sale = calc.Calc(countB, costB);
  79. foreach (BookShop booke in books)
  80. {
  81. booke.Sale = Sale;
  82. }
  83. }
  84. else
  85. MessageBox.Show("Данный товар невозможно приобрести!");
  86. }
  87. else
  88. MessageBox.Show("Данный товар невозможно приобрести!");
  89. }
  90. else
  91. MessageBox.Show("Данный товар невозможно приобрести!");
  92. if (Sale > 0 && Sale <= 100)
  93. {
  94. tbPrice.TextDecorations = TextDecorations.Strikethrough;
  95. tbPer.Visibility = Visibility.Visible;
  96. tbSale.Visibility = Visibility.Visible;
  97. tbSalePrice.Visibility = Visibility.Visible;
  98. tbPer.Visibility = Visibility.Visible;
  99. decimal salePrice;
  100. salePrice = Convert.ToDecimal(tbPrice.Text) - Convert.ToDecimal(tbPrice.Text) * (Convert.ToDecimal(Sale) / 100);
  101. tbSalePrice.Text = salePrice.ToString();
  102. tbSale.Text = Sale.ToString();
  103. }
  104. }
  105. private void btnGoCart_Click(object sender, RoutedEventArgs e)
  106. {
  107. LoadPages.GoPage.Navigate(new pgBasket(books));
  108. }
  109. }
  110. }