Иван Прытов 8 ay önce
ebeveyn
işleme
686c2c5cb2
2 değiştirilmiş dosya ile 136 ekleme ve 4 silme
  1. 4 4
      GGwp/Window/Cart.xaml
  2. 132 0
      GGwp/Window/Cart.xaml.cs

+ 4 - 4
GGwp/Window/Cart.xaml

@@ -30,11 +30,11 @@
                                     <TextBlock Foreground="Green" Margin="10,0,0,0" Text="{Binding ProductDescription}"/>
                                     <StackPanel  Margin="10,0,0,0" Orientation="Vertical">
                                         <StackPanel Orientation="Horizontal">
-                                            <Button Content="-"/>
-                                            <TextBlock Foreground="Green" Margin="10,0,0,0" Uid="{Binding ProductArticleNumber}" Loaded="AmountCalculate"/>
-                                            <Button Margin="10,0,0,0" Content="+"/>
+                                            <Button Uid="{Binding ProductArticleNumber}" Click="MinusAmount" Content="-"/>
+                                            <TextBlock Margin="10,0,0,0" Uid="{Binding ProductArticleNumber}" Loaded="AmountCalculate"/>
+                                            <Button Uid="{Binding ProductArticleNumber}" Click="PlusAmount" Margin="10,0,0,0" Content="+"/>
                                         </StackPanel>
-                                        <Button BorderBrush="White" BorderThickness="2" Foreground="Green" Background="LightGreen" Margin="0,10,0,0" Content="Удалить"/>
+                                        <Button  Uid="{Binding ProductArticleNumber}" Click="DeleteProduct" Margin="0,10,0,0" Content="Удалить"/>
                                     </StackPanel>
                                 </StackPanel>
                             </Grid>

+ 132 - 0
GGwp/Window/Cart.xaml.cs

@@ -23,16 +23,27 @@ namespace GGwp
         TradePrytovEntities DB = new TradePrytovEntities();
         public static List<Product> Products = new List<Product>();
         public static List<AmountProduct> AmountProduct = new List<AmountProduct>();
+        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)
@@ -70,5 +81,126 @@ namespace GGwp
             tb.Text = AmountProduct[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();
+            AmountProduct pa = AmountProduct.Where(x => x.Article == article).First();
+            if ((pa.Amount - 1) == 0)
+            {
+                AmountProduct.Remove(pa);
+                Products.Remove(pr);
+                if (Products.Count == 0)
+                {
+                    this.Close();
+                }
+            }
+            else
+            {
+
+                foreach (AmountProduct pp in AmountProduct)
+                {
+                    if (pp.Article == article)
+                    {
+                        pp.Amount--;
+                    }
+                }
+            }
+            TotalCost = 0;
+            foreach (Product p in Products)
+            {
+                TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount)) * AmountProduct.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 (AmountProduct pa in AmountProduct)
+            {
+                if (pa.Article == article)
+                {
+                    pa.Amount++;
+                }
+            }
+            TotalCost = 0;
+            foreach (Product p in Products)
+            {
+                TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount)) * AmountProduct.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();
+            AmountProduct pa = AmountProduct.Where(x => x.Article == article).First();
+            AmountProduct.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)) * AmountProduct.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 =     AmountProduct.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());
+            Products.Clear();
+            AmountProduct.Clear();
+            this.Close();
+        }
+
     }
 }