示例#1
0
        public Error PrintSale(SalePrintOptionsViewModel model,
                               CompanyModel company, UserModel currentUser, string selectedIds)
        {
            // Prints the sale according to the user selections
            var error = new Error();

            var soh = FindSalesOrderHeaderModelFromTempId(model.SalesOrderHeaderTempId, company, false);

            if (soh != null)
            {
                var customer = CustomerService.FindCustomerModel(soh.CustomerId.Value, company, false);
                if (customer != null)
                {
                    // Check the recipients and new contacts
                    var recipients = new List <UserModel>();
                    var copies     = new List <UserModel>();
                    error = BuildRecipientLists(selectedIds.ToLower(),
                                                model.CustomerContact,
                                                company,
                                                customer,
                                                model.SaveAsContact,
                                                recipients,
                                                copies);
                    if (!error.IsError)
                    {
                        // Create the required document PDF
                        string outputFile = "";
                        string pdfFile    = MediaServices.GetMediaFolder(MediaFolder.Temp, soh.CompanyId) +
                                            "\\" + soh.OrderNumber + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                        error = CreateSalesDocumentPdf(soh, model.TemplateId, pdfFile, model.ShowCancelledItems, ref outputFile);
                        if (!error.IsError)
                        {
                            string url = MediaServices.GetMediaFolder(MediaFolder.Temp, soh.CompanyId, -1, -1, true) + "/" + outputFile.FileName();

                            if (model.SaveInSaleNotesAttachments)
                            {
                                // Save it against sale notes/attachments
                                error = NoteService.AttachNoteToSalesOrder(soh,
                                                                           currentUser,
                                                                           (string.IsNullOrEmpty(model.Subject) ? "Sale Preview Document" : model.Subject),
                                                                           model.Message,
                                                                           outputFile.ToStringList(),
                                                                           FileCopyType.Copy);
                            }
                            if (!error.IsError)
                            {
                                if (model.SendAsEMail)
                                {
                                    // Send it as an email attachment
                                    Dictionary <string, string> dict = new Dictionary <string, string>();

                                    if (!error.IsError)
                                    {
                                        var message = new EMailMessage(currentUser, recipients, model.Subject, model.Message);
                                        message.AddCopies(copies);
                                        message.AddProperties(dict);
                                        message.AddAttachment(outputFile, FileCopyType.Copy);

                                        EMailService.EMailService emailService = new Evolution.EMailService.EMailService(db, company);
                                        error = emailService.SendEMail(message);
                                        if (!error.IsError)
                                        {
                                            error.SetInfo(EvolutionResources.infEMailSuccessfullySent);

                                            if (model.ViewCreatedDocument)
                                            {
                                                error.URL = url;
                                            }
                                        }
                                    }
                                }
                                else if (model.ViewCreatedDocument)
                                {
                                    error.URL = url;
                                }
                                else
                                {
                                    error.SetInfo(EvolutionResources.infDocumentSuccessfullyCreated);
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(outputFile))
                        {
                            MediaServices.AddFileToLog(outputFile, 30);
                        }
                    }
                }
                else
                {
                    error.SetRecordError("Customer", soh.CustomerId.Value);
                }
            }
            else
            {
                error.SetError(EvolutionResources.errFailedToFindSOGForSOHT, "", model.SalesOrderHeaderTempId.ToString());
            }
            model.SetError(error.Icon, error.Message, null, null, null, null, true);

            return(error);
        }
示例#2
0
        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 SendEMailThroughTaskServiceTest()
        {
            // Get a test user and test company
            var testUser    = GetTestUser();
            var testCompany = GetTestCompany(testUser);

            EMailService.EMailService emailService = new EMailService.EMailService(db, testCompany);
            MediaService.MediaService mediaService = new Evolution.MediaService.MediaService(db);

            var sender = new UserModel {
                EMail = GetAppSetting("SenderAddress", "")
            };
            UserModel recipient = new UserModel {
                EMail = GetAppSetting("RecipientAddress", "")
            };

            // Create messages, the first 10 without attachments, the rest with attachments
            int numMsgs = 20;

            for (int i = 0; i < numMsgs; i++)
            {
                var error   = new Error();
                var message = new EMailMessage(sender, recipient, MessageTemplateType.TestMessage);

                if (i >= 10)
                {
                    message.AddProperty("MESSAGECONTENT", $"This message is {i + 1} of {numMsgs} and has been sent with an attachment by an Evolution outomated test - please ignore.");

                    string tempFile = MediaService.MediaService.GetTempFile(".txt");
                    db.LogTestFile(tempFile);
                    message.AddAttachment(tempFile, FileCopyType.Move);

                    using (StreamWriter sw = new StreamWriter(tempFile)) {
                        sw.WriteLine("Attachment " + (i - 9).ToString());
                        sw.WriteLine("");
                        sw.WriteLine(LorumIpsum());
                    }

                    error = emailService.SendEMail(message);
                }
                else
                {
                    message.AddProperty("MESSAGECONTENT", $"This message is {i + 1} of {numMsgs} and has been sent by an Evolution outomated test - please ignore.");

                    error = emailService.SendEMail(message);
                }
                Assert.IsTrue(!error.IsError, error.Message);
            }

            if (GetAppSetting("EMailSendingServiceInTests", false))
            {
                TaskService.TaskService ts = new TaskService.TaskService(db);

                var mailSender = ts.FindScheduledTaskModel(TaskName.MailSenderTask);
                Assert.IsTrue(mailSender != null, "Error: MailSender Scheduled Task not found");

                var enabled = mailSender.Enabled;
                if (!enabled)
                {
                    ts.EnableTask(mailSender, true);
                }

                //ts.RunTask(mailSender.Id, true);
                var      mailSenderTask = new MailSenderTask(db);
                string[] args           = null;
                mailSenderTask.Run(args);

                mailSender = ts.FindScheduledTaskModel(TaskName.MailSenderTask);
                Assert.IsTrue(mailSender != null, "Error: MailSender Scheduled Task not found");
                ts.EnableTask(mailSender, enabled);
            }
        }
示例#4
0
        private Error processPurchaseOrder(PurchaseOrderHeader poh,
                                           FileTransferConfigurationModel template,
                                           FileRecipient fileRecipient)
        {
            var error = new Error();

            // Load the template configuration file
            string configFile = getTemplateFileName(template),
                   tempFile   = "",
                   zipFile    = "";

            XElement doc = XElement.Load(configFile);

            var file = doc.Element("File");

            error = createPurchaseOrderDataFile(poh, file, ref tempFile);
            if (!error.IsError)
            {
                // Check if the file is compressed individually and sent
                bool bCompress = file.Attribute("CompressFile").Value.ParseBool();
                if (bCompress)
                {
                    // File is to be compressed
                    zipFile = tempFile.ChangeExtension(".zip");

                    error = Zip.ZipFile(tempFile, zipFile);

                    if (error.IsError)
                    {
                        FileManagerService.FileManagerService.DeleteFile(zipFile);
                    }
                    else
                    {
                        tempFile = zipFile;
                    }
                }

                bool bDelTempFile = true;
                if (!error.IsError)
                {
                    if (file.Attribute("FTPFile").Value.ParseBool())
                    {
                        // Copy the file to the FTP pickup folder
                        error = moveFileToFTPFolder(tempFile, template.SourceFolder, poh.OrderNumber + (bCompress ? ".zip" : ".txt"));
                    }
                    if (!error.IsError && file.Attribute("EMailFile").Value.ParseBool())
                    {
                        // Queue the file to be sent as an email
                        var company = CompanyService.FindCompanyModel(poh.CompanyId);

                        var purchaser = MembershipManagementService.FindUserModel(poh.SalespersonId.Value);

                        string email = "";
                        if (fileRecipient == FileRecipient.FreightForwarder)
                        {
                            var fforwarder = LookupService.FindFreightForwarderModel(poh.FreightForwarderId.Value);
                            if (string.IsNullOrEmpty(fforwarder.Email))
                            {
                                error.SetError(EvolutionResources.errCantSendEMailToBlankFreightForwarderAddress);
                            }
                            else
                            {
                                email = fforwarder.Email;
                            }
                        }
                        else
                        {
                            error.SetError(EvolutionResources.errEMailingWarehouseNotSupported);
                        }

                        if (!error.IsError)
                        {
                            var recipient = new UserModel {
                                EMail = email
                            };
                            var message = new EMailMessage(purchaser, recipient, MessageTemplateType.PurchaseOrderNotificationFF);

                            EMailService(company).AddOrganisationDetails(message.Dict);
                            EMailService().AddUserDetails(purchaser, message.Dict);
                            message.AddProperty("PURCHASEORDERNO", poh.OrderNumber.Value);

                            message.AddAttachment(tempFile);

                            error = EMailService().SendEMail(message);
                            if (!error.IsError)
                            {
                                bDelTempFile = false;
                            }
                        }
                    }
                }
                if (bDelTempFile)
                {
                    FileManagerService.FileManagerService.DeleteFile(tempFile);
                }
            }

            return(error);
        }