PgBooks.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using MathSale;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. namespace TestDemo
  9. {
  10. /// <summary>
  11. /// Логика взаимодействия для PgBooks.xaml
  12. /// </summary>
  13. public partial class PgBooks : Page, INotifyPropertyChanged
  14. {
  15. ViewModel ViewModel = new ViewModel();
  16. public int CountTakedBooks { get; set; }
  17. public decimal ItogPriceNonSale { get; set; } = 0;
  18. public decimal PriceSale { get; set; } = 0;
  19. public decimal Sale { get; set; } = 0;
  20. public string Visible { get; set; }
  21. public string TextDecor { get; set; }
  22. Sale sale = new Sale();
  23. List<books> takedBooks = new List<books>();
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. public PgBooks()
  26. {
  27. try
  28. {
  29. InitializeComponent();
  30. MailList.ItemsSource = ViewModel.BuildBooks();
  31. }
  32. catch
  33. {
  34. }
  35. }
  36. /// <summary>
  37. /// Добавление в корзину
  38. /// </summary>
  39. /// <param name="sender"></param>
  40. /// <param name="e"></param>
  41. private void BtnAddCart_Click(object sender, RoutedEventArgs e)
  42. {
  43. try
  44. {
  45. Button button = (Button)sender;
  46. int id = Convert.ToInt32(button.Uid);
  47. books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
  48. if ((book.count_in_stock <= 0) && (book.count_in_shop <= 0))
  49. {
  50. throw new Exception("Данный товар закончился");
  51. }
  52. if (book.CountInCart+1 > (book.count_in_shop + book.count_in_stock))
  53. {
  54. throw new Exception("Превышен лимит товара");
  55. }
  56. book.CountInCart++;
  57. takedBooks.Add(book);
  58. CountTakedBooks = takedBooks.Count;
  59. PropertyChanged(this, new PropertyChangedEventArgs("CountTakedBooks"));
  60. decimal price = 0;
  61. foreach (books booki in takedBooks.Distinct().ToList())
  62. {
  63. price += (decimal)booki.price * booki.CountInCart;
  64. }
  65. ItogPriceNonSale = price;
  66. PropertyChanged(this, new PropertyChangedEventArgs("ItogPriceNonSale"));
  67. Sale sale = new Sale();
  68. Sale = (int)sale.sale(takedBooks.Count, ItogPriceNonSale);
  69. PropertyChanged(this, new PropertyChangedEventArgs("Sale"));
  70. if (Sale != 0)
  71. {
  72. PriceSale = ItogPriceNonSale - (ItogPriceNonSale * Sale / 100);
  73. TextDecor = "Strikethrough";
  74. TxtYesSale.Visibility = Visibility.Visible;
  75. PropertyChanged(this, new PropertyChangedEventArgs("TextDecor"));
  76. }
  77. PropertyChanged(this, new PropertyChangedEventArgs("PriceSale"));
  78. }
  79. catch (Exception ex)
  80. {
  81. MessageBox.Show(ex.ToString());
  82. }
  83. }
  84. private void BtnGoCart_Click(object sender, RoutedEventArgs e)
  85. {
  86. try
  87. {
  88. new WinCart(takedBooks.Distinct().ToList(), Sale).ShowDialog();
  89. }
  90. catch
  91. {
  92. }
  93. }
  94. }
  95. }