Browse Source

+ shopping cart

ivana 7 months ago
parent
commit
640421a02b

+ 14 - 0
WriteErase/AmountPR.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace WriteErase
+{
+    public class AmountPR
+    {
+        public string Article;
+        public int Amount;
+    }
+}

+ 1 - 1
WriteErase/App.config

@@ -8,7 +8,7 @@
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
   </startup>
   <connectionStrings>
-    <add name="TradeEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sql;initial catalog=TradeAstashin;user id=33П;password=12357;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
+    <add name="TradeEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ngknn.ru;initial catalog=TradeAstashin;user id=33П;password=12357;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
   </connectionStrings>
   <entityFramework>
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">

+ 11 - 0
WriteErase/Pages/Guest.xaml

@@ -16,6 +16,8 @@
         </Grid.RowDefinitions>
         
         <StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal">
+
+            
             <GroupBox Header="Сортировка" Margin="10,0,0,0">
                 <ComboBox Foreground="#4f4f4f" FontSize="15" VerticalContentAlignment="Center" SelectionChanged="SortFilterChanged" Name="SortByName" Height="30" Margin="10,0,0,0" Width="200"/>
             </GroupBox>
@@ -25,6 +27,10 @@
             <GroupBox Header="Поиск" Margin="5,0,0,0">
                 <TextBox Background="#0000" FontSize="15" VerticalContentAlignment="Center" BorderBrush="#0000" Foreground="#4f4f4f" TextChanged="SearchChanged" Name="SearchByName" Height="30" TextAlignment="Left"  Margin="10,0,0,0"  Width="200"/>
             </GroupBox>
+            <TextBlock VerticalAlignment="Center" TextAlignment="Center" FontSize="15" Margin="5,0,0,0" Foreground="#4f4f4f" Name="AmountOfData"/>
+            <Border BorderThickness="1" CornerRadius="7" BorderBrush="#bababa" Background="LightGray" Width="80" Height="40" Margin="5,8,0,0" >
+                <Button Click="CartGo" Name="ToCart" Content="Корзина" Visibility="Visible" Background="#0000" BorderBrush="#0000" FontSize="14"></Button>
+            </Border>
         </StackPanel>
 
         <ListView Grid.Row="1" Name="ProductList" d:ItemsSource="{d:SampleData ItemCount=5}">
@@ -37,6 +43,11 @@
                 <DataTemplate>
                     <Border  Width="800" Height="170" BorderThickness="1" BorderBrush="Black">
                         <StackPanel>
+                            <StackPanel.ContextMenu>
+                                <ContextMenu>
+                                    <MenuItem Header="Добавить в корзину" Uid="{Binding ProductArticleNumber}" Click="CartAdd"/>
+                                </ContextMenu>
+                            </StackPanel.ContextMenu>
                             <Grid Width="800" Height="170"  Loaded="BackgroundLoad" Uid="{Binding ProductArticleNumber}">
                                 <StackPanel Orientation="Horizontal">
                                     <Image Width="150" Height="150" Margin="10 0 0 0" Grid.Column="0" Uid="{Binding ProductArticleNumber}" Loaded="Photo"/>

+ 36 - 0
WriteErase/Pages/Guest.xaml.cs

@@ -12,6 +12,7 @@ using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
+using WriteErase.Windows;
 
 namespace WriteErase.Pages
 {
@@ -22,6 +23,7 @@ namespace WriteErase.Pages
     {
 
         TradeEntities DB = new TradeEntities();
+        int AllData;
 
         public Guest()
         {
@@ -31,6 +33,8 @@ namespace WriteErase.Pages
             SortByName.ItemsSource = new List<string>() { "Без сортировки", "По имени А-Я", "По имени Я-А" };
             SortByName.SelectedIndex = 0;
             FilterByPromo.SelectedIndex = 0;
+            AllData = ProductList.Items.Count;
+            AmountOfData.Text = AllData + " из " + AllData + " товаров";
         }
 
         public Guest(int id)
@@ -133,6 +137,7 @@ namespace WriteErase.Pages
                 products = products.Where(x => x.ProductName.ToUpper().Contains(SearchByName.Text.ToUpper())).ToList();
             }
             ProductList.ItemsSource = products;
+            AmountOfData.Text = products.Count() + " из " + AllData + " товаров";
         }
 
         private void SortFilterChanged(object sender, SelectionChangedEventArgs e)
@@ -144,5 +149,36 @@ namespace WriteErase.Pages
         {
             Filter();
         }
