public SigmaResultType ImportMeterialLib(string filePath, string exportfilepath) { MaterialMgr materialMgr = new MaterialMgr(); CostCodeMgr costCodeMgr = new CostCodeMgr(); SigmaCodeMgr sigmaCodeMgr = new SigmaCodeMgr(); TaskCategoryMgr taskCategoryMgr = new TaskCategoryMgr(); SigmaResultType sigmaResult = new SigmaResultType(); TypeMaterial typeMaterial = new TypeMaterial(); DataLoaderMaterialLib loader = new DataLoaderMaterialLib(); DataTable Exceldt = Element.Shared.Common.ImportHelper.ImportWorkSheet(filePath, true); DataTable ErrDataTable = SetErrTable(Exceldt); int failCount = 0; int rowCount = Exceldt.Rows.Count; int columnCount = Exceldt.Columns.Count; if (rowCount > 0) { loader = MTOImportHelper.GetDataLoaderMaterialLib(Exceldt); string codeName = "ALL"; string codeCategoryUOM = "UOM"; string codeCategoryDiscipline = "DISCIPLINE"; DataSet CostCodeDS = costCodeMgr.ListCostCodeForMaterial(); DataSet SigmaCodeDisciplineDS = sigmaCodeMgr.ListSigmaCodeByCodeCategory(codeName, codeCategoryDiscipline); DataSet SigmaCodeUOMDS = sigmaCodeMgr.ListSigmaCodeByCodeCategory(codeName, codeCategoryUOM); DataRow DisciplineCode = SigmaCodeDisciplineDS.Tables[0].Select("CodeName = '" + Exceldt.Rows[0][loader.Ord_Disicipline] + "'").FirstOrDefault(); DataSet TaskCategoryDS = taskCategoryMgr.ListTaskCategoryByDisciplineCode(DisciplineCode[0].ToString()); DataSet TaskTypeDS = taskCategoryMgr.ListTaskTypeByDisciplineCode(DisciplineCode[0].ToString()); foreach (DataRow row in Exceldt.Rows) { bool isValidation = true; #region Mandatory Check (*) for (int i = 0; i < columnCount; i++) { if (Exceldt.Columns[i].ToString().Substring(0, 1).ToUpper() == "*" && string.IsNullOrEmpty(row.ItemArray.GetValue(i).ToString())) { ErrDataTable.Rows.Add(row.ItemArray); ErrDataTable.Rows[ErrDataTable.Rows.Count - 1]["Fail Reason"] = "The value of [" + Exceldt.Columns[i].ToString() + "] is required."; failCount = failCount + 1; isValidation = false; break; } } #endregion Mandatory Check #region Reference Check (SigmaCode Table) DataRow Discipline = null; if (isValidation) { Discipline = SigmaCodeDisciplineDS.Tables[0].Select("CodeName = '" + row[loader.Ord_Disicipline] + "'").FirstOrDefault(); if (Discipline == null) { ErrDataTable.Rows.Add(row.ItemArray); ErrDataTable.Rows[ErrDataTable.Rows.Count - 1]["Fail Reason"] = "There is no item in the library to match up with the value of [" + row[loader.Ord_Disicipline].ToString() + "]"; failCount = failCount + 1; isValidation = false; } } DataRow TaskCategory = null; if (isValidation) { TaskCategory = TaskCategoryDS.Tables[0].Select("TaskCategoryName = '" + row[loader.Ord_TaskCategory] + "'").FirstOrDefault(); if (TaskCategory == null) { ErrDataTable.Rows.Add(row.ItemArray); ErrDataTable.Rows[ErrDataTable.Rows.Count - 1]["Fail Reason"] = "There is no item in the library to match up with the value of [" + row[loader.Ord_TaskCategory].ToString() + "]"; failCount = failCount + 1; isValidation = false; } } DataRow TaskType = null; if (isValidation) { TaskType = TaskTypeDS.Tables[0].Select("TaskTypeName = '" + row[loader.Ord_TaskType] + "'").FirstOrDefault(); if (TaskType == null) { ErrDataTable.Rows.Add(row.ItemArray); ErrDataTable.Rows[ErrDataTable.Rows.Count - 1]["Fail Reason"] = "There is no item in the library to match up with the value of [" + row[loader.Ord_TaskType].ToString() + "]"; failCount = failCount + 1; isValidation = false; } } DataRow UOM = null; if (isValidation) { UOM = SigmaCodeUOMDS.Tables[0].Select("CodeShortName = '" + row[loader.Ord_UOM] + "' OR CodeName = '" + row[loader.Ord_UOM] + "'").FirstOrDefault(); if (UOM == null) { ErrDataTable.Rows.Add(row.ItemArray); ErrDataTable.Rows[ErrDataTable.Rows.Count - 1]["Fail Reason"] = "There is no item in the library to match up with the value of [" + row[loader.Ord_UOM].ToString() + "]"; failCount = failCount + 1; isValidation = false; } } DataRow CostCode = null; if (isValidation && !string.IsNullOrEmpty(row[loader.Ord_CostCode].ToString())) { CostCode = CostCodeDS.Tables[0].Select("CostCode = '" + row[loader.Ord_CostCode] + "'").FirstOrDefault(); if (CostCode == null) { ErrDataTable.Rows.Add(row.ItemArray); ErrDataTable.Rows[ErrDataTable.Rows.Count - 1]["Fail Reason"] = "There is no item in the library to match up with the value of [" + row[loader.Ord_CostCode].ToString() + "]"; failCount = failCount + 1; isValidation = false; } } #endregion Reference Check #region Duplication Check if (isValidation) { string disciplineCode = Discipline["Code"].ToString().Trim(); string taskCategoryId = TaskCategory["TaskCategoryId"].ToString(); string taskTypeId = TaskType["TaskTypeId"].ToString(); string uomCode = UOM["Code"].ToString().Trim(); string description = row[loader.Ord_MaterialDescription] == null ? "" : row[loader.Ord_MaterialDescription].ToString(); DataSet MaterialDS = materialMgr.ListMaterialByDisciplineTaskCategory(disciplineCode, taskCategoryId, taskTypeId, description, uomCode); if (MaterialDS.Tables[0].Rows.Count > 0) { ErrDataTable.Rows.Add(row.ItemArray); ErrDataTable.Rows[ErrDataTable.Rows.Count - 1]["Fail Reason"] = "This data[Materail] is duplicated."; failCount = failCount + 1; isValidation = false; } } #endregion Duplication Check #region AddMaterial if (isValidation) { typeMaterial.DisciplineCode = Discipline["Code"].ToString().Trim(); typeMaterial.TaskCategoryId = Utilities.ToInt32(TaskCategory["TaskCategoryId"].ToString().Trim()); typeMaterial.TaskTypeId = Utilities.ToInt32(TaskType["TaskTypeId"].ToString().Trim()); typeMaterial.Manhours = Utilities.ToDecimal(row[loader.Ord_ManHour].ToString().Trim()); typeMaterial.UomCode = UOM["Code"].ToString().Trim(); typeMaterial.Vendor = row[loader.Ord_Vendor].ToString(); typeMaterial.Description = row[loader.Ord_MaterialDescription].ToString().Trim(); typeMaterial.PartNumber = row[loader.Ord_PartNumber].ToString().Trim(); typeMaterial.CostCodeId = (CostCode == null) ? 0 : Utilities.ToInt32(CostCode["CostCodeId"].ToString().Trim()); typeMaterial.CreatedBy = userinfo.SigmaUserId; sigmaResult = materialMgr.AddMaterial(typeMaterial); if (sigmaResult.IsSuccessful) { // CustomField for (int i = 0; i < columnCount; i++) { if (Exceldt.Columns[i].ToString().Substring(0, 3).ToUpper() == "UD_") { string RowValue = row.ItemArray.GetValue(i).ToString(); if (!string.IsNullOrEmpty(RowValue)) { sigmaResult.IsSuccessful = CheckMaterialCustomField(Exceldt, Exceldt.Columns[i].ToString().Trim(), sigmaResult.ScalarValue, RowValue); } } } } } #endregion AddMaterial } // Set ImportHistory(SuccessCount/FailCount) sigmaResult = AddImportHistory(Exceldt.Rows.Count, failCount, Path.GetFileName(filePath).ToString(), "MaterialLibrary"); // ConvertExcel file && CSV file Export2Excel.ConvertExcelfromData(ErrDataTable, sigmaResult.ScalarValue.ToString() + ".xlsx", exportfilepath); Export2Excel.ConvertCSVFile(ErrDataTable, sigmaResult.ScalarValue.ToString() + ".csv", exportfilepath); sigmaResult.IsSuccessful = true; } else { sigmaResult.IsSuccessful = false; sigmaResult.ErrorMessage = "no record from file."; } return sigmaResult; }