Example #1
0
        public async Task <IHttpActionResult> Invoice(int id)
        {
            try {
                var order = await _repo.Get(id);

                if (order == null)
                {
                    return(NotFound());
                }

                var report = new InvoiceReport(order) as SectionReport;
                report.Run();
                return(Ok(report));
            }
            catch (Exception ex) {
                return(Ok(ex));
            }
        }
Example #2
0
        private static void DisplayReportWriterExamples()
        {
            bool exitToProducts = false;

            while (!exitToProducts)
            {
                Console.WriteLine("Select a DynamicPDF ReportWriter for .NET example to run:");
                Console.WriteLine("     A : Contact List");
                Console.WriteLine("     B : Contact List With Group By");
                Console.WriteLine("     C : Form Fill");
                Console.WriteLine("     D : Form Letter");
                Console.WriteLine("     E : Invoice");
                Console.WriteLine("     F : Place Holder");
                Console.WriteLine("     G : Simple Sub Report");
                Console.WriteLine();
                Console.WriteLine("Press 'Backspace' for the main products menu");
                Console.WriteLine("Press 'Esc' to exit application");
                Console.WriteLine();

                ConsoleKeyInfo runKey = Console.ReadKey();
                Console.WriteLine();
                Console.WriteLine();

                string exampleName = "";
                string fileName    = "";
                switch (runKey.Key)
                {
                case ConsoleKey.A:
                    exampleName = "Contact List";
                    fileName    = "ContactListReport.pdf";
                    Console.WriteLine("Example " + exampleName + " is Running...");
                    ContactListReport.Run(fileName);
                    break;

                case ConsoleKey.B:
                    exampleName = "Contact List With Group By";
                    fileName    = "ContactListWithGroupByReport.pdf";
                    Console.WriteLine("Example " + exampleName + " is Running...");
                    ContactListWithGroupByReport.Run(fileName);
                    break;

                case ConsoleKey.C:
                    exampleName = "Form Fill";
                    fileName    = "FormFillReport.pdf";
                    Console.WriteLine("Example " + exampleName + " is Running...");
                    FormFillReport.Run(fileName);
                    break;

                case ConsoleKey.D:
                    exampleName = "Form Letter";
                    fileName    = "FormLetterReport.pdf";
                    Console.WriteLine("Example " + exampleName + " is Running...");
                    FormLetterReport.Run(fileName);
                    break;

                case ConsoleKey.E:
                    exampleName = "Invoice";
                    fileName    = "InvoiceReport.pdf";
                    Console.WriteLine("Example " + exampleName + " is Running...");
                    InvoiceReport.Run(fileName);
                    break;

                case ConsoleKey.F:
                    exampleName = "Place Holder";
                    fileName    = "PlaceHolderReport.pdf";
                    Console.WriteLine("Example " + exampleName + " is Running...");
                    PlaceHolderReport.Run(fileName);
                    break;

                case ConsoleKey.G:
                    exampleName = "Simple Sub Report";
                    fileName    = "SimpleSubReport.pdf";
                    Console.WriteLine("Example " + exampleName + " is Running...");
                    SimpleSubReport.Run(fileName);
                    break;

                case ConsoleKey.Escape:
                    System.Environment.Exit(0);
                    break;

                case ConsoleKey.Backspace:
                    exitToProducts = true;
                    break;

                default:
                    Console.WriteLine();
                    Console.WriteLine("Key not recognized.");
                    break;
                }

                if (fileName != string.Empty)
                {
                    DisplayOutputPathWithOptionToOpen(fileName);
                }
            }
        }
Example #3
0
        public async Task <IHttpActionResult> EmailInvoice(EmailInputModel model)
        {
            var fromAddress = ConfigurationManager.AppSettings["EmailFromAddress"];
            var user        = Request.GetOwinContext().Request.User;

            var order = await _repo.Get(model.OrderId);

            var report = new InvoiceReport(order) as SectionReport;

            report.Run();
            var memStream = new MemoryStream();
            var pdfExport = new PdfExport();

            pdfExport.Export(report.Document, memStream);
            memStream.Position = 0;
            var attachments = new Dictionary <string, MemoryStream> {
                { $"Invoice {order.Id.ToString("0000")}.pdf", memStream }
            };

            var mailMessage = new SendGridMessage {
                Subject             = model.Subject,
                From                = new MailAddress(fromAddress, user.Identity.Name),
                Text                = model.Body,
                Html                = model.Body.Replace("\n", "<br>"),
                StreamedAttachments = attachments
            };
            var to = model.Address.Where(a => !string.IsNullOrWhiteSpace(a)).ToList();

            if (to.Any())
            {
                mailMessage.AddTo(to);

                model.Bcc.ToList().ForEach(bcc => {
                    if (!string.IsNullOrWhiteSpace(bcc))
                    {
                        mailMessage.AddBcc(bcc);
                    }
                });
            }
            else
            {
                to = model.Bcc.Where(a => !string.IsNullOrWhiteSpace(a)).ToList();
                mailMessage.AddTo(to);
            }

            var delivery = await _repo.RecordInvoiceEmail(mailMessage, order.Id, user.Identity.Name);

            mailMessage.AddUniqueArgs(new Dictionary <string, string> {
                ["order_id"]    = order.Id.ToString(),
                ["delivery_id"] = delivery.Id.ToString()
            });

            try {
                var apiKey       = ConfigurationManager.AppSettings["SendGridApiKey"];
                var transportWeb = new Web(apiKey);
                await transportWeb.DeliverAsync(mailMessage);
            } catch (InvalidApiRequestException ex) {
                var       errorMessage = string.Join(", ", ex.Errors);
                Exception error        = ex;
                while (error != null)
                {
                    errorMessage += $",{error.Message}";
                    error         = error.InnerException;
                }
                await _repo.LogEmailDeliveryError(delivery.Id, errorMessage);

                throw;
            } catch (Exception ex) {
                var errorMessage = string.Empty;
                var error        = ex;
                while (error != null)
                {
                    errorMessage += $",{error.Message}";
                    error         = error.InnerException;
                }
                await _repo.LogEmailDeliveryError(delivery.Id, errorMessage);

                throw;
            }

            var email = string.Join(";", model.Address);

            if (!string.IsNullOrWhiteSpace(email))
            {
                order.Inquiry.Email = email;
            }
            if (order.InvoiceDate == null)
            {
                order.InvoiceDate = DateTime.Now;
            }
            var edited = await _repo.Edit(order, user.Identity.Name);

            return(Ok(edited));
        }