public ActionResult GenerateCreateBOMTextFile(int bomFileID, string fileName, string userSAP, string validDateText, string pathText, int pageNo, List <string> options, List <string> sheets) { try { var enUser = Environment.UserName; var userName = !string.IsNullOrEmpty(enUser) ? enUser : Session["Username"].ToString(); log.Info("========== Generate By " + userName + " ========="); DateTime validDate = DateTime.ParseExact(validDateText, "dd/MM/yyyy", usCulture); string path = pathText; string extension = Path.GetExtension(pathText).ToLower(); //var pathRangeExcelJson = Path.Combine(Server.MapPath("~/"), "rangeExcel.json"); //List<RangeExcelModel> items = new List<RangeExcelModel>(); //using (StreamReader r = new StreamReader(pathRangeExcelJson)) //{ // string json = r.ReadToEnd(); // items = JsonConvert.DeserializeObject<List<RangeExcelModel>>(json); //} List <SheetsModel> objSheetsList = new List <SheetsModel>(); List <SheetsModel> objSheetsActivityList = new List <SheetsModel>(); if (sheets.Contains("Special Pack")) { objSheetsList.Add(new SheetsModel() { Name = "Special Pack", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "Special Pack Activity", Count = 1 }); } if (sheets.Contains("CCS Cut and Pack")) { objSheetsList.Add(new SheetsModel() { Name = "CCS Cut and Pack", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "CCS Cut and Pack Activity", Count = 1 }); } if (sheets.Contains("CCS PMMA")) { objSheetsList.Add(new SheetsModel() { Name = "CCS PMMA", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "CCS PMMA Activity", Count = 1 }); } if (sheets.Contains("Additive")) { objSheetsList.Add(new SheetsModel() { Name = "Additive", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "Additive Activity", Count = 1 }); } if (sheets.Contains("CCS Syrup")) { objSheetsList.Add(new SheetsModel() { Name = "CCS Syrup", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "CCS Syrup Activity", Count = 1 }); } if (sheets.Contains("CCS Initiator")) { objSheetsList.Add(new SheetsModel() { Name = "CCS Initiator", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "CCS Initiator Activity", Count = 1 }); } if (sheets.Contains("Packing Pattern")) { objSheetsList.Add(new SheetsModel() { Name = "Packing Pattern", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "Packing Pattern Activity", Count = 1 }); } if (sheets.Contains("Gasket")) { objSheetsList.Add(new SheetsModel() { Name = "Gasket", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "Gasket Activity", Count = 1 }); } if (sheets.Contains("Value Cullet")) { objSheetsList.Add(new SheetsModel() { Name = "Value Cullet", Count = 1 }); objSheetsActivityList.Add(new SheetsModel() { Name = "Value Cullet Activity", Count = 1 }); } List <List <DataTable> > dtList = ExcelUtility.ReadCCSBOMExcel(path, extension, objSheetsList); List <List <DataTable> > dtActivityList = ExcelUtility.ReadCCSBOMActivityExcel(path, extension, objSheetsActivityList); // delete all files before generate new files string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/CCS/SAP/BOM")); foreach (string filePath in filePaths) { System.IO.File.Delete(filePath); } int i = 0; foreach (var o in dtList) // map 1 by 1 bom and activity { List <BOMHeaderModel> list1 = null; List <BOMItemModel> list2 = null; ExcelUtility.ConvertCCSBOMExcelToCCSBOMModel(o, ref list1, ref list2); List <BOMHeaderModel> acList1 = null; List <BOMItemModel> acList2 = null; ExcelUtility.ConvertCCSBOMActivityExcelToCCSBOMActivityModel(dtActivityList[i], ref acList1, ref acList2); //int limit = 100; // 100 items limit by header //if (list1.Count() > limit) // int j = 1; //{ // int ht = 0; // int hc = 100; // number of hlist // int countHList = 100; // while (ht < list1.Count()) // { // List<BOMHeaderModel> listHcut = new List<BOMHeaderModel>(); // List<BOMHeaderModel> listHactcut = new List<BOMHeaderModel>(); // int hlast = list1.Count(); // int hCount = countHList > hlast ? hlast - ht : hc; // bom & act same // listHcut = list1.GetRange(ht, hCount); // listHactcut = acList1.GetRange(ht, hCount); // List<BOMHeaderModel> newList1 = BOMUtility.CheckBOMAlt(listHcut); // string textName = fileName + sheets[i].Replace(" ", "") + j; // string textExtension = ".txt"; // string textPath = Path.Combine(Server.MapPath("~/Files/CCS/SAP/BOM"), textName); // SAPUtility.ConvertToMMABOMTextFile(newList1, list2, listHactcut, acList2, textPath, fileName, textExtension, userSAP, validDate, options); // ht += hc; // countHList += hc; // j++; // } //} //else //{ List <BOMHeaderModel> newList1 = BOMUtility.CheckBOMAlt(list1); string textName = fileName + sheets[i].Replace(" ", ""); log.Info("========== " + textName + " Start ========="); string textExtension = ".txt"; string textPath = Path.Combine(Server.MapPath("~/Files/CCS/SAP/BOM"), textName); SAPUtility.ConvertToMMABOMTextFile(newList1, list2, acList1, acList2, textPath, fileName, textExtension, userSAP, validDate, options); //} i++; } int BOMFileStatus = 3; // Create ResponseModel res = core.UpdateStatusBOMFile(bomFileID, BOMFileStatus); if (res.Status) // update status success { log.Info("========== Update Status Success. ========="); BOMFileFilterModel filter = new BOMFileFilterModel(); filter.ProductsTypeID = 2; // CCS filter.Sort = "desc"; filter.Pagination.Page = pageNo; BOMFileViewModel model = core.GetBOMFileView(filter); return(Json(model, JsonRequestBehavior.AllowGet)); } else { log.Error("========== " + res.Message + " ========="); return(Json(res, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { log.Error("========== " + ex.Message + " ========="); return(Json(new ResponseModel() { Status = false, Message = ex.Message }, JsonRequestBehavior.AllowGet)); } }