Corzina.xaml.cs 8.1 KB

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