public ActionResult Invoice(Guid?id)
        {
            try
            {
                var order = Db.Orders
                            .Include(x => x.Client)
                            .Include(x => x.Pianos.Select(y => y.PianoMake))
                            .Include(x => x.Pianos.Select(y => y.PianoType))
                            .Include(x => x.Pianos.Select(y => y.PianoSize))
                            .Include(x => x.Assignments.Select(y => y.Drivers))
                            .Include(x => x.PickupAddress)
                            .Include(x => x.DeliveryAddress)
                            .Include(x => x.OrderCharges.Select(y => y.PianoCharges))
                            .FirstOrDefault(x => x.Id == id);

                string _directoryPath = UploadsPath + "\\InvoiceCodes";

                string html = InvoiceHtmlHelper.GenerateInvoiceHtml(order, _directoryPath);

                JsonResponse Path      = HtmlToPdf(html, order.OrderNumber);
                string       pathValue = Path.Result.ToString();

                if (!System.IO.File.Exists(pathValue))
                {
                    return(null);
                }
                return(File(pathValue, "application/pdf", "Invoice.pdf"));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.StackTrace);
                return(File(new byte[0], "application/pdf", "Error.pdf"));
            }
        }
        public ActionResult EmailInvoice(string ClientId, string StartDate, string EndDate)
        {
            try
            {
                if (ClientId == string.Empty || StartDate == null || EndDate == null)
                {
                    return(Json(new { IsSucess = false }, JsonRequestBehavior.AllowGet));
                }


                Client client = Db.Clients.Include(x => x.Address).FirstOrDefault(x => x.Id.ToString() == ClientId);

                IEnumerable <Order> Orders = GetOrders(ClientId, StartDate, EndDate);
                List <Piano>        Pianos = new List <Piano>();

                foreach (var item in Orders)
                {
                    List <Piano> PianosNew = item.Pianos.ToList();
                    Pianos.AddRange(PianosNew);
                }

                int    invoiceNumber = Db.Invoices.Count() + 3000;
                long   totalAmount   = 0;
                string html          = InvoiceHtmlHelper.GenerateClientInvoiceHtml(Orders, client, Pianos, invoiceNumber, out totalAmount);

                JsonResponse Path = HtmlToPdf(html, invoiceNumber.ToString());

                string pathValue = Path.Result.ToString();

                string subject = "Client Invoice";
                string body    = string.Format(@"<p>Please find the attached invoice for client {0} </p>",
                                               client.Name);
                List <string> attachment = new List <string>();

                attachment.Add(pathValue);

                EmailHelper.SendEmail(client.EmailAddress, subject, body, null, attachment);

                SaveInvoice(ClientId, StartDate, EndDate, invoiceNumber, totalAmount, InvoiceStatusEnum.Sent);

                return(Json(new { IsSucess = true }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new JsonResponse {
                    ErrorMessage = ex.ToString(), IsSucess = false
                }, JsonRequestBehavior.AllowGet));

                throw;
            }
        }
        public ActionResult DownloadInvoice(string ClientId, string StartDate, string EndDate)
        {
            if (ClientId == string.Empty || StartDate == null || EndDate == null)
            {
                return(File(new byte[0], "application/octet-stream", "Error.pdf"));
            }

            Client client = Db.Clients.Include(x => x.Address).FirstOrDefault(x => x.Id.ToString() == ClientId);

            IEnumerable <Order> Orders = GetOrders(ClientId, StartDate, EndDate);

            List <Piano> Pianos = new List <Piano>();

            foreach (var item in Orders)
            {
                List <Piano> PianosNew = item.Pianos.ToList();
                Pianos.AddRange(PianosNew);
            }

            int    invoiceNumber = Db.Invoices.Count() + 3000;
            long   totalAmount   = 0;
            string html          = InvoiceHtmlHelper.GenerateClientInvoiceHtml(Orders, client, Pianos, invoiceNumber, out totalAmount);

            JsonResponse Path = HtmlToPdf(html, invoiceNumber.ToString());

            SaveInvoice(ClientId, StartDate, EndDate, invoiceNumber, totalAmount, InvoiceStatusEnum.Generated);

            string pathValue = Path.Result.ToString();

            if (!System.IO.File.Exists(pathValue))
            {
                return(File(new byte[0], "application/octet-stream", "Error.pdf"));
            }

            return(File(pathValue, "application/pdf", "Invoice.pdf"));
        }