private async void btadd_Click(object sender, RoutedEventArgs e) { using (var db = new QuanLyNongSanDBEntities()) { 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 (listcthd.Count == 0) { MessageBox.Show("Hóa Đơn Phải Có Ít Nhất 1 SP"); return; } Invoice newHD = new Invoice() { TenKH = tbtenkh.Text, DiaChiKH = tbdiachi.Text, SDT = tbsdt.Text, NgayTao = tblngaydat.Text, NgayUpdate = tblngaydat.Text, IdStatus = 1, TongTien = float.Parse(tbltongtienhoadon.Text), }; db.Invoices.Add(newHD); await db.SaveChangesAsync(); foreach (var CT in listcthd) { InvoiceDetail newdetail = new InvoiceDetail() { IdInvoice = newHD.Id, IdProduct = CT.IdProduct, GiaBan = CT.GiaBan, SoLuong = CT.SoLuong, TongTien = CT.TongTien, }; newHD.InvoiceDetails.Add(newdetail); await db.SaveChangesAsync(); } } this.Close(); }
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 LoadProducts() { var db = new QuanLyNongSanDBEntities(); listproduct = db.Products.ToList(); //Load Datagrid productdatagrid.ItemsSource = listproduct.ToList(); tbCountSp.Text = listproduct.Count().ToString(); }
private void LoadInvoices() { var db = new QuanLyNongSanDBEntities(); listinvoice = db.Invoices.ToList(); var list = db.InvoiceStatus.ToList(); //Load Datagrid billListView.ItemsSource = listinvoice.ToList(); }
private async void btadd_Click(object sender, RoutedEventArgs e) { using (var db = new QuanLyNongSanDBEntities()) { Product newprd = new Product(); if (tbname.Text == "") { MessageBox.Show("Ten San Pham Khong Duoc Trong"); } else if (tbGia.Text == "0") { MessageBox.Show("Gia San Pham Khong Duoc Trong"); } else if (imgproduct.Source == null) { MessageBox.Show("Chua Nhap Anh San Pham"); } else { newprd.Equal(prd); //Đường Dẫn File Ảnh Gốc var sourceImageFileInfo = new FileInfo(prd.Image); //Tao ten duy nhat var uniqueName = $"{Guid.NewGuid()}{sourceImageFileInfo.Extension}"; newprd.Image = uniqueName; //Đường dẫn tập tin exe var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; //Đường dẫn folder Image trong file exe var destinationPath = $"{baseDirectory}Image_Product\\{newprd.Image}"; //Copy Ảnh từ File Ảnh Gốc Sang Folder Ảnh Trong File Exe if (!File.Exists(destinationPath)) { File.Copy(prd.Image, destinationPath); } db.Products.Add(newprd); await db.SaveChangesAsync(); MessageBox.Show("Thêm Sản Phẩm Thành Công"); prd.ResetValue(); data.ItemsSource = db.Products.ToList(); } } }
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; }
/// <SanPham private async void btimprot_SP_Click(object sender, RoutedEventArgs e) { try { var screen = new OpenFileDialog(); if (screen.ShowDialog() == true) { var workbook = new Workbook(screen.FileName); var tabs = workbook.Worksheets; var db = new QuanLyNongSanDBEntities(); foreach (var tab in tabs)//Duyet Qua Tung Tab { if (tab.Name == "Nông Sản") { var col = 'A'; var row = 3; var cell = tab.Cells[$"{ col}{ row}"]; while (cell.Value != null)//Duyet Qua Tung Dong { var id = tab.Cells[$"A{row}"].IntValue; var name = tab.Cells[$"B{row}"].StringValue; var Gia = tab.Cells[$"C{row}"].FloatValue; var StrImage = tab.Cells[$"D{row}"].StringValue; //lay duong dan cu Foder anh var srcImageInfo = new FileInfo(screen.FileName); var srcPath = $"{srcImageInfo.DirectoryName}\\ImagesProduct\\{StrImage}"; var sourceImageInfo = new FileInfo(srcPath); //Tao ten duy nhat var uniqueName = $"{Guid.NewGuid()}{sourceImageInfo.Extension}"; var basePath = AppDomain.CurrentDomain.BaseDirectory; var desPath = $"{basePath}Image_Product\\{uniqueName}"; if (!File.Exists(desPath)) { File.Copy(srcPath, desPath); } var newPro = new Product() { Id = id, Name = name, Gia = Gia, Image = uniqueName }; db.Products.Add(newPro); await db.SaveChangesAsync(); row++; cell = tab.Cells[$"{ col}{ row}"]; } } } listproduct = db.Products.ToList(); // Update Datagrid productdatagrid.ItemsSource = listproduct.ToList(); tbCountSp.Text = listproduct.Count().ToString(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btimprot_HD_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == true) { var wookbook = new Workbook(openFileDialog.FileName); var tabs = wookbook.Worksheets; using (var db = new QuanLyNongSanDBEntities()) { foreach (var tab in tabs) { try { if (tab.Name == "InvoiceStatus") { var col = 'B'; var row = 3; var cell = tab.Cells[$"{ col}{ row}"]; while (cell.Value != null) { var key = tab.Cells[$"C{row}"].IntValue; var tinhtrang = tab.Cells[$"D{row}"].StringValue; var newInvoiceStatus = new InvoiceStatu() { Key = key, Value = tinhtrang, }; db.InvoiceStatus.Add(newInvoiceStatus); db.SaveChanges(); row++; cell = tab.Cells[$"{ col}{ row}"]; } } } catch (Exception ex) { throw ex; } } foreach (var tab in tabs) { try { if (tab.Name == "Invoice") { var col = 'B'; var row = 3; var cell = tab.Cells[$"{ col}{ row}"]; while (cell.Value != null) { var tenkh = tab.Cells[$"C{row}"].StringValue; var diachikh = tab.Cells[$"D{row}"].StringValue; var sdt = tab.Cells[$"E{row}"].StringValue; var ngaytao = tab.Cells[$"F{row}"].StringValue; var ngayupdate = tab.Cells[$"G{row}"].StringValue; var tongtien = tab.Cells[$"H{row}"].DoubleValue; var idloai = tab.Cells[$"I{row}"].IntValue; var newInvoice = new Invoice() { TenKH = tenkh, DiaChiKH = diachikh, SDT = sdt, NgayTao = ngaytao, NgayUpdate = ngayupdate, TongTien = tongtien, IdStatus = idloai }; db.Invoices.Add(newInvoice); db.SaveChanges(); row++; cell = tab.Cells[$"{ col}{ row}"]; } } } catch (Exception ex) { throw ex; } } foreach (var tab in tabs) { try { if (tab.Name == "InvoiceDetail") { var col = 'B'; var row = 3; var cell = tab.Cells[$"{ col}{ row}"]; while (cell.Value != null) { var idinvoice = tab.Cells[$"C{row}"].IntValue; var idproduct = tab.Cells[$"D{row}"].IntValue; var soluong = tab.Cells[$"E{row}"].IntValue; var giaban = tab.Cells[$"F{row}"].DoubleValue; var tongtien = tab.Cells[$"G{row}"].DoubleValue; var newInvoiceDetail = new InvoiceDetail() { IdInvoice = idinvoice, IdProduct = idproduct, SoLuong = soluong, GiaBan = giaban, TongTien = tongtien }; db.InvoiceDetails.Add(newInvoiceDetail); db.SaveChanges(); row++; cell = tab.Cells[$"{ col}{ row}"]; } } } catch (Exception ex) { throw ex; } } } LoadInvoices(); } }