public string GenerateRequestListReport(Guid diaryId, DateTime date, int testingPeriod) { var diaryW = Db.Diaries.Where(x => x.Id == diaryId).Select(DiaryMappings.ToDiaryW).FirstOrDefault(); var acreditedItems = new List <RequestListModel>(); var notAcreditedItems = new List <RequestListModel>(); foreach (var product in diaryW.Products.OrderBy(dp => dp.Number)) { if (product.ProductTests.Any(pt => pt.Test.AcredetationLevel.Level == AcreditationLevels.Acredited)) { var item = new RequestListModel(); item.ProductNumber = product.Number; item.ProductName = product.Name; item.ProductTests = product.ProductTests .Where(pt => pt.Test.AcredetationLevel.Level == AcreditationLevels.Acredited) .Select(pt => new SubListModel() { TestType = pt.Test.TestType.ShortName, TestName = pt.Test.Name, Method = pt.TestMethod.Method, MethodValue = pt.Test.MethodValue, Remark = pt.Remark }) .ToList(); acreditedItems.Add(item); } if (product.ProductTests.Any(pt => pt.Test.AcredetationLevel.Level == AcreditationLevels.NotAcredited)) { var item = new RequestListModel(); item.ProductNumber = product.Number; item.ProductName = product.Name; item.ProductTests = product.ProductTests .Where(pt => pt.Test.AcredetationLevel.Level == AcreditationLevels.NotAcredited) .Select(pt => new SubListModel() { TestType = pt.Test.TestType.ShortName, TestName = pt.Test.Name, Method = pt.TestMethod.Method, MethodValue = pt.Test.MethodValue, Remark = pt.Remark }) .ToList(); notAcreditedItems.Add(item); } } string requestsGeneratedCount = string.Empty; if (acreditedItems.Count > 0) { //GENERATE REQUEST A var model = new ReportModel(); model.ReportParameters.Add("RequestNumber", AcreditationLevels.Acredited + diaryW.Number); model.ReportParameters.Add("TestingPeriod", testingPeriod); model.ReportParameters.Add("Date", date); model.ReportItems = acreditedItems; var report = new RequestListReport(model); var data = report.GenerateReport(); //this is supposed to create all the necessary directories for the file. CheckAndGenerateDirectories(diaryW.Number); var fileProps = GetFileProperties(diaryW.Number, FileNames.RequestListReport, AcreditationLevels.Acredited); if (File.Exists(fileProps.FullPath)) { string newDestination = fileProps.FullPath.Substring(0, fileProps.FullPath.Length - 5) + "_" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".xlsx"; File.Move(fileProps.FullPath, newDestination); } var file = File.Create(fileProps.FullPath); file.Write(data, 0, data.Length); file.Close(); requestsGeneratedCount += "A"; } if (notAcreditedItems.Count > 0) { //Generate Request B var model = new ReportModel(); model.ReportParameters.Add("RequestNumber", AcreditationLevels.NotAcredited + diaryW.Number); model.ReportParameters.Add("TestingPeriod", testingPeriod); model.ReportParameters.Add("Date", date); model.ReportItems = notAcreditedItems; var report = new RequestListReport(model); var data = report.GenerateReport(); //this is supposed to create all the necessary directories for the file. CheckAndGenerateDirectories(diaryW.Number); var fileProps = GetFileProperties(diaryW.Number, FileNames.RequestListReport, AcreditationLevels.NotAcredited); if (File.Exists(fileProps.FullPath)) { string newDestination = fileProps.FullPath.Substring(0, fileProps.FullPath.Length - 5) + "_" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".xlsx"; File.Move(fileProps.FullPath, newDestination); } var file = File.Create(fileProps.FullPath); file.Write(data, 0, data.Length); file.Close(); requestsGeneratedCount += "B"; } return(requestsGeneratedCount); }
public string GenerateRequestListReport(Guid diaryId, DateTime date, int testingPeriod) { var diary = db.Diaries.Single(d => d.Id == diaryId); var diaryW = new DiaryW(diary); var acreditedItems = new List<RequestListModel>(); var notAcreditedItems = new List<RequestListModel>(); foreach (var product in diary.Products.OrderBy(dp => dp.Number)) { if (product.ProductTests.Any(pt => pt.Test.AcredetationLevel.Level == AcreditationLevels.Acredited)) { var item = new RequestListModel(); item.ProductNumber = product.Number; item.ProductName = product.Name; item.ProductTests = product.ProductTests .Where(pt => pt.Test.AcredetationLevel.Level == AcreditationLevels.Acredited) .Select(pt => new SubListModel() { TestType = pt.Test.TestType.ShortName, TestName = pt.Test.Name, Method = pt.TestMethod.Method, MethodValue = pt.Test.MethodValue, Remark = pt.Remark }) .ToList(); acreditedItems.Add(item); } if (product.ProductTests.Any(pt => pt.Test.AcredetationLevel.Level == AcreditationLevels.NotAcredited)) { var item = new RequestListModel(); item.ProductNumber = product.Number; item.ProductName = product.Name; item.ProductTests = product.ProductTests .Where(pt => pt.Test.AcredetationLevel.Level == AcreditationLevels.NotAcredited) .Select(pt => new SubListModel() { TestType = pt.Test.TestType.ShortName, TestName = pt.Test.Name, Method = pt.TestMethod.Method, MethodValue = pt.Test.MethodValue, Remark = pt.Remark }) .ToList(); notAcreditedItems.Add(item); } } string requestsGeneratedCount = string.Empty; if (acreditedItems.Count > 0) { //GENERATE REQUEST A var model = new ReportModel(); model.ReportParameters.Add("RequestNumber", AcreditationLevels.Acredited + diaryW.Number); model.ReportParameters.Add("TestingPeriod", testingPeriod); model.ReportParameters.Add("Date", date); model.reportItems = acreditedItems; var report = new RequestListReport(model); var data = report.GenerateReport(); //this is supposed to create all the necessary directories for the file. CheckAndGenerateDirectories(diary.Number); var fileProps = GetFileProperties(diary.Number, FileNames.RequestListReport, AcreditationLevels.Acredited); if (File.Exists(fileProps.FullPath)) { string newDestination = fileProps.FullPath.Substring(0, fileProps.FullPath.Length - 5) + "_" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".xlsx"; File.Move(fileProps.FullPath, newDestination); } var file = File.Create(fileProps.FullPath); file.Write(data, 0, data.Length); file.Close(); requestsGeneratedCount += "A"; } if (notAcreditedItems.Count > 0) { //Generate Request B var model = new ReportModel(); model.ReportParameters.Add("RequestNumber", AcreditationLevels.NotAcredited + diaryW.Number); model.ReportParameters.Add("TestingPeriod", testingPeriod); model.ReportParameters.Add("Date", date); model.reportItems = notAcreditedItems; var report = new RequestListReport(model); var data = report.GenerateReport(); //this is supposed to create all the necessary directories for the file. CheckAndGenerateDirectories(diary.Number); var fileProps = GetFileProperties(diary.Number, FileNames.RequestListReport, AcreditationLevels.NotAcredited); if (File.Exists(fileProps.FullPath)) { string newDestination = fileProps.FullPath.Substring(0, fileProps.FullPath.Length - 5) + "_" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".xlsx"; File.Move(fileProps.FullPath, newDestination); } var file = File.Create(fileProps.FullPath); file.Write(data, 0, data.Length); file.Close(); requestsGeneratedCount += "B"; } return requestsGeneratedCount; }