OrderCard.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using iTextSharp.text.pdf;
  2. using iTextSharp.text;
  3. using Sessioa.BaseModel;
  4. using Sessioa.Classes;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using Microsoft.Win32;
  12. using System.Drawing;
  13. using System.Windows.Documents;
  14. using System.Data.Entity;
  15. namespace Sessioa.Pages
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для OrderCard.xaml
  19. /// </summary>
  20. public partial class OrderCard : Page
  21. {
  22. List<Product> products = (List<Product>)Application.Current.Properties["ListOrderUser"];
  23. List<PointsOfIssue> points = BaseClass.C_Base.PointsOfIssue.ToList();
  24. List<StatusOrder> statesOrder = BaseClass.C_Base.StatusOrder.ToList();
  25. Dictionary<string, int> keyValuePairs = (Dictionary<string, int>)(Application.Current.Properties["ListOrderCountUser"]);
  26. User user = null;
  27. Order orderEdit = null;
  28. bool state = false;
  29. public OrderCard(User userSign)
  30. {
  31. InitializeComponent();
  32. user = userSign;
  33. LB_ProductsOrder.ItemsSource = products;
  34. OrderTitle.Text = $"Заказ #{BaseClass.C_Base.Order.OrderByDescending(x => x.OrderID).First().OrderID + 1}";
  35. if (keyValuePairs != null)
  36. {
  37. if (keyValuePairs.Count == 0)
  38. {
  39. foreach (Product product in products)
  40. {
  41. keyValuePairs.Add($"{product.ProductArticleNumber}", 1);
  42. }
  43. }
  44. }
  45. else
  46. {
  47. foreach (Product product in products)
  48. {
  49. try
  50. {
  51. keyValuePairs.Add($"{product.ProductArticleNumber}", 1);
  52. }
  53. catch { }
  54. }
  55. }
  56. CB_Punkt.ItemsSource = points;
  57. CB_Punkt.DisplayMemberPath = "PointName";
  58. CB_Punkt.SelectedValuePath = "PointID";
  59. ItogoSkidka();
  60. }
  61. public OrderCard(User userSign, int order_ID)
  62. {
  63. InitializeComponent();
  64. state = true;
  65. user = userSign;
  66. if (user.UserRole < 2)
  67. {
  68. B_Save.Visibility = Visibility.Collapsed;
  69. }
  70. orderEdit = BaseClass.C_Base.Order.FirstOrDefault(x => x.OrderID == order_ID);
  71. OrderTitle.Text = $"Заказ #{orderEdit.OrderID}";
  72. if (orderEdit != null)
  73. {
  74. foreach (OrderProduct op in BaseClass.C_Base.OrderProduct.Where(x => x.OrderID == orderEdit.OrderID).ToList())
  75. {
  76. products.Add(op.Product);
  77. try
  78. {
  79. keyValuePairs.Add($"{op.Product.ProductArticleNumber}", op.ProductCount);
  80. }
  81. catch { }
  82. }
  83. }
  84. LB_ProductsOrder.ItemsSource = products;
  85. B_Save.Content = "Сохранить";
  86. CB_Punkt.Visibility = Visibility.Collapsed;
  87. TB_Punkt.Visibility = Visibility.Visible;
  88. TB_Punkt.Text = $"{points[orderEdit.OrderPickupPoint - 1].PointName}";
  89. StateOrder.ItemsSource = statesOrder;
  90. StateOrder.DisplayMemberPath = "StatusTitle";
  91. StateOrder.SelectedValuePath = "StatusId";
  92. StateOrder.SelectedItem = statesOrder[BaseClass.C_Base.Order.FirstOrDefault(x => x.OrderID == order_ID).OrderStatus - 1];
  93. if (orderEdit.OrderDeliveryDate != null)
  94. DP_Order.SelectedDate = orderEdit.OrderDeliveryDate;
  95. OrderCode.Text = $"Код выдачи: {orderEdit.IssueCode}";
  96. ItogoSkidka();
  97. }
  98. private void countPr_Loaded(object sender, RoutedEventArgs e)
  99. {
  100. TextBlock textBlock = (TextBlock)sender;
  101. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  102. if (product.ProductDiscountAmount != null)
  103. textBlock.TextDecorations = TextDecorations.Strikethrough;
  104. }
  105. private void countDPr_Loaded(object sender, RoutedEventArgs e)
  106. {
  107. TextBlock textBlock = (TextBlock)sender;
  108. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  109. if (product.ProductDiscountAmount != null)
  110. {
  111. textBlock.Visibility = Visibility.Visible;
  112. textBlock.Text = $" {(double)(product.ProductCost - product.ProductCost * product.ProductDiscountAmount / 100)} ₽";
  113. }
  114. }
  115. private void Discount_Loaded(object sender, RoutedEventArgs e)
  116. {
  117. TextBlock textBlock = (TextBlock)sender;
  118. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == textBlock.Uid);
  119. if (product.ProductDiscountAmount != null)
  120. {
  121. textBlock.Visibility = Visibility.Visible;
  122. textBlock.Text = $"Скидка {product.ProductDiscountAmount}%";
  123. }
  124. }
  125. private void B_Back_Click(object sender, RoutedEventArgs e)
  126. {
  127. if (state == false)
  128. {
  129. Application.Current.Properties["ListOrderCountUser"] = keyValuePairs;
  130. FrameClass.C_Frame.Navigate(new ProductsPage(user));
  131. }
  132. else
  133. {
  134. Application.Current.Properties["ListOrderCountUser"] = new Dictionary<string, int>();
  135. Application.Current.Properties["ListOrderUser"] = new List<Product>();
  136. FrameClass.C_Frame.Navigate(new OrderNew(user));
  137. }
  138. }
  139. public static string filePath = "";
  140. private void B_Save_Click(object sender, RoutedEventArgs e)
  141. {
  142. if (state == false)
  143. {
  144. Application.Current.Properties["ListOrderUser"] = new List<Product>();
  145. Application.Current.Properties["ListOrderCountUser"] = new Dictionary<string, int>();
  146. if (CB_Punkt.SelectedIndex == -1)
  147. MessageBox.Show("Выберите пункт выдачи.", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Warning);
  148. else
  149. {
  150. try
  151. {
  152. Random rnd = new Random();
  153. int code = rnd.Next(100, 999);
  154. Order newOrder = new Order()
  155. {
  156. //OrderID = BaseClass.C_Base.Order.OrderByDescending(x => x.OrderID).FirstOrDefault().OrderID + 1,
  157. OrderStatus = 2,
  158. OrderDate = DateTime.Now,
  159. OrderPickupPoint = ((PointsOfIssue)CB_Punkt.SelectedItem).PointID,
  160. UserID = user.UserID,
  161. IssueCode = code
  162. };
  163. BaseClass.C_Base.Order.Add(newOrder);
  164. BaseClass.C_Base.SaveChanges();
  165. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  166. {
  167. OrderProduct op = new OrderProduct()
  168. {
  169. OrderID = newOrder.OrderID,
  170. ProductArticleNumber = pair.Key,
  171. ProductCount = pair.Value
  172. };
  173. BaseClass.C_Base.OrderProduct.Add(op);
  174. }
  175. BaseClass.C_Base.SaveChanges();
  176. MessageBoxResult res = MessageBox.Show($"Заказ оформлен. Код для выдачи: {code}. Отследить заказ можно в разделе \"Заказы\" на странице товаров.\nСохранить чек в pdf?", "Уведомление", MessageBoxButton.YesNo, MessageBoxImage.Information);
  177. if (res == MessageBoxResult.Yes)
  178. {
  179. SaveFileDialog dlg = new SaveFileDialog();
  180. dlg.FileName = $"Талон #{newOrder.OrderID}.pdf";
  181. dlg.DefaultExt = ".pdf";
  182. dlg.Filter = "PDF documents (.pdf)|*.pdf";
  183. if (dlg.ShowDialog() == true)
  184. {
  185. try
  186. {
  187. decimal itogoBezSkidki = 0;
  188. decimal itogoSoSkidkoy = 0;
  189. List<OrderProduct> tmpOP = BaseClass.C_Base.OrderProduct.Where(x => x.OrderID == newOrder.OrderID).ToList();
  190. foreach (OrderProduct opItem in tmpOP)
  191. {
  192. Product prAdd = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == opItem.ProductArticleNumber);
  193. itogoBezSkidki += prAdd.ProductCost * opItem.ProductCount;
  194. if (prAdd.ProductDiscountAmount != null && prAdd.ProductDiscountAmount != 0)
  195. {
  196. itogoSoSkidkoy += (prAdd.ProductCost - (prAdd.ProductCost * ((decimal)prAdd.ProductDiscountAmount / 100))) * opItem.ProductCount;
  197. }
  198. else
  199. itogoSoSkidkoy += prAdd.ProductCost * opItem.ProductCount;
  200. }
  201. newOrder.Cost = itogoSoSkidkoy;
  202. newOrder.Discount = Math.Round((1 - (itogoSoSkidkoy / itogoBezSkidki)) * 100, 2);
  203. filePath = dlg.FileName;
  204. Document document = new Document();
  205. document.AddLanguage("ru-RU");
  206. PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
  207. document.Open();
  208. BaseFont baseFont = BaseFont.CreateFont("C:\\Windows\\Fonts\\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  209. iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, iTextSharp.text.Font.DEFAULTSIZE, iTextSharp.text.Font.NORMAL); //чтобы не было проблем с русскими символами
  210. document.Add(new iTextSharp.text.Paragraph($"Заказ #{newOrder.OrderID}", font));
  211. document.Add(new iTextSharp.text.Paragraph($"Дата заказа: {newOrder.OrderDate}", font));
  212. document.Add(new iTextSharp.text.Paragraph($"\nСостав заказа:", font));
  213. List<OrderProduct> op = BaseClass.C_Base.OrderProduct.Include(x => x.Product).Where(x => x.OrderID == newOrder.OrderID).ToList();
  214. int i = 1;
  215. foreach (OrderProduct opItem in op)
  216. {
  217. document.Add(new iTextSharp.text.Paragraph($"{i}. " + opItem.ToString(), font));
  218. i++;
  219. }
  220. document.Add(new iTextSharp.text.Paragraph($"Общая скидка: {newOrder.Discount}%", font));
  221. document.Add(new iTextSharp.text.Paragraph($"Стоимость заказа: {Math.Round(newOrder.Cost, 2)} руб.", font));
  222. document.Add(new iTextSharp.text.Paragraph($"Пункт выдачи: {BaseClass.C_Base.PointsOfIssue.FirstOrDefault(x=>x.PointID == newOrder.OrderPickupPoint).PointName}", font));
  223. document.Add(new iTextSharp.text.Paragraph($"Код выдачи: {newOrder.IssueCode}", font));
  224. document.Close();
  225. MessageBox.Show("Документ сохранен", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
  226. }
  227. catch
  228. {
  229. MessageBox.Show("Не удалось сохранить документ", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  230. }
  231. }
  232. }
  233. FrameClass.C_Frame.Navigate(new ProductsPage(user));
  234. }
  235. catch
  236. {
  237. MessageBoxResult res = MessageBox.Show("что-то сломалось.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  238. }
  239. }
  240. }
  241. else
  242. {
  243. orderEdit.OrderStatus = ((StatusOrder)StateOrder.SelectedItem).StatusId;
  244. if (DP_Order.SelectedDate != null)
  245. orderEdit.OrderDeliveryDate = DP_Order.SelectedDate;
  246. BaseClass.C_Base.SaveChanges();
  247. MessageBoxResult res = MessageBox.Show("Изменения внесены.", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
  248. FrameClass.C_Frame.Navigate(new OrderNew(user));
  249. }
  250. }
  251. private void Back_B_Click(object sender, RoutedEventArgs e)
  252. {
  253. Button bBack = (Button)sender;
  254. Grid grid = (Grid)bBack.Parent;
  255. TextBlock textBlock = (TextBlock)grid.FindName("CountProduct");
  256. Product pr = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == bBack.Uid);
  257. if (pr != null)
  258. {
  259. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  260. {
  261. if (pair.Key == bBack.Uid)
  262. {
  263. if (pair.Value > 1)
  264. {
  265. keyValuePairs[pair.Key]--;
  266. textBlock.Text = keyValuePairs[pair.Key].ToString();
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. ItogoSkidka();
  273. }
  274. private void Next_B_Click(object sender, RoutedEventArgs e)
  275. {
  276. Button bNext = (Button)sender;
  277. Grid grid = (Grid)bNext.Parent;
  278. TextBlock textBlock = (TextBlock)grid.FindName("CountProduct");
  279. Product pr = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == bNext.Uid);
  280. if (pr != null)
  281. {
  282. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  283. {
  284. if (pair.Key == bNext.Uid)
  285. {
  286. keyValuePairs[pair.Key]++;
  287. textBlock.Text = keyValuePairs[pair.Key].ToString();
  288. break;
  289. }
  290. }
  291. }
  292. ItogoSkidka();
  293. }
  294. private void CountProduct_Loaded(object sender, RoutedEventArgs e)
  295. {
  296. TextBlock tb = (TextBlock)sender;
  297. if (keyValuePairs.Count > 0)
  298. {
  299. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  300. {
  301. if (pair.Key == tb.Uid)
  302. {
  303. tb.Text = keyValuePairs[pair.Key].ToString();
  304. break;
  305. }
  306. }
  307. }
  308. }
  309. public void ItogoSkidka()
  310. {
  311. decimal itogoBezSkidki = 0;
  312. foreach (Product item in products)
  313. {
  314. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  315. {
  316. if (pair.Key == item.ProductArticleNumber)
  317. {
  318. itogoBezSkidki += item.ProductCost * pair.Value;
  319. }
  320. }
  321. }
  322. decimal itogoSoSkidkoy = 0;
  323. foreach (Product item in products)
  324. {
  325. foreach (KeyValuePair<string, int> pair in keyValuePairs)
  326. {
  327. if (pair.Key == item.ProductArticleNumber)
  328. {
  329. if (item.ProductDiscountAmount != null)
  330. {
  331. itogoSoSkidkoy += (item.ProductCost - (item.ProductCost * ((decimal)item.ProductDiscountAmount / 100))) * pair.Value;
  332. }
  333. else
  334. itogoSoSkidkoy += item.ProductCost * pair.Value;
  335. }
  336. }
  337. }
  338. decimal ITOGO = Math.Round((1 - (itogoSoSkidkoy / itogoBezSkidki)) * 100, 2);
  339. TB_ItogoSkidka.Text = $"Скидка {ITOGO}%";
  340. TB_Itogo.Text = $"{(double)itogoSoSkidkoy} ₽";
  341. }
  342. private void SP_EditOrder_Loaded(object sender, RoutedEventArgs e)
  343. {
  344. StackPanel sp = (StackPanel)sender;
  345. if (state == false || user.UserRole < 2)
  346. sp.Visibility = Visibility.Collapsed;
  347. }
  348. private void Back_B_Loaded(object sender, RoutedEventArgs e)
  349. {
  350. Button btn = (Button)sender;
  351. if (state == true)
  352. btn.Visibility = Visibility.Collapsed;
  353. }
  354. private void Next_B_Loaded(object sender, RoutedEventArgs e)
  355. {
  356. Button btn = (Button)sender;
  357. if (state == true)
  358. btn.Visibility = Visibility.Collapsed;
  359. }
  360. private void OrderCode_Loaded(object sender, RoutedEventArgs e)
  361. {
  362. TextBlock tb = (TextBlock)sender;
  363. if (state == false)
  364. tb.Visibility = Visibility.Collapsed;
  365. }
  366. private void DelProduct_Click(object sender, RoutedEventArgs e)
  367. {
  368. foreach (KeyValuePair<string, int> item in keyValuePairs)
  369. {
  370. Product tmp = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == ((Product)LB_ProductsOrder.SelectedItem).ProductArticleNumber);
  371. if (item.Key == tmp.ProductArticleNumber)
  372. {
  373. keyValuePairs.Remove(item.Key);
  374. products.Remove(tmp);
  375. break;
  376. }
  377. }
  378. LB_ProductsOrder.ItemsSource = null;
  379. LB_ProductsOrder.ItemsSource = products;
  380. }
  381. }
  382. }