Exemple #1
0
        public JsonResult EmailInvoice(int id, string email)
        {
            try
            {
                var model          = GetInvoiceBalanceModel(id);
                var fileName       = string.Format("Invoice_{0}_{1}.pdf", model.Invoice.Id, model.Invoice.Date.ToString("MM-dd-yyyy"));
                var client         = db.Clients.FirstOrDefault(c => c.Id == model.Invoice.Client.Id);
                var invoiceService = new InvoiceService();

                var viewAsPef = new ViewAsPdf("~/Views/Sales/Print.cshtml", model)
                {
                    FileName        = fileName,
                    PageSize        = Size.Letter,
                    PageOrientation = Orientation.Portrait,
                    PageMargins     = { Left = 1, Right = 1 }
                };

                byte[] bytesPDFData = viewAsPef.BuildPdf(ControllerContext);
                var    stream       = new MemoryStream(bytesPDFData);
                var    emailService = new SendEmailService(db);
                client.Email = email;
                emailService.SendInvoiceToClient(model.Invoice, client, stream, fileName);

                return(Json(new { Result = true, Data = new { ClientId = client.Id }, Message = "The Invoice has send" }));
            }
            catch (Exception ex)
            {
                return(Json(new { Result = false, Message = ex.Message }));
            }
        }