Ejemplo n.º 1
0
        public ActionResult Edit(EditIncomingLettersViewModel editIncomingLettersViewModel)
        {
            ReceiveLetterManager receiveLetterManager = new ReceiveLetterManager();
            ReceivedLetter       oldLetter            = receiveLetterManager.Read(editIncomingLettersViewModel.ReceivedLetter.Id);

            #region temp

            TempReceiveLetterManager tempReceiveLetterManager = new TempReceiveLetterManager();
            //Get Letter before Edit
            ReceivedLetter receivedLetter = tempReceiveLetterManager.ReturnReceivedLetter(editIncomingLettersViewModel.ReceivedLetter.Id);
            if (receivedLetter == null)
            {
                throw new Exception("نامه مورد نظر یافت نگردید.");
            }
            TempReceivedLetter tempReceivedLetter = new TempReceivedLetter
            {
                User                       = receivedLetter.User,
                Sender                     = receivedLetter?.Sender ?? null,
                ParentType                 = receivedLetter.ParentType,
                AppendageContent           = receivedLetter.AppendageContent,
                LetterFileContent          = receivedLetter.LetterFileContent,
                ArchiveDateTime            = receivedLetter.ArchiveDateTime,
                LetterDateTime             = receivedLetter.LetterDateTime,
                ModifyDateTime             = receivedLetter.LastModifyDateTime,
                Assortment                 = receivedLetter.Assortment,
                Cases                      = new List <Case>(),
                RowNumber                  = receivedLetter.RowNumber,
                Description                = receivedLetter.Description,
                ReceivedLetter             = receivedLetter,
                ParentSendLetterOutTemp    = receivedLetter.ParentSendLetterOut,
                ParentReceiveLetter        = receivedLetter.ParentReceiveLetter,
                TempReceiveLetterFiles     = new List <TempReceiveLetterFile>(),
                TempReceiveAppendages      = new List <TempReceiveAppendage>(),
                LetterFileAppendageContent = receivedLetter.LetterFileAppendageContent,
                Active                     = true
            };
            //Temp Collection of Cases
            foreach (var item in receivedLetter.Cases)
            {
                tempReceivedLetter.Cases.Add(item);
            }
            //Copy files to temp
            foreach (var item in receivedLetter.ReceiveLetterFiles)
            {
                TempReceiveLetterFile newTempReceiveLetterFile = new TempReceiveLetterFile {
                    File = item.File
                };
                tempReceivedLetter.TempReceiveLetterFiles.Add(newTempReceiveLetterFile);
            }
            //Copy Appendages to temp
            foreach (var item in receivedLetter.Appendages)
            {
                TempReceiveAppendage newTempReceiveLetterFile = new TempReceiveAppendage {
                    File = item.File
                };
                tempReceivedLetter.TempReceiveAppendages.Add(newTempReceiveLetterFile);
            }

            tempReceiveLetterManager.Add(tempReceivedLetter);
            tempReceiveLetterManager.saveChanges();

            #endregion

            oldLetter.Assortment = editIncomingLettersViewModel.ReceivedLetter.Assortment;

            //oldLetter.RowNumber = editIncomingLettersViewModel.ReceivedLetter.RowNumber;
            oldLetter.Description = editIncomingLettersViewModel.ReceivedLetter.Description;

            //oldLetter.ParentReceiveLetter = editIncomingLettersViewModel.ReceivedLetter.ParentReceiveLetter;

            if (!string.IsNullOrEmpty(editIncomingLettersViewModel.LetterDateTime))
            {
                oldLetter.LetterDateTime = editIncomingLettersViewModel.LetterDateTime.toEnglishNumber().ToGeorgianDateTime();
            }

            receiveLetterManager.UpdateReceiveLetterFiles(oldLetter, editIncomingLettersViewModel.FileUploadLetters, editIncomingLettersViewModel.FilesId);
            receiveLetterManager.UpdateReceiveLetterAppendages(oldLetter, editIncomingLettersViewModel.FileUploadAppendages, editIncomingLettersViewModel.AppendagesId);

            oldLetter.LastModifyDateTime = DateTime.Now;

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

            //oldLetter.ParentReceiveLetter = editIncomingLettersViewModel.ParentReceiveletterId != null ? receiveLetterManager.Read((Guid)editIncomingLettersViewModel.ParentReceiveletterId) : null;


            if (editIncomingLettersViewModel.PersonGuid != Guid.Empty || editIncomingLettersViewModel.OrganizationGuid != Guid.Empty)
            {
                receiveLetterManager.EditSender(oldLetter, editIncomingLettersViewModel.OrganizationGuid, editIncomingLettersViewModel.PersonGuid, editIncomingLettersViewModel.PostGuid);
            }



            receiveLetterManager.EditCases(oldLetter, editIncomingLettersViewModel.CasesGuid);



            oldLetter.RowNumber  = editIncomingLettersViewModel.RowNumber;
            oldLetter.ParentType = editIncomingLettersViewModel.ReceivedLetter.ParentType;
            //Set Parent Letter
            receiveLetterManager.SetParent(oldLetter, editIncomingLettersViewModel.ParentletterId);



            receiveLetterManager.GeneratePDF(oldLetter);
            receiveLetterManager.Update(oldLetter);

            receiveLetterManager.saveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public ActionResult Add(AddIncomingLettersViewModel incomingLettersViewModel)
        {
            ReceiveLetterManager receiveLetterManager = new ReceiveLetterManager();

            //Add letter files
            if (incomingLettersViewModel.FileUploadLetters != null && incomingLettersViewModel.FileUploadLetters.Count > 0)
            {
                incomingLettersViewModel.receivedLetter.ReceiveLetterFiles = new List <ReceiveLetterFile>();

                foreach (var item in incomingLettersViewModel.FileUploadLetters)
                {
                    if (item == null || item.ContentLength <= 0)
                    {
                        continue;
                    }
                    var target = new MemoryStream();
                    ReceiveLetterFile receiveLetterFile = new ReceiveLetterFile();
                    item.InputStream.CopyTo(target);
                    receiveLetterFile.File = new ArchiveFile
                    {
                        Content     = target.ToArray(),
                        Name        = item.FileName,
                        ContentType = item.ContentType
                    };
                    incomingLettersViewModel.receivedLetter.ReceiveLetterFiles.Add(receiveLetterFile);
                }
            }
            //Add letter appendages
            if (incomingLettersViewModel.FileUploadAppendages != null && incomingLettersViewModel.FileUploadAppendages.Count > 0)
            {
                incomingLettersViewModel.receivedLetter.Appendages = new List <ReceiveAppendage>();
                foreach (var item in incomingLettersViewModel.FileUploadAppendages)
                {
                    if (item != null && item.ContentLength > 0)
                    {
                        var target = new MemoryStream();
                        ReceiveAppendage receiveAppendage = new ReceiveAppendage();
                        item.InputStream.CopyTo(target);
                        receiveAppendage.File = new ArchiveFile
                        {
                            Content     = target.ToArray(),
                            Name        = item.FileName,
                            ContentType = item.ContentType
                        };
                        incomingLettersViewModel.receivedLetter.Appendages.Add(receiveAppendage);
                    }
                }
            }
            //Date Times
            if (!string.IsNullOrEmpty(incomingLettersViewModel.LetterDateTime))
            {
                incomingLettersViewModel.receivedLetter.LetterDateTime =
                    incomingLettersViewModel.LetterDateTime.ToGeorgianDateTime();
            }
            incomingLettersViewModel.receivedLetter.ArchiveDateTime    = DateTime.Now;
            incomingLettersViewModel.receivedLetter.LastModifyDateTime = DateTime.Now;
            //Row number and user
            incomingLettersViewModel.receivedLetter.RowNumber = incomingLettersViewModel.RowNumber;
            receiveLetterManager.AddUser(incomingLettersViewModel.receivedLetter, new Guid(User.Identity.Name));

            //Set Parent Letter
            receiveLetterManager.SetParent(incomingLettersViewModel.receivedLetter, incomingLettersViewModel.ParentletterId);


            //Add organization , post and person
            if (incomingLettersViewModel.PersonGuid != Guid.Empty || incomingLettersViewModel.OrganizationGuid != Guid.Empty)
            {
                receiveLetterManager.AddSender(incomingLettersViewModel.receivedLetter, incomingLettersViewModel.OrganizationGuid, incomingLettersViewModel.PersonGuid, incomingLettersViewModel.PostGuid);
            }


            //Add cases
            if (incomingLettersViewModel.SelectedCasesGuid != null && incomingLettersViewModel.SelectedCasesGuid.Count > 0)
            {
                receiveLetterManager.AddCases(incomingLettersViewModel.receivedLetter, incomingLettersViewModel.SelectedCasesGuid);
            }
            //Add 3 pdf to db
            receiveLetterManager.GeneratePDF(incomingLettersViewModel.receivedLetter);

            //Add letter to db and save changes
            receiveLetterManager.Add(incomingLettersViewModel.receivedLetter);
            receiveLetterManager.saveChanges();

            return(RedirectToAction("Index"));
        }