private void dgvHoaDonMua_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { try { selectedBuyInvoice = db.hoa_don_mua.Find(dgvHoaDonMua.Rows[e.RowIndex].Cells[0].Value); } catch (Exception) { MessageBox.Show("Click chuột sai vị trí", "Lỗi", MessageBoxButtons.OK); } }
public void load() { selectedBuyInvoice = QuanLy.selectedBuyInvoice; dgvDetail.DataSource = db.chi_tiet_hoa_don_mua.Where(x => x.ma_hoa_don == selectedBuyInvoice.ma_hoa_don).ToList(); lblTitle.Text = "Mã hóa đơn: " + selectedBuyInvoice.ma_hoa_don; cbProduct.DataSource = db.san_pham.ToList(); cbProduct.DisplayMember = "ten_san_pham"; cbProduct.ValueMember = "ma_san_pham"; cbProduct.Enabled = false; txtQuantity.Enabled = false; btnDelete.Enabled = false; btnSave.Enabled = false; }
private void btnSave_Click(object sender, EventArgs e) { try { int quantity = txtQuantity.Value; if (quantity <= 0) { MessageBox.Show("Số lượng phải là số nguyên dương!", "Thông báo", MessageBoxButtons.OK); return; } } catch (Exception) { MessageBox.Show("Yêu cầu nhập đúng định dạng của thông tin!", "Thông báo", MessageBoxButtons.OK); } try { san_pham product = db.san_pham.Find(int.Parse(cbProduct.SelectedValue.ToString())); //Thêm chi tiết hóa đơn chi_tiet_hoa_don_mua entity = new chi_tiet_hoa_don_mua(); entity.ma_hoa_don = selectedBuyInvoice.ma_hoa_don; entity.ma_san_pham = product.ma_san_pham; entity.so_luong = txtQuantity.Value; entity.don_gia = product.gia_san_pham; entity.thanh_tien = entity.so_luong * entity.don_gia; db.chi_tiet_hoa_don_mua.Add(entity); db.SaveChanges(); //Cập nhật số lượng sản phẩm product.so_luong += entity.so_luong; product.tinh_trang = (product.so_luong > 0) ? true : false; db.SaveChanges(); //Cập nhật tổng tiền của hóa đơn hoa_don_mua buyInvoice = db.hoa_don_mua.Find(selectedBuyInvoice.ma_hoa_don); List <chi_tiet_hoa_don_mua> listDetail = db.chi_tiet_hoa_don_mua.Where(x => x.ma_hoa_don == selectedBuyInvoice.ma_hoa_don).ToList(); double sum = 0; foreach (chi_tiet_hoa_don_mua item in listDetail) { sum += item.thanh_tien; } buyInvoice.tong_tien = sum; db.SaveChanges(); load(); } catch (Exception) { MessageBox.Show("Sản phẩm không tồn tại hoặc đã có trong hóa đơn!", "Thông báo", MessageBoxButtons.OK); } }
private void btnDelete_Click(object sender, EventArgs e) { san_pham product = db.san_pham.Find(int.Parse(cbProduct.SelectedValue.ToString())); chi_tiet_hoa_don_mua entity = db.chi_tiet_hoa_don_mua.SingleOrDefault(x => x.ma_hoa_don == selectedBuyInvoice.ma_hoa_don && x.ma_san_pham == product.ma_san_pham); db.chi_tiet_hoa_don_mua.Remove(entity); db.SaveChanges(); //Cập nhật tổng tiền của hóa đơn hoa_don_mua buyInvoice = db.hoa_don_mua.Find(selectedBuyInvoice.ma_hoa_don); List <chi_tiet_hoa_don_mua> listDetail = db.chi_tiet_hoa_don_mua.Where(x => x.ma_hoa_don == selectedBuyInvoice.ma_hoa_don).ToList(); double sum = 0; foreach (chi_tiet_hoa_don_mua item in listDetail) { sum += item.thanh_tien; } buyInvoice.tong_tien = sum; db.SaveChanges(); MessageBox.Show("Xóa dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK); load(); }
public void loadBuyInvoice() { selectedBuyInvoice = null; List <ActionForm> listAction = new List <ActionForm>() { new ActionForm("choose", "Chọn thao tác"), new ActionForm("view", "Xem chi tiết"), new ActionForm("create", "Thêm hóa đơn"), new ActionForm("delete", "Xóa hóa đơn") }; cbActionBuyInvoice.DataSource = listAction; cbActionBuyInvoice.DisplayMember = "action"; cbActionBuyInvoice.ValueMember = "key"; dgvHoaDonMua.DataSource = new db_sale_managementEntities().hoa_don_mua.ToList(); List <nha_cung_cap> listSupplier = new db_sale_managementEntities().nha_cung_cap.ToList(); cbNhaCungCap.DataSource = listSupplier; cbNhaCungCap.DisplayMember = "ten_nha_cung_cap"; cbNhaCungCap.ValueMember = "ma_nha_cung_cap"; }
private void cbActionBuyInvoice_SelectedValueChanged(object sender, EventArgs e) { if (USER_LOGIN.phan_quyen != 1 && USER_LOGIN.phan_quyen != 3 && check == 1) { MessageBox.Show("Bạn không có quyền thực hiện thao tác này!", "Thông báo", MessageBoxButtons.OK); return; } string selectedAction = cbActionBuyInvoice.SelectedValue.ToString(); switch (selectedAction) { case "view": if (selectedBuyInvoice == null) { MessageBox.Show("Chọn hóa đơn muốn xem chi tiết!", "Thông báo", MessageBoxButtons.OK); refresh(); return; } else { ChiTietHoaDonMua xform = new ChiTietHoaDonMua(); xform.ShowDialog(this); refresh(); } break; case "create": hoa_don_mua entity = new hoa_don_mua(); entity.ngay_lap = DateTime.Now; entity.tong_tien = 0; entity.ma_nha_cung_cap = int.Parse(cbNhaCungCap.SelectedValue.ToString()); db.hoa_don_mua.Add(entity); db.SaveChanges(); MessageBox.Show("Thêm dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK); refresh(); break; case "delete": try { if (selectedBuyInvoice == null) { MessageBox.Show("Chọn hóa đơn cần xóa!", "Thông báo", MessageBoxButtons.OK); refresh(); return; } else { if (MessageBox.Show(null, "Việc xóa hóa đơn này sẽ làm mất đi cả các thông tin chi tiết của hóa đơn. Bạn có chắc chắn muốn xóa không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { List <chi_tiet_hoa_don_mua> listDetail = db.chi_tiet_hoa_don_mua.Where(x => x.ma_hoa_don == selectedBuyInvoice.ma_hoa_don).ToList(); foreach (chi_tiet_hoa_don_mua item in listDetail) { db.chi_tiet_hoa_don_mua.Remove(item); db.SaveChanges(); } db.hoa_don_mua.Remove(selectedBuyInvoice); db.SaveChanges(); refresh(); MessageBox.Show("Xóa dữ liệu thành công!", "Chúc mừng", MessageBoxButtons.OK); } } } catch (Exception) { MessageBox.Show("Xóa thất bại", "Lỗi", MessageBoxButtons.OK); } break; } }