public void AddLetter(DiaryW diary)
        {
            diary.Id = Guid.NewGuid();

            int activeMax = 0;
            if (db.Diaries.Any())
            {
                activeMax = db.Diaries.Max(d => d.Number);
            }

            int archivedMax = 0;
            if (db.ArchivedDiaries.Any())
            {
                archivedMax = db.ArchivedDiaries.Max(ad => ad.Number);
            }

            diary.Number = activeMax > archivedMax ? activeMax + 1 : archivedMax + 1;

            diary.AcceptanceDateAndTime = DateTime.UtcNow;

            int i = 1;
            foreach (var item in diary.Products)
            {
                item.Id = Guid.NewGuid();
                item.Number = i;
                item.DiaryId = diary.Id;
                //item.Test = db.Tests.FirstOrDefault(x => x.Id == item.TestId);
                i++;

                foreach (var pt in item.ProductTests)
                {
                    pt.Id = Guid.NewGuid();
                    pt.ProductId = item.Id;
                }
            }

            db.Diaries.Add(diary.ToBase());
            db.SaveChanges();
        }
        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;
        }
        public void Edit(DiaryW diaryW)
        {
            var diary = db.Diaries.Single(c => c.Id == diaryW.Id);
            diary.LetterNumber = diaryW.LetterNumber;
            diary.LetterDate = diaryW.LetterDate;
            diary.Contractor = diaryW.Contractor;
            diary.ClientId = diaryW.ClientId;

            //diary.Products.Clear();
            foreach (var item in diary.Products)
            {
                var pts = db.ProductTests.Where(pt => pt.ProductId == item.Id);
                db.ProductTests.RemoveRange(pts);
            }
            db.Products.RemoveRange(diary.Products);

            int i = 1;
            foreach (var item in diaryW.Products)
            {
                item.Id = Guid.NewGuid();
                item.DiaryId = diary.Id;
                item.Number = i;
                //item.Test = db.Tests.FirstOrDefault(x => x.Id == item.TestId);
                i++;

                foreach (var test in item.ProductTests)
                {
                    test.Id = Guid.NewGuid();
                    test.ProductId = item.Id;
                }

                diary.Products.Add(item);
            }

            db.SaveChanges();

            var request = diary.Requests.FirstOrDefault();
            if (request != null)
            {
                var filesRepository = new FilesRepository();
                string charGenerated = filesRepository.GenerateRequestListReport(diary.Id, request.Date, request.TestingPeriod ?? 0);
            }
        }