CardOrder.xaml.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using UP_Venediktov.Classes;
  16. using UP_Venediktov.ModelBase;
  17. namespace UP_Venediktov.Pages
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для CardOrder.xaml
  21. /// </summary>
  22. public partial class CardOrder : Page
  23. {
  24. Entities DB = new Entities();
  25. public static List<Product> Products = new List<Product>();
  26. public static List<AmountofProduct> AmountofProducts = new List<AmountofProduct>();
  27. public decimal? TotalCost = 0;
  28. public CardOrder()
  29. {
  30. InitializeComponent();
  31. CartList.ItemsSource = Products;
  32. foreach (Product p in Products)
  33. {
  34. TotalCost += p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount);
  35. }
  36. CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
  37. }
  38. public CardOrder(int id)
  39. {
  40. InitializeComponent();
  41. CartList.ItemsSource = Products;
  42. foreach (Product p in Products)
  43. {
  44. TotalCost += p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount);
  45. }
  46. CostOrder.Text = string.Format("{0:C2}", TotalCost);
  47. }
  48. private void CostCalculate(object sender, RoutedEventArgs e)
  49. {
  50. TextBlock tb = (TextBlock)sender;
  51. string Article = tb.Uid.ToString();
  52. Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
  53. if (pr.ProductDiscountAmount > 0)
  54. {
  55. tb.Text = "Цена за единицу: " + string.Format("{0:C2}", pr.ProductCost - (pr.ProductCost / 100 * pr.ProductDiscountAmount));
  56. }
  57. }
  58. private void Photo(object sender, RoutedEventArgs e)
  59. {
  60. Image pic = (Image)sender;
  61. string Article = pic.Uid.ToString();
  62. string photoPath = DB.Product.Where(x => x.ProductArticleNumber == Article).Select(x => x.ProductPhoto).FirstOrDefault();
  63. if (photoPath != null && photoPath != "")
  64. {
  65. pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\" + photoPath, UriKind.RelativeOrAbsolute));
  66. }
  67. else
  68. {
  69. pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\picture.png", UriKind.RelativeOrAbsolute));
  70. }
  71. }
  72. private void AmountCalculate(object sender, RoutedEventArgs e)
  73. {
  74. TextBlock tb = (TextBlock)sender;
  75. string Article = tb.Uid.ToString();
  76. Product pr = Products.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
  77. int index = Products.IndexOf(pr);
  78. tb.Text = AmountofProducts[index].Amount.ToString();
  79. }
  80. private void MinusAmount(object sender, RoutedEventArgs e)
  81. {
  82. Button bt = (Button)sender;
  83. string article = bt.Uid.ToString();
  84. Product pr = Products.Where(x => x.ProductArticleNumber == article).First();
  85. AmountofProduct pa = AmountofProducts.Where(x => x.Article == article).First();
  86. if ((pa.Amount - 1) == 0)
  87. {
  88. AmountofProducts.Remove(pa);
  89. Products.Remove(pr);
  90. if (Products.Count == 0)
  91. {
  92. MessageBox.Show("Корзина пуста");
  93. }
  94. }
  95. else
  96. {
  97. foreach (AmountofProduct pp in AmountofProducts)
  98. {
  99. if (pp.Article == article)
  100. {
  101. pp.Amount--;
  102. }
  103. }
  104. }
  105. TotalCost = 0;
  106. foreach (Product p in Products)
  107. {
  108. TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount)) * AmountofProducts.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).First();
  109. }
  110. CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
  111. CartList.ItemsSource = Products.ToList();
  112. }
  113. private void PlusAmount(object sender, RoutedEventArgs e)
  114. {
  115. Button bt = (Button)sender;
  116. string article = bt.Uid.ToString();
  117. foreach (AmountofProduct pa in AmountofProducts)
  118. {
  119. if (pa.Article == article)
  120. {
  121. pa.Amount++;
  122. }
  123. }
  124. TotalCost = 0;
  125. foreach (Product p in Products)
  126. {
  127. TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount)) * AmountofProducts.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).First();
  128. }
  129. CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
  130. CartList.ItemsSource = Products.ToList();
  131. }
  132. private void DeleteProduct(object sender, RoutedEventArgs e)
  133. {
  134. Button bt = (Button)sender;
  135. string article = bt.Uid.ToString();
  136. Product pr = Products.Where(x => x.ProductArticleNumber == article).First();
  137. AmountofProduct pa = AmountofProducts.Where(x => x.Article == article).First();
  138. AmountofProducts.Remove(pa);
  139. Products.Remove(pr);
  140. if (Products.Count == 0)
  141. {
  142. MessageBox.Show("Корзина пуста");
  143. }
  144. TotalCost = 0;
  145. foreach (Product p in Products)
  146. {
  147. TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductDiscountAmount)) * AmountofProducts.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).First();
  148. }
  149. CostOrder.Text = "Стоимость заказа: " + string.Format("{0:C2}", TotalCost);
  150. CartList.ItemsSource = Products.ToList();
  151. }
  152. private void CreateOrder(object sender, RoutedEventArgs e)
  153. {
  154. Random random = new Random();
  155. Order newOrder = new Order();
  156. newOrder.OrderDate = DateTime.Now;
  157. bool ThreeOrLess = false;
  158. foreach (Product p in Products)
  159. {
  160. if (p.ProductQuantityInStock <= 3)
  161. {
  162. ThreeOrLess = true;
  163. }
  164. }
  165. if (ThreeOrLess)
  166. {
  167. newOrder.OrderDeliveryDate = DateTime.Now.AddDays(6);
  168. }
  169. else
  170. {
  171. newOrder.OrderDeliveryDate = DateTime.Now.AddDays(3);
  172. }
  173. newOrder.OrderPickupPoint = random.Next(1, 36);
  174. newOrder.OrderCode = random.Next(100, 999);
  175. newOrder.OrderStatus = "Новый";
  176. DB.Order.Add(newOrder);
  177. DB.SaveChanges();
  178. int id = DB.Order.Where(x => x.OrderDate == newOrder.OrderDate).Select(x => x.OrderID).FirstOrDefault();
  179. foreach (Product p in Products)
  180. {
  181. OrderProduct newOrderProduct = new OrderProduct();
  182. newOrderProduct.OrderID = id;
  183. newOrderProduct.ProductArticleNumber = p.ProductArticleNumber;
  184. newOrderProduct.ProductCont = AmountofProducts.Where(x => x.Article == p.ProductArticleNumber).Select(x => x.Amount).FirstOrDefault();
  185. DB.OrderProduct.Add(newOrderProduct);
  186. }
  187. DB.SaveChanges();
  188. MessageBox.Show("Заказ был сформирован, код получения: " + newOrder.OrderCode + " \nПункт выдачи: " +
  189. DB.PickupPoint.Where(x => x.Id_PickupPoint == newOrder.OrderPickupPoint).Select(x => x.AddressPP).FirstOrDefault() +
  190. "\nЗаказ будет доставлен " + newOrder.OrderDeliveryDate.ToShortDateString());
  191. Products.Clear();
  192. AmountofProducts.Clear();
  193. FrameClass.MainFrame.Navigate(new ProductListUser());
  194. }
  195. }
  196. }