private void UserControl_Initialized(object sender, EventArgs e)
        {
            //Hien thi danh sach san pham de ban hang
            SanPhamDatabaseEntities productDB = new SanPhamDatabaseEntities();
            var productElement = productDB.Products.Select(p => p);

            list = productElement.ToList();
            HienThiToanBoListView.ItemsSource = list;


            #region PHÂN TRANG
            //Phan trang
            Total = productDB.Products.ToList().Count();
            //Get products quantity of every categories.
            this.DataContext = this;

            itemsPerPage = int.Parse(itemsPerPageComboBox.Text);
            totalPages   = (int)Math.Ceiling((decimal)Total / itemsPerPage);

            HienThiToanBoListView.ItemsSource = list
                                                .Skip((currentPage - 1) * itemsPerPage)
                                                .Take(itemsPerPage);

            pagingInfoLabel.Content = $"Trang {currentPage} của {totalPages}";
            #endregion
        }
        private void XoaSanPham_Click(object sender, RoutedEventArgs e)
        {
            var pro = (sender as Button).DataContext as Product;

            if (pro != null)
            {
                var data    = new SanPhamDatabaseEntities();
                var getData = (
                    from x in data.Products
                    where x.IDProduct == pro.IDProduct
                    select x
                    ).ToList().Last();
                MessageBoxResult result = MessageBox.Show("Bạn có muốn xóa không ?", "Thông báo", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    try
                    {
                        data.Products.Remove(getData);
                        data.SaveChanges();
                        UserControl_Initialized(sender, e);
                        MessageBox.Show("Xóa thành công !");
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Bạn không thể xóa sản phẩm này !");
                    }
                }
            }
            else
            {
                MessageBox.Show("Xóa không thành công !");
            }
        }
        private void ItemsPerPageComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var combobox = sender as ComboBox;
            var item     = combobox.SelectedValue.ToString();

            string temp = item.Split(' ')[1];

            if (temp.Equals("Tất_cả") == false)
            {
                itemsPerPage = int.Parse(temp);
                totalPages   = (int)Math.Ceiling((decimal)Total / itemsPerPage);

                HienThiToanBoListView.ItemsSource = list
                                                    .Skip((currentPage - 1) * itemsPerPage)
                                                    .Take(itemsPerPage);

                pagingInfoLabel.Content = $"Trang {currentPage} của {totalPages}";
            }
            else
            {
                SanPhamDatabaseEntities db = new SanPhamDatabaseEntities();
                itemsPerPage = (from x in db.Products select x).ToList().Count();
                totalPages   = (int)Math.Ceiling((decimal)Total / itemsPerPage);

                HienThiToanBoListView.ItemsSource = list
                                                    .Skip((currentPage - 1) * itemsPerPage)
                                                    .Take(itemsPerPage);

                pagingInfoLabel.Content = $"Trang {currentPage} của {totalPages}";
            }
        }
        private void OkBtn_Click(object sender, RoutedEventArgs e)
        {
            SanPhamDatabaseEntities _data = new SanPhamDatabaseEntities();
            var getdata = (
                from x in _data.Products
                where x.IDProduct == _newProduct.IDProduct
                select x
                ).ToList().Last();

            _newProduct.NameProduct     = getdata.NameProduct = txtUpdateName.Text;
            _newProduct.PriceProduct    = getdata.PriceProduct = decimal.Parse(txtUpdatePrice.Text);
            _newProduct.QuantityProduct = getdata.QuantityProduct = int.Parse(txtUpdateQuantity.Text);
            _newProduct.InfoProduct     = getdata.InfoProduct = txtUpdateInfo.Text;
            if (NewName != null)
            {
                _newProduct.ImageProduct = getdata.ImageProduct = NewName;
            }

            _data.SaveChanges();
            MessageBox.Show("Cập nhật thành công !", "Thông báo");
        }
예제 #5
0
        private void LuuBtn_Click(object sender, RoutedEventArgs e)
        {
            if (_detail.Count != 0)
            {
                SanPhamDatabaseEntities db = new SanPhamDatabaseEntities();
                //Tạo dữ liệu cho Đơn hàng
                Purchase order;
                if (DaThanhToanCheckBox.IsChecked != true)
                {
                    order = new Purchase
                    {
                        CreatedAt = DateTime.Now,
                        UpdateAt  = DateTime.Now,
                        Status    = "New",
                        Total     = FinalTotal
                    };
                }
                else
                {
                    order = new Purchase
                    {
                        CreatedAt = DateTime.Now,
                        UpdateAt  = DateTime.Now,
                        Status    = "Complete",
                        Total     = FinalTotal
                    };
                }
                db.Purchases.Add(order);
                db.SaveChanges();

                foreach (var detail in _detail)
                {
                    //Tạo dữ liệu từ _detail để thêm vào CSDL
                    var orderDetail = new OrderDetail()
                    {
                        ProductID           = detail.ProductID,
                        PriceOrderDetail    = detail.PriceOrderDetail,
                        QuantityOrderDetail = detail.QuantityOrderDetail,
                        TotalOrderDetal     = detail.TotalOrderDetal,
                        //Lấy ID đơn hàng vừa tạo để update cho chi tiết đơn hàng
                        PurchaseID = order.IDPurchase,
                        CreatedAt  = DateTime.Now,
                        UpdateAt   = DateTime.Now
                    };
                    db.OrderDetails.Add(orderDetail);
                }
                db.SaveChanges();

                //Làm mới _detail = Chi tiết đơn hàng
                _detail = new BindingList <OrderDetail>();

                donHangDataGrid.ItemsSource = _detail;
                NotifyChange("FinalTotal");

                BrushConverter bc    = new BrushConverter();
                Brush          brush = (Brush)bc.ConvertFrom("#1CBFAE");
                brush.Freeze();
                ThongBaoSnackbar.Background = brush;
                ThongBaoSnackbar.MessageQueue.Enqueue("Thêm đơn hàng thành công!");
                TotalProduct = 0;
            }
            else
            {
                BrushConverter bc    = new BrushConverter();
                Brush          brush = (Brush)bc.ConvertFrom("#F03674");
                brush.Freeze();
                ThongBaoSnackbar.Background = brush;
                ThongBaoSnackbar.MessageQueue.Enqueue("Đơn hàng trống!");
            }
        }