+        private void CartAdd(object sender, RoutedEventArgs e)
+        {
+            MenuItem bt = (MenuItem)sender;
+            string article = bt.Uid.ToString();
+            Product pr = DB.Product.Where(x => x.ProductArticleNumber == article).First();
+            AmountPR pa = new AmountPR();
+            if (ShoppingCart.Products.Count != 0 && ShoppingCart.Products.Contains(pr))
+            {
+                pa = ShoppingCart.ProductsAmount.Where(x => x.Article == article).First();
+            }
+            if (pa.Article != null)
+            {
+                int index = ShoppingCart.ProductsAmount.IndexOf(pa);
+                ShoppingCart.ProductsAmount[index].Amount++;
+            }
+            else
+            {
+                pa.Amount = 1;
+                pa.Article = article;
+                ShoppingCart.Products.Add(pr);
+                ShoppingCart.ProductsAmount.Add(pa);
+            }
+            ToCart.Visibility = Visibility.Visible;
+        }
+
+        private void CartGo(object sender, RoutedEventArgs e)
+        {
+            ShoppingCart ct = new ShoppingCart();
+            ct.ShowDialog();
+        }
+
     }
 }

+ 61 - 0
WriteErase/Windows/ShoppingCart.xaml

@@ -0,0 +1,61 @@
+<Window x:Class="WriteErase.Windows.ShoppingCart"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:WriteErase.Windows"
+        mc:Ignorable="d"
+        Title="ShoppingCart" Height="450" Width="800">
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="10*"/>
+            <RowDefinition Height="*"/>
+        </Grid.RowDefinitions>
+        <ListView Grid.Row="0" Name="CartList" d:ItemsSource="{d:SampleData ItemCount=5}" >
+            <ListView.ItemsPanel>
+                <ItemsPanelTemplate>
+                    <WrapPanel Orientation="Vertical" HorizontalAlignment="Center"/>
+                </ItemsPanelTemplate>
+            </ListView.ItemsPanel>
+            <ListView.ItemTemplate>
+                <DataTemplate>
+                    <Border  Width="800" Height="170"  BorderThickness="1" BorderBrush="Black">
+                        <Grid Width="800" Height="170">
+                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
+                                <Image Width="150" Height="150" Margin="10 0 0 0" Uid="{Binding ProductArticleNumber}" Loaded="Photo"/>
+                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
+                                    <TextBlock FontSize="15" Margin="30,0,0,0" Width="100" TextWrapping="Wrap" Text="{Binding StringFormat=Артикль: {0}, Path=ProductArticleNumber}"/>
+                                    <TextBlock FontSize="15" Margin="10,0,0,0" Width="100" TextWrapping="Wrap" Text="{Binding ProductName}"/>
+                                    <TextBlock FontSize="15" Margin="10,0,0,0" Width="100" TextWrapping="Wrap" Uid="{Binding ProductArticleNumber}" Loaded="CostCalculate"/>
+                                    <TextBlock FontSize="15" Margin="10,0,0,0" Width="100" TextWrapping="Wrap" Text="{Binding ProductDescription}"/>
+                                    <StackPanel Margin="10,0,0,0" Orientation="Vertical">
+                                        <StackPanel Orientation="Horizontal">
+                                            <Border BorderThickness="1" CornerRadius="7" BorderBrush="#bababa" Width="30" Height="30" Background="LightGray" Margin="5,0,0,0" >
+                                                <Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="#0000" BorderBrush="#0000" FontSize="14"  Uid="{Binding ProductArticleNumber}" Click="MinusAmount" Content="➖"/>
+                                            </Border>
+                                            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Margin="10,0,0,0" Uid="{Binding ProductArticleNumber}" Loaded="AmountCalculate"/>
+                                            <Border BorderThickness="1" CornerRadius="7" BorderBrush="#bababa" Width="30" Height="30" Background="LightGray" Margin="10,0,0,0" >
+                                                <Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="#0000" BorderBrush="#0000" FontSize="14" Uid="{Binding ProductArticleNumber}" Click="PlusAmount" Content="➕"/>
+                                            </Border>
+                                        </StackPanel>
+                                        <Border BorderThickness="1" CornerRadius="7" BorderBrush="#bababa" Width="80" Height="30" Background="LightGray" Margin="5,5,0,0" >
+                                            <Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="#0000" BorderBrush="#0000" FontSize="14"  Uid="{Binding ProductArticleNumber}" Click="DeleteProduct" Content="Удалить"/>
+                                        </Border>
+                                    </StackPanel>
+                                </StackPanel>
+                            </StackPanel>
+                        </Grid>
+                    </Border>
+                </DataTemplate>
+            </ListView.ItemTemplate>
+        </ListView>
+
+        <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
+            <TextBlock FontSize="15" VerticalAlignment="Center" Name="CostOrder"/>
+            <Border BorderThickness="1" CornerRadius="7" BorderBrush="#bababa" Width="100" Height="30" Background="LightGray" Margin="5,0,0,0" >
+                <Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="#0000" BorderBrush="#0000" FontSize="14" Click="CreateOrder" Content="Заказать"/>
+            </Border>
+            
+        </StackPanel>
+    </Grid>
+</Window>

