123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using WriteErase.ClassBase;
- using WriteErase.ClassData;
- namespace WriteErase.ClassWindows
- {
- /// <summary>
- /// Логика взаимодействия для WindowOrderView.xaml
- /// </summary>
- public partial class WindowOrderView : Window,INotifyPropertyChanged
- {
- public WindowOrderView()
- {
- InitializeComponent();
- init();
- }
- private void init()
- {
- if(UserSession.user == null)
- btnNewOrder.Visibility = Visibility.Collapsed;
- cbPoint.ItemsSource = BaseConnection.kotBase.PickupPoint.ToList().Select(tb => tb.PickupPointAdress);
- lvProduct.ItemsSource = UserBasket.basket;
- calculateCost();
- }
- private void calculateCost()
- {
- tbkCountProduct.Text = "Количество товаров в корзине: " + UserBasket.basket.Count;
- double oldCost = 0;
- double newCost = 0;
- foreach (Product product in UserBasket.basket)
- {
- oldCost += (double)product.ProductCost * product.CountProduct;
- newCost += (double)product.Cost * product.CountProduct;
- }
- int discount = (int)Math.Round((1 - (newCost / oldCost)) * 100);
- if (oldCost == newCost)
- {
- tbkOldCost.Visibility = Visibility.Collapsed;
- tbkDiscount.Visibility = Visibility.Collapsed;
- }
- tbkDiscount.Text = discount.ToString() + " %";
- tbkOldCost.Text = string.Format("{0:C2}", oldCost);
- tbkNowCost.Text = string.Format("{0:C2}", newCost);
- if (UserBasket.basket.Count == 0)
- btnNewOrder.Visibility = Visibility.Collapsed;
- }
- private void cbPointTextChanged(object sender, TextChangedEventArgs e)
- {
- cbPoint.IsDropDownOpen = true;
- TextBox tb = (TextBox)e.OriginalSource;
- tb.Select(tb.SelectionStart + tb.SelectionLength, 0);
- CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(cbPoint.ItemsSource);
- cv.Filter = s => ((string)s).IndexOf(cbPoint.Text, StringComparison.CurrentCultureIgnoreCase) >= 0;
- }
- private void ProductDeleteClick(object sender, RoutedEventArgs e)
- {
- Button btn = (Button)sender;
- Product product = BaseConnection.kotBase.Product.FirstOrDefault(tb => tb.ProductArticleNumber == btn.Uid);
- UserBasket.basket.Remove(product);
- calculateCost();
- }
- private void ChangeCountClick(object sender, RoutedEventArgs e)
- {
- Button btn = (Button)sender;
- Product product = BaseConnection.kotBase.Product.FirstOrDefault(tb => tb.ProductArticleNumber == btn.Uid);
- switch (btn.Content)
- {
- case "+":
- {
- product.CountProduct += 1;
- break;
- }
- case "+10":
- {
- product.CountProduct += 10;
- break;
- }
- case "-":
- {
- product.CountProduct -= 1;
- break;
- }
- case "-10":
- {
- product.CountProduct -= 10;
- break;
- }
- }
- if(product.CountProduct <= 0)
- {
- product.CountProduct = 0;
- UserBasket.basket.Remove(product);
- }
- calculateCost();
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public void OnPropertyChanged([CallerMemberName] string prop = "")
- {
- if (PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs(prop));
- }
- private void btnNewOrderClick(object sender, RoutedEventArgs e)
- {
- try
- {
- if (cbPoint.SelectedIndex >= 0 && UserBasket.basket.Count > 0)
- {
- Random rnd = new Random();
- int code;
- while (true)//генерируем код
- {
- code = rnd.Next(100, 1000);
- if (BaseConnection.kotBase.Order.Where(tb => tb.OrderCode == code).Count() == 0)
- break;
- }
- PickupPoint point = BaseConnection.kotBase.PickupPoint.First(tb => tb.PickupPointAdress == cbPoint.SelectedValue);
- Order newOrder = new Order()
- {
- OrderStatus = "Новый",
- OrderDate = DateTime.Now,
- OrderCode = code,
- OrderPickupPoint = point.PickupPointId,
- OrderClienr = UserSession.user.UserSurname + " " + UserSession.user.UserName + " " + UserSession.user.UserPatronymic
- };
- BaseConnection.kotBase.Order.Add(newOrder);
- BaseConnection.kotBase.SaveChanges();
- int count = 0;
- foreach (Product item in UserBasket.basket)
- {
- if (item.ProductQuantityInStock >= item.CountProduct)
- count++;
- OrderProduct op = new OrderProduct()
- {
- OrderID = newOrder.OrderID,
- ProductArticleNumber = item.ProductArticleNumber,
- ProductCount = item.CountProduct
- };
- BaseConnection.kotBase.OrderProduct.Add(op);
- }
- if (count > 3)
- newOrder.OrderDeliveryDate = DateTime.Now.AddDays(3);
- else
- newOrder.OrderDeliveryDate = DateTime.Now.AddDays(6);
- BaseConnection.kotBase.SaveChanges();
- MessageBox.Show("Заказ успешно добавлен\nКод получения: " + newOrder.OrderCode);
- UserBasket.basket.Clear();
- }
- else
- {
- if(cbPoint.SelectedIndex < 0) MessageBox.Show("Выберите пункт выдачи");
- if(UserBasket.basket.Count <= 0) MessageBox.Show("Количество товаров в корзине должно быть больше 1");
- }
- }
- catch(Exception ex)
- {
- Debug.WriteLine(ex.Message);
- }
- }
- }
- }
|