/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUpdateImportExcel_Click(object sender, DirectEventArgs e) { var workbook = new WorkBook(); // upload file if (fileExcel.HasFile) { var path = UploadFile(fileExcel, Constant.PathTemplate); if (path != null) { // Read data from excel workbook.readXLSX(Path.Combine(Server.MapPath("~/"), Constant.PathTemplate, path)); // Check validation workbook if (CheckValidation(workbook, out _, out _, txtFromRow, txtToRow, txtSheetName) == false) { return; } // Export to datatable var dataTable = workbook.ExportDataTable(0, //first row 0, //first col workbook.LastRow + 1, //last row workbook.LastCol + 1, //last col true, //first row as header false //convert to DateTime object if it match date pattern ); var argumentModels = ArgumentController.GetAll(null, false, KpiStatus.Active, null, null, null); foreach (DataRow row in dataTable.Rows) { //get employee code var employeeCode = row[EmployeeCode].ToString(); if (string.IsNullOrEmpty(employeeCode)) { continue; } // get record by employee code var record = RecordController.GetByEmployeeCode(employeeCode); foreach (DataColumn col in dataTable.Columns) { foreach (var argument in argumentModels) { //check column name exists if (argument.Name != col.ColumnName) { continue; } //check empty string var value = Convert.ToString(row[col], CultureInfo.InvariantCulture); if (string.IsNullOrEmpty(value)) { continue; } //check if value exists var employeeArgument = EmployeeArgumentController.GetUnique(Convert.ToInt32(hdfGroup.Text), record.Id, argument.Id, Convert.ToInt32(hdfMonth.Text), Convert.ToInt32(hdfYear.Text)); if (employeeArgument != null) { //update value employeeArgument.Value = value; employeeArgument.GroupId = !string.IsNullOrEmpty(hdfGroup.Text) ? Convert.ToInt32(hdfGroup.Text) : 0; employeeArgument.EditedBy = CurrentUser.User.UserName; employeeArgument.EditedDate = DateTime.Now; EmployeeArgumentController.Update(employeeArgument); } else { var model = new EmployeeArgumentModel() { RecordId = record.Id, ArgumentId = argument.Id, GroupId = !string.IsNullOrEmpty(hdfGroup.Text) ? Convert.ToInt32(hdfGroup.Text) : 0, Month = Convert.ToInt32(hdfMonth.Text), Year = Convert.ToInt32(hdfYear.Text), Value = value }; EmployeeArgumentController.Create(model); } } } } //reset hdfGroup.Reset(); cboGroup.Reset(); hdfDepartmentId.Reset(); cboDepartment.Reset(); } } else { Dialog.Alert("Bạn chưa chọn tệp tin đính kèm. Vui lòng chọn."); return; } Dialog.Alert("Cập nhật thành công"); //Reset form excel fileExcel.Reset(); txtSheetName.Reset(); //reload grid gpEmployeeArgument.Reload(); }
/// <summary> /// insert or update /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void InsertOrUpdate(object sender, DirectEventArgs e) { try { // init entity var model = new EmployeeArgumentModel(); // check id if (!string.IsNullOrEmpty(hdfId.Text)) { if (Convert.ToInt32(hdfId.Text) > 0) { var result = EmployeeArgumentController.GetById(Convert.ToInt32(hdfId.Text)); ; if (result != null) { model = result; } } } // set new props for entity if (!string.IsNullOrEmpty(hdfArgumentId.Text)) { model.ArgumentId = Convert.ToInt32(hdfArgumentId.Text); } if (!string.IsNullOrEmpty(hdfGroupInput.Text)) { model.GroupId = Convert.ToInt32(hdfGroupInput.Text); } if (!string.IsNullOrEmpty(hdfChooseEmployee.Text)) { model.RecordId = Convert.ToInt32(hdfChooseEmployee.Text); } model.Value = txtValue.Text; if (!string.IsNullOrEmpty(hdfMonth.Text)) { model.Month = Convert.ToInt32(hdfMonth.Text); } if (!string.IsNullOrEmpty(hdfYear.Text)) { model.Year = Convert.ToInt32(hdfYear.Text); } // check entity id if (model.Id > 0) { model.EditedDate = DateTime.Now; model.EditedBy = CurrentUser.User.UserName; // update EmployeeArgumentController.Update(model); } else { model.CreatedBy = CurrentUser.User.UserName; model.CreatedDate = DateTime.Now; model.EditedDate = DateTime.Now; model.EditedBy = ""; //check argument exist var employeeArgument = EmployeeArgumentController.GetUnique(model.GroupId, model.RecordId, model.ArgumentId, model.Month, model.Year); if (employeeArgument != null) { Dialog.ShowNotification("Giá trị tham số này đã được tạo. Vui lòng chọn tham số khác."); } else { // insert EmployeeArgumentController.Create(model); } } // hide window wdSetting.Hide(); //reset form ResetForm(); // reload data gpEmployeeArgument.Reload(); } catch (Exception exception) { Dialog.ShowError(exception); } }