123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- namespace TestDemo
- {
- /// <summary>
- /// Логика взаимодействия для WinCart.xaml
- /// </summary>
- public partial class WinCart : Window
- {
- List<books> Knigi = new List<books>();
- public decimal Sale { get; set; }
- decimal sale;
- public WinCart(List<books> books, decimal sale)
- {
- try
- {
- InitializeComponent();
- this.sale = sale;
- Knigi = books;
- foreach (books books1 in Knigi)
- {
- if (books1.CountInCart >= 2)
- {
- books1.VisibleMainPrice = "Visible";
- }
- if (sale != 0)
- {
- books1.TextDecor = "Strikethrough";
- books1.OldPrice = (decimal)books1.CountInCart * (decimal)books1.price;
- books1.NewPrice = books1.OldPrice - (books1.OldPrice * sale / 100);
- books1.VisibleNewPrice = "Visible";
- }
- else
- {
- books1.OldPrice = (decimal)books1.CountInCart * (decimal)books1.price;
- }
- }
- ListCart.ItemsSource = Knigi;
- ListCart.Items.Refresh(); ;
- }
- catch
- {
- }
- }
- private void BtnClearAllList_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- ListCart.ItemsSource = null;
- this.Close();
- LoadPages.SwitchPages.Navigate(new PgBooks());
- }
- catch
- {
- }
- }
- private void BtnPlus_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- Button button = (Button)sender;
- int id = Convert.ToInt32(button.Uid);
- books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
- book.CountInCart++;
- if (book.CountInCart > (book.count_in_shop + book.count_in_stock))
- {
- MessageBox.Show("Превышен лимит товара");
- book.CountInCart--;
- }
- ListCart.Items.Refresh();
- }
- catch
- {
- }
- }
- private void BtnMinus_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- Button button = (Button)sender;
- int id = Convert.ToInt32(button.Uid);
- books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
- book.CountInCart--;
- if (book.CountInCart < 1)
- {
- MessageBox.Show("Количество товаров не может быть меньше 0");
- book.CountInCart++;
- }
- ListCart.Items.Refresh();
- if (ListCart.Items == null)
- BtnZakazat.IsEnabled = false;
- }
- catch
- {
- }
- }
- private void BtnDelete_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- Button button = (Button)sender;
- int id = Convert.ToInt32(button.Uid);
- books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
- Knigi.Remove(book);
- ListCart.Items.Refresh();
- }
- catch
- {
- }
- }
- private void BtnZakazat_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- List<books> books = (List<books>)ListCart.ItemsSource;
- decimal totalprice = 0;
- decimal totalpricenonsale = 0;
- int count = 0;
- string ZabratTovar = "";
- foreach (books books1 in books)
- {
- if (sale == 0)
- {
- totalprice += (decimal)books1.price * books1.CountInCart;
- }
- else
- {
- totalprice += books1.NewPrice;
- }
- totalpricenonsale += (decimal)books1.price * books1.CountInCart;
- count += books1.CountInCart;
- if (books1.count_in_shop >= books1.CountInCart)
- {
- ZabratTovar = "Сейчас";
- }
- else
- ZabratTovar = DateTime.Now.AddDays(3).AddHours(9).ToUniversalTime().ToString();
- }
- foreach (books books1 in books)
- {
- if (books1.count_in_shop > books1.CountInCart)
- {
- books1.count_in_shop -= books1.CountInCart;
- }
- else
- {
- int minus = books1.count_in_shop - books1.CountInCart;
- books1.count_in_shop -= books1.CountInCart;
- if (minus < 0)
- {
- books1.count_in_shop = 0;
- books1.count_in_stock -= minus * -1;
- }
- }
- }
- BaseModel.BaseConnect.SaveChanges();
- orders order = new orders() { sale = (int)sale, total_price = totalprice, count_book = count, date_reserve = DateTime.Now.AddDays(7) };
- BaseModel.BaseConnect.orders.Add(order);
- BaseModel.BaseConnect.SaveChanges();
- orders ordercur = BaseModel.BaseConnect.orders.FirstOrDefault(x => x.total_price == totalprice && x.sale == (int)sale && x.count_book == count);
- ListCart.Visibility = Visibility.Collapsed;
- StackInfo.Visibility = Visibility.Visible;
- TxtCount.Text = order.count_book.ToString();
- TxtDateZakaza.Text = ZabratTovar;
- TxtTotalPrice.Text = order.total_price.ToString("0.00");
- TxtSale.Text = order.sale.ToString();
- TxtNumber.Text = order.id_zakaza.ToString();
- TxtDateReserv.Text = order.date_reserve.ToShortDateString();
- TxtTotalPriceNonSale.Text = totalpricenonsale.ToString("0.00");
- Knigi.Clear();
- LoadPages.SwitchPages.Navigate(new PgBooks());
- BtnZakazat.IsEnabled = false;
- ListCart.ItemsSource = null;
- ListCart.Items.Refresh();
- }
- catch
- {
- }
- }
- private void BtnGoBack_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- this.Close();
- LoadPages.SwitchPages.Navigate(new PgBooks());
- }
- catch
- {
- }
- }
- }
- }
|