private async void DeleteCustomer(int?obj)
        {
            try
            {
                dc = new DialogContent()
                {
                    Content = "Bạn muốn xóa customer này ?", Tilte = "Thông Báo"
                };
                var dialogYS = new DialogYesNo()
                {
                    DataContext = dc
                };
                var result = (bool)await DialogHost.Show(dialogYS, DialogHostId);

                if (result)
                {
                    if (obj != null)
                    {
                        if (await customer_repo.Remove((int)obj))
                        {
                            ListCustomer.Remove(ListCustomer.SingleOrDefault(t => t.CusID == (int)obj));
                            dc = new DialogContent()
                            {
                                Content = "Xóa Thành Công", Tilte = "Thông Báo"
                            };
                            dialog = new DialogOk()
                            {
                                DataContext = dc
                            };
                            await DialogHost.Show(dialog, DialogHostId);
                        }
                        else
                        {
                            dc = new DialogContent()
                            {
                                Content = "Xóa Thất Bại", Tilte = "Thông Báo"
                            };
                            dialog = new DialogOk()
                            {
                                DataContext = dc
                            };
                            await DialogHost.Show(dialog, DialogHostId);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Пример #2
0
        public CustomerMngViewVM()
        {
            IsEnabledListView = true;
            IsAdding          = true;
            AddCommand        = new RelayCommand <Button>((p) => { return(true); }, (p) =>
            {
                IsEnabledListView  = false;
                IsEnabledTextBox   = true;
                IsEnabledIDTextBox = true;

                ID = DataProvider.Ins.DB.Database.SqlQuery <string>("MAKHTIEPTHEO").First();
            });
            SaveCommand = new RelayCommand <Button>((p) =>
            {
                if (!IsEnabledListView)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }, (p) =>
            {
                IsEnabledListView  = true;
                IsEnabledTextBox   = false;
                IsEnabledIDTextBox = false;
                if (!IsAdding)
                {
                    var customer = DataProvider.Ins.DB.KHACHHANGs.Where(x => x.MAKH == SelectedItem.MAKH).SingleOrDefault();
                    if (customer == null)
                    {
                        MessageBox.Show("Không tìm thấy khách hàng này", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    customer.TENKH       = NameOfCustomer;
                    customer.DIACHI      = Address;
                    customer.SODIENTHOAI = PhoneNumber;
                    customer.EMAIL       = Mail;
                    customer.TIENNO      = Debt;
                    try
                    {
                        DataProvider.Ins.DB.SaveChanges();
                        ListCustomer = new ObservableCollection <KHACHHANG>(DataProvider.Ins.DB.Database.SqlQuery <KHACHHANG>("GET_KH").OrderByDescending(x => x.MAKH == ID));
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Trường giá trị vượt quá giới hạn cho phép", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                    }

                    IsAdding = true;
                }
                else
                {
                    var customer = new KHACHHANG()
                    {
                        MAKH = ID, TENKH = NameOfCustomer, DIACHI = Address, EMAIL = Mail, SODIENTHOAI = PhoneNumber, TIENNO = Debt
                    };
                    if (DataProvider.Ins.DB.KHACHHANGs.Where(x => x.MAKH == customer.MAKH).Count() > 0)
                    {
                        MessageBox.Show("Mã Khách hàng đã tồn tại!", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        DataProvider.Ins.DB.KHACHHANGs.Add(customer);

                        try
                        {
                            DataProvider.Ins.DB.SaveChanges();
                            ListCustomer.Add(customer);
                            ListCustomer = new ObservableCollection <KHACHHANG>(DataProvider.Ins.DB.Database.SqlQuery <KHACHHANG>("GET_KH").OrderByDescending(x => x.MAKH == ID));
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Trường giá trị vượt quá giới hạn cho phép", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            });
            EditCommand = new RelayCommand <Button>((p) => { return(true); }, (p) =>
            {
                IsAdding           = false;
                IsEnabledListView  = false;
                IsEnabledTextBox   = true;
                IsEnabledIDTextBox = false;
            });
            DeleteCommand = new RelayCommand <Button>((p) => { return(true); }, (p) =>
            {
                var customer = DataProvider.Ins.DB.KHACHHANGs.Where(x => x.MAKH == SelectedItem.MAKH).SingleOrDefault();
                DataProvider.Ins.DB.KHACHHANGs.Remove(customer);
                try
                {
                    DataProvider.Ins.DB.SaveChanges();

                    ListCustomer.Remove(SelectedItem);
                }
                catch (Exception)
                {
                    MessageBox.Show("Không thể xóa khách hàng này", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            });
            SearchCmd = new RelayCommand <TextBox>((p) => { return(true); }, (p) =>
            {
                if (p.Text != "")
                {
                    var newList  = new ObservableCollection <KHACHHANG>(DataProvider.Ins.DB.Database.SqlQuery <KHACHHANG>("USP_TIMKHTHEOTEN @TEN", new SqlParameter("TEN", p.Text)));
                    ListCustomer = newList;
                }
                else
                {
                    var newList  = new ObservableCollection <KHACHHANG>(DataProvider.Ins.DB.KHACHHANGs);
                    ListCustomer = newList;
                }
            });
            ListCustomer = new ObservableCollection <KHACHHANG>(DataProvider.Ins.DB.KHACHHANGs);
        }