//
        // GET: //GenerateInvoice
        public ActionResult GenerateInvoice(int id = 0)
        {
            int nInvoiceNo = 0;
            int nTrackingNo = 0;
            int nPos = -1;
            double dTax = 0;
            string szMsg = "";
            string[] szHlp = null;
            InitialInfo initialinfo = null;
            Invoice invoice = null;
            InvoiceDetail invoicedetail = null;

            TimelyDepotContext db01 = new TimelyDepotContext();

            IQueryable<SalesOrderDetail> qrySODetails = null;

            SalesOrder salesorder = db.SalesOrders.Find(id);
            if (salesorder != null)
            {
                //Get the next payment No
                initialinfo = db.InitialInfoes.FirstOrDefault<InitialInfo>();
                if (initialinfo == null)
                {
                    initialinfo = new InitialInfo();
                    initialinfo.InvoiceNo = 1;
                    initialinfo.PaymentNo = 0;
                    initialinfo.PurchaseOrderNo = 0;
                    initialinfo.SalesOrderNo = 0;
                    initialinfo.TaxRate = 0;
                    initialinfo.TrackingNo = 1;
                    db.InitialInfoes.Add(initialinfo);
                }
                else
                {
                    nInvoiceNo = initialinfo.InvoiceNo;
                    nInvoiceNo++;
                    initialinfo.InvoiceNo = nInvoiceNo;
                    nTrackingNo = Convert.ToInt32(initialinfo.TrackingNo);
                    nTrackingNo++;
                    initialinfo.TrackingNo = nTrackingNo;
                    dTax = initialinfo.TaxRate;
                    db.Entry(initialinfo).State = EntityState.Modified;
                }

                //Use the sales order tax information
                if (salesorder.Tax_rate != null)
                {
                    if (Convert.ToDecimal(salesorder.Tax_rate) >= 0)
                    {
                        dTax = Convert.ToDouble(salesorder.Tax_rate);
                    }
                }

                //Create the Invoice
                invoice = new Invoice();
                invoice.InvoiceNo = nInvoiceNo.ToString();
                invoice.CustomerId = salesorder.CustomerId;
                invoice.BussinesType = salesorder.BussinesType;
                invoice.CreaditCardNo = salesorder.CreaditCardNo;
                invoice.CustomerShipLocation = salesorder.CustomerShipLocation;
                invoice.CustomerShiptoId = salesorder.CustomerShiptoId;
                //invoice.InvoiceDate = DateTime.Now;
                invoice.InvoiceDate = Convert.ToDateTime(salesorder.SODate);
                invoice.IsBlindShip = salesorder.IsBlindShip;
                invoice.Note = salesorder.Note;
                invoice.PaymentAmount = salesorder.PaymentAmount;
                invoice.PaymentDate = salesorder.PaymentDate;
                invoice.PaymentTerms = salesorder.PaymentTerms;
                invoice.PurchaseOrderNo = salesorder.PurchaseOrderNo;
                invoice.SalesOrderId = salesorder.SalesOrderId;
                invoice.SalesOrderNo = salesorder.SalesOrderNo;
                invoice.SalesRep = salesorder.SalesRep;
                invoice.ShipDate = salesorder.ShipDate;
                invoice.ShippingHandling = salesorder.ShippingHandling;
                invoice.ShipVia = salesorder.ShipVia;
                invoice.TrackingNo = nTrackingNo.ToString();
                invoice.TradeId = salesorder.TradeId;
                invoice.VendorAddress = salesorder.VendorAddress;
                invoice.VendorId = salesorder.VendorId;
                invoice.Tax_rate = Convert.ToDecimal(dTax);
                invoice.Invs_Tax = Convert.ToDecimal(dTax);

                //Set the shipment information
                invoice.FromAddress1 = salesorder.FromAddress1;
                invoice.FromAddress2 = salesorder.FromAddress2;
                invoice.FromCity = salesorder.FromCity;
                invoice.FromCompany = salesorder.FromCompany;
                invoice.FromCountry = salesorder.FromCountry;
                invoice.FromEmail = salesorder.FromEmail;
                invoice.FromFax = salesorder.FromFax;
                invoice.FromName = salesorder.FromName;
                invoice.FromState = salesorder.FromState;
                invoice.FromTel = salesorder.FromTel;
                invoice.FromTitle = salesorder.FromTitle;
                invoice.FromZip = salesorder.FromZip;

                invoice.ToAddress1 = salesorder.ToAddress1;
                invoice.ToAddress2 = salesorder.ToAddress2;
                invoice.ToCity = salesorder.ToCity;
                invoice.ToCompany = salesorder.ToCompany;
                invoice.ToCountry = salesorder.ToCountry;
                invoice.ToEmail = salesorder.ToEmail;
                invoice.ToFax = salesorder.ToFax;
                invoice.ToName = salesorder.ToName;
                invoice.ToState = salesorder.ToState;
                invoice.ToTel = salesorder.ToTel;
                invoice.ToTitle = salesorder.ToTitle;
                invoice.ToZip = salesorder.ToZip;

                db.Invoices.Add(invoice);
                db.SaveChanges();

                //Create the details
                qrySODetails = db.SalesOrderDetails.Where(sodt => sodt.SalesOrderId == salesorder.SalesOrderId);
                if (qrySODetails.Count() > 0)
                {
                    foreach (var item in qrySODetails)
                    {
                        invoicedetail = new InvoiceDetail();
                        invoicedetail.BackOrderQuantity = 0;
                        invoicedetail.Description = item.Description;

                        nPos = -1;
                        nPos = item.Description.IndexOf("Set up Charge");
                        if (nPos != -1)
                        {
                            szHlp = item.Description.Split(' ');
                            szHlp[3] = invoice.InvoiceId.ToString();
                            szMsg = string.Format("{0} {1} {2} {3} {4}", szHlp[0], szHlp[1], szHlp[2], szHlp[3], szHlp[4]);
                            invoicedetail.Description = szMsg;
                        }

                        nPos = -1;
                        nPos = item.Description.IndexOf("Run Charge");
                        if (nPos != -1)
                        {
                            szHlp = item.Description.Split(' ');
                            szHlp[2] = invoice.InvoiceId.ToString();
                            szMsg = string.Format("{0} {1} {2} {3}", szHlp[0], szHlp[1], szHlp[2], szHlp[3]);
                            invoicedetail.Description = szMsg;
                        }

                        invoicedetail.InvoiceId = invoice.InvoiceId;
                        invoicedetail.ItemID = item.ItemID;
                        invoicedetail.ItemOrder = item.ItemOrder;
                        invoicedetail.ItemPosition = item.ItemPosition;
                        invoicedetail.Quantity = item.Quantity;
                        invoicedetail.ShipQuantity = item.Quantity;
                        invoicedetail.Sub_ItemID = item.Sub_ItemID;
                        invoicedetail.Tax = item.Tax;
                        invoicedetail.UnitPrice = item.UnitPrice;
                        invoicedetail.Tax = item.Tax;
                        db.InvoiceDetails.Add(invoicedetail);

                    }

                    db.SaveChanges();
                }

            }

            return RedirectToAction("Edit", new { id = invoice.InvoiceId });
        }
        public ActionResult Create(Invoice invoice)
        {
            if (ModelState.IsValid)
            {
                db.Invoices.Add(invoice);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(invoice);
        }
        public ActionResult Edit(Invoice invoice, string InvoiceDateHlp01, string InvoiceDateHlp02)
        {
            int nPos = -1;
            int nYear = 0;
            int nMonth = 0;
            int nDay = 0;
            DateTime dDate = DateTime.Now;
            DateTime dDate02 = DateTime.Now;
            string szMsg = "";
            string[] szdateHlp = null;
            if (!string.IsNullOrEmpty(InvoiceDateHlp01))
            {
                szdateHlp = InvoiceDateHlp01.Split('/');
                if (szdateHlp != null)
                {
                    nMonth = Convert.ToInt32(szdateHlp[0]);
                    nDay = Convert.ToInt32(szdateHlp[1]);
                    nYear = Convert.ToInt32(szdateHlp[2]);
                    dDate = new DateTime(nYear, nMonth, nDay);
                }
            }
            if (!string.IsNullOrEmpty(InvoiceDateHlp02))
            {
                szdateHlp = InvoiceDateHlp02.Split('/');
                if (szdateHlp != null)
                {
                    nMonth = Convert.ToInt32(szdateHlp[0]);
                    nDay = Convert.ToInt32(szdateHlp[1]);
                    nYear = Convert.ToInt32(szdateHlp[2]);
                    dDate02 = new DateTime(nYear, nMonth, nDay);
                }
            }
            if (ModelState.IsValid)
            {
                invoice.InvoiceDate = dDate;
                invoice.ShipDate = dDate02;

                if (invoice.Tax_rate == null)
                {
                    invoice.Tax_rate = 0;
                }

                db.Entry(invoice).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                szMsg = string.Format("(Date Help: {0} - {1})", InvoiceDateHlp01, dDate.ToString());
                foreach (var item in ModelState.Values)
                {
                    if (item.Errors.Count > 0)
                    {
                        foreach (var itemError in item.Errors)
                        {
                            szMsg = string.Format("{0} {1}", szMsg, itemError.ErrorMessage);
                        }
                    }
                }

                if (invoice.Tax_rate == null)
                {
                    invoice.Tax_rate = 0;
                }

                nPos = szMsg.IndexOf("is not valid for Invoice Date.");
                if (nPos != -1)
                {
                    if (invoice.InvoiceDate != dDate)
                    {
                        invoice.InvoiceDate = dDate;
                    }
                    db.Entry(invoice).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                nPos = -1;
                nPos = szMsg.IndexOf("no es válido para Invoice Date.");
                if (nPos != -1)
                {
                    if (invoice.InvoiceDate != dDate)
                    {
                        invoice.InvoiceDate = dDate;
                    }
                    if (invoice.ShipDate != dDate02)
                    {
                        invoice.ShipDate = dDate02;
                    }
                    db.Entry(invoice).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                //if (invoice.InvoiceDate != dDate)
                //{
                //    invoice.InvoiceDate = dDate;
                //}
                //db.Entry(invoice).State = EntityState.Modified;
                //db.SaveChanges();

                //return RedirectToAction("Index");
            }

            return RedirectToAction("Edit0", new { errorMsg = szMsg });
            //return View(invoice);
        }
        private PdfPTable GetInvoiceData(Font times04, Font times05, Invoice invoice)
        {
            string szMsg = "";
            string szData01 = "09/20/2013";
            string szData02 = "UPS GROUND";
            string szData03 = " ";
            string szData04 = "PAID BY VISA";
            string szData05 = "5586";
            string szData06 = "9/10/2013";
            string szData07 = "AD";
            string szData08 = "99067";
            DateTime dDate = DateTime.Now;

            PurchaseOrders purchaseorder = db.PurchaseOrders.Where(pror => pror.PurchaseOrderNo == invoice.PurchaseOrderNo).FirstOrDefault<PurchaseOrders>();
            if (purchaseorder == null)
            {
                szData06 = string.Empty;
            }

            PdfPTable invoicedatatbl = new PdfPTable(numColumns: 4);
            invoicedatatbl.SetTotalWidth(new float[] { 138.105f, 138.105f, 138.105f, 138.105f });
            invoicedatatbl.LockedWidth = true;

            Paragraph hlpPar = null;
            PdfPCell hlpinv = null;

            //First Row
            szMsg = string.Format("{0}", "Date:");
            hlpPar = new Paragraph(szMsg, times05);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szMsg = string.Format("{0}", "Ship Via");
            hlpPar = new Paragraph(szMsg, times05);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szMsg = string.Format("{0}", "Tracking No.");
            hlpPar = new Paragraph(szMsg, times05);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szMsg = string.Format("{0}", "Terms");
            hlpPar = new Paragraph(szMsg, times05);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            //Second Row
            dDate = Convert.ToDateTime(invoice.InvoiceDate);
            szData01 = dDate.ToString("MM/dd/yyyy");
            szMsg = string.Format("{0}", szData01);
            hlpPar = new Paragraph(szMsg, times04);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szData02 = invoice.ShipVia;
            szMsg = string.Format("{0}", szData02);
            hlpPar = new Paragraph(szMsg, times04);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szData03 = invoice.TrackingNo;
            szMsg = string.Format("{0}", szData03);
            hlpPar = new Paragraph(szMsg, times04);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szData04 = invoice.PaymentTerms;
            szMsg = string.Format("{0}", szData04);
            hlpPar = new Paragraph(szMsg, times04);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            //Third Row
            szMsg = string.Format("{0}", "P/O No.");
            hlpPar = new Paragraph(szMsg, times05);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szMsg = string.Format("{0}", "Order Date");
            hlpPar = new Paragraph(szMsg, times05);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szMsg = string.Format("{0}", "Sales Rep.");
            hlpPar = new Paragraph(szMsg, times05);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szMsg = string.Format("{0}", "Sales Order No.");
            hlpPar = new Paragraph(szMsg, times05);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0f;
            hlpinv.BorderWidthBottom = 0f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            //Fourth Row
            if (string.IsNullOrEmpty(invoice.PurchaseOrderNo))
            {
                szData06 = string.Empty;
            }
            else
            {
                dDate = Convert.ToDateTime(purchaseorder.PODate);
                szData06 = dDate.ToString("MM/dd/yyyy");
            }
            szData05 = invoice.PurchaseOrderNo;
            szMsg = string.Format("{0}", szData05);
            hlpPar = new Paragraph(szMsg, times04);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0.5f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szMsg = string.Format("{0}", szData06);
            hlpPar = new Paragraph(szMsg, times04);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0.5f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szData07 = invoice.SalesRep;
            szMsg = string.Format("{0}", szData07);
            hlpPar = new Paragraph(szMsg, times04);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0.5f;
            hlpinv.BorderWidthBottom = 0.5f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            szData08 = invoice.SalesOrderNo;
            szMsg = string.Format("{0}", szData08);
            hlpPar = new Paragraph(szMsg, times04);
            hlpinv = new PdfPCell(hlpPar);
            hlpinv.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            hlpinv.BorderWidthTop = 0.5f;
            hlpinv.BorderWidthLeft = 0f;
            hlpinv.BorderWidthRight = 0f;
            hlpinv.BorderWidthBottom = 0.5f;
            hlpinv.PaddingTop = 1;
            hlpinv.PaddingLeft = 1;
            hlpinv.PaddingRight = 1;
            hlpinv.PaddingBottom = 3;
            hlpinv.HorizontalAlignment = Element.ALIGN_CENTER;
            invoicedatatbl.AddCell(hlpinv);

            return invoicedatatbl;
        }