public static void Delete(Model.CHITIETPHIEUXUAT item)
 {
     Model.DataProvider.Ins.DB.CHITIETPHIEUXUATs.Remove(item);
 }
        public Xuathang_ViewModel()
        {
            List       = new ObservableCollection <Model.CHITIETPHIEUXUAT>(Model.DataProvider.Ins.DB.CHITIETPHIEUXUATs.Where(x => x.IsDeleted == false).ToList().OrderByDescending(x => MyStaticMethods.ConvertStringtoDate(x.PHIEUXUAT.ngayxuat)));
            DeleteList = new ObservableCollection <Model.CHITIETPHIEUXUAT>();

            ListMathang   = new ObservableCollection <Model.MATHANG>(Model.DataProvider.Ins.DB.MATHANGs.Where(x => x.IsDeleted == false));
            ListKhachhang = new ObservableCollection <Model.KHACHHANG>(Model.DataProvider.Ins.DB.KHACHHANGs.Where(x => x.IsDeleted == false));
            ListPhieunhap = new ObservableCollection <Model.CHITIETPHIEUNHAP>();

            ListKH_Print = new ObservableCollection <Model.KHACHHANG>(Model.DataProvider.Ins.DB.KHACHHANGs.Where(x => x.IsDeleted == false));

            ListSoluong = new ObservableCollection <string>();

            Active        = false;
            IsOpen        = false;
            IsOpen_insert = false;
            IsOpen_Filter = false;
            IsOpen_prt    = false;

            Active_Command = new RelayCommand <object>(p =>
            {
                if (Active == false)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                Active = false;
            });

            CloseDialog_Command = new RelayCommand <object>(p =>
            {
                return(true);
            }, p =>
            {
                IsOpen_prt = false;
            });

            Load_Command = new RelayCommand <object>(p =>
            {
                return(true);
            }, p =>
            {
                List       = new ObservableCollection <Model.CHITIETPHIEUXUAT>(Model.DataProvider.Ins.DB.CHITIETPHIEUXUATs.Where(x => x.IsDeleted == false).ToList().OrderByDescending(x => MyStaticMethods.ConvertStringtoDate(x.PHIEUXUAT.ngayxuat)));
                DeleteList = new ObservableCollection <Model.CHITIETPHIEUXUAT>();

                ListMathang   = new ObservableCollection <Model.MATHANG>(Model.DataProvider.Ins.DB.MATHANGs.Where(x => x.IsDeleted == false));
                ListKhachhang = new ObservableCollection <Model.KHACHHANG>(Model.DataProvider.Ins.DB.KHACHHANGs.Where(x => x.IsDeleted == false));
                ListPhieunhap = new ObservableCollection <Model.CHITIETPHIEUNHAP>();

                ListSoluong = new ObservableCollection <string>();

                Active        = false;
                IsOpen        = false;
                IsOpen_insert = false;
                IsOpen_Filter = false;
                IsOpen_prt    = false;

                ListLoai_Filter      = new ObservableCollection <Model.LOAIHANG>(Model.DataProvider.Ins.DB.LOAIHANGs.Where(x => x.IsDeleted == false));
                ListMathang_Filter   = new ObservableCollection <Model.MATHANG>(Model.DataProvider.Ins.DB.MATHANGs.Where(x => x.IsDeleted == false));
                ListKhachhang_Filter = new ObservableCollection <Model.KHACHHANG>(Model.DataProvider.Ins.DB.KHACHHANGs.Where(x => x.IsDeleted == false));

                ListKH_Print = new ObservableCollection <Model.KHACHHANG>(Model.DataProvider.Ins.DB.KHACHHANGs.Where(x => x.IsDeleted == false));
            });

            #region Lua chon mat hang va phieu nhap

            Select_mathang_Command = new RelayCommand <ComboBox>(p =>
            {
                if (SMathang == null)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                ListPhieunhap = new ObservableCollection <Model.CHITIETPHIEUNHAP>();
                foreach (var item in Model.DataProvider.Ins.DB.CHITIETPHIEUNHAPs.Where(x => x.IsDeleted == false))
                {
                    while (item.MATHANG == SMathang)
                    {
                        ListPhieunhap.Add(item);
                        break;
                    }
                }
            });

            Select_phieunhap_Command = new RelayCommand <ComboBox>(p =>
            {
                if (SMathang == null || SPhieunhap == null)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                Dongiaxuat = SPhieunhap.giaxuat.ToString();

                ListSoluong = new ObservableCollection <string>();
                for (int i = 1; i <= SPhieunhap.soluongton; i++)
                {
                    ListSoluong.Add(i.ToString());
                }
            });

            #endregion

            #region Them moi

            InsertShow_Command = new RelayCommand <object>(p =>
            {
                if (SPhieunhap == null || SKhachhang == null || SMathang == null)
                {
                    return(false);
                }

                if (string.IsNullOrEmpty(Soluongxuat) || string.IsNullOrEmpty(Soluongthucxuat))
                {
                    return(false);
                }

                int soluongnhap = 0;
                if (!int.TryParse(Soluongxuat.Replace(" ", String.Empty), out soluongnhap))
                {
                    return(false);
                }

                int soluongthucnhap = 0;
                if (!int.TryParse(Soluongthucxuat.Replace(" ", String.Empty), out soluongthucnhap))
                {
                    return(false);
                }

                if (IsOpen_insert == true || IsOpen == true)
                {
                    return(false);
                }

                if (Convert.ToInt32(Dongiaxuat) <= 0 || Convert.ToInt32(Soluongxuat) <= 0 || Convert.ToInt32(Soluongthucxuat) <= 0)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                IsOpen_insert = true;

                string date_str = MyStaticMethods.ConvertDate2Vn_Today();
                Ngay            = date_str;
            });

            Insert_Command = new RelayCommand <object>(p =>
            {
                if (SPhieunhap == null || SKhachhang == null || SMathang == null)
                {
                    return(false);
                }

                if (string.IsNullOrEmpty(Soluongxuat) || string.IsNullOrEmpty(Soluongthucxuat))
                {
                    return(false);
                }

                int soluongnhap = 0;
                if (!int.TryParse(Soluongxuat.Replace(" ", String.Empty), out soluongnhap))
                {
                    return(false);
                }

                int soluongthucnhap = 0;
                if (!int.TryParse(Soluongthucxuat.Replace(" ", String.Empty), out soluongthucnhap))
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                string date_str       = MyStaticMethods.ConvertDate2Vn_Today();
                Model.PHIEUXUAT Phieu = CheckPhieuxuat(date_str);

                Model.CHITIETPHIEUXUAT newPhieu = new Model.CHITIETPHIEUXUAT()
                {
                    ma_ctphieuxuat   = MyStaticMethods.RandomInt(5) + "-" + SPhieunhap.MATHANG.ma_mathang,
                    CHITIETPHIEUNHAP = SPhieunhap,
                    KHACHHANG        = SKhachhang,
                    PHIEUXUAT        = Phieu,

                    soluongxuat     = Convert.ToInt32(Soluongxuat),
                    soluongthucxuat = Convert.ToInt32(Soluongthucxuat),

                    nguoitao  = getCurrentUser(),
                    ghichu    = Ghichu != "" ? Ghichu : null,
                    IsDeleted = false,
                };

                Model.Xuathang_Service.Insert(newPhieu);

                Update_luonghang(Convert.ToInt32(Soluongthucxuat), newPhieu.ma_ctphieunhap);

                List.Insert(0, newPhieu);
                SelectedItem = newPhieu;

                Active  = true;
                Message = "Thêm mới thành công !!!";

                IsOpen_insert = false;
            });

            #endregion

            #region Phan xoa

            DeleteShow_Command = new RelayCommand <object>(p =>
            {
                if (DeleteList.Count() == 0)
                {
                    return(false);
                }

                if (IsOpen_insert == true || IsOpen == true)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                IsOpen  = true;
                Content = "  Xóa các bản ghi được chọn ???";
            });

            AddDeleteList_Command = new RelayCommand <CheckBox>(p =>
            {
                return(true);
            }, p =>
            {
                DeleteList.Add(List.Where(x => x.ma_ctphieuxuat == p.Uid.ToString()).SingleOrDefault());
            });

            RemoveDeleteList_Command = new RelayCommand <CheckBox>(p =>
            {
                return(true);
            }, p =>
            {
                DeleteList.Remove(List.Where(x => x.ma_ctphieuxuat == p.Uid.ToString()).SingleOrDefault());
            });

            Delete_Command = new RelayCommand <object>(p =>
            {
                if (DeleteList.Count() == 0)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                RemoveIteminDb();
                RemoveIteminList();

                DeleteList   = new ObservableCollection <Model.CHITIETPHIEUXUAT>();
                IsOpen       = false;
                SelectedItem = null;
            });
            #endregion

            #region Tao moi
            Reset_Command = new RelayCommand <object>(p =>
            {
                return(true);
            }, p =>
            {
                SMathang        = null;
                SPhieunhap      = null;
                SKhachhang      = null;
                Soluongxuat     = "";
                Soluongthucxuat = "";
                Dongiaxuat      = "";
                SelectedItem    = null;

                ListMathang   = new ObservableCollection <Model.MATHANG>(Model.DataProvider.Ins.DB.MATHANGs.Where(x => x.IsDeleted == false));
                ListKhachhang = new ObservableCollection <Model.KHACHHANG>(Model.DataProvider.Ins.DB.KHACHHANGs.Where(x => x.IsDeleted == false));
                ListPhieunhap = new ObservableCollection <Model.CHITIETPHIEUNHAP>();

                ListSoluong = new ObservableCollection <string>();
            });
            #endregion

            #region Phan loc

            ListLoai_Filter      = new ObservableCollection <Model.LOAIHANG>(Model.DataProvider.Ins.DB.LOAIHANGs.Where(x => x.IsDeleted == false));
            ListMathang_Filter   = new ObservableCollection <Model.MATHANG>(Model.DataProvider.Ins.DB.MATHANGs.Where(x => x.IsDeleted == false));
            ListKhachhang_Filter = new ObservableCollection <Model.KHACHHANG>(Model.DataProvider.Ins.DB.KHACHHANGs.Where(x => x.IsDeleted == false));

            OpenFilter_Command = new RelayCommand <object>(p =>
            {
                if (IsOpen_Filter == true || IsOpen_insert == true || IsOpen_prt == true || IsOpen == true)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                IsOpen_Filter = true;
            });

            FilterLoai_Command = new RelayCommand <ComboBox>(p =>
            {
                return(true);
            }, p =>
            {
                if (SLoai_Filter != null)
                {
                    FilterMathangby_Loai(SLoai_Filter.ma_loaihang);
                }
                else
                {
                    ListMathang_Filter = new ObservableCollection <Model.MATHANG>(Model.DataProvider.Ins.DB.MATHANGs.Where(x => x.IsDeleted == false));
                }
            });

            ResetFilter_Command = new RelayCommand <Button>(p =>
            {
                return(true);
            }, p =>
            {
                ListLoai_Filter      = new ObservableCollection <Model.LOAIHANG>(Model.DataProvider.Ins.DB.LOAIHANGs.Where(x => x.IsDeleted == false));
                ListMathang_Filter   = new ObservableCollection <Model.MATHANG>(Model.DataProvider.Ins.DB.MATHANGs.Where(x => x.IsDeleted == false));
                ListKhachhang_Filter = new ObservableCollection <Model.KHACHHANG>(Model.DataProvider.Ins.DB.KHACHHANGs.Where(x => x.IsDeleted == false));

                SKhachhang_Filter = null;
                SMathang_Filter   = null;
                SLoai_Filter      = null;
                Date_Start        = String.Empty;
                Date_End          = String.Empty;

                List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(Model.DataProvider.Ins.DB.CHITIETPHIEUXUATs.Where(x => x.IsDeleted == false).OrderByDescending(x => x.ma_phieuxuat));

                IsOpen_Filter = false;
            });

            Filter_Command = new RelayCommand <Button>(p =>
            {
                if (string.IsNullOrEmpty(Date_Start) || string.IsNullOrEmpty(Date_End))
                {
                    return(false);
                }

                DateTime date_start = Convert.ToDateTime(Date_Start);
                DateTime date_end   = Convert.ToDateTime(Date_End);
                if ((date_start > date_end) && DateTime.TryParse(Date_Start, out date_start) && DateTime.TryParse(Date_End, out date_end))
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(Model.DataProvider.Ins.DB.CHITIETPHIEUXUATs.Where(x => x.IsDeleted == false).ToList().OrderByDescending(x => MyStaticMethods.ConvertStringtoDate(x.PHIEUXUAT.ngayxuat)));

                DateTime date_start = Convert.ToDateTime(Date_Start);
                DateTime date_end   = Convert.ToDateTime(Date_End);

                FindByDate(date_start, date_end);

                if (SMathang_Filter != null)
                {
                    FindByMH(SMathang_Filter.ma_mathang);
                }

                if (SKhachhang_Filter != null)
                {
                    FindByKH(SKhachhang_Filter.ma_khachhang);
                }

                if (SLoai_Filter != null)
                {
                    FindByLOAI(SLoai_Filter.ma_loaihang);
                }

                IsOpen_Filter = false;
            });

            #endregion

            #region Phan sap xep

            OrderbyTenMathang_Command = new RelayCommand <object>(p =>
            {
                if (List.Count() == 0)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                ObservableCollection <Model.CHITIETPHIEUXUAT> chkList = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.OrderByDescending(x => x.CHITIETPHIEUNHAP.MATHANG.ten_mathang));

                if (List[0] == chkList[0])
                {
                    List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.OrderBy(x => x.CHITIETPHIEUNHAP.MATHANG.ten_mathang));
                }
                else
                {
                    List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(chkList);
                }
            });

            OrderbyTennhacungcap_Command = new RelayCommand <object>(p =>
            {
                if (List.Count() == 0)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                ObservableCollection <Model.CHITIETPHIEUXUAT> chkList = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.OrderByDescending(x => x.CHITIETPHIEUNHAP.MATHANG.NHACUNGCAP.ten_nhacungcap));

                if (List[0] == chkList[0])
                {
                    List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.OrderBy(x => x.CHITIETPHIEUNHAP.MATHANG.NHACUNGCAP.ten_nhacungcap));
                }
                else
                {
                    List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(chkList);
                }
            });

            OrderbyNgay_Command = new RelayCommand <object>(p =>
            {
                if (List.Count() == 0)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                ObservableCollection <Model.CHITIETPHIEUXUAT> chkList = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.OrderByDescending(x => MyStaticMethods.ConvertStringtoDate(x.PHIEUXUAT.ngayxuat)));

                if (List[0] == chkList[0])
                {
                    List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.OrderBy(x => MyStaticMethods.ConvertStringtoDate(x.PHIEUXUAT.ngayxuat)));
                }
                else
                {
                    List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(chkList);
                }
            });

            OrderbyTendonvi_Command = new RelayCommand <object>(p =>
            {
                if (List.Count() == 0)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                ObservableCollection <Model.CHITIETPHIEUXUAT> chkList = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.OrderByDescending(x => x.CHITIETPHIEUNHAP.MATHANG.DONVITINH.ten_donvi));

                if (List[0] == chkList[0])
                {
                    List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.OrderBy(x => x.CHITIETPHIEUNHAP.MATHANG.DONVITINH.ten_donvi));
                }
                else
                {
                    List = new ObservableCollection <Model.CHITIETPHIEUXUAT>(chkList);
                }
            });

            #endregion

            #region Printer

            PrinterOpen_Command = new RelayCommand <object>(p =>
            {
                if (SKhachhangPrint == null)
                {
                    return(false);
                }

                if (string.IsNullOrEmpty(DayPrint))
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                List_Print = new ObservableCollection <Model.CHITIETPHIEUXUAT>(List.Where(x => x.KHACHHANG == SKhachhangPrint &&
                                                                                          x.PHIEUXUAT.ngayxuat == MyStaticMethods.FormatDateString(DayPrint)));

                var list_chk = new ObservableCollection <Model.CHITIETPHIEUXUAT>();

                ItemsCount = 0;
                Tongtien   = 0;

                foreach (var item in List_Print)
                {
                    Tongtien += (double)(item.CHITIETPHIEUNHAP.giaxuat * item.soluongthucxuat);

                    if (list_chk.Where(x => x.CHITIETPHIEUNHAP.MATHANG == item.CHITIETPHIEUNHAP.MATHANG).Count() == 0)
                    {
                        list_chk.Add(item);
                    }
                }

                ItemsCount = list_chk.Count();


                DayPrintVN = MyStaticMethods.FormatDateString(DayPrint);

                View.View_Thukho.In_Xuat w = new View.View_Thukho.In_Xuat();
                w.ShowDialog();
            });

            OpenPrintDialog_Command = new RelayCommand <object>(p =>
            {
                if (IsOpen == true)
                {
                    return(false);
                }

                if (IsOpen_insert == true)
                {
                    return(false);
                }

                if (IsOpen_Filter == true)
                {
                    return(false);
                }

                return(true);
            }, p =>
            {
                IsOpen_prt = true;
            });

            Print_Command = new RelayCommand <Grid>(p =>
            {
                return(true);
            }, p =>
            {
                try
                {
                    PrintDialog printDialog = new PrintDialog();
                    if (printDialog.ShowDialog() == true)
                    {
                        printDialog.PrintVisual(p, "invoice");
                    }

                    MessageBox.Show("Thành công !!!", "THÔNG BÁO");
                }
                catch (Exception) { MessageBox.Show("Có lỗi xảy ra !!!", "THÔNG BÁO"); };
            });

            PrinterFormClose_Command = new RelayCommand <Window>(p =>
            {
                return(true);
            }, p =>
            {
                if (p != null)
                {
                    p.Close();
                    IsOpen_prt = false;
                }
            });

            #endregion
        }
 public static void Insert(Model.CHITIETPHIEUXUAT item)
 {
     Model.DataProvider.Ins.DB.CHITIETPHIEUXUATs.Add(item);
     Model.DataProvider.Ins.DB.SaveChanges();
 }