private void dgvSanPham_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { try { selectedProduct = db.san_pham.Find(dgvSanPham.Rows[e.RowIndex].Cells[0].Value); } catch (Exception) { MessageBox.Show("Click chuột sai vị trí", "Lỗi", MessageBoxButtons.OK); } }
private void dgvDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (this.dgvDetail.Columns[e.ColumnIndex].Name == "ma_san_pham") { if (e.Value != null) { san_pham product = db.san_pham.Find(e.Value); e.Value = product.ten_san_pham; } } }
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())); if (txtQuantity.Value > product.so_luong) { MessageBox.Show("Số lượng sản phẩm không đủ!", "Thông báo", MessageBoxButtons.OK); return; } //Thêm chi tiết hóa đơn chi_tiet_hoa_don_ban entity = new chi_tiet_hoa_don_ban(); entity.ma_hoa_don = selectedSaleInvoice.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_ban.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_ban saleInvoice = db.hoa_don_ban.Find(selectedSaleInvoice.ma_hoa_don); List <chi_tiet_hoa_don_ban> listDetail = db.chi_tiet_hoa_don_ban.Where(x => x.ma_hoa_don == selectedSaleInvoice.ma_hoa_don).ToList(); double sum = 0; foreach (chi_tiet_hoa_don_ban item in listDetail) { sum += item.thanh_tien; } saleInvoice.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); } }
public void loadSanPham() { selectedProduct = null; actionView = false; 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 sản phẩm"), new ActionForm("update", "Sửa thông tin"), new ActionForm("delete", "Xóa sản phẩm") }; cbActionProduct.DataSource = listAction; cbActionProduct.DisplayMember = "action"; cbActionProduct.ValueMember = "key"; dgvSanPham.DataSource = new db_sale_managementEntities().san_pham.ToList(); }
private void btnSubmit_Click(object sender, EventArgs e) { if (txtTenSanPham.Text.Length <= 0 || txtDonViTinh.Text.Length <= 0 || txtThongSoKT.Text.Length <= 0) { MessageBox.Show("Yêu cầu nhập đầy đủ thông tin!", "Thông báo", MessageBoxButtons.OK); return; } try { double price = double.Parse(txtGiaSP.Text); } catch (Exception) { MessageBox.Show("Số điện thoại phải là số!", "Thông báo", MessageBoxButtons.OK); return; } if (selectedProduct == null) { san_pham entity = new san_pham(); entity.ten_san_pham = txtTenSanPham.Text; entity.gia_san_pham = double.Parse(txtGiaSP.Text); entity.so_luong = 0; entity.don_vi_tinh = txtDonViTinh.Text; entity.thong_so_ky_thuat = txtThongSoKT.Text; entity.tinh_trang = false; entity.ma_loai_san_pham = int.Parse(cbLoaiSanPham.SelectedValue.ToString()); db.san_pham.Add(entity); db.SaveChanges(); MessageBox.Show("Thêm dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK); } else { san_pham entity = db.san_pham.Find(selectedProduct.ma_san_pham); entity.ten_san_pham = txtTenSanPham.Text; entity.gia_san_pham = double.Parse(txtGiaSP.Text); entity.don_vi_tinh = txtDonViTinh.Text; entity.thong_so_ky_thuat = txtThongSoKT.Text; entity.ma_loai_san_pham = int.Parse(cbLoaiSanPham.SelectedValue.ToString()); db.SaveChanges(); MessageBox.Show("Chỉnh sửa dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK); } this.Hide(); }
private void btnDelete_Click(object sender, EventArgs e) { san_pham product = db.san_pham.Find(int.Parse(cbProduct.SelectedValue.ToString())); chi_tiet_hoa_don_ban entity = db.chi_tiet_hoa_don_ban.SingleOrDefault(x => x.ma_hoa_don == selectedSaleInvoice.ma_hoa_don && x.ma_san_pham == product.ma_san_pham); db.chi_tiet_hoa_don_ban.Remove(entity); db.SaveChanges(); //Cập nhật tổng tiền của hóa đơn hoa_don_ban saleInvoice = db.hoa_don_ban.Find(selectedSaleInvoice.ma_hoa_don); List <chi_tiet_hoa_don_ban> listDetail = db.chi_tiet_hoa_don_ban.Where(x => x.ma_hoa_don == selectedSaleInvoice.ma_hoa_don).ToList(); double sum = 0; foreach (chi_tiet_hoa_don_ban item in listDetail) { sum += item.thanh_tien; } saleInvoice.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 loadForm() { cbTinhTrang.Enabled = false; txtSoLuong.Enabled = false; List <ComboboxUtils> listStatus = new List <ComboboxUtils>() { new ComboboxUtils(0, "Hết hàng"), new ComboboxUtils(1, "Còn hàng") }; ComboboxUtils status = new ComboboxUtils(); cbTinhTrang.DataSource = listStatus; cbTinhTrang.DisplayMember = "text"; cbTinhTrang.ValueMember = "value"; List <loai_san_pham> list = db.loai_san_pham.ToList(); cbLoaiSanPham.DataSource = list; cbLoaiSanPham.DisplayMember = "ten_loai_san_pham"; cbLoaiSanPham.ValueMember = "ma_loai_san_pham"; selectedProduct = (selectedProduct != null) ? new db_sale_managementEntities().san_pham.Find(selectedProduct.ma_san_pham) : null; }
private void cbActionProduct_SelectedValueChanged(object sender, EventArgs e) { string selectedAction = cbActionProduct.SelectedValue.ToString(); switch (selectedAction) { case "view": if (selectedProduct == null) { MessageBox.Show("Chọn sản phẩm muốn xem chi tiết!", "Thông báo", MessageBoxButtons.OK); refresh(); return; } else { actionView = true; SanPhamForm xform = new SanPhamForm(); xform.ShowDialog(this); refresh(); } break; case "create": if (USER_LOGIN.phan_quyen == 1 || USER_LOGIN.phan_quyen == 4) { selectedProduct = null; SanPhamForm form = new SanPhamForm(); form.ShowDialog(this); refresh(); } else { 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; } break; case "update": if (USER_LOGIN.phan_quyen == 1 || USER_LOGIN.phan_quyen == 4) { if (selectedProduct == null) { MessageBox.Show("Chọn bản ghi cần sửa!", "Thông báo", MessageBoxButtons.OK); refresh(); return; } else { SanPhamForm x = new SanPhamForm(); x.ShowDialog(this); refresh(); } } else { 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; } break; case "delete": try { if (USER_LOGIN.phan_quyen == 1 || USER_LOGIN.phan_quyen == 4) { if (selectedProduct == null) { MessageBox.Show("Chọn bản ghi cần xóa!", "Thông báo", MessageBoxButtons.OK); refresh(); return; } else { if (db.chi_tiet_hoa_don_ban.Where(x => x.ma_san_pham == selectedProduct.ma_san_pham).ToList().Count > 0 || db.chi_tiet_hoa_don_mua.Where(x => x.ma_san_pham == selectedProduct.ma_san_pham).ToList().Count > 0) { MessageBox.Show("Sản phẩm này liên quan đến nhiều dữ liệu khác, không thể xóa!", "Chúc mừng", MessageBoxButtons.OK); return; } if (MessageBox.Show(null, "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) { db.san_pham.Remove(selectedProduct); db.SaveChanges(); refresh(); MessageBox.Show("Xóa dữ liệu thành công!", "Chúc mừng", MessageBoxButtons.OK); } } } else { 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; } } catch (Exception) { MessageBox.Show("Xóa thất bại", "Lỗi", MessageBoxButtons.OK); } break; } }