private void LoadDataGridView_Haunt(int MaHD) { int stt = 1; decimal sum = 0; DataTable dt = new DataTable(); dt.Columns.Add("STT"); dt.Columns.Add("Mã sản phẩm"); dt.Columns.Add("Tên sản phẩm"); dt.Columns.Add("Size"); dt.Columns.Add("Số lượng"); dt.Columns.Add("Đơn giá"); dt.Columns.Add("khuyến mãi"); dt.Columns.Add("Thành tiền"); List <HauntDetail> listHauntDetail = listHauntDetail_Database; for (int i = 0; i < listHauntDetail.Count; i++, stt++)// thêm khách hàng vào DataTable { if (listHauntDetail[i].OrderHauntID == MaHD) { HauntDetail hdt = listHauntDetail[i]; dt.Rows.Add(stt.ToString(), hdt.ProductID, hdt.Name, hdt.SizeCode, hdt.Quantity, hdt.Price, hdt.Discount + "%", hdt.TotalPrice); sum = sum + (decimal)hdt.TotalPrice; } } txtTongTienHoaDon.Text = sum.ToString(); dgvChiTietHoaDon.DataSource = dt; // chắc là điền dữ liệu vào dgv for (int i = 0; i < dt.Columns.Count; i++) // chỗ này không biết nó dùng để làm gì luôn => chỗ này là đổ dữ liều từ datatable lên datagridview nhưng chưa hiểu rõ { if (i < dt.Columns.Count - 1) { dgvChiTietHoaDon.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; } else { dgvChiTietHoaDon.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } } }
private void BtnLuuHD_Click(object sender, EventArgs e) { try { if (listHauntDetail_Database.Count != 0) { int hauntDetailID; if (db.HauntDetails.ToList().Count == 0) { hauntDetailID = 1; } else { hauntDetailID = (db.HauntDetails.OrderByDescending(o => o.HauntDetailID).First().HauntDetailID) + 1; } int mahd = int.Parse(txtMaHoaDon.Text); OrderHaunt odh = db.OrderHaunts.FirstOrDefault(o => o.OrderHauntID == mahd); HauntDetail hd = new HauntDetail(); ProductDetail prdt = new ProductDetail(); int dem = hauntDetailID; for (int i = 0; i < listHauntDetail_Database.Count; i++, dem++) { listHauntDetail_Database[i].HauntDetailID = dem; hd = listHauntDetail_Database[i]; prdt = db.ProductDetails.FirstOrDefault(p => p.SizeCode == hd.SizeCode && p.ProductID == hd.ProductID); prdt.Quantity = prdt.Quantity - hd.Quantity; db.HauntDetails.Add(hd); } MessageBox.Show("lưu hóa đơn thành công"); btnLuuHD.Enabled = false; btnThemHoaDon.Enabled = true; odh.ToTalPrice = listHauntDetail_Database.Sum(s => s.TotalPrice); db.SaveChanges(); } else if (listOrderDetail_Database.Count != 0)// nhớ là khi khởi tạo 1 new list nó sẽ được cấp 1 ô nhớ để lưu địa chỉ ô nhớ khác => nó không null { int orderDetailID; if (db.OrderDetails.ToList().Count == 0) { orderDetailID = 1; } else { orderDetailID = (db.OrderDetails.OrderByDescending(o => o.OrderDetailID).First().OrderDetailID) + 1; } int mahd = int.Parse(txtMaHoaDon.Text); Order odr = db.Orders.FirstOrDefault(o => o.OrderID == mahd); OrderDetail od = new OrderDetail(); ProductDetail prdt = new ProductDetail(); int dem = orderDetailID; for (int i = 0; i < listOrderDetail_Database.Count; i++, dem++) { listOrderDetail_Database[i].OrderDetailID = dem; od = listOrderDetail_Database[i]; prdt = db.ProductDetails.FirstOrDefault(p => p.SizeCode == od.SizeCode && p.ProductID == od.ProductID); prdt.Quantity = prdt.Quantity - od.Quantity; db.OrderDetails.Add(od); } odr.ToTalPrice = listOrderDetail_Database.Sum(s => s.TotalPrice); Customer ct = db.Customers.FirstOrDefault(c => c.SDT == txtSDT.Text); ct.BuyTotal = ct.BuyTotal + odr.ToTalPrice; db.SaveChanges(); MessageBox.Show("lưu hóa đơn thành công"); Customer update = db.Customers.FirstOrDefault(x => x.SDT == txtSDT.Text); if (update != null) { txtTongTienDaMua.Text = update.BuyTotal.ToString(); } btnLuuHD.Enabled = false; btnThemHoaDon.Enabled = true; } else { MessageBox.Show("hóa đơn rỗng, vui lòng thêm sản phẩm."); } } catch (Exception ex) { Exception exception = ex.GetBaseException(); MessageBox.Show(exception.Message); MessageBox.Show("đã bị lỗi ở lưu hóa đơn"); } }
private void addDataToView() // thêm dữ liệu vào trong chi tiết hóa đơn_Database { try { btnLuuHD.Enabled = true; Product prd = new Product(); ProductDetail prdt = new ProductDetail(); HauntDetail hdt = new HauntDetail();// tạo 1 chi tiêt hóa đơn mới OrderDetail od = new OrderDetail(); string check = cmbSize.Text; prd = db.Products.FirstOrDefault(p => p.ProductID == txtMaSanPham.Text); prdt = db.ProductDetails.FirstOrDefault(p => p.ProductID == txtMaSanPham.Text && p.SizeCode == check); if (txtMaKH.Text == "") { if (prd == null) { MessageBox.Show("không tồn tại sản phẩm bạn cần tìm"); } else { int soluongkho = int.Parse(prdt.Quantity.ToString()); if (txtSoLuong.Text != "") { if (soluongkho >= int.Parse(txtSoLuong.Text)) { // int hauntDetailID = (db.HauntDetails.OrderByDescending(o => o.HauntDetailID).First().HauntDetailID) + 1; ; txtTenSanPham.Text = prd.Name; txtDonGia.Text = prd.BuyPrice.ToString(); hdt.OrderHauntID = int.Parse(txtMaHoaDon.Text);// gán mã hóa đơn hdt.SizeCode = cmbSize.Text; if (txtGiamGia.Text != "") { hdt.Discount = int.Parse(txtGiamGia.Text); } //hdt.HauntDetailID = hauntDetailID; hdt.ProductID = txtMaSanPham.Text; //gán mã sản phẩm hdt.Name = txtTenSanPham.Text; //gán tên sản phẩm hdt.Quantity = int.Parse(txtSoLuong.Text); // gán số lượng !!!nhớ trừ số lượng trong productdetail(chưa xử lý) hdt.Price = decimal.Parse(txtDonGia.Text); // gán đơn giá hdt.TotalPrice = decimal.Parse(txtThanhTien.Text); //gán thành tiền //prdt.Quantity = prdt.Quantity - hdt.Quantity; //xử lý phần trừ được nhắc đến ở trên if (listHauntDetail_Database.FirstOrDefault(h => h.ProductID == hdt.ProductID && h.SizeCode == hdt.SizeCode) != null) { HauntDetail temp = listHauntDetail_Database.FirstOrDefault(h => h.ProductID == hdt.ProductID && h.SizeCode == hdt.SizeCode); temp.Quantity = temp.Quantity + hdt.Quantity; temp.TotalPrice = temp.TotalPrice + hdt.TotalPrice; } else { listHauntDetail_Database.Add(hdt); txtTongTienHoaDon.Text = listHauntDetail_Database.Sum(s => s.TotalPrice).ToString(); } LoadDataGridView_Haunt(hdt.OrderHauntID); txtMaSanPham.Text = ""; txtSoLuong.Text = ""; if (IsNumeric(txtMaKH.Text)) { txtGiamGia.Text = ""; } } } } } else if (txtMaKH.Text != "" && IsNumeric(txtMaKH.Text) && txtSDT.Text != "") { if (prd == null) { MessageBox.Show("không tồn tại sản phẩm bạn cần tìm"); } else { int soluongkho = int.Parse(prdt.Quantity.ToString()); if (txtSoLuong.Text != "") { if (soluongkho >= int.Parse(txtSoLuong.Text)) { //int orderDetailID = (db.OrderDetails.OrderByDescending(o => o.OrderID).First().OrderDetailID) + 1; ; txtTenSanPham.Text = prd.Name; txtDonGia.Text = prd.BuyPrice.ToString(); od.OrderID = int.Parse(txtMaHoaDon.Text);// gán mã hóa đơn if (txtGiamGia.Text != "" && IsNumeric(txtGiamGia.Text)) { od.DisCount = int.Parse(txtGiamGia.Text); } od.SizeCode = cmbSize.Text; //od.OrderDetailID = orderDetailID; od.ProductID = txtMaSanPham.Text; //gán mã sản phẩm od.Name = txtTenSanPham.Text; //gán tên sản phẩm od.Quantity = int.Parse(txtSoLuong.Text); // gán số lượng !!!nhớ trừ số lượng trong productdetail(chưa xử lý) od.Price = decimal.Parse(txtDonGia.Text); // gán đơn giá od.TotalPrice = decimal.Parse(txtThanhTien.Text); //gán thành tiền //prdt.Quantity = prdt.Quantity - od.Quantity; //xử lý phần trừ được nhắc đến ở trên if (listOrderDetail_Database.FirstOrDefault(h => h.ProductID == od.ProductID && h.SizeCode == od.SizeCode) != null) { OrderDetail temp = listOrderDetail_Database.FirstOrDefault(h => h.ProductID == od.ProductID && h.SizeCode == od.SizeCode); temp.Quantity = temp.Quantity + od.Quantity; temp.TotalPrice = temp.TotalPrice + od.TotalPrice; } else { listOrderDetail_Database.Add(od); txtTongTienHoaDon.Text = listOrderDetail_Database.Sum(s => s.TotalPrice).ToString(); } LoadDataGridView_Order(od.OrderID); txtMaSanPham.Text = ""; txtSoLuong.Text = ""; } } } } else { MessageBox.Show("khách hàng không tồn tại"); } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show("bị lỗi ở phần thêm sảm phẩm!"); } }