bool createNoteWithAttachment(PurchaseOrderHeaderModel poh, CompanyModel company,
                                      string fileName, ref NoteModel note, ref string errorMsg)
        {
            bool bError = false;

            note = new NoteModel {
                CompanyId   = company.Id,
                NoteType    = NoteType.Purchase,
                ParentId    = poh.Id,
                DateCreated = DateTimeOffset.Now,
                CreatedById = user.Id,
                Subject     = "Unpack Slip - Order " + poh.OrderNumber.ToString(),
                Message     = ""
            };

            var error = NoteService.InsertOrUpdateNote(note, user, "");

            if (error.IsError)
            {
                errorMsg = error.Message;
                bError   = true;
            }
            else
            {
                // Move the file to the purchase order note attachment folder for the
                // specific purchase order
                error = NoteService.AttachMediaItemToNote(note,
                                                          user,
                                                          fileName,
                                                          fileName.FileName(),
                                                          FileCopyType.Move);
                if (error.IsError)
                {
                    errorMsg = error.Message;
                    bError   = true;
                }
            }

            return(bError);
        }
        public void GetMediaFileNameTest()
        {
            var testUser     = GetTestUser();
            var testCompany  = GetTestCompany(testUser);
            var testCustomer = GetTestCustomer(testCompany, testUser);

            // Create a note
            NoteService.NoteService noteService = new NoteService.NoteService(db);

            var note = new NoteModel {
                CompanyId   = testCompany.Id,
                ParentId    = testCustomer.Id,
                NoteType    = NoteType.Customer,
                CreatedById = testUser.Id,
                Subject     = RandomString(),
                Message     = LorumIpsum()
            };
            var error = noteService.InsertOrUpdateNote(note, testUser, "");

            Assert.IsTrue(!error.IsError, error.Message);

            // Add a reference attachment to it
            var tempFile1 = GetTempFile(".doc");

            using (var sw = new StreamWriter(tempFile1)) {
                sw.WriteLine(LorumIpsum());
            }
            error = NoteService.AttachMediaItemToNote(note, testUser, tempFile1, tempFile1.FileName(), FileCopyType.Move);
            Assert.IsTrue(!error.IsError, error.Message);

            // Add a second attachment - an actual file
            var tempFile2 = GetTempFile(".doc");

            using (var sw = new StreamWriter(tempFile2)) {
                sw.WriteLine(LorumIpsum());
            }
            error = NoteService.AttachMediaItemToNote(note, testUser, tempFile2, tempFile2.FileName(), FileCopyType.Move);
            Assert.IsTrue(!error.IsError, error.Message);

            // Get the note attachments and check the names
            var attachments = NoteService.FindNoteAttachmentsModel(note, MediaSize.Medium, 0, 0);

            int expected = 2,
                actual   = attachments.Count();

            Assert.IsTrue(actual == expected, $"Error: {actual} attachment(s) were returned when {expected} were expected");

            var attachmentFile = MediaServices.GetMediaFileName(attachments[0].Media, false);

            Assert.IsTrue(attachmentFile.CountOf("/") == 0, "Error: The file name returned contained / characters when none were expected");
            Assert.IsTrue(attachmentFile.CountOf("\\") > 0, "Error: The file name returned contained no \\ characters when some were expected");

            var mediaFolder = GetAppSetting("MediaFolder", "");

            attachmentFile = attachmentFile.Substring(mediaFolder.Length + 1);

            // 936\\Customer\\0\\378\\tmpC3E3.doc"
            string[] parts = attachmentFile.Split('\\');
            Assert.IsTrue(parts[0] == testCompany.Id.ToString(), "Error: Part 0 is '{parts[0]}' when '{testCompanyId}' was expected");
            Assert.IsTrue(parts[1] == Enumerations.MediaFolder.Customer.ToString(), $"Error: Part 1 is '{parts[1]}' when 'Customers' was expected");
            Assert.IsTrue(parts[2] == testCustomer.Id.ToString(), $"Error: Part 2 is '{parts[2]}' when '{testCustomer.Id}' was expected");
            Assert.IsTrue(parts[3] == attachments[0].NoteId.ToString(), $"Error: Part 3 is '{parts[3]}' when '{attachments[0].NoteId}' was expected");
            Assert.IsTrue(parts[4] == attachmentFile.FileName(), $"Error: Part 4 is '{parts[4]}' when '{attachmentFile.FileName()}' was expected");

            // Check the second note attachment
            attachmentFile = MediaServices.GetMediaFileName(attachments[1].Media, false);
            Assert.IsTrue(attachmentFile.CountOf("/") == 0, "Error: The file name returned contained / characters when none were expected");
            Assert.IsTrue(attachmentFile.CountOf("\\") > 0, "Error: The file name returned contained no \\ characters when some were expected");
            Assert.IsTrue(File.Exists(attachmentFile), $"Error: Attachment file '{attachmentFile}' could not be found");

            attachmentFile = attachmentFile.Substring(mediaFolder.Length + 1);

            // 936\\Customer\\0\\378\\tmpC3E3.doc"
            parts = attachmentFile.Split('\\');
            Assert.IsTrue(parts[0] == testCompany.Id.ToString(), "Error: Part 0 is '{parts[0]}' when '{testCompanyId}' was expected");
            Assert.IsTrue(parts[1] == Enumerations.MediaFolder.Customer.ToString(), $"Error: Part 1 is '{parts[1]}' when 'Customers' was expected");
            Assert.IsTrue(parts[2] == testCustomer.Id.ToString(), $"Error: Part 2 is '{parts[2]}' when '{testCustomer.Id}' was expected");
            Assert.IsTrue(parts[3] == attachments[0].NoteId.ToString(), $"Error: Part 3 is '{parts[3]}' when '{attachments[0].NoteId}' was expected");
            Assert.IsTrue(parts[4] == attachmentFile.FileName(), $"Error: Part 4 is '{parts[4]}' when '{attachmentFile.FileName()}' was expected");
        }