public void Edit(ArchivedDiaryW adiary)
        {
            var archivedDiary = db.ArchivedDiaries.Single(ad => ad.Id == adiary.Id);

            archivedDiary.Number = adiary.Number;
            archivedDiary.LetterNumber = adiary.LetterNumber;
            archivedDiary.LetterDate = adiary.LetterDate.ToUniversalTime();
            archivedDiary.AcceptanceDateAndTime = adiary.AcceptanceDateAndTime.ToUniversalTime();
            archivedDiary.Contractor = adiary.Contractor;
            archivedDiary.Client = adiary.Client;
            archivedDiary.ClientMobile = adiary.ClientMobile;
            archivedDiary.Comment = adiary.Comment;
            archivedDiary.RequestDate = adiary.RequestDate.ToUniversalTime();
            archivedDiary.RequestAcceptedBy = adiary.RequestAcceptedBy;
            archivedDiary.ProtocolIssuedDate = adiary.ProtocolIssuedDate.ToUniversalTime();
            archivedDiary.ProtocolTesterMKB = adiary.ProtocolTesterMKB;
            archivedDiary.ProtocolTesterFZH = adiary.ProtocolTesterFZH;
            archivedDiary.ProtocolLabLeader = adiary.ProtocolLabLeader;
            archivedDiary.Remark = adiary.Remark;
            archivedDiary.RequestTestingPeriod = adiary.RequestTestingPeriod;

            db.SaveChanges();
        }
        //Archived shit
        public void RegenerateProtocolReport(ArchivedDiaryW adiary)
        {
            var acreditedProducts = adiary.ArchivedProducts.Where(p => p.ArchivedProductTests.Any(apt => apt.TestAcredetationLevel.Trim() == AcreditationLevels.Acredited));
            var notAcreditedProducts = adiary.ArchivedProducts.Where(p => p.ArchivedProductTests.Any(apt => apt.TestAcredetationLevel.Trim() == AcreditationLevels.NotAcredited));

            if (acreditedProducts.Count() > 0)
            {
                RewriteProtocolReport(adiary, "A", acreditedProducts);
            }
            if (notAcreditedProducts.Count() > 0)
            {
                RewriteProtocolReport(adiary, "B", notAcreditedProducts);
            }
        }
        private void RewriteProtocolReport(ArchivedDiaryW adiary, string category, IEnumerable<ArchivedProduct> products)
        {
            var model = new ReportModel();

            model.ReportParameters.Add("AcredetationLevel", category);
            model.ReportParameters.Add("ProtocolNumber", category + adiary.Number);
            model.ReportParameters.Add("ProtocolIssuedDate", adiary.ProtocolIssuedDate);

            var convertedProducts = new List<Product>();
            var productConverter = new ProductConverter();
            foreach (var pr in products)
            {
                var product = productConverter.ConvertFromArchived(pr);
                convertedProducts.Add(product);
            }

            model.ReportParameters.Add("Products", convertedProducts);
            //var methods = convertedProducts.SelectMany(p => p.ProductTests.Where(pt => pt.Test.AcredetationLevel.Level.Trim() == category).Select(pt => pt.Test.TestMethods)).Distinct();
            var methods = convertedProducts.SelectMany(p => p.ProductTests.Where(pt => pt.Test.AcredetationLevel.Level.Trim() == category)
                            .Select(pt => new MethodsModel() { TestName = pt.Test.Name, TestMethod = pt.TestMethod.Method })).ToList().Distinct();
            model.ReportParameters.Add("Methods", methods);
            var quantities = convertedProducts.OrderBy(p => p.Number).Select(p => p.Quantity);
            model.ReportParameters.Add("Quantities", quantities);

            var protocolResultsConverter = new ProtocolResultsConverter();
            var theProtocolResults = adiary.ArchivedProtocolResults.Select(apr => protocolResultsConverter.ConvertFromArchived(apr));

            var protocolResults = theProtocolResults.Where(pr => pr.ProductTest.Test.AcredetationLevel.Level.Trim() == category)
                .OrderBy(x => x.ProductTest.Product.Number).ThenBy(x => x.ProductTest.Test.Name).ThenBy(x => x.ResultNumber);

            model.ReportParameters.Add("ProtocolResults", protocolResults);

            model.ReportParameters.Add("Contractor", adiary.Contractor);
            model.ReportParameters.Add("Client", adiary.Client);
            model.ReportParameters.Add("LetterNumber", adiary.LetterNumber);
            model.ReportParameters.Add("LetterDate", adiary.LetterDate);
            model.ReportParameters.Add("RequestDate", adiary.RequestDate.ToLocalTime());
            model.ReportParameters.Add("LabLeader", adiary.ProtocolLabLeader);
            model.ReportParameters.Add("TesterMKB", adiary.ProtocolTesterMKB);
            model.ReportParameters.Add("TesterFZH", adiary.ProtocolTesterFZH);

            var remarksConverter = new RemarksConverter();
            var remarks = adiary.ArchivedProtocolRemarks.Where(r => r.AcredetationLevel.Trim() == category).
                Select(r => new ProtocolsRemark()
                {
                    Remark = remarksConverter.ConvertFromArchived(r),
                    Number = r.Number
                });

            model.ReportParameters.Add("Remarks", remarks);

            if (category == "A")
            {
                string acredetationString = @"АКРЕДИТИРАНА СЪГЛАСНО БДС EN ISO/IEC 17025:2006
            СЕРТИФИКАТ №55 ЛИ ОТ 08.04.2015 г./ ИА „БСА”
            С ВАЛИДНОСТ НА АКРЕДИТАЦИЯТА ДО 31.03.2016 г.
            ";
                model.ReportParameters.Add("AcredetationString", acredetationString);
            }

            var report = new ProtocolReport(model);
            var data = report.GenerateReport();

            CheckAndGenerateDirectories(adiary.Number);

            var fileProps = GetFileProperties(adiary.Number, FileNames.Protocol, category);

            if (File.Exists(fileProps.FullPath))
            {
                string newDestination = fileProps.FullPath.Substring(0, fileProps.FullPath.Length - 5) + "_" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".docx";
                File.Move(fileProps.FullPath, newDestination);
            }

            var file = File.Create(fileProps.FullPath);
            file.Write(data, 0, data.Length);
            file.Close();
        }
        public ActionResponse RegenerateArchivedProtocol(ArchivedDiaryW adiary)
        {
            var response = new ActionResponse();

            try
            {
                var filesRep = new FilesRepository();
                filesRep.RegenerateProtocolReport(adiary);

                response.IsSuccess = true;
                response.SuccessMsg = "Успешно опресняване на архивирания протокол!";
            }
            catch (Exception exc)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(exc);
                response.Error = ErrorFactory.UnableToRefreshArchivedProtocol;
            }

            return response;
        }