Example #1
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);
        }
        private async void Submit(string obj)
        {
            try
            {
                var newcus = new Customer()
                {
                    Email   = Email,
                    Address = Address,
                    Avatar  = ImgCus.ConvertToByte(),
                    Name    = Name,
                    Phone   = Phone,
                };


                if (IsB)
                {
                    newcus.Sex = true;
                }
                if (IsG)
                {
                    newcus.Sex = false;
                }

                if (Kind != null)
                {
                    newcus.Kind = ListKinds.IndexOf(Kind);
                }

                if (string.IsNullOrEmpty(obj))
                {
                    //Create new customer

                    var objresult = await customer_repo.Add(newcus);

                    if (objresult != null)
                    {
                        dc = new DialogContent()
                        {
                            Content = "Thêm Thành Công", Tilte = "Thông Báo"
                        };
                        dialog = new DialogOk()
                        {
                            DataContext = dc
                        };
                        DialogHost.CloseDialogCommand.Execute(null, null);
                        await DialogHost.Show(dialog, DialogHostId);

                        ListCustomer.Add(objresult);
                    }
                    else
                    {
                        dc = new DialogContent()
                        {
                            Content = "Thêm Thất Bại", Tilte = "Thông Báo"
                        };
                        dialog = new DialogOk()
                        {
                            DataContext = dc
                        };
                        DialogHost.CloseDialogCommand.Execute(null, null);
                        await DialogHost.Show(dialog, DialogHostId);
                    }
                }
                else
                {
                    //update customer

                    newcus.CusID = Convert.ToInt32(obj);

                    if (await customer_repo.Update(newcus))
                    {
                        dc = new DialogContent()
                        {
                            Content = "Cập Nhật Thành Công", Tilte = "Thông Báo"
                        };
                        dialog = new DialogOk()
                        {
                            DataContext = dc
                        };
                        DialogHost.CloseDialogCommand.Execute(null, null);
                        await DialogHost.Show(dialog, DialogHostId);

                        ListCustomer = new ObservableCollection <Customer>(await customer_repo.GetAllAsync());
                    }
                    else
                    {
                        dc = new DialogContent()
                        {
                            Content = "Cập Nhật Thất Bại", Tilte = "Thông Báo"
                        };
                        dialog = new DialogOk()
                        {
                            DataContext = dc
                        };
                        DialogHost.CloseDialogCommand.Execute(null, null);
                        await DialogHost.Show(dialog, DialogHostId);
                    }
                }
            }
            catch
            {
                dc.Content = "Có Lỗi";
                dc.Tilte   = "Thông Báo";
                dialog     = new DialogOk()
                {
                    DataContext = dc
                };
                DialogHost.CloseDialogCommand.Execute(null, null);
                await DialogHost.Show(dialog, DialogHostId);
            }
        }