private Error createRevisedPurchaseOrder(int pohId, CompanyModel company, UserModel sender, string subject) { var error = new Error(); var poh = db.FindPurchaseOrderHeader(pohId); string pdfFile = ""; error = CreatePurchaseOrderPdf(poh, company.POSupplierTemplateId, //DocumentTemplateType.PurchaseOrder, null, ref pdfFile); if (!error.IsError) { NoteService.NoteService noteService = new NoteService.NoteService(db); error = noteService.AttachNoteToPurchaseOrder(poh, sender, subject, "", pdfFile.ToStringList(), FileCopyType.Copy); } return(error); }
public Error SendPurchaseOrderToSupplier(int purchaseOrderHeaderTempId, UserModel sender, string pdfFile) { var error = new Error(); if (string.IsNullOrEmpty(sender.EMail)) { error.SetError(EvolutionResources.errInvalidSenderEMailAddress, "", (string.IsNullOrEmpty(sender.EMail) ? "" : sender.EMail), sender.FullName); } else { var poht = db.FindPurchaseOrderHeaderTemp(purchaseOrderHeaderTempId); if (poht == null) { error.SetError(EvolutionResources.errRecordError, "", "PurchaseOrderHeaderTemp", purchaseOrderHeaderTempId.ToString()); } else { var purchaseOrderHeaderId = (poht.OriginalRowId == null ? 0 : poht.OriginalRowId.Value); var poh = db.FindPurchaseOrderHeader(poht.OriginalRowId.Value); if (poh == null) { error.SetError(EvolutionResources.errRecordError, "", "PurchaseOrderHeader", purchaseOrderHeaderId.ToString()); } else if (poh.Supplier == null) { error.SetError(EvolutionResources.errCantSendOrderPurchaseOrderHasNoSupplier); } else if (string.IsNullOrEmpty(poh.Supplier.Email)) { error.SetError(EvolutionResources.errCantSendOrderSupplierHasNoEMail, "", poh.Supplier.Name); } else { // Attach the PDF to a note against the Purchase Order NoteService.NoteService noteService = new NoteService.NoteService(db); error = noteService.AttachNoteToPurchaseOrder(poh, sender, "Purchase Order Sent to Supplier", "", pdfFile.ToStringList(), FileCopyType.Copy); if (!error.IsError) { poh.DatePOSentToSupplier = DateTimeOffset.Now; db.InsertOrUpdatePurchaseOrderHeader(poh); poht.DatePOSentToSupplier = poh.DatePOSentToSupplier; db.InsertOrUpdatePurchaseOrderHeaderTemp(poht); // EMail the purchase order to the supplier var message = new EMailMessage(sender, MessageTemplateType.PurchaseOrderNotificationSupplier); var company = CompanyService.FindCompanyModel(poh.CompanyId); message.AddProperty("PURCHASEORDERNO", poh.OrderNumber.Value); message.AddProperty("COMPANYNAME", company.FriendlyName); message.AddProperty("USERNAME", sender.FullName); message.AddProperty("EMAIL", sender.EMail); message.AddRecipient(poh.Supplier.Email); message.AddAttachment(pdfFile, FileCopyType.Move); EMailService.EMailService emailService = new Evolution.EMailService.EMailService(db, company); error = emailService.SendEMail(message); } } } } return(error); }
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"); }