/// <summary> /// Thêm mới hoặc Cập nhật thông tin Nhà Cung Cấp /// </summary> /// <param name="supplierName"></param> private Data.Supplier InsertOrUpdateSupplier(string supplierName) { if (!string.IsNullOrEmpty(supplierName)) { Supplier supplier; if (!_suppliersService.CheckSupplierNameExit(supplierName)) { supplier = _suppliersService.GetSupplierByName(supplierName); } else { supplier = new Supplier() { SupplierID = _suppliersService.NextId(), SupplierName = supplierName, CreatedBy = _userName, CreatedDate = DateTime.Now, IsActive = true, }; try { _suppliersService.Add(supplier); } catch (Exception ex) { XtraMessageBox.Show(string.Format("Lỗi thêm Nhà Cung Cấp \n{0}", ex.Message)); } } return(supplier); } return(null); }
/// <summary> /// Lưu thông tin /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveDataFormExel_Click(object sender, EventArgs e) { string strUpdate = null; string strInsert = null; int countUpdate = 0; int countInsert = 0; int countExits = 0; if (!string.IsNullOrEmpty(textEditPathFileExel.Text)) { const string sheetName = "Sheet1"; string pathToExcelFile = textEditPathFileExel.Text.Trim(); var excelFile = new ExcelQueryFactory(pathToExcelFile); excelFile.AddMapping<Supplier>(x => x.AreaID, "AreaName"); excelFile.AddMapping<Supplier>(x => x.SupplierName, "SupplierName"); excelFile.AddMapping<Supplier>(x => x.PhoneNumber, "PhoneNumber"); excelFile.AddMapping<Supplier>(x => x.Address, "Address"); excelFile.AddMapping<Supplier>(x => x.Email, "Email"); excelFile.AddMapping<Supplier>(x => x.AccountNumber, "AccountNumber"); excelFile.AddMapping<Supplier>(x => x.Bank, "Bank"); excelFile.AddMapping<Supplier>(x => x.TaxCode, "TaxCode"); excelFile.AddMapping<Supplier>(x => x.Fax, "Fax"); excelFile.AddMapping<Supplier>(x => x.Website, "Website"); excelFile.TrimSpaces = TrimSpacesType.Both; excelFile.ReadOnly = true; IQueryable<Supplier> suppliers = (from a in excelFile.Worksheet<Supplier>(sheetName) select a); try { foreach (Supplier supplier in suppliers) { // Kiểm tra nếu ID Khu Vực // => Đã tồn tại rồi thì trả về thông tin của Khu vực đó // => Chưa tồn tại Tên khu vực này thì thực hiện thêm mới khu vực // => Nếu Tên Khu vực không được người dùng nhập vào thì gán = null _areaId = InsertOrUpdateArea(supplier.AreaID) != null ? InsertOrUpdateArea(supplier.AreaID).AreaID : null; if (!_suppliersService.CheckSupplierNameExit(supplier.SupplierName)) { // Bỏ qua nếu đã tồn tại rồi if (radioButtonIgnoreIfDepartmentExits.Checked) { countExits++; } // Cập nhật nếu tên Bộ Phận đã tồn tại rồi if (radioButtonUpdateIfDepartmentExits.Checked) { Supplier updateSupplier = _suppliersService.GetSupplierByName(supplier.SupplierName); updateSupplier.UpdateBy = _userName; updateSupplier.ModifyDate = DateTime.Now; if (!string.IsNullOrEmpty(_areaId)) { updateSupplier.AreaID = _areaId; } try { _suppliersService.Update(updateSupplier); countUpdate++; strUpdate += string.Format("{0}, ", supplier.SupplierName); } catch (Exception ex) { XtraMessageBox.Show(string.Format("Lỗi cập nhật \n{0}", ex.Message)); } } } // Nếu tên chưa tồn tại thì thực hiện thêm mới else { supplier.SupplierID = NextId(); if (!string.IsNullOrEmpty(_areaId)) { supplier.AreaID = _areaId; } supplier.CreatedDate = DateTime.Now; supplier.CreatedBy = _userName; supplier.SupplierName = supplier.SupplierName; supplier.IsActive = true; try { _suppliersService.Add(supplier); countInsert++; strInsert += string.Format("{0}, ", supplier.SupplierName); } catch (Exception ex) { XtraMessageBox.Show(string.Format("Lỗi thêm mới \n{0}", ex.Message)); } } } if (XtraMessageBox.Show( string.Format("Thực hiện thành công.\n" + "=> Bỏ qua: {3} - Nhà Cung Cấp đã tồn tại \n" + "=> Thêm mới: {0} - {2} \n" + "=> Cập nhật: {1} - {4} \n" + "Bạn có muốn thêm mới Nhà Cung Cấp nữa không?", countInsert, countUpdate, strInsert, countExits, strUpdate), "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { gridControl1.DataSource = null; textEditPathFileExel.Text = string.Empty; } else { DialogResult = DialogResult.No; } } catch (DbEntityValidationException ex) { var sb = new StringBuilder(); foreach (var eve in ex.EntityValidationErrors) { sb.AppendLine(String.Format("Entity of type '{0}' in state '{1}' has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { sb.AppendLine(String.Format("- Property: '{0}', Error: '{1}'", ve.PropertyName, ve.ErrorMessage)); } } throw new Exception(sb.ToString(), ex); } } else { XtraMessageBox.Show("Vui lòng chọn tập tin để nhập", "Thông Báo Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); textEditPathFileExel.Focus(); } }