public ActionResult GetTotalByOrderId(int id, int customerId)
        {
            CakesPosRepository cpr   = new CakesPosRepository(_connectionString);
            decimal            total = cpr.GetTotalByOrderId(id, customerId);

            return(Json(total, JsonRequestBehavior.AllowGet));
        }
        public void CreateStatementPDF(StatementsModel s, string path)
        {
            var customer = s.Orders.FirstOrDefault().customer;
            var doc5     = new Document();

            PdfPTable table = new PdfPTable(5);

            /*
             * default table width => 80%
             */
            table.WidthPercentage = 100;
            // then set the column's __relative__ widths
            table.SetWidths(new Single[] { 3, 3, 6, 3, 3 });

            /*
             * by default tables 'collapse' on surrounding elements,
             * so you need to explicitly add spacing
             */
            table.SpacingBefore = 10;


            PdfPCell cell1 = new PdfPCell(new Phrase("Date"));

            cell1.BackgroundColor = new BaseColor(204, 204, 204);
            table.AddCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Phrase("Invoice"));

            cell2.BackgroundColor = new BaseColor(204, 204, 204);
            table.AddCell(cell2);

            PdfPCell cell3 = new PdfPCell(new Phrase("Description"));

            cell3.BackgroundColor = new BaseColor(204, 204, 204);
            table.AddCell(cell3);

            //PdfPCell cell4 = new PdfPCell(new Phrase("Payment"));
            //cell4.BackgroundColor = new BaseColor(204, 204, 204);
            //table.AddCell(cell4);

            PdfPCell cell5 = new PdfPCell(new Phrase("Amount"));

            cell5.BackgroundColor = new BaseColor(204, 204, 204);
            table.AddCell(cell5);

            PdfPCell cell6 = new PdfPCell(new Phrase("Balance"));

            cell6.BackgroundColor = new BaseColor(204, 204, 204);
            table.AddCell(cell6);

            Phrase BillFrom = new Phrase();
            Chunk  a        = new Chunk("Siegelman Cakes \n");
            Chunk  csz      = new Chunk("922 46th Street \nBrooklyn NY 11219\n");
            Chunk  p        = new Chunk("Phone: 718-438-0772 \n");
            //Chunk f = new Chunk("Fax: 787-854-7785 \n");
            Chunk e = new Chunk("Email: [email protected] \n______________________________\n\n");

            BillFrom.Add(a);
            BillFrom.Add(csz);
            BillFrom.Add(p);
            //BillFrom.Add(f);
            BillFrom.Add(e);

            Phrase BillTo = new Phrase();
            Chunk  cn     = new Chunk(customer.FirstName + " " + customer.LastName + " \n");
            Chunk  ca     = new Chunk(customer.Address + " \n");
            Chunk  ccsz   = new Chunk(customer.City + " " + customer.State + " " + customer.Zip + " \n");

            //Chunk cp = new Chunk(customer.Phone + " \n");
            //Chunk cf = new Chunk(customer.Cell + " \n");
            BillTo.Add(cn);
            BillTo.Add(ca);
            BillTo.Add(ccsz);
            //BillTo.Add(cp);
            //BillTo.Add(cf);

            Font      headerFont = FontFactory.GetFont("Verdana", 36, BaseColor.BLUE);
            Paragraph header     = new Paragraph("Statement", headerFont);

            header.Alignment = 2;

            Font      invoiceFont = FontFactory.GetFont("Verdana", 16);
            Paragraph title       = new Paragraph("Statement #" + s.Statement.Id, invoiceFont);

            title.Alignment = 2;

            Paragraph date = new Paragraph("Date: " + s.Statement.Date.ToShortDateString(), invoiceFont);

            date.Alignment = 2;


            //Paragraph subTotal = new Paragraph("Subtotal: " + "$450.00");
            //subTotal.Alignment = 2;
            //Paragraph discount = new Paragraph("Discount: " + "$0.00");
            //discount.Alignment = 2;

            Paragraph paymentMessage = new Paragraph("\nMake all checks payable to\nSiegelman Cakes");

            paymentMessage.Alignment = 2;

            Font      greetingFont = FontFactory.GetFont("Ariel", 18);
            Paragraph greeting1    = new Paragraph("Thank you for your business!", greetingFont);

            Paragraph greeting2 = new Paragraph("If you have any questions with this invoice, please contact\n718-438-0772");

            Font      hFont       = FontFactory.GetFont("Verdana", 20, BaseColor.LIGHT_GRAY);
            Paragraph companyName = new Paragraph("Siegelman Cakes", hFont);

            Paragraph billToHeader = new Paragraph("Bill To:\n", hFont);

            CakesPosRepository cpr      = new CakesPosRepository(_connectionString);
            decimal            balance  = 0;
            decimal            discount = 0;

            foreach (OrderDetailsViewModel o in s.Orders)
            {
                var orderDate = "";
                if (o.order.RequiredDate.HasValue)
                {
                    orderDate = o.order.RequiredDate.Value.ToShortDateString();
                }
                var invoice    = o.order.Id;
                var descripton = o.orderedProducts.Sum(pr => pr.quantity) + " Items ordered";
                //var payment = "";
                var amount = cpr.GetTotalByOrderId(o.order.Id, o.order.CustomerId);
                balance += amount;

                table.AddCell(orderDate);
                table.AddCell(invoice.ToString());
                table.AddCell(descripton);
                //table.AddCell(payment);
                table.AddCell(amount.ToString("C"));
                table.AddCell(balance.ToString("C"));

                foreach (Payment payment in o.payments)
                {
                    DateTime paymentDate  = (DateTime)payment.Date;
                    var      invoiceBlank = "Payment";
                    var      pDescripton  = "Thank you for your payment!";
                    //var payment = "";
                    var pAmount = (decimal)payment.Payment1;
                    balance -= (decimal)pAmount;
                    table.AddCell(paymentDate.ToShortDateString());
                    table.AddCell(invoiceBlank);
                    table.AddCell(pDescripton);
                    //table.AddCell(payment);
                    table.AddCell(pAmount.ToString("C"));
                    table.AddCell(balance.ToString("C"));
                }
            }

            discount = balance - s.Statement.Balance;

            //Paragraph disc = new Paragraph("Discount: " + discount.ToString("C"));
            //disc.Alignment = 2;


            Paragraph balanceDue = new Paragraph("Balance Due: " + balance.ToString("C"));

            balanceDue.Alignment = 2;

            Paragraph total = new Paragraph("_________________\nTotal Due: " + balance.ToString("C"));

            total.Alignment = 2;

            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

            PdfWriter.GetInstance(doc5, fs);


            doc5.Open();
            doc5.Add(header);
            doc5.Add(date);
            doc5.Add(title);
            doc5.Add(balanceDue);
            doc5.Add(companyName);
            doc5.Add(BillFrom);
            doc5.Add(billToHeader);
            doc5.Add(BillTo);
            doc5.Add(table);
            //doc5.Add(subTotal);
            //doc5.Add(discount);
            //doc5.Add(disc);
            doc5.Add(total);
            doc5.Add(paymentMessage);
            doc5.Add(greeting2);
            doc5.Add(greeting1);
            doc5.Close();
        }