public BillViewModel() { LoadOutputData(); loadUserCurrentLogin(); LoadEditCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { LoadDialogAccountEdit(); }); DeleteDetailCommand = new RelayCommand <object>((p) => { if (SelectedItemOutputDetail == null) { return(false); } return(true); }, (p) => { DeleteNotificationMessage msg = new DeleteNotificationMessage(); Message = "Xóa Thông Tin Có Thể Dẫn Đến Sai Lệch Dữ Liệu! Bạn Chắc Chắn Chứ?"; DialogHost.Show(msg, "BillDialog"); }); DeleteConfirmCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { OutPutDetailTable detailItem = DataProvider.Ins.Entities.OutPutDetailTable.Where(x => x.ID == SelectedItemOutputDetail.Output.ID).FirstOrDefault(); DataProvider.Ins.Entities.OutPutDetailTable.Remove(detailItem); DataProvider.Ins.Entities.SaveChanges(); ListOutputDetail = new List <OutputDetailModel>(); var List = DataProvider.Ins.Entities.OutPutDetailTable.Where(x => x.ID_Output == SelectedItemOutput.Output.ID); foreach (var item in List) { OutputDetailModel detail = new OutputDetailModel(); ProductTable product = new ProductTable(); product = DataProvider.Ins.Entities.ProductTable.Where(x => x.ID == item.ID_Product).FirstOrDefault(); detail.Output = item; detail.Product = product; ListOutputDetail.Add(detail); } DialogHost.CloseDialogCommand.Execute(null, null); SelectedItemOutputDetail = null; IsActiveSnackBar = true; Message = "Xóa Chi Tiết Thành Công!"; System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 5000; timer.Enabled = true; timer.Elapsed += ShowSnackBar; timer.Start(); }); DeleteOutputCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { var List = DataProvider.Ins.Entities.OutPutDetailTable.Where(x => x.ID_Output == SelectedItemOutput.Output.ID); if (List.Count() != 0) { ErrorNotificationMessage msg = new ErrorNotificationMessage(); Message = "Phiếu Nhập Này Đang Có Chi Tiết! Bạn Vui Lòng Xóa Bỏ Các Thông Tin Liên Quan!"; DialogHost.Show(msg, "BillDialog"); return; } DataProvider.Ins.Entities.OutputTable.Remove(SelectedItemOutput.Output); DataProvider.Ins.Entities.SaveChanges(); LoadOutputData(); DialogHost.CloseDialogCommand.Execute(null, null); SelectedItemOutput = null; IsActiveSnackBar = true; Message = "Xóa Phiếu Nhập Thành Công!"; System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 5000; timer.Enabled = true; timer.Elapsed += ShowSnackBar; timer.Start(); }); SearchCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { if (SearchTermOutput == null) { return; } var ListInput = DataProvider.Ins.Entities.OutputTable; List <OutputModel> Result = new List <OutputModel>(); foreach (var item in ListInput) { OutputModel output = new OutputModel(); output.Output = item; output.User = DataProvider.Ins.Entities.UserTable.Where(x => x.ID == item.ID_User).FirstOrDefault(); output.Customer = DataProvider.Ins.Entities.CustomerTable.Where(x => x.ID == item.ID_Customer).FirstOrDefault(); Result.Add(output); } ListOutput = new List <OutputModel>(Result.Where( x => x.Output.DateOutput.ToString().ToLower().Contains(SearchTermOutput) || x.User.DisplayName.ToLower().Contains(SearchTermOutput) || x.Customer.FullName.ToLower().Contains(SearchTermOutput) )); }); }
public SellViewModel() { loadUserCurrentLogin(); TotalPrice = 0; IsEnableName = false; List = new List <ProductTable>(DataProvider.Ins.Entities.ProductTable); ListSupplier = new List <SupplierTable>(DataProvider.Ins.Entities.SupplierTable); ListUnit = new List <UnitTable>(DataProvider.Ins.Entities.UnitTable); ListBill = new List <BillModel>(); _ListTemp = new List <BillModel>(); LoadEditCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { LoadDialogAccountEdit(); }); AddBillCommand = new RelayCommand <object>((p) => { if (SelectedItems == null) { return(false); } SelectedItemsBill = null; return(true); }, (p) => { BillModel bill = new BillModel(); ProductTable billProduct = new ProductTable(); billProduct.ID = SelectedItems.ID; billProduct.DisplayName = SelectedItems.DisplayName; billProduct.SupplierTable = SelectedItems.SupplierTable; billProduct.UnitTable = SelectedItems.UnitTable; int Price = 0; var listPrice = DataProvider.Ins.Entities.ProductTable.Where(x => x.ID == SelectedItems.ID).Select(x => x.Price); foreach (var item in listPrice) { Price = item; } bill.product = billProduct; bill.Price = Price; bill.Amount = 1; bill.PriceItems = Price; bill.AmountChange = AmountChange; if (LoadInventory(bill.product.ID) <= 0) { LoadDialogError("Sản Phẩm Không Còn Trong Kho! Vui Lòng Liên Hệ Quản Trị Viên!"); return; } TotalPrice += Price; var listCheckDuplicate = _ListTemp.Where(x => x.product.ID == bill.product.ID); if (listCheckDuplicate.Count() != 0) { int DuplicateProduct = _ListTemp.Where(x => x.product.ID == bill.product.ID).FirstOrDefault().Amount; _ListTemp.Where(x => x.product.ID == bill.product.ID).FirstOrDefault().Amount = DuplicateProduct + 1; _ListTemp.Where(x => x.product.ID == bill.product.ID).FirstOrDefault().PriceItems += Price; ListBill = new List <BillModel>(_ListTemp); SelectedItems = null; return; } _ListTemp.Add(bill); ListBill = new List <BillModel>(_ListTemp); SelectedItems = null; }); DeleteItemInBill = new RelayCommand <object>((p) => { if (SelectedItemsBill == null) { return(false); } SelectedItems = null; return(true); }, (p) => { var FindToDelete = _ListTemp.Where(x => x.product.ID == SelectedItemsBill.product.ID).FirstOrDefault(); TotalPrice -= FindToDelete.PriceItems; _ListTemp.Remove(FindToDelete); ListBill = new List <BillModel>(_ListTemp); SelectedItemsBill = null; }); AmountChange = new RelayCommand <object>((p) => { return(true); }, (p) => { try { var FindToEdit = _ListTemp.Where(x => x.product.ID == SelectedItemsBill.product.ID).FirstOrDefault(); TotalPrice -= FindToEdit.PriceItems; FindToEdit.Amount = SelectedItemsBill.Amount; FindToEdit.PriceItems = FindToEdit.Amount * FindToEdit.Price; TotalPrice += FindToEdit.PriceItems; ListBill = new List <BillModel>(_ListTemp); } catch { MessageBox.Show(Message); } }); PhoneCustomerChange = new RelayCommand <object>((p) => { if (PhoneCustomer.ToString().Length < 9) { return(false); } return(true); }, (p) => { var Customer = DataProvider.Ins.Entities.CustomerTable.Where(x => x.Phone.ToLower().Contains(PhoneCustomer.ToString())).FirstOrDefault(); if (Customer != null) { NameCustomer = Customer.FullName; IsEnableName = false; return; } NameCustomer = ""; IsEnableName = true; }); CreateBill = new RelayCommand <object>((p) => { if (TotalPrice <= 0 || NameCustomer == null || PhoneCustomer.ToString().Length < 9) { return(false); } return(true); }, (p) => { foreach (var item in _ListTemp) { if (LoadInventory(item.product.ID) - item.Amount < 0) { LoadDialogError("Số Lượng Sản Phẩm Không Đủ Đáp Ứng Nhu Cầu!"); return; } } if (IsEnableName) { CustomerTable customer = new CustomerTable(); customer.FullName = NameCustomer; customer.Phone = "0" + PhoneCustomer.ToString(); customer.Address = ""; DataProvider.Ins.Entities.CustomerTable.Add(customer); DataProvider.Ins.Entities.SaveChanges(); } var ID_Customer = DataProvider.Ins.Entities.CustomerTable.Where(x => x.Phone.ToLower().Contains(PhoneCustomer.ToString())).FirstOrDefault(); OutputTable output = new OutputTable(); output.ID_User = (int)App.Current.Properties["UserID"]; output.DateOutput = DateTime.Now; output.ID_Customer = ID_Customer.ID; DataProvider.Ins.Entities.OutputTable.Add(output); DataProvider.Ins.Entities.SaveChanges(); var ID_Output = DataProvider.Ins.Entities.OutputTable.Where(x => x.ID_User == output.ID_User && x.ID_Customer == output.ID_Customer && x.DateOutput == output.DateOutput.Date).FirstOrDefault(); int ID = ID_Output.ID; OutPutDetailTable outPutDetail = new OutPutDetailTable(); foreach (var item in _ListTemp) { outPutDetail.Count = item.Amount; outPutDetail.ID_Product = item.product.ID; outPutDetail.ID_Output = ID; outPutDetail.Price = item.Price; outPutDetail.TotalPrice = item.PriceItems; DataProvider.Ins.Entities.OutPutDetailTable.Add(outPutDetail); DataProvider.Ins.Entities.SaveChanges(); } LoadDialogSuccessSale(); TotalPrice = 0; _ListBill = null; ListBill = null; NameCustomer = null; PhoneCustomer = 0; IsEnableName = false; }); DestroyCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { TotalPrice = 0; ListBill = null; _ListBill = null; NameCustomer = null; PhoneCustomer = 0; IsEnableName = false; LoadDialogDeleted(); }); SearchCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { if (SearchTerm == null) { return; } List = new List <ProductTable>(DataProvider.Ins.Entities.ProductTable.Where( x => x.DisplayName.ToLower().Contains(SearchTerm) || x.SupplierTable.DisplayName.ToLower().Contains(SearchTerm) || x.UnitTable.Descriptions.ToLower().Contains(SearchTerm))); }); OpenBillWindow = new RelayCommand <Window>((p) => { return(true); }, (p) => { p.Close(); BillWindow wd = new BillWindow(); wd.ShowDialog(); }); }