123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- using iTextSharp.text.pdf;
- using iTextSharp.text;
- using Sessioa.BaseModel;
- using Sessioa.Classes;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using Microsoft.Win32;
- using System.Drawing;
- using System.Windows.Documents;
- using System.Data.Entity;
- namespace Sessioa.Pages
- {
- /// <summary>
- /// Логика взаимодействия для OrderCard.xaml
- /// </summary>
- public partial class OrderCard : Page
- {
- List<Product> products = (List<Product>)Application.Current.Properties["ListOrderUser"];
- List<PointsOfIssue> points = BaseClass.C_Base.PointsOfIssue.ToList();
- List<StatusOrder> statesOrder = BaseClass.C_Base.StatusOrder.ToList();
- Dictionary<string, int> keyValuePairs = (Dictionary<string, int>)(Application.Current.Properties["ListOrderCountUser"]);
- User user = null;
- Order orderEdit = null;
- bool state = false;
- public OrderCard(User userSign)
- {
- InitializeComponent();
- user = userSign;
- LB_ProductsOrder.ItemsSource = products;
- OrderTitle.Text = $"Заказ #{BaseClass.C_Base.Order.OrderByDescending(x => x.OrderID).First().OrderID + 1}";
- if (keyValuePairs != null)
- {
- if (keyValuePairs.Count == 0)
- {
- foreach (Product product in products)
- {
- keyValuePairs.Add($"{product.ProductArticleNumber}", 1);
- }
- }
- }
- else
- {
- foreach (Product product in products)
- {
- try
- {
- keyValuePairs.Add($"{product.ProductArticleNumber}", 1);
- }
- catch { }
- }
- }
- CB_Punkt.ItemsSource = points;
- CB_Punkt.DisplayMemberPath = "PointName";
- CB_Punkt.SelectedValuePath = "PointID";
- ItogoSkidka();
- }
- public OrderCard(User userSign, int order_ID)
- {
- InitializeComponent();
- state = true;
- user = userSign;
- if (user.UserRole < 2)
- {
- B_Save.Visibility = Visibility.Collapsed;
- }
- orderEdit = BaseClass.C_Base.Order.FirstOrDefault(x => x.OrderID == order_ID);
- OrderTitle.Text = $"Заказ #{orderEdit.OrderID}";
- if (orderEdit != null)
- {
- foreach (OrderProduct op in BaseClass.C_Base.OrderProduct.Where(x => x.OrderID == orderEdit.OrderID).ToList())
- {
- products.Add(op.Product);
- try
- {
- keyValuePairs.Add($"{op.Product.ProductArticleNumber}", op.ProductCount);
- }
- catch { }
- }
- }
- LB_ProductsOrder.ItemsSource = products;
- B_Save.Content = "Сохранить";
- CB_Punkt.Visibility = Visibility.Collapsed;
- TB_Punkt.Visibility = Visibility.Visible;
- TB_Punkt.Text = $"{points[orderEdit.OrderPickupPoint - 1].PointName}";
- StateOrder.ItemsSource = statesOrder;
- StateOrder.DisplayMemberPath = "StatusTitle";
- StateOrder.SelectedValuePath = "StatusId";
- StateOrder.SelectedItem = statesOrder[BaseClass.C_Base.Order.FirstOrDefault(x => x.OrderID == order_ID).OrderStatus - 1];
- if (orderEdit.OrderDeliveryDate != null)
- DP_Order.SelectedDate = orderEdit.OrderDeliveryDate;
- OrderCode.Text = $"Код выдачи: {orderEdit.IssueCode}";
- ItogoSkidka();
- }
- private void countPr_Loaded(object sender, RoutedEventArgs e)
- {
- TextBlock textBlock = (TextBlock)sender;
- Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
- if (product.ProductDiscountAmount != null)
- textBlock.TextDecorations = TextDecorations.Strikethrough;
- }
- private void countDPr_Loaded(object sender, RoutedEventArgs e)
- {
- TextBlock textBlock = (TextBlock)sender;
- Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
- if (product.ProductDiscountAmount != null)
- {
- textBlock.Visibility = Visibility.Visible;
- textBlock.Text = $" {(double)(product.ProductCost - product.ProductCost * product.ProductDiscountAmount / 100)} ₽";
- }
- }
- private void Discount_Loaded(object sender, RoutedEventArgs e)
- {
- TextBlock textBlock = (TextBlock)sender;
- Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
- if (product.ProductDiscountAmount != null)
- {
- textBlock.Visibility = Visibility.Visible;
- textBlock.Text = $"Скидка {product.ProductDiscountAmount}%";
- }
- }
- private void B_Back_Click(object sender, RoutedEventArgs e)
- {
- if (state == false)
- {
- Application.Current.Properties["ListOrderCountUser"] = keyValuePairs;
- FrameClass.C_Frame.Navigate(new ProductsPage(user));
- }
- else
- {
- Application.Current.Properties["ListOrderCountUser"] = new Dictionary<string, int>();
- Application.Current.Properties["ListOrderUser"] = new List<Product>();
- FrameClass.C_Frame.Navigate(new OrderNew(user));
- }
- }
- public static string filePath = "";
- private void B_Save_Click(object sender, RoutedEventArgs e)
- {
- if (state == false)
- {
- Application.Current.Properties["ListOrderUser"] = new List<Product>();
- Application.Current.Properties["ListOrderCountUser"] = new Dictionary<string, int>();
- if (CB_Punkt.SelectedIndex == -1)
- MessageBox.Show("Выберите пункт выдачи.", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Warning);
- else
- {
- try
- {
- Random rnd = new Random();
- int code = rnd.Next(100, 999);
- Order newOrder = new Order()
- {
- //OrderID = BaseClass.C_Base.Order.OrderByDescending(x => x.OrderID).FirstOrDefault().OrderID + 1,
- OrderStatus = 2,
- OrderDate = DateTime.Now,
- OrderPickupPoint = ((PointsOfIssue)CB_Punkt.SelectedItem).PointID,
- UserID = user.UserID,
- IssueCode = code
- };
- BaseClass.C_Base.Order.Add(newOrder);
- BaseClass.C_Base.SaveChanges();
- foreach (KeyValuePair<string, int> pair in keyValuePairs)
- {
- OrderProduct op = new OrderProduct()
- {
- OrderID = newOrder.OrderID,
- ProductArticleNumber = pair.Key,
- ProductCount = pair.Value
- };
- BaseClass.C_Base.OrderProduct.Add(op);
- }
- BaseClass.C_Base.SaveChanges();
- MessageBoxResult res = MessageBox.Show($"Заказ оформлен. Код для выдачи: {code}. Отследить заказ можно в разделе \"Заказы\" на странице товаров.\nСохранить чек в pdf?", "Уведомление", MessageBoxButton.YesNo, MessageBoxImage.Information);
- if (res == MessageBoxResult.Yes)
- {
- SaveFileDialog dlg = new SaveFileDialog();
- dlg.FileName = $"Талон #{newOrder.OrderID}.pdf";
- dlg.DefaultExt = ".pdf";
- dlg.Filter = "PDF documents (.pdf)|*.pdf";
- if (dlg.ShowDialog() == true)
- {
- try
- {
- decimal itogoBezSkidki = 0;
- decimal itogoSoSkidkoy = 0;
- List<OrderProduct> tmpOP = BaseClass.C_Base.OrderProduct.Where(x => x.OrderID == newOrder.OrderID).ToList();
- foreach (OrderProduct opItem in tmpOP)
- {
- Product prAdd = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == opItem.ProductArticleNumber);
- itogoBezSkidki += prAdd.ProductCost * opItem.ProductCount;
- if (prAdd.ProductDiscountAmount != null && prAdd.ProductDiscountAmount != 0)
- {
- itogoSoSkidkoy += (prAdd.ProductCost - (prAdd.ProductCost * ((decimal)prAdd.ProductDiscountAmount / 100))) * opItem.ProductCount;
- }
- else
- itogoSoSkidkoy += prAdd.ProductCost * opItem.ProductCount;
- }
- newOrder.Cost = itogoSoSkidkoy;
- newOrder.Discount = Math.Round((1 - (itogoSoSkidkoy / itogoBezSkidki)) * 100, 2);
- filePath = dlg.FileName;
- Document document = new Document();
- document.AddLanguage("ru-RU");
- PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
- document.Open();
- BaseFont baseFont = BaseFont.CreateFont("C:\\Windows\\Fonts\\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
- iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, iTextSharp.text.Font.DEFAULTSIZE, iTextSharp.text.Font.NORMAL); //чтобы не было проблем с русскими символами
-
- document.Add(new iTextSharp.text.Paragraph($"Заказ #{newOrder.OrderID}", font));
- document.Add(new iTextSharp.text.Paragraph($"Дата заказа: {newOrder.OrderDate}", font));
- document.Add(new iTextSharp.text.Paragraph($"\nСостав заказа:", font));
- List<OrderProduct> op = BaseClass.C_Base.OrderProduct.Include(x => x.Product).Where(x => x.OrderID == newOrder.OrderID).ToList();
- int i = 1;
- foreach (OrderProduct opItem in op)
- {
- document.Add(new iTextSharp.text.Paragraph($"{i}. " + opItem.ToString(), font));
- i++;
- }
- document.Add(new iTextSharp.text.Paragraph($"Общая скидка: {newOrder.Discount}%", font));
- document.Add(new iTextSharp.text.Paragraph($"Стоимость заказа: {Math.Round(newOrder.Cost, 2)} руб.", font));
- document.Add(new iTextSharp.text.Paragraph($"Пункт выдачи: {BaseClass.C_Base.PointsOfIssue.FirstOrDefault(x=>x.PointID == newOrder.OrderPickupPoint).PointName}", font));
- document.Add(new iTextSharp.text.Paragraph($"Код выдачи: {newOrder.IssueCode}", font));
- document.Close();
- MessageBox.Show("Документ сохранен", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- catch
- {
- MessageBox.Show("Не удалось сохранить документ", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
- FrameClass.C_Frame.Navigate(new ProductsPage(user));
- }
- catch
- {
- MessageBoxResult res = MessageBox.Show("что-то сломалось.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
- else
- {
- orderEdit.OrderStatus = ((StatusOrder)StateOrder.SelectedItem).StatusId;
- if (DP_Order.SelectedDate != null)
- orderEdit.OrderDeliveryDate = DP_Order.SelectedDate;
- BaseClass.C_Base.SaveChanges();
- MessageBoxResult res = MessageBox.Show("Изменения внесены.", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
- FrameClass.C_Frame.Navigate(new OrderNew(user));
- }
- }
- private void Back_B_Click(object sender, RoutedEventArgs e)
- {
- Button bBack = (Button)sender;
- Grid grid = (Grid)bBack.Parent;
- TextBlock textBlock = (TextBlock)grid.FindName("CountProduct");
- Product pr = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == bBack.Uid);
- if (pr != null)
- {
- foreach (KeyValuePair<string, int> pair in keyValuePairs)
- {
- if (pair.Key == bBack.Uid)
- {
- if (pair.Value > 1)
- {
- keyValuePairs[pair.Key]--;
- textBlock.Text = keyValuePairs[pair.Key].ToString();
- break;
- }
- }
- }
- }
- ItogoSkidka();
- }
- private void Next_B_Click(object sender, RoutedEventArgs e)
- {
- Button bNext = (Button)sender;
- Grid grid = (Grid)bNext.Parent;
- TextBlock textBlock = (TextBlock)grid.FindName("CountProduct");
- Product pr = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == bNext.Uid);
- if (pr != null)
- {
- foreach (KeyValuePair<string, int> pair in keyValuePairs)
- {
- if (pair.Key == bNext.Uid)
- {
- keyValuePairs[pair.Key]++;
- textBlock.Text = keyValuePairs[pair.Key].ToString();
- break;
- }
- }
- }
- ItogoSkidka();
- }
- private void CountProduct_Loaded(object sender, RoutedEventArgs e)
- {
- TextBlock tb = (TextBlock)sender;
- if (keyValuePairs.Count > 0)
- {
- foreach (KeyValuePair<string, int> pair in keyValuePairs)
- {
- if (pair.Key == tb.Uid)
- {
- tb.Text = keyValuePairs[pair.Key].ToString();
- break;
- }
- }
- }
- }
- public void ItogoSkidka()
- {
- decimal itogoBezSkidki = 0;
- foreach (Product item in products)
- {
- foreach (KeyValuePair<string, int> pair in keyValuePairs)
- {
- if (pair.Key == item.ProductArticleNumber)
- {
- itogoBezSkidki += item.ProductCost * pair.Value;
- }
- }
- }
- decimal itogoSoSkidkoy = 0;
- foreach (Product item in products)
- {
- foreach (KeyValuePair<string, int> pair in keyValuePairs)
- {
- if (pair.Key == item.ProductArticleNumber)
- {
- if (item.ProductDiscountAmount != null)
- {
- itogoSoSkidkoy += (item.ProductCost - (item.ProductCost * ((decimal)item.ProductDiscountAmount / 100))) * pair.Value;
- }
- else
- itogoSoSkidkoy += item.ProductCost * pair.Value;
- }
- }
- }
- decimal ITOGO = Math.Round((1 - (itogoSoSkidkoy / itogoBezSkidki)) * 100, 2);
- TB_ItogoSkidka.Text = $"Скидка {ITOGO}%";
- TB_Itogo.Text = $"{(double)itogoSoSkidkoy} ₽";
- }
- private void SP_EditOrder_Loaded(object sender, RoutedEventArgs e)
- {
- StackPanel sp = (StackPanel)sender;
- if (state == false || user.UserRole < 2)
- sp.Visibility = Visibility.Collapsed;
- }
- private void Back_B_Loaded(object sender, RoutedEventArgs e)
- {
- Button btn = (Button)sender;
- if (state == true)
- btn.Visibility = Visibility.Collapsed;
- }
- private void Next_B_Loaded(object sender, RoutedEventArgs e)
- {
- Button btn = (Button)sender;
- if (state == true)
- btn.Visibility = Visibility.Collapsed;
- }
- private void OrderCode_Loaded(object sender, RoutedEventArgs e)
- {
- TextBlock tb = (TextBlock)sender;
- if (state == false)
- tb.Visibility = Visibility.Collapsed;
- }
- private void DelProduct_Click(object sender, RoutedEventArgs e)
- {
- foreach (KeyValuePair<string, int> item in keyValuePairs)
- {
- Product tmp = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == ((Product)LB_ProductsOrder.SelectedItem).ProductArticleNumber);
- if (item.Key == tmp.ProductArticleNumber)
- {
- keyValuePairs.Remove(item.Key);
- products.Remove(tmp);
- break;
- }
- }
- LB_ProductsOrder.ItemsSource = null;
- LB_ProductsOrder.ItemsSource = products;
- }
- }
- }
|