public TestsReferenceW(ArchivedProductTest test)
 {
     this.TestName = test.TestName;
     this.ProductName = test.ArchivedProduct.Name;
     this.ProductQuantity = test.ArchivedProduct.Quantity;
     this.ClientName = test.ArchivedProduct.ArchivedDiary.Client;
 }
        /// <summary>
        /// Shallow copy !
        /// </summary>
        public ArchivedProductTest ConvertToArchived(ProductTest productTest)
        {
            var archivedProductTest = new ArchivedProductTest();

            archivedProductTest.TestName = productTest.Test.Name;
            archivedProductTest.TestUnitName = productTest.Test.UnitName;
            archivedProductTest.TestMethods = productTest.TestMethod.Method;
            archivedProductTest.TestAcredetationLevel = productTest.Test.AcredetationLevel.Level;
            archivedProductTest.TestTemperature = productTest.Test.Temperature;
            archivedProductTest.TestCategory = productTest.Test.TestCategory.Name;

            return archivedProductTest;
        }
        public ArchivedProductTestW(ArchivedProductTest atest)
        {
            this.Id = atest.Id;
            this.ArchivedProductId = atest.ArchivedProductId;
            this.TestName = atest.TestName;
            this.TestUnitName = atest.TestUnitName;
            this.TestMethods = atest.TestMethods;
            this.TestAcredetationLevel = atest.TestAcredetationLevel;
            this.TestTemperature = atest.TestTemperature;
            this.TestCategory = atest.TestCategory;
            this.TestType = atest.TestType;
            this.TestTypeShortName = atest.TestTypeShortName;
            this.MethodValue = atest.MethodValue;
            this.Remark = atest.Remark;
            //this.Units = atest.Units;

            this.Results = atest.ArchivedProtocolResults.Select(ar => new ArchivedProtocolResultW(ar));
        }
        /// <summary>
        /// Shallow copy !
        /// </summary>
        public ProductTest ConvertFromArchived(ArchivedProductTest aproductTest)
        {
            var productTest = new ProductTest();

            productTest.Test = new Test();
            productTest.Test.Name = aproductTest.TestName;
            productTest.Test.UnitName = aproductTest.TestUnitName;
            productTest.TestMethod = new TestMethod() { Id = Guid.NewGuid(), Method = aproductTest.TestMethods };
            productTest.Test.AcredetationLevel = new AcredetationLevel();
            productTest.Test.AcredetationLevel.Level = aproductTest.TestAcredetationLevel;
            productTest.Test.Temperature = aproductTest.TestTemperature;
            productTest.Test.TestCategory = new TestCategory();
            productTest.Test.TestCategory.Name = aproductTest.TestCategory;
            productTest.Test.TestType = new TestType() { Id = Guid.NewGuid(), Type = aproductTest.TestType, ShortName = aproductTest.TestTypeShortName };

            productTest.Product = new Product(); //not using converter because RECURSION
            productTest.Product.Number = aproductTest.ArchivedProduct.Number;
            productTest.Product.Name = aproductTest.ArchivedProduct.Name;
            productTest.Product.Quantity = aproductTest.ArchivedProduct.Quantity;
            //and no product tests in it !

            return productTest;
        }
        public ArchivedProductTest ToBase()
        {
            var atest = new ArchivedProductTest();

            atest.Id = this.Id;
            atest.ArchivedProductId = this.ArchivedProductId;
            atest.TestName = this.TestName;
            atest.TestUnitName = this.TestUnitName;
            atest.TestMethods = this.TestMethods;
            atest.TestAcredetationLevel = this.TestAcredetationLevel;
            atest.TestTemperature = this.TestTemperature;
            atest.TestCategory = this.TestCategory;
            atest.TestType = this.TestType;
            atest.TestTypeShortName = this.TestTypeShortName;
            atest.MethodValue = this.MethodValue;
            atest.Remark = this.Remark;
            //atest.Units = this.Units;

            if (this.Results != null)
            {
                atest.ArchivedProtocolResults = this.Results.Select(ar => ar.ToBase()).ToList();
            }
            else
            {
                atest.ArchivedProtocolResults = new List<ArchivedProtocolResult>();
            }

            return atest;
        }
        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;
        }