+ 201 - 0
WriteErase/Windows/ShoppingCart.xaml.cs

@@ -0,0 +1,201 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+
+namespace WriteErase.Windows
+{
+    /// <summary>
+    /// Логика взаимодействия для ShoppingCart.xaml
+    /// </summary>
+    public partial class ShoppingCart : Window
+    {
+
+        TradeEntities DB = new TradeEntities();
+        public static List<Product> Products = new List<Product>();
+        public static List<AmountPR> ProductsAmount = new List<AmountPR>();
+        public decimal? TotalCost = 0;
+        public ShoppingCart()
+        {
+            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 ShoppingCart(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)
+        {
+            TextBlock tb = (TextBlock)sender;
+            string Article = tb.Uid.ToString();
+            Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
+            if (pr.ProductMaxDiscountAmount > 0)
+            {
+                tb.Text = "Цена за единицу: " + string.Format("{0:C2}", pr.ProductCost - (pr.ProductCost / 100 * pr.ProductMaxDiscountAmount));
+            }
+        }
+
+        private void Photo(object sender, RoutedEventArgs e)
+        {
+            Image pic = (Image)sender;
+            string Article = pic.Uid.ToString();
+            string photoPath = DB.Product.Where(x => x.ProductArticleNumber == Article).Select(x => x.ProductPhoto).FirstOrDefault();
+            if (photoPath != null && photoPath != "")
+            {
+                pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Picture\\" + photoPath, UriKind.RelativeOrAbsolute));
+            }
+            else
+            {
+                pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Picture\\picture.png", UriKind.RelativeOrAbsolute));
+            }
+        }
+
+        private void AmountCalculate(object sender, RoutedEventArgs e)
+        {
+            TextBlock tb = (TextBlock)sender;
+            string Article = tb.Uid.ToString();
+            Product pr = Products.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
+            int index = Products.IndexOf(pr);
+            tb.Text = ProductsAmount[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();
+            AmountPR pa = ProductsAmount.Where(x => x.Article == article).First();
+            if ((pa.Amount - 1) == 0)
+            {
+                ProductsAmount.Remove(pa);
+                Products.Remove(pr);
+                if (Products.Count == 0)
+                {
+                    this.Close();
+                }
+            }
+            else
+            {
+
+                foreach (AmountPR pp in ProductsAmount)
+                {
+                    if (pp.Article == article)
+                    {
+                        pp.Amount--;
+                    }
+                }
+            }
+            TotalCost = 0;
+            foreach (Product p in Products)
+            {
+                TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount)) * ProductsAmount.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 (AmountPR pa in ProductsAmount)
+            {
+                if (pa.Article == article)
+                {
+                    pa.Amount++;
+                }
+            }
+            TotalCost = 0;
+            foreach (Product p in Products)
+            {
+                TotalCost += (p.ProductCost - (p.ProductCost / 100 * p.ProductMaxDiscountAmount)) * ProductsAmount.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();
+            AmountPR pa = ProductsAmount.Where(x => x.Article == article).First();
+            ProductsAmount.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)) * ProductsAmount.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 = ProductsAmount.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());
+        }
+    }
+}

+ 8 - 0
WriteErase/WriteErase.csproj

@@ -64,6 +64,7 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
+    <Compile Include="AmountPR.cs" />
     <Compile Include="Categories.cs">
       <DependentUpon>Model1.tt</DependentUpon>
     </Compile>
@@ -111,6 +112,9 @@
     <Compile Include="User.cs">
       <DependentUpon>Model1.tt</DependentUpon>
     </Compile>
+    <Compile Include="Windows\ShoppingCart.xaml.cs">
+      <DependentUpon>ShoppingCart.xaml</DependentUpon>
+    </Compile>
     <Page Include="MainWindow.xaml">
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
@@ -140,6 +144,10 @@
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
     </Page>
+    <Page Include="Windows\ShoppingCart.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Model1.Context.cs">