Exemple #1
0
        private void toDayDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var db = new MyShopEntities();

            var orders       = db.OrderDetails;
            var products     = db.Products;
            var orderdetails = db.OrderDetails;

            if (fromDayDatePicker.SelectedDate != null)
            {
                var from = (DateTime)fromDayDatePicker.SelectedDate;
                var to   = (DateTime)toDayDatePicker.SelectedDate;

                if (DateTime.Compare(from, to) <= 0)
                {
                    var query = from orderdetail in orderdetails
                                join p in products on orderdetail.ProductId equals p.Product_Id
                                group p by p.CatId into pGroup
                                select new
                    {
                        Sum = orderdetails.Sum(o => o.Total)
                    };
                }
                else
                {
                    var dialogError = new Messenge()
                    {
                        Message = "The end date must be greater than the start date."
                    };
                    dialogError.Sounds();
                    dialogError.Owner = Window.GetWindow(this);
                    dialogError.ShowDialog();
                    return;
                }
            }
            else
            {
                var dialogError = new Messenge()
                {
                    Message = "Please select a start date."
                };
                dialogError.Sounds();
                dialogError.Owner = Window.GetWindow(this);
                dialogError.ShowDialog();
                return;
            }
        }
Exemple #2
0
        /// <summary>
        /// Import data từ file excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImportExcel_MouseEnter(object sender, MouseButtonEventArgs e)
        {
            var imPortExel = new OpenFileDialog();

            if (imPortExel.ShowDialog() == true)
            {
                var filename = imPortExel.FileName;
                var fileinfo = new FileInfo(filename);

                var excelFile = new Workbook(filename);
                var tabs      = excelFile.Worksheets;

                var db = new MyShopEntities();
                foreach (var tab in tabs)
                {
                    var category = new Category()
                    {
                        Category_Name = tab.Name
                    };
                    db.Categories.Add(category);
                    db.SaveChanges();

                    var row = 3;

                    var cell = tab.Cells[$"C3"];
                    while (cell.Value != null)
                    {
                        var product = new Product()
                        {
                            CatId        = category.Category_Id,
                            SKU          = tab.Cells[$"C{row}"].StringValue,
                            Product_Name = tab.Cells[$"D{row}"].StringValue,
                            Price        = tab.Cells[$"E{row}"].IntValue,
                            Quantity     = tab.Cells[$"F{row}"].IntValue,
                            Description  = tab.Cells[$"G{row}"].StringValue
                        };

                        category.Products.Add(product);
                        db.SaveChanges();

                        var imageName = tab.Cells[$"H{row}"].StringValue;
                        var imageFull = $"{fileinfo.Directory}\\images\\{imageName}";
                        var image     = new BitmapImage(new Uri(imageFull, UriKind.Absolute));
                        var encoder   = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(image));

                        using (var stream = new MemoryStream())
                        {
                            encoder.Save(stream);
                            var photo = new Photo()
                            {
                                ProductId = product.Product_Id,
                                Data      = stream.ToArray()
                            };
                            db.Photos.Add(photo);
                            db.SaveChanges();
                        }
                        row++;
                        cell = tab.Cells[$"C{row}"];
                    }
                }

                var dialog = new Dialog()
                {
                    Message = "Successfully imported data!"
                };
                dialog.SoundsSuccessful();
                dialog.Owner = Window.GetWindow(this);
                dialog.ShowDialog();
            }
        }
 private DataProvider()
 {
     db = new MyShopEntities();
 }