using Reks;
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.Shapes;
namespace Reks.Windows
{
///
/// Логика взаимодействия для Cart.xaml
///
public partial class Cart : Window
{
TradeBase DB = new TradeBase();
public static List Products = new List();
public static List ProductsAmount = new List();
public decimal? TotalCost = 0;
public Cart()
{
InitializeComponent();
CartList.ItemsSource = Products;
foreach (Product p in Products)
{
TotalCost += p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount);
}
CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
}
public Cart(int id)
{
InitializeComponent();
CartList.ItemsSource = Products;
foreach (Product p in Products)
{
TotalCost += p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount);
}
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.ProductMaxDiscountAmount > 0)
{
tb.Text = "Цена за единицу: " + string.Format("{0:C2}", pr.ProductCost - (pr.ProductCost / 100 * pr.ProductMaxDiscountAmount));
}
}
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 = ProductsAmount[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();
ProductAmount pa = ProductsAmount.Where(x => x.Article == article).First();
if ((pa.Amount - 1) == 0)
{
ProductsAmount.Remove(pa);
Products.Remove(pr);
if (Products.Count == 0)
{
this.Close();
}
}
else
{
foreach (ProductAmount pp in ProductsAmount)
{
if (pp.Article == article)
{
pp.Amount--;
}
}
}
TotalCost = 0;
foreach (Product p in Products)
{
TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount)) * ProductsAmount.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 (ProductAmount pa in ProductsAmount)
{
if (pa.Article == article)
{
pa.Amount++;
}
}
TotalCost = 0;
foreach (Product p in Products)
{
TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount)) * ProductsAmount.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();
ProductAmount pa = ProductsAmount.Where(x => x.Article == article).First();
ProductsAmount.Remove(pa);
Products.Remove(pr);
if (Products.Count == 0)
{
this.Close();
}
TotalCost = 0;
foreach (Product p in Products)
{
TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount)) * ProductsAmount.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.OrderCreateDate = 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.OrderReceivingCode = random.Next(100, 999);
newOrder.OrderStatus = 1;
DB.Order.Add(newOrder);
//DB.SaveChanges();
int id = DB.Order.Where(x => x.OrderCreateDate == newOrder.OrderCreateDate).Select(x => x.OrderID).FirstOrDefault();
foreach (Product p in Products)
{
OrderProduct newOrderProduct = new OrderProduct();
newOrderProduct.OrderID = id;
newOrderProduct.ProductArticleNumber = p.ProductArticleNumber;
newOrderProduct.ProductAmount = ProductsAmount.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).FirstOrDefault();
DB.OrderProduct.Add(newOrderProduct);
}
DB.SaveChanges();
MessageBox.Show("Заказ собран, код получения: " + newOrder.OrderReceivingCode + " \nАдрес пунката выдачи: " +
DB.PickupPoints.Where(x => x.PickupID == newOrder.OrderPickupPoint).Select(x => x.PuckupAddress).FirstOrDefault() +
"\nЗаказ будет доставлен " + newOrder.OrderDeliveryDate.ToShortDateString());
}
}
}