private void btapply_Click(object sender, RoutedEventArgs e) { using (var db = new QuanLyNongSanDBEntities()) { this.IsEnabled = false; var creen = new ConfirmWindow($"Ban Co Chac Muon Edit San Phan {item.Id}"); creen.ShowDialog(); if (creen.DialogResult == true) { var result = db.Products.SingleOrDefault(b => b.Id == it.Id); if (result != null) { try { result.Name = it.Name; result.Gia = it.Gia; string pathDelete; if (destinationPath != it.Image) { pathDelete = destinationPath; //Đường Dẫn File Ảnh Gốc var sourceImageFileInfo = new FileInfo(it.Image); //Tao ten duy nhat var uniqueName = $"{Guid.NewGuid()}{sourceImageFileInfo.Extension}"; //Đường dẫn tập tin exe var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; //Đường dẫn folder Image trong file exe destinationPath = $"{baseDirectory}Image_Product\\{uniqueName}"; //Copy Ảnh từ File Ảnh Gốc Sang Folder Ảnh Trong File Exe if (!File.Exists(destinationPath)) { File.Copy(it.Image, destinationPath); } result.Image = uniqueName; item.Image = uniqueName; } db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } item.Equal(it); it.Image = destinationPath; stpShow.Height = stpEdit.Height; stpEdit.Height = 0; btedit.Content = "Edit"; btapply.IsEnabled = false; } this.IsEnabled = true; } }
private void btdelete_Click(object sender, RoutedEventArgs e) { using (var db = new QuanLyNongSanDBEntities()) { this.IsEnabled = false; var creen = new ConfirmWindow($"Ban Co Chac Muon Xoa San Phan {item.Id}"); creen.ShowDialog(); if (creen.DialogResult == true) { var itemdelete = db.Products.Find(item.Id); db.Products.Remove(itemdelete); db.SaveChanges(); this.Close(); } this.IsEnabled = true; } }
private void btdelete_Click(object sender, RoutedEventArgs e) { using (var db = new QuanLyNongSanDBEntities()) { this.IsEnabled = false; var creen = new ConfirmWindow($"Ban Co Chac Muon Xoa Hoa Don {HD.Id}"); creen.ShowDialog(); if (creen.DialogResult == true) { foreach (var CT in HD.InvoiceDetails) { var dele = db.InvoiceDetails.Find(CT.Id); db.InvoiceDetails.Remove(dele); db.SaveChanges(); } var itemdelete = db.Invoices.Find(HD.Id); db.Invoices.Remove(itemdelete); db.SaveChanges(); this.Close(); } this.IsEnabled = true; } }
private async void btadd_Click(object sender, RoutedEventArgs e) { if (tbtenkh.Text == "") { MessageBox.Show("Tên Khách Hàng Không Được Trống"); return; } else if (tbdiachi.Text == "") { MessageBox.Show("Địa Chỉ Khách Hàng Không Được Trống"); return; } else if (tbsdt.Text == "") { MessageBox.Show("SĐT Khách Hàng Không Được Trống"); return; } else if (tblngaydat.Text == "") { MessageBox.Show("Ngay Đạt Không Được Trống"); return; } else if (view.Count == 0) { MessageBox.Show("Hóa Đơn Phải Có Ít Nhất 1 SP"); return; } using (var db = new QuanLyNongSanDBEntities()) { this.IsEnabled = false; var creen = new ConfirmWindow($"Ban Co Chac Muon Edit Hoa Don {HD.Id}"); creen.ShowDialog(); if (creen.DialogResult == true) { var result = db.Invoices.SingleOrDefault(b => b.Id == HD.Id); if (result != null) { result.TenKH = tbtenkh.Text; result.DiaChiKH = tbdiachi.Text; result.SDT = tbsdt.Text; UpdateStatus(result); result.TongTien = double.Parse(tbltongtienhoadon.Text); foreach (var CT in result.InvoiceDetails) { foreach (var ct in view) { if (ct.Id == CT.Id && CT.SoLuong != ct.SoLuong) { CT.SoLuong = ct.SoLuong; CT.TinhTongGia(); } } } foreach (var CT in insert) { InvoiceDetail newDetail = new InvoiceDetail() { IdInvoice = CT.IdInvoice, IdProduct = CT.IdProduct, SoLuong = CT.SoLuong, TongTien = CT.TongTien, GiaBan = CT.GiaBan, }; result.InvoiceDetails.Add(newDetail); await db.SaveChangesAsync(); } foreach (var delete in delete) { var dele = db.InvoiceDetails.Find(delete.Id); db.InvoiceDetails.Remove(dele); await db.SaveChangesAsync(); } } db.SaveChanges(); } this.IsEnabled = true; } var dba = new QuanLyNongSanDBEntities(); HD = dba.Invoices.Find(HD.Id); datagridgiohang.ItemsSource = HD.InvoiceDetails.ToList(); stackpanelshow.Height = stackpaneledit.Height; this.Width = 800; stackpaneledit.Height = 0; btedit.Content = "Edit"; btapply.IsEnabled = false; DataContext = null; DataContext = HD; }