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; }
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(); } }