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
{
///
/// Логика взаимодействия для CardOrder.xaml
///
public partial class CardOrder : Page
{
Entities DB = new Entities();
public static List Products = new List();
public static List AmountofProducts = new List();
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());
}
}
}