/// <summary> /// Tạo bảng đầy đủ các phụ cấp /// </summary> /// <returns></returns> private DataTable CreateSalaryAllowanceTable(List <SalaryAllowanceModel> existedSalaryAllowances) { // get catalog var catalogAllowances = CatalogAllowanceController.GetAll(null, null, null, null, CatalogStatus.Active, false, null, null); var count = 0; // fill data to datatable foreach (var catalogAllowance in catalogAllowances) { var match = existedSalaryAllowances.Where(s => s.AllowanceCode.Contains(catalogAllowance.Code)).ToList(); if (match.Count == 0) { existedSalaryAllowances.Add(new SalaryAllowanceModel { AllowanceCode = catalogAllowance.Code, AllowanceName = catalogAllowance.Name }); } } // add taxable column var data = existedSalaryAllowances.ToDataTable(); data.Columns.Add("Taxable"); // fill data to taxable column foreach (var catalogAllowance in catalogAllowances) { foreach (DataRow row in data.Rows) { if (row["AllowanceCode"].ToString() == catalogAllowance.Code) { data.Rows[count++]["Taxable"] = catalogAllowance.Taxable ? 1 : 0; } } } return(data); }
/// <summary> /// Insert or Update Catalog /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void InsertOrUpdate(object sender, DirectEventArgs e) { try { // init model var model = new CatalogAllowanceModel { EditedBy = CurrentUser.User.UserName }; // check id if (!string.IsNullOrEmpty(hdfId.Text) && Convert.ToInt32(hdfId.Text) > 0) { var result = CatalogAllowanceController.GetById(Convert.ToInt32(hdfId.Text));; if (result != null) { model = result; } } // validate form if (string.IsNullOrEmpty(hdfValueType.Text) || string.IsNullOrEmpty(hdfValueType.Text)) { Dialog.ShowError("Bạn chưa nhập đủ dữ liệu"); return; } // set new props for model model.Code = hdfType.Text; model.Name = txtName.Text; model.Description = txtDescription.Text; model.Value = !string.IsNullOrEmpty(txtValue.Text) ? Convert.ToDecimal(txtValue.Text) : 0; model.Formula = txtFormula.Text; model.ValueType = (AllowanceValueType)Convert.ToInt32(hdfValueType.Text); model.Type = hdfType.Text; model.Group = hdfGroup.Text; model.Order = !string.IsNullOrEmpty(txtOrder.Text) ? Convert.ToInt32(txtOrder.Text) : 0; model.Status = !string.IsNullOrEmpty(hdfStatus.Text) ? (CatalogStatus)Convert.ToInt32(hdfStatus.Text) : CatalogStatus.Active; // check model id if (model.Id > 0) { // update var result = CatalogAllowanceController.Update(model); if (result != null) { // log action SystemLogController.Create(new SystemLogModel(CurrentUser.User.UserName, "Danh mục phụ cấp", SystemAction.Edit, "Sửa danh mục {0}".FormatWith(model.Name))); } else { // show error Dialog.ShowError("Có lỗi xảy ra trong quá trình xử lý"); } } else { // set created user model.CreatedBy = CurrentUser.User.UserName; // insert var result = CatalogAllowanceController.Create(model); if (result != null) { // log action SystemLogController.Create(new SystemLogModel(CurrentUser.User.UserName, "Danh mục phụ cấp", SystemAction.Create, "Tạo danh mục {0}".FormatWith(model.Name))); } else { // show error Dialog.ShowError("Có lỗi xảy ra trong quá trình xử lý"); } } // hide window wdSetting.Hide(); // reload data gpCatalog.Reload(); } catch (Exception ex) { // show dilog Dialog.ShowError(ex); // log exception SystemLogController.Create(new SystemLogModel(CurrentUser.User.UserName, "Danh mục phụ cấp - InserOrUpdate", ex)); } }
/// <summary> /// Init setting window /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void InitWindow(object sender, DirectEventArgs e) { try { // init id var param = e.ExtraParams["Id"]; // parse id if (int.TryParse(param, out var id)) { // init window props if (id > 0) { // edit wdSetting.Title = @"Sửa"; wdSetting.Icon = Icon.Pencil; } else { // insert wdSetting.Title = @"Thêm mới"; wdSetting.Icon = Icon.Add; } // init id hdfId.Text = id.ToString(); // init object var model = new CatalogAllowanceModel(); // check id if (id > 0) { var result = CatalogAllowanceController.GetById(id); if (result != null) { model = result; } } // set catalog props txtName.Text = model.Name; txtDescription.Text = model.Description; txtValue.Text = model.Value.ToString("0.##"); txtFormula.Text = model.Formula; hdfValueType.Text = ((int)model.ValueType).ToString(); cboValueType.Text = model.ValueTypeName; hdfType.Text = model.Type; cboType.Text = model.TypeName; hdfGroup.Text = model.Group; cboGroup.Text = model.GroupName; txtOrder.Text = model.Order.ToString(); hdfStatus.Text = ((int)model.Status).ToString(); cboStatus.Text = model.Status.Description(); // show window wdSetting.Show(); } } catch (Exception ex) { // show dilog Dialog.ShowError(ex); // log exception SystemLogController.Create(new SystemLogModel(CurrentUser.User.UserName, "Danh mục phụ cấp - InitWindow", ex)); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUpdateImportExcel_Click(object sender, DirectEventArgs e) { var workbook = new WorkBook(); if (fileExcel.HasFile) { var path = UploadFile(fileExcel, Constant.PathTemplate); if (path != null) { // init list changes var allowanceChanges = new List <SalaryAllowanceModel>(); // Read data from excel workbook.readXLSX(Path.Combine(Server.MapPath("~/"), Constant.PathTemplate, path)); // Export to data table 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 ); foreach (DataRow row in dataTable.Rows) { // get value from cell var allowanceCode = row[nameof(SalaryAllowanceModel.AllowanceCode)].ToString(); var allowanceName = row[nameof(SalaryAllowanceModel.AllowanceName)].ToString(); var factor = row[nameof(SalaryAllowanceModel.Factor)].ToString(); var percent = row[nameof(SalaryAllowanceModel.Percent)].ToString(); var value = row[nameof(SalaryAllowanceModel.Value)].ToString(); var salaryDecisionId = int.TryParse(hdfId.Text, out var result) && result > 0 ? result : 0; // check condition if (string.IsNullOrEmpty(allowanceCode)) { continue; } if (!decimal.TryParse(factor, out var factorResult)) { continue; } if (!decimal.TryParse(percent, out var percentResult)) { continue; } if (!decimal.TryParse(value, out var valueResult)) { continue; } // get catalog by code var catalogAllowance = CatalogAllowanceController.GetByCode(allowanceCode); if (catalogAllowance == null) { continue; } // init model var salaryAllowance = new SalaryAllowanceModel(); // get allowance by decision and code var salaryAllowances = SalaryAllowanceController.GetAll(null, salaryDecisionId, allowanceCode, null, 1); if (salaryAllowances.Any()) { salaryAllowance = salaryAllowances.First(); } else { salaryAllowance.Id = 0; salaryAllowance.AllowanceCode = allowanceCode; } // check are there any changes if (salaryAllowance.Factor != factorResult || salaryAllowance.Percent != percentResult || salaryAllowance.Value != valueResult) { // set value salaryAllowance.AllowanceName = allowanceName; salaryAllowance.Factor = factorResult; salaryAllowance.Percent = percentResult; salaryAllowance.Value = valueResult; // save data to hidden field SaveEditData(JSON.Serialize(salaryAllowance)); // add data to list changes allowanceChanges.Add(salaryAllowance); } if (salaryAllowance.Factor != 0 || salaryAllowance.Percent != 0 || salaryAllowance.Value != 0) { allowanceChanges.Add(salaryAllowance); } } // hide window wdExcel.Hide(); // bind data storeAllowanceCatalog.DataSource = CreateSalaryAllowanceTable(allowanceChanges); storeAllowanceCatalog.DataBind(); } Dialog.ShowNotification("Thêm thành công"); } else { Dialog.Alert("Bạn chưa chọn tệp tin đính kèm. Vui lòng chọn."); } }