public ActionResult Edit(EditOutgoingLettersViewModel editOutgoingLettersViewModel)
        {
            SendLetterManager sendLetterManager = new SendLetterManager();
            SendLetter        oldLetter         = sendLetterManager.Read(editOutgoingLettersViewModel.sendLetter.Id);

            #region temp

            TempSendLetterManager tempSendLetterManager = new TempSendLetterManager();
            //Get Letter before Edit
            SendLetter sendletter = tempSendLetterManager.ReturnReceivedLetter(editOutgoingLettersViewModel.sendLetter.Id);
            if (sendletter == null)
            {
                throw new Exception("نامه مورد نظر یافت نگردید.");
            }
            TempSendLetter tempSendLetter = new TempSendLetter
            {
                User                        = sendletter.User,
                Recievers                   = sendletter?.Recievers ?? null,
                ParentType                  = sendletter.ParentType,
                AppendageContent            = sendletter.AppendageContent,
                LetterFileContent           = sendletter.LetterFileContent,
                ArchiveDateTime             = sendletter.ArchiveDateTime,
                LetterDateTime              = sendletter.LetterDateTime,
                ModifyDateTime              = sendletter.LastModifyDateTime,
                Assortment                  = sendletter.Assortment,
                Cases                       = sendletter.Cases,
                RowNumber                   = sendletter.RowNumber,
                Description                 = sendletter.Description,
                SendLetter                  = sendletter,
                ParentReceivedLetterOutTemp = sendletter.ParentReceivedLetterOut,
                ParentSendLetter            = sendletter.ParentSendLetter,
                TempSendLetterFiles         = new List <TempSendLetterFile>(),
                Appendages                  = new List <TempSendAppendage>(),
                LetterFileAppendageContent  = sendletter.LetterFileAppendageContent,
                Active                      = true,
                SenderEmployee              = sendletter.SenderEmployee
            };
            //Temp Collection of Cases
            //foreach (var item in sendletter.Cases)
            //{
            //    tempSendLetter.Cases.Add(item);
            //}
            //Copy files to temp
            foreach (var item in sendletter.sendLetterFiles)
            {
                TempSendLetterFile tempSendLetterFile = new TempSendLetterFile {
                    File = item.File
                };
                tempSendLetter.TempSendLetterFiles.Add(tempSendLetterFile);
            }
            //Copy Appendages to temp
            foreach (var item in sendletter.Appendages)
            {
                TempSendAppendage tempSendAppendage = new TempSendAppendage {
                    File = item.File
                };
                tempSendLetter.Appendages.Add(tempSendAppendage);
            }
            //Copy Recievers
            //foreach (var item in sendletter.Recievers)
            //{
            //    Reciever r = new Reciever();
            //    r.Id = item.Id;
            //    r.Organization = item.Organization;
            //    r.Person = item.Person;
            //    r.Post = item.Post;
            //}

            tempSendLetterManager.Add(tempSendLetter);
            tempSendLetterManager.saveChanges();

            #endregion
            //Set assortments
            oldLetter.Assortment = editOutgoingLettersViewModel.sendLetter.Assortment;
            //Set Description
            oldLetter.Description = editOutgoingLettersViewModel.sendLetter.Description;
            //Set letter content
            oldLetter.Body = editOutgoingLettersViewModel.sendLetter.Body;
            //Set Letter DateTime
            if (!string.IsNullOrEmpty(editOutgoingLettersViewModel.LetterDateTime))
            {
                oldLetter.LetterDateTime = editOutgoingLettersViewModel.LetterDateTime.toEnglishNumber().ToGeorgianDateTime();
            }

            oldLetter.LastModifyDateTime = DateTime.Now;

            if (oldLetter.User.Id != new Guid(User.Identity.Name))
            {
                sendLetterManager.AddUser(oldLetter, new Guid(User.Identity.Name));
            }

            //upload files
            sendLetterManager.UpdateSendLetterFiles(oldLetter, editOutgoingLettersViewModel.fileUploadLetters, editOutgoingLettersViewModel.FilesID);
            sendLetterManager.UpdateSendLetterAppendages(oldLetter, editOutgoingLettersViewModel.fileUploadAppendages, editOutgoingLettersViewModel.AppendagesID);


            oldLetter.ParentType = editOutgoingLettersViewModel.sendLetter.ParentType;
            //Set Parent Letter
            sendLetterManager.AddParent(oldLetter, editOutgoingLettersViewModel.ParentletterId);
            //update organization , post and person
            if (editOutgoingLettersViewModel.PersonGuid != Guid.Empty || editOutgoingLettersViewModel.OrganizationGuid != Guid.Empty)
            {
                sendLetterManager.EditReciever(oldLetter, editOutgoingLettersViewModel.OrganizationGuid, editOutgoingLettersViewModel.PersonGuid, editOutgoingLettersViewModel.PostGuid);
            }
            //update department , post and employee
            if (editOutgoingLettersViewModel.EmployeeGuid != Guid.Empty || editOutgoingLettersViewModel.DepartmentGuid != Guid.Empty)
            {
                sendLetterManager.EditSenderEmployee(oldLetter, editOutgoingLettersViewModel.DepartmentGuid, editOutgoingLettersViewModel.EmployeeGuid, editOutgoingLettersViewModel.DepartmentPostGuid);
            }

            //set rowNumber Logic
            oldLetter.RowNumber = editOutgoingLettersViewModel.autoRowNumber ? sendLetterManager.AutoRowNumber(oldLetter.SenderEmployee?.Department) : editOutgoingLettersViewModel.RowNumber;

            //Add cases
            sendLetterManager.EditCases(oldLetter, editOutgoingLettersViewModel.SelectedCasesGuid);


            //Start Processing Files and Appendages to Save them as pdf
            sendLetterManager.GeneratePDF(oldLetter, editOutgoingLettersViewModel.LetterTemplate);
            sendLetterManager.Update(oldLetter);
            sendLetterManager.saveChanges();
            return(RedirectToAction("Index"));
        }
        //[ValidateAntiForgeryToken]
        public ActionResult Add(AddOutgoingViewModel outgoingLettersViewModel)
        {
            SendLetterManager sendLetterManager = new SendLetterManager();

            //Add letter files
            if (outgoingLettersViewModel.fileUploadLetters != null && outgoingLettersViewModel.fileUploadLetters.Count > 0)
            {
                outgoingLettersViewModel.sendLetter.sendLetterFiles = new List <SendLetterFile>(); //

                foreach (var item in outgoingLettersViewModel.fileUploadLetters)
                {
                    if (item == null || item.ContentLength <= 0)
                    {
                        continue;
                    }
                    var            target         = new MemoryStream();
                    SendLetterFile sendLetterFile = new SendLetterFile();
                    item.InputStream.CopyTo(target);
                    sendLetterFile.File = new ArchiveFile
                    {
                        Content     = target.ToArray(),
                        Name        = item.FileName,
                        ContentType = item.ContentType
                    };
                    outgoingLettersViewModel.sendLetter.sendLetterFiles.Add(sendLetterFile);
                }
            }

            //Add letter appendages
            if (outgoingLettersViewModel.fileUploadAppendages != null && outgoingLettersViewModel.fileUploadAppendages.Count > 0)
            {
                outgoingLettersViewModel.sendLetter.Appendages = new List <SendAppendage>();
                foreach (var item in outgoingLettersViewModel.fileUploadAppendages)
                {
                    if (item == null || item.ContentLength <= 0)
                    {
                        continue;
                    }
                    var target        = new MemoryStream();
                    var sendAppendage = new SendAppendage();
                    item.InputStream.CopyTo(target);
                    sendAppendage.File = new ArchiveFile
                    {
                        Content     = target.ToArray(),
                        Name        = item.FileName,
                        ContentType = item.ContentType
                    };
                    outgoingLettersViewModel.sendLetter.Appendages.Add(sendAppendage);
                }
            }

            //Date Times
            if (!string.IsNullOrEmpty(outgoingLettersViewModel.LetterDateTime))
            {
                outgoingLettersViewModel.sendLetter.LetterDateTime =
                    outgoingLettersViewModel.LetterDateTime.ToGeorgianDateTime();
            }
            outgoingLettersViewModel.sendLetter.ArchiveDateTime    = DateTime.Now;
            outgoingLettersViewModel.sendLetter.LastModifyDateTime = DateTime.Now;
            //outgoingLettersViewModel.sendLetter.User = Sessions.CurrentUser;
            sendLetterManager.AddUser(outgoingLettersViewModel.sendLetter, new Guid(User.Identity.Name));


            //Set Parent Letter
            sendLetterManager.AddParent(outgoingLettersViewModel.sendLetter, outgoingLettersViewModel.ParentletterId);
            //Add organization , post and person
            if (outgoingLettersViewModel.PersonGuid != Guid.Empty || outgoingLettersViewModel.OrganizationGuid != Guid.Empty)
            {
                sendLetterManager.AddReciever(outgoingLettersViewModel.sendLetter, outgoingLettersViewModel.OrganizationGuid, outgoingLettersViewModel.PersonGuid, outgoingLettersViewModel.PostGuid);
            }
            //Add department , post and employee
            if (outgoingLettersViewModel.EmployeeGuid != Guid.Empty || outgoingLettersViewModel.DepartmentGuid != Guid.Empty)
            {
                sendLetterManager.AddSenderEmployee(outgoingLettersViewModel.sendLetter, outgoingLettersViewModel.DepartmentGuid, outgoingLettersViewModel.EmployeeGuid, outgoingLettersViewModel.DepartmentPostGuid);
            }

            //set rowNumber Logic
            outgoingLettersViewModel.sendLetter.RowNumber = outgoingLettersViewModel.autoRowNumber ? sendLetterManager.AutoRowNumber(outgoingLettersViewModel.sendLetter.SenderEmployee?.Department) : outgoingLettersViewModel.RowNumber;

            //Add cases
            if (outgoingLettersViewModel.SelectedCasesGuid != null && outgoingLettersViewModel.SelectedCasesGuid.Count > 0)
            {
                sendLetterManager.AddCases(outgoingLettersViewModel.sendLetter, outgoingLettersViewModel.SelectedCasesGuid);
            }


            //Start Processing Files and Appendages to Save them as pdf
            sendLetterManager.GeneratePDF(outgoingLettersViewModel.sendLetter, outgoingLettersViewModel.LetterTemplate);
            sendLetterManager.Add(outgoingLettersViewModel.sendLetter);
            sendLetterManager.saveChanges();



            return(RedirectToAction("Index"));
        }