/// <summary> /// /// </summary> /// <param name="e"></param> private void Insert(DirectEventArgs e) { DateTime?startTime = null; DateTime?endTime = null; var modelTimeSheet = new TimeSheetCodeModel(null) { CreatedDate = DateTime.Now }; //Edit data EditData(modelTimeSheet, ref startTime, ref endTime); var checkTime = TimeSheetCodeController.GetAll(null, null, null, txtTimeSheetCode.Text, null, true, startTime, endTime, null, null); if (checkTime != null && checkTime.Count > 0) { Dialog.Alert("Mã chấm công đã tồn tại. Vui lòng nhập mã chấm công khác!"); return; } TimeSheetCodeController.Create(modelTimeSheet); if (e.ExtraParams["Close"] == "True") { wdTimeSheetCode.Hide(); ResetForm(); } }
/// <summary> /// Read data from excel file and save to db /// </summary> /// <param name="dataTable"></param> /// <param name="currentRow"></param> private void MainProcess(DataTable dataTable, int currentRow) { // Finish import file if (currentRow >= dataTable.Rows.Count) { Dialog.Alert("Success"); gridTimeSheetCode.Reload(); txtSheetName.Clear(); txtFromRow.Clear(); txtToRow.Clear(); return; } count++; var listRecordId = TimeSheetCodeController.GetAll(null, null, null, null, null, true, null, null, null, null); var timeSheetCode = new hr_TimeSheetCode() { StartTime = DateTime.Now, IsActive = true, CreatedDate = DateTime.Now, CreatedBy = CurrentUser.User.DisplayName, }; foreach (DataColumn col in dataTable.Columns) { switch (col.ColumnName) { case nameof(TimeSheetCodeModel.EmployeeCode): var record = RecordController.GetByEmployeeCode(dataTable.Rows[currentRow][col].ToString()); if (record != null) { timeSheetCode.RecordId = record.Id; } break; default: var reg = @"\(([^)]*)\)"; var propVal = dataTable.Rows[currentRow][col]; if (Regex.IsMatch(propVal.ToString(), reg)) { propVal = Regex.Match(propVal.ToString(), reg).Groups[1].Value; } //get the property information based on the type var propInfo = timeSheetCode.GetType().GetProperty(col.ColumnName); //find the property type if (propInfo == null) { continue; } if (!propInfo.CanWrite) { continue; } var propType = propInfo.PropertyType; //equivalent to the specified object. if (propType.IsEnum) { propVal = Enum.ToObject(propType, int.Parse(propVal.ToString())); } if (propType.Name == typeof(Nullable <>).Name) { if (Nullable.GetUnderlyingType(propType) == typeof(DateTime)) { if (!DateTime.TryParseExact(propVal.ToString(), "dd/MM/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out var date)) { break; } propVal = Convert.ChangeType(date, typeof(DateTime)); } else { propVal = Convert.ChangeType(propVal, Nullable.GetUnderlyingType(propType)); } } else { propVal = Convert.ChangeType(propVal, propType); } //Set the value of the property propInfo.SetValue(timeSheetCode, propVal, null); break; } } // Check if RecordId exists in TimeSheetCode table if (!string.IsNullOrEmpty(timeSheetCode.Code)) { // create model var timeSheetCodeMode = new TimeSheetCodeModel(timeSheetCode); if (listRecordId.All(tsc => tsc.RecordId != timeSheetCode.RecordId)) { TimeSheetCodeController.Create(timeSheetCodeMode); ContinueProcess(count); } else { var timeSheetCodeJson = JSON.Serialize(timeSheetCode); hdfTimeSheetCode.Text = timeSheetCodeJson; // Open Messagebox RM.RegisterClientScriptBlock("confirm", "showResult(" + count + ", '" + (string.IsNullOrEmpty(timeSheetCodeMode.EmployeeCode) ? "0" : timeSheetCodeMode.EmployeeCode) + "', '" + (string.IsNullOrEmpty(timeSheetCodeMode.FullName) ? "0" : timeSheetCodeMode.FullName) + "');"); } } }