public ArchivedProtocol(ArchivedDiary adiary)
 {
     this.ArchivedDiary = adiary;
     this.DiaryId = adiary.Id;
     this.IssuedDate = adiary.ProtocolIssuedDate;
     this.ProtocolResults = adiary.ArchivedProtocolResults;
     this.TesterMKB = adiary.ProtocolTesterMKB;
     this.TesterFZH = adiary.ProtocolTesterFZH;
     this.LabLeader = adiary.ProtocolLabLeader;
 }
        //public ICollection<Protocol> Protocols { get; set; }
        public ArchivedRequest(ArchivedDiary adiary)
        {
            this.DiaryId = adiary.Id;
            this.Date = adiary.RequestDate;
            this.AcceptedBy = adiary.RequestAcceptedBy;
            this.IsAccepted = true;
            this.TestingPeriod = adiary.RequestTestingPeriod ?? 0;

            this.Diary = adiary;
        }
        public ArchivedDiaryW(ArchivedDiary diary)
        {
            this.Id = diary.Id;
            this.Number = diary.Number;
            this.LetterNumber = diary.LetterNumber;
            this.LetterDate = diary.LetterDate.ToLocalTime();
            this.AcceptanceDateAndTime = diary.AcceptanceDateAndTime.ToLocalTime();
            this.Contractor = diary.Contractor;
            this.Client = diary.Client;
            this.ClientMobile = diary.ClientMobile;
            this.Comment = diary.Comment;
            this.RequestDate = diary.RequestDate.ToLocalTime();
            this.RequestAcceptedBy = diary.RequestAcceptedBy;
            this.ProtocolIssuedDate = diary.ProtocolIssuedDate.ToLocalTime();
            this.ProtocolTesterMKB = diary.ProtocolTesterMKB;
            this.ProtocolTesterFZH = diary.ProtocolTesterFZH;
            this.ProtocolLabLeader = diary.ProtocolLabLeader;
            this.Remark = diary.Remark;
            this.RequestTestingPeriod = diary.RequestTestingPeriod;

            this.ArchivedProducts = diary.ArchivedProducts;
            this.ArchivedProtocolResults = diary.ArchivedProtocolResults;
            this.ArchivedProtocolRemarks = diary.ArchivedProtocolRemarks;
        }
        public ArchivedDiary ToBase()
        {
            ArchivedDiary diary = new ArchivedDiary();

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

            diary.ArchivedProducts = this.ArchivedProducts;
            diary.ArchivedProtocolResults = this.ArchivedProtocolResults;
            diary.ArchivedProtocolRemarks = this.ArchivedProtocolRemarks;

            return diary;
        }
        public ActionResponse ArchiveDiary(Guid diaryId)
        {
            var res = new ActionResponse();

            try
            {
                var diary = db.Diaries.Single(d => d.Id == diaryId);

                ArchivedDiary archivedDiary = new ArchivedDiary();

                archivedDiary.Id = Guid.NewGuid();
                archivedDiary.Number = diary.Number;
                archivedDiary.AcceptanceDateAndTime = diary.AcceptanceDateAndTime;
                archivedDiary.LetterNumber = diary.LetterNumber != null ? diary.LetterNumber.ToString() : "";
                archivedDiary.LetterDate = diary.LetterDate;
                archivedDiary.Contractor = diary.Contractor;
                archivedDiary.Client = diary.Client.Name;
                archivedDiary.ClientMobile = diary.Client.Mobile;
                archivedDiary.Comment = diary.Comment;
                var request = diary.Requests.First();
                archivedDiary.RequestDate = request.Date;
                archivedDiary.RequestAcceptedBy = request.User.FirstName.Substring(0,1) + ". " + request.User.LastName;
                archivedDiary.RequestTestingPeriod = request.TestingPeriod;
                archivedDiary.Remark = new DiaryW(diary).Remark;
                var protocol = request.Protocols.First();
                archivedDiary.ProtocolIssuedDate = protocol.IssuedDate;
                archivedDiary.ProtocolTesterMKB = protocol.TesterMKB;
                archivedDiary.ProtocolTesterFZH = protocol.TesterFZH;
                archivedDiary.ProtocolLabLeader = protocol.LabLeader;

                foreach (var remark in protocol.ProtocolsRemarks)
                {
                    ArchivedProtocolRemark aremark = new ArchivedProtocolRemark();
                    aremark.Id = Guid.NewGuid();
                    aremark.ArchivedDiaryId = archivedDiary.Id;
                    aremark.Remark = remark.Remark.Text;
                    aremark.AcredetationLevel = remark.AcredetationLevel.Level;
                    aremark.Number = remark.Number;

                    db.ArchivedProtocolRemarks.Add(aremark);
                }
                db.ProtocolsRemarks.RemoveRange(protocol.ProtocolsRemarks);

                var products = diary.Products;
                foreach (var product in products)
                {
                    ArchivedProduct aproduct = new ArchivedProduct();

                    aproduct.Id = Guid.NewGuid();
                    aproduct.Name = product.Name;
                    aproduct.Number = product.Number;
                    aproduct.Quantity = product.Quantity;

                    var productTests = product.ProductTests;
                    foreach (var ptest in productTests)
                    {
                        ArchivedProductTest aptest = new ArchivedProductTest();

                        aptest.Id = Guid.NewGuid();
                        aptest.TestName = ptest.Test.Name;
                        aptest.TestUnitName = ptest.Test.UnitName;
                        aptest.TestMethods = ptest.TestMethod.Method;
                        aptest.TestAcredetationLevel = ptest.Test.AcredetationLevel.Level;
                        aptest.TestTemperature = ptest.Test.Temperature;
                        aptest.TestCategory = ptest.Test.TestCategory.Name;
                        aptest.TestType = ptest.Test.TestType.Type;
                        aptest.TestTypeShortName = ptest.Test.TestType.ShortName;
                        aptest.MethodValue = ptest.MethodValue;
                        aptest.Remark = ptest.Remark;

                        //aptest.Units = ptest.Units;

                        var protocolResults = ptest.ProtocolResults;
                        foreach (var presult in protocolResults)
                        {
                            ArchivedProtocolResult apresult = new ArchivedProtocolResult();

                            apresult.Id = Guid.NewGuid();
                            apresult.Results = presult.Results;
                            //apresult.MethodValue = presult.MethodValue;
                            apresult.ResultNumber = presult.ResultNumber;
                            apresult.ArchivedDiaryId = archivedDiary.Id;

                            aptest.ArchivedProtocolResults.Add(apresult);
                        }

                        db.ProtocolResults.RemoveRange(ptest.ProtocolResults);
                        aproduct.ArchivedProductTests.Add(aptest);
                    }

                    db.ProductTests.RemoveRange(product.ProductTests);
                    archivedDiary.ArchivedProducts.Add(aproduct);
                }

                db.Products.RemoveRange(diary.Products);
                db.ArchivedDiaries.Add(archivedDiary);

                db.Protocols.RemoveRange(request.Protocols);
                db.Requests.Remove(request);

                db.Diaries.Remove(diary);

                db.SaveChanges();

                res.IsSuccess = true;
                res.ResponseObject = archivedDiary.Id;
                res.SuccessMsg = "Архивирането на дневник: " + diary.Number + " премина успешно.";
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
            catch (Exception exc)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(exc);
                res.Error = ErrorFactory.UnableToArchiveDiary;
            }

            return res;
        }