private void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(gridLookUpEditStock.Text)) { Ultils.GridLookUpEditControlNotNull(gridLookUpEditStock, "Kho Hàng"); } else if (string.IsNullOrEmpty(gridLookUpEditProductGroup.Text)) { Ultils.GridLookUpEditControlNotNull(gridLookUpEditProductGroup, "Nhóm Hàng"); } else if (string.IsNullOrEmpty(txtProductID.Text)) { Ultils.TextControlNotNull(txtProductID, "Mã Hàng"); } else if (string.IsNullOrEmpty(txtProductName.Text)) { Ultils.TextControlNotNull(txtProductName, "Tên Hàng"); } else if (string.IsNullOrEmpty(gridLookUpEditUnit.Text)) { Ultils.GridLookUpEditControlNotNull(gridLookUpEditUnit, "Đơn Vị Tính"); } else if (string.IsNullOrEmpty(txtPrice.Text)) { Ultils.TextControlNotNull(txtPrice, "Giá Nhập"); } else { if (string.IsNullOrEmpty(_productId)) { _product = new Product() { ProductID = txtProductID.Text, ProductName = txtProductName.Text, ProductGroupID = gridLookUpEditProductGroup.EditValue.ToString(), StockID = gridLookUpEditStock.EditValue.ToString(), UnitID = gridLookUpEditUnit.EditValue.ToString(), Origin = txtOrigin.Text, Barcode = string.IsNullOrEmpty(txtBarcode.Text) ? _productService.NextId() : txtBarcode.Text, //Quantity = int.Parse(txtQuantity.Text), Color = txtColor.Text, Supplier = txtSupplier.Text, Description = txtDescription.Text, Active = checkActive.Checked, CreatedDate = DateTime.Now, Price = Convert.ToInt32(txtPrice.Text), CreatedBy = Program.CurrentUser.Username, }; //if (!string.IsNullOrEmpty(gridLookUpEditColor.SelectedText)) //{ // product.ColorID = Convert.ToInt32(gridLookUpEditColor.EditValue.ToString()); //} try { _productService.Add(_product); ResetControls(); _logService.InsertLog(Program.CurrentUser.Username, "Thêm", this.Text); MessageBoxHelper.ShowMessageBoxInfo("Thêm thành công"); } catch (Exception ex) { MessageBoxHelper.ShowMessageBoxError(ex.Message); } } else { // Update _product = _productService.GetProductById(_productId); if (_product != null) { _product.ProductName = txtProductName.Text; _product.ProductGroupID = gridLookUpEditProductGroup.EditValue.ToString(); _product.StockID = gridLookUpEditStock.EditValue.ToString(); _product.UnitID = gridLookUpEditUnit.EditValue.ToString(); _product.Supplier = txtSupplier.Text; _product.Barcode = txtBarcode.Text; _product.Origin = txtOrigin.Text; //product.Quantity = int.Parse(txtQuantity.Text); _product.Description = txtDescription.Text; _product.Active = checkActive.Checked; _product.ModifyDate = DateTime.Now; _product.ModifyBy = Program.CurrentUser.Username; _product.Price = int.Parse(txtPrice.Text); //if (!string.IsNullOrEmpty(gridLookUpEditColor.SelectedText)) //{ // product.ColorID = Convert.ToInt32(gridLookUpEditColor.EditValue.ToString()); //} } try { _productService.Update(_product); _logService.InsertLog(Program.CurrentUser.Username, "Sửa", this.Text); MessageBoxHelper.ShowMessageBoxInfo("Sửa thành công!"); this.Dispose(); } catch (Exception ex) { MessageBoxHelper.ShowMessageBoxError(ex.Message); } } } }
/// <summary> /// Xóa theo ID /// </summary> /// <param name="product"></param> public void Delete(Product product) { _context.Products.Remove(product); SaveChanges(); }
/// <summary> /// Thêm mới /// </summary> /// <param name="product"></param> /// <returns></returns> public void Add(Product product) { _context.Products.Add(product); SaveChanges(); }
/// <summary> /// Cập nhật thông tin /// </summary> /// <param name="product"></param> public void Update(Product product) { _context.Products.Attach(product); _context.Entry(product).State = EntityState.Modified; SaveChanges(); }
/// <summary> /// Lưu /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(gridLookUpEditStock.Text)) { gridLookUpEditStock.Properties.Appearance.BackColor = System.Drawing.Color.Red; XtraMessageBox.Show("Vui lòng chọn một Nhà Cung Cấp!", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning); gridLookUpEditStock.Focus(); } else if (string.IsNullOrEmpty(gridLookUpEditProductGroup.Text)) { gridLookUpEditProductGroup.Properties.Appearance.BackColor = System.Drawing.Color.Red; XtraMessageBox.Show("Vui lòng chọn một Nhóm Hàng!", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning); gridLookUpEditProductGroup.Focus(); } else if (string.IsNullOrEmpty(txtProductID.Text)) { txtProductID.Properties.Appearance.BackColor = System.Drawing.Color.Red; XtraMessageBox.Show("Mã Hàng Hóa không được bỏ trống!", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtProductID.Focus(); } else if (string.IsNullOrEmpty(txtProductName.Text)) { txtProductName.Properties.Appearance.BackColor = System.Drawing.Color.Red; XtraMessageBox.Show("Tên Hàng Hóa không được bỏ trống!", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtProductName.Focus(); } else if (string.IsNullOrEmpty(gridLookUpEditSuppliers.Text)) { gridLookUpEditSuppliers.Properties.Appearance.BackColor = System.Drawing.Color.Red; XtraMessageBox.Show("Vui lòng chọn một Nhà Cung Cấp!", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning); gridLookUpEditSuppliers.Focus(); } else if (string.IsNullOrEmpty(gridLookUpEditUnit.Text)) { gridLookUpEditUnit.Properties.Appearance.BackColor = System.Drawing.Color.Red; XtraMessageBox.Show("Vui lòng chọn một Đơn Vị Tính!", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning); gridLookUpEditUnit.Focus(); } else if (string.IsNullOrEmpty(txtPrice.Text)) { txtPrice.Properties.Appearance.BackColor = System.Drawing.Color.Red; XtraMessageBox.Show("Giá không được bỏ trống!", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtPrice.Focus(); } else { var product = new Product() { ProductID = txtProductID.Text, ProductName = txtProductName.Text, ProductGroupID = gridLookUpEditProductGroup.EditValue.ToString(), StockID = gridLookUpEditStock.EditValue.ToString(), UnitID = gridLookUpEditUnit.EditValue.ToString(), SupplierID = gridLookUpEditSuppliers.EditValue.ToString(), Origin = txtOrigin.Text, Barcode = string.IsNullOrEmpty(txtBarcode.Text) ? _productService.NextId() : txtBarcode.Text, //Quantity = int.Parse(txtQuantity.Text), Description = txtDescription.Text, IsActive = checkActive.Checked, CreatedDate = DateTime.Now, Price = Convert.ToInt32(txtPrice.Text), }; if (!string.IsNullOrEmpty(gridLookUpEditColor.SelectedText)) { product.ColorID = Convert.ToInt32(gridLookUpEditColor.EditValue.ToString()); } try { _productService.Add(product); InsertSysLog(txtProductName.Text); if (XtraMessageBox.Show("Thêm thành công.\n Bạn có muốn thêm mới Hàng Hóa nữa không?", "HỎI", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { ResetControls(); } else { DialogResult = DialogResult.No; } } catch (Exception ex) { XtraMessageBox.Show(string.Format("Lỗi {0}", ex.Message), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }