public void Import()
        {
            if (ImportList.Any() == false)
            {
                ShowMessage(new Models.Message("Thông báo", "Không có sách trong Phiếu nhập sách"));
                return;
            }
            PHIEUNHAPSACH p = new PHIEUNHAPSACH()
            {
                MATK = dataProvider.User.MATK, NGAYLAP = DateTime.Today
            };

            dataProvider.Create <PHIEUNHAPSACH>(p);
            foreach (ImportItem item in ImportList)
            {
                for (int i = 0; i < item.Amount; i++)
                {
                    CTSACH c = new CTSACH()
                    {
                        MASACH = item.BookId, MAPNS = p.MAPNS, GIABIA = item.CoverPrice, GIANHAP = item.ImportPrice, MATT = 1
                    };
                    dataProvider.Create <CTSACH>(c);
                }
            }
            HistoryList = dataProvider.GetImportBillList(x => true);
            ImportList  = new BindableCollection <ImportItem>();
            ShowMessage(new Models.Message("Thông báo", "Đã nhập thành công"));
        }
Beispiel #2
0
        public BookImportViewVM()
        {
            ButtonName         = "Thêm sách";
            IsEnabledListView2 = true;
            isSaved            = true;
            AddCommand         = new RelayCommand <Button>((p) => { return(isSaved ? true : false); }, (p) =>
            {
                IsEnabledTextBox  = true;
                IsEnabledCBBox    = true;
                IsEnabledListView = true;
                IsAdding          = true;
                isSaved           = false;
                var receiptNote   = new PHIEUNHAPSACH()
                {
                    NGAYNHAP = DateTime.Now, TONGCHI = 0
                };
                Current      = receiptNote;
                SelectedItem = Current;
                DataProvider.Ins.DB.PHIEUNHAPSACHes.Add(receiptNote);
                DataProvider.Ins.DB.SaveChanges();

                ListReceiptNote.Add(receiptNote);
            });
            AddBookCmd = new RelayCommand <Button>((p) =>
            {
                if (SelectedBook == null || NumOfBook < 1 || SelectedItem == null)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }, (p) =>
            {
                if (!IsAdding)
                {
                    var book = DataProvider.Ins.DB.CTPHIEUNHAPs.Where(x => x.MASACH == SelectedReceiptNoteDetail.MASACH && x.MAPHIEUNHAP == SelectedReceiptNoteDetail.MAPHIEUNHAP).SingleOrDefault();
                    if (book == null)
                    {
                        MessageBoxWindow mess2 = new MessageBoxWindow();
                        mess2.Tag = "Không tìm thấy cuôn sách này";
                        mess2.ShowDialog();

                        // MessageBox.Show("Không tìm thấy cuôn sách này", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    book.SOLUONG = NumOfBook;
                    book.DONGIA  = ImportPrice;
                    DataProvider.Ins.DB.SaveChanges();
                    IsAdding   = true;
                    ButtonName = "Thêm sách";
                }
                else
                {
                    var book = new CTPHIEUNHAP()
                    {
                        MAPHIEUNHAP = SelectedItem.MAPHIEUNHAP, MASACH = SelectedBook.MASACH, SOLUONG = NumOfBook, DONGIA = ImportPrice, THANHTIEN = 0
                    };
                    if (DataProvider.Ins.DB.CTPHIEUNHAPs.Where(x => x.MASACH == book.MASACH && x.MAPHIEUNHAP == book.MAPHIEUNHAP).Count() > 0)
                    {
                        MessageBoxWindow mess2 = new MessageBoxWindow();
                        mess2.Tag = "Đã thêm sách này";
                        mess2.ShowDialog();

                        // MessageBox.Show("Đã thêm sách này", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        DataProvider.Ins.DB.CTPHIEUNHAPs.Add(book);
                        DataProvider.Ins.DB.SaveChanges();
                        ListReceiptNoteDetail.Add(book);

                        ListReceiptNoteDetail = new ObservableCollection <CTPHIEUNHAP>(DataProvider.Ins.DB.Database.SqlQuery <CTPHIEUNHAP>("SELECT * FROM CTPHIEUNHAP WHERE MAPHIEUNHAP = (SELECT MAX(MAPHIEUNHAP) FROM PHIEUNHAPSACH)"));
                    }
                }
                IsEnabledListView = true;
                IsEnabledCBBox    = true;
            });
            EditCommand = new RelayCommand <Button>((p) => { return(SelectedReceiptNoteDetail == null?false:true); }, (p) =>
            {
                IsAdding          = false;
                IsEnabledListView = false;
                IsEnabledTextBox  = true;
                IsEnabledCBBox    = false;
                ButtonName        = "Sửa dữ liệu";
            });
            SaveCommand = new RelayCommand <Button>((p) =>
            {
                if (IsEnabledListView)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }, (p) =>
            {
                DataProvider.Ins.DB.PHIEUNHAPSACHes.Where(x => x.MAPHIEUNHAP == SelectedItem.MAPHIEUNHAP).SingleOrDefault().TONGCHI = UpdateTotalPrice();
                DataProvider.Ins.DB.SaveChanges();
                UpdateNumOfBook();
                IsEnabledListView = false;
                Current           = null;
                isSaved           = true;
                ListReceiptNote   = new ObservableCollection <PHIEUNHAPSACH>(DataProvider.Ins.DB.Database.SqlQuery <PHIEUNHAPSACH>("SELECT * FROM PHIEUNHAPSACH WHERE MAPHIEUNHAP = (SELECT MAX(MAPHIEUNHAP) FROM PHIEUNHAPSACH)"));
            });
            DeleteReceiptNoteCommand = new RelayCommand <Button>((p) => { return(true); }, (p) =>
            {
                var receiptNote = DataProvider.Ins.DB.PHIEUNHAPSACHes.Where(x => x.MAPHIEUNHAP == SelectedItem.MAPHIEUNHAP).SingleOrDefault();
                if (DataProvider.Ins.DB.CTPHIEUNHAPs.Where(x => x.MAPHIEUNHAP == SelectedItem.MAPHIEUNHAP).Count() > 0)
                {
                    DataProvider.Ins.DB.CTPHIEUNHAPs.RemoveRange(DataProvider.Ins.DB.CTPHIEUNHAPs.Where(x => x.MAPHIEUNHAP == SelectedItem.MAPHIEUNHAP));
                    DataProvider.Ins.DB.SaveChanges();
                    ListReceiptNoteDetail.Clear();
                }
                DataProvider.Ins.DB.PHIEUNHAPSACHes.Remove(receiptNote);
                DataProvider.Ins.DB.SaveChanges();

                ListReceiptNote.Remove(SelectedItem);
            });
            DeleteReceiptNoteDetailCommand = new RelayCommand <Button>((p) => { return(true); }, (p) =>
            {
                var receiptNoteDetail = DataProvider.Ins.DB.CTPHIEUNHAPs.Where(x => x.MAPHIEUNHAP == SelectedReceiptNoteDetail.MAPHIEUNHAP && x.MASACH == SelectedReceiptNoteDetail.MASACH).SingleOrDefault();

                DataProvider.Ins.DB.CTPHIEUNHAPs.Remove(receiptNoteDetail);
                DataProvider.Ins.DB.SaveChanges();

                ListReceiptNoteDetail.Remove(SelectedReceiptNoteDetail);
            });
            ListBook        = new ObservableCollection <SACH>(DataProvider.Ins.DB.SACHes);
            ListReceiptNote = new ObservableCollection <PHIEUNHAPSACH>(DataProvider.Ins.DB.PHIEUNHAPSACHes);
        }