public ActionResult SubmitQuote(string title = null)
        {
            var userId = User.Identity.GetUserId();
            var user   = db.Users.Find(userId);
            var org    = db.Organizations.Where(o => o.OrganizationId == user.OrganizationId).Include(o => o.Discounts).FirstOrDefault();
            var items  = db.ShoppingCartItems.Where(c => c.UserId == userId)
                         .Include(i => i.Actuator)
                         .Include(i => i.Valve)
                         .Include(i => i.Kit);

            if (items.Count() == 0)
            {
                ViewBag.Error = "There are no items in your shopping cart.  Please add some items.";
                return(View("Error"));
            }

            var sales = user.Contacts.Where(c => c.IsSales).FirstOrDefault();

            if (sales == null)
            {
                sales = db.Users.Where(u => u.IsSales).FirstOrDefault();
            }


            var quoteNumber = items.FirstOrDefault().QuoteNumber;
            var revision    = "";

            if (quoteNumber == null)
            {
                // get the next quote number
                var e = db.QuoteNumber.OrderByDescending(n => n.Number).FirstOrDefault();
                if (e == null)
                {
                    e = new QuoteNumber()
                    {
                        Number = 20000
                    };
                    db.QuoteNumber.Add(e);
                }
                e.Number += 1;
                db.SaveChanges();
                quoteNumber = e.Number;
            }
            else
            {
                // lets get the next rev
                var d = db.Quotes.Where(q => q.QuoteNumber == quoteNumber).OrderByDescending(q => q.Revision).FirstOrDefault();

                revision = RevManager.Next(d.Revision);
            }


            // take the shopping cart and turn it into a quote
            Quote quote = new Quote()
            {
                CreatedById    = userId,
                CreatedDate    = DateTimeOffset.Now,
                IsSent         = true,
                QuoteNumber    = (int)quoteNumber,
                OrganizationId = org.OrganizationId,
                Revision       = revision,
                CustomerName   = user.Name,
                CreatedByName  = user.Name,
                CustomerId     = user.Id,
                CompanyName    = user.Organization.Name,
                Address1       = user.Organization.Address1,
                Address2       = user.Organization.Address2,
                City           = user.Organization.City,
                Country        = user.Organization.Country,
                State          = user.Organization.State,
                PostalCode     = user.Organization.PostalCode,
                SalesPerson    = sales != null ? sales.Name : "VanAire",
                Title          = title,
                IsOrder        = false
            };

            db.Quotes.Add(quote);

            foreach (var i in items)
            {
                if (i.Actuator != null && i.Valve != null && i.Kit != null)
                {
                    // get the discount
                    var    dis      = org.Discounts.Where(d => d.Quantity >= i.Quantity).OrderBy(d => d.Quantity).FirstOrDefault();
                    double discount = 0;
                    if (dis != null)
                    {
                        discount = dis.DiscountPercentage / 100;
                    }

                    QuoteItem item = new QuoteItem()
                    {
                        Actuator    = i.Actuator.ToString(),
                        Valve       = i.Valve.ToString(),
                        KitNumber   = i.Kit.KitNumber,
                        Description = i.ToString(),
                        Discount    = discount,
                        Quantity    = i.Quantity,
                        PriceEach   = i.Kit.Price * (1 - discount),
                        TotalPrice  = i.Kit.Price * (1 - discount) * i.Quantity
                    };

                    quote.Total += item.TotalPrice;
                    quote.Items.Add(item);
                }
            }


            // clear cart
            db.ShoppingCartItems.RemoveRange(db.ShoppingCartItems.Where(c => c.UserId == userId));

            db.SaveChanges();

            IUserMailer mailer = new UserMailer();

            // send a message
            string message = $"Thank you for submitting quote number {quoteNumber}.  You should receive an email with the quote and drawings attached.  ";

            if (quote.Items.Any(i => i.PriceEach == 0))
            {
                message += "It looks like some of the items you requested have not been priced yet.  A salesperson will review the items and get back to you.";
            }

            var msg = mailer.Quote(quote, quote.Customer.Email);

            HashSet <string> numbers = new HashSet <string>();

            foreach (var item in quote.Items)
            {
                string image = Server.MapPath($"~/Content/Thumbnails/{item.KitNumber}.jpg");
                if (System.IO.File.Exists(image))
                {
                    string code = item.KitNumber.Split('-').ElementAt(1);

                    string mat = db.KitMaterials.Where(m => m.Code == code).FirstOrDefault().Name;

                    string url = Url.Action("ViewDrawing", "ShoppingCartItems", new { kitNumber = item.KitNumber, description = mat + " " + item.Description }, Request.Url.Scheme);

                    HtmlToPdfConverter htmlToPdf = new HtmlToPdfConverter();
                    htmlToPdf.TriggeringMode  = TriggeringMode.Auto;
                    htmlToPdf.LicenseKey      = "jwERABMTABcYABcOEAATEQ4REg4ZGRkZABA=";
                    htmlToPdf.ConversionDelay = 0;
                    htmlToPdf.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Landscape;
                    htmlToPdf.PdfDocumentOptions.PdfPageSize        = new PdfPageSize(792, 612);
                    htmlToPdf.PdfDocumentOptions.FitHeight          = true;

                    byte[] data = htmlToPdf.ConvertUrl(url);

                    //string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), item.KitNumber + ".PDF");
                    //System.IO.FileStream fs = System.IO.File.OpenWrite(tempPath);
                    //fs.Write(data, 0, data.Length);
                    //fs.Close();
                    System.IO.MemoryStream ms = new System.IO.MemoryStream(data, false);
                    msg.Attachments.Add(new System.Net.Mail.Attachment(ms, $"{item.KitNumber}_{item.Actuator}_{item.Valve}_.PDF"));
                }
                else
                {
                    string file = Server.MapPath($"~/Content/Drawings/{item.KitNumber}.pdf");
                    if (System.IO.File.Exists(file))
                    {
                        msg.Attachments.Add(new System.Net.Mail.Attachment(file));
                    }
                }

                // get the VES doc
                var vesNum = item.KitNumber.Split('-').LastOrDefault();
                if (!string.IsNullOrEmpty(vesNum))
                {
                    if (!numbers.Contains(vesNum))
                    {
                        numbers.Add(vesNum);
                        var doc = db.Documents.Where(d => d.Name.Contains(vesNum)).FirstOrDefault();
                        if (doc != null)
                        {
                            System.IO.MemoryStream ms = new System.IO.MemoryStream(doc.FileData, false);
                            msg.Attachments.Add(new System.Net.Mail.Attachment(ms, doc.FileName));
                        }
                    }
                }
            }

            msg.SendAsync();

            db.SystemMessages.Add(new SystemMessage()
            {
                UserId   = userId,
                DateSent = DateTimeOffset.Now,
                Message  = message
            });

            if (sales != null)
            {
                db.SystemMessages.Add(new SystemMessage()
                {
                    UserId   = sales.Id,
                    DateSent = DateTimeOffset.Now,
                    Message  = $"{user.Name} from {user.Organization.Name} submitted quote number {quoteNumber}.  Please review and get in touch with the customer.  Email: {user.Email}, Phone: {user.PhoneNumber}. "
                });

                var msg2 = mailer.QuoteSubmit(quote, sales.Email);
                msg2.SendAsync();
            }

            db.SaveChanges();

            return(RedirectToAction("ViewQuote", "Quotes", new { quoteId = quote.QuoteId }));
        }