123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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.Navigation;
- using System.Windows.Shapes;
- using UP_Venediktov.Classes;
- using UP_Venediktov.ModelBase;
- namespace UP_Venediktov.Pages
- {
- /// <summary>
- /// Логика взаимодействия для CardOrder.xaml
- /// </summary>
- public partial class CardOrder : Page
- {
- Entities DB = new Entities();
- public static List<Product> Products = new List<Product>();
- public static List<AmountofProduct> AmountofProducts = new List<AmountofProduct>();
- public decimal? TotalCost = 0;
- public CardOrder()
- {
- InitializeComponent();
- CartList.ItemsSource = Products;
- foreach (Product p in Products)
- {
- TotalCost += p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount);
- }
- CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
- }
- public CardOrder(int id)
- {
- InitializeComponent();
- CartList.ItemsSource = Products;
- foreach (Product p in Products)
- {
- TotalCost += p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount);
- }
- CostOrder.Text = string.Format("{0:C2}", TotalCost);
- }
- private void CostCalculate(object sender, RoutedEventArgs e)
- {
- TextBlock tb = (TextBlock)sender;
- string Article = tb.Uid.ToString();
- Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
- if (pr.ProductDiscountAmount > 0)
- {
- tb.Text = "Цена за единицу: " + string.Format("{0:C2}", pr.ProductCost - (pr.ProductCost / 100 * pr.ProductDiscountAmount));
- }
- }
- private void Photo(object sender, RoutedEventArgs e)
- {
- Image pic = (Image)sender;
- string Article = pic.Uid.ToString();
- string photoPath = DB.Product.Where(x => x.ProductArticleNumber == Article).Select(x => x.ProductPhoto).FirstOrDefault();
- if (photoPath != null && photoPath != "")
- {
- pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\" + photoPath, UriKind.RelativeOrAbsolute));
- }
- else
- {
- pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\picture.png", UriKind.RelativeOrAbsolute));
- }
- }
- private void AmountCalculate(object sender, RoutedEventArgs e)
- {
- TextBlock tb = (TextBlock)sender;
- string Article = tb.Uid.ToString();
- Product pr = Products.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
- int index = Products.IndexOf(pr);
- tb.Text = AmountofProducts[index].Amount.ToString();
- }
- private void MinusAmount(object sender, RoutedEventArgs e)
- {
- Button bt = (Button)sender;
- string article = bt.Uid.ToString();
- Product pr = Products.Where(x => x.ProductArticleNumber == article).First();
- AmountofProduct pa = AmountofProducts.Where(x => x.Article == article).First();
- if ((pa.Amount - 1) == 0)
- {
- AmountofProducts.Remove(pa);
- Products.Remove(pr);
- if (Products.Count == 0)
- {
- MessageBox.Show("Корзина пуста");
- }
- }
- else
- {
- foreach (AmountofProduct pp in AmountofProducts)
- {
- if (pp.Article == article)
- {
- pp.Amount--;
- }
- }
- }
- TotalCost = 0;
- foreach (Product p in Products)
- {
- TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount)) * AmountofProducts.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).First();
- }
- CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
- CartList.ItemsSource = Products.ToList();
- }
- private void PlusAmount(object sender, RoutedEventArgs e)
- {
- Button bt = (Button)sender;
- string article = bt.Uid.ToString();
- foreach (AmountofProduct pa in AmountofProducts)
- {
- if (pa.Article == article)
- {
- pa.Amount++;
- }
- }
- TotalCost = 0;
- foreach (Product p in Products)
- {
- TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount)) * AmountofProducts.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).First();
- }
- CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
- CartList.ItemsSource = Products.ToList();
- }
- private void DeleteProduct(object sender, RoutedEventArgs e)
- {
- Button bt = (Button)sender;
- string article = bt.Uid.ToString();
- Product pr = Products.Where(x => x.ProductArticleNumber == article).First();
- AmountofProduct pa = AmountofProducts.Where(x => x.Article == article).First();
- AmountofProducts.Remove(pa);
- Products.Remove(pr);
- if (Products.Count == 0)
- {
- MessageBox.Show("Корзина пуста");
- }
- TotalCost = 0;
- foreach (Product p in Products)
- {
- TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount)) * AmountofProducts.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).First();
- }
- CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
- CartList.ItemsSource = Products.ToList();
- }
- private void CreateOrder(object sender, RoutedEventArgs e)
- {
- Random random = new Random();
- Order newOrder = new Order();
- newOrder.OrderDate = DateTime.Now;
- bool ThreeOrLess = false;
- foreach (Product p in Products)
- {
- if (p.ProductQuantityInStock <= 3)
- {
- ThreeOrLess = true;
- }
- }
- if (ThreeOrLess)
- {
- newOrder.OrderDeliveryDate = DateTime.Now.AddDays(6);
- }
- else
- {
- newOrder.OrderDeliveryDate = DateTime.Now.AddDays(3);
- }
- newOrder.OrderPickupPoint = random.Next(1, 36);
- newOrder.OrderCode = random.Next(100, 999);
- newOrder.OrderStatus = "Новый";
- DB.Order.Add(newOrder);
- DB.SaveChanges();
- int id = DB.Order.Where(x => x.OrderDate == newOrder.OrderDate).Select(x => x.OrderID).FirstOrDefault();
- foreach (Product p in Products)
- {
- OrderProduct newOrderProduct = new OrderProduct();
- newOrderProduct.OrderID = id;
- newOrderProduct.ProductArticleNumber = p.ProductArticleNumber;
- newOrderProduct.ProductCont = AmountofProducts.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).FirstOrDefault();
- DB.OrderProduct.Add(newOrderProduct);
- }
- DB.SaveChanges();
- MessageBox.Show("Заказ был сформирован, код получения: " + newOrder.OrderCode + " \nПункт выдачи: " +
- DB.PickupPoint.Where(x => x.Id_PickupPoint == newOrder.OrderPickupPoint).Select(x => x.AddressPP).FirstOrDefault() +
- "\nЗаказ будет доставлен " + newOrder.OrderDeliveryDate.ToShortDateString());
- Products.Clear();
- AmountofProducts.Clear();
- FrameClass.MainFrame.Navigate(new ProductListUser());
- }
- }
-
- }
|