public ActionResult CreateInvoice(int? Id)
        {
            Order order = new Order();
            Invoice invoice = new Invoice();
            order = db.Orders.Find(Id);

            invoice.Annotation = order.Annotation + " - Factuur van order " + order.OrderNumber;
            invoice.Box = order.Box;
            invoice.CellPhone = order.CellPhone;
            invoice.CustomerId = order.CustomerId;
            invoice.Date = order.Date;
            invoice.Email = order.Email;
            invoice.FirstName = order.FirstName;
            invoice.LastName = order.LastName;
            invoice.PostalCodeNumber = order.PostalCodeNumber;
            invoice.StreetName = order.StreetName;
            invoice.StreetNumber = order.StreetNumber;
            invoice.TotalPrice = order.TotalPrice;
            invoice.Town = order.Town;
            invoice.Active = true;
            order.Active = false;

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

            invoice.customerDeliveryAddress = order.customerDeliveryAddress;

            //find highest order number
            int maxInvoiceNumber = 1;
            invoice.InvoiceNumber = maxInvoiceNumber;
            var listInvoices = db.Invoices.ToList();

            if (listInvoices.Count > 1)
            {
                maxInvoiceNumber = listInvoices.Max(i => i.InvoiceNumber);
                invoice.InvoiceNumber = maxInvoiceNumber + 1;
            }

            foreach (var item in order.OrderDetail)
            {
                var id = new InvoiceDetail();
                id.InvoiceId = invoice.InvoiceId;
                id.Quantity = item.Quantity;
                id.PriceExVAT = item.PriceExVAT;
                id.TotalExVat = item.TotalExVat;
                id.TotalIncVat = item.TotalIncVat;
                id.Auvibel = item.Auvibel;
                id.Bebat = item.Bebat;
                id.Brand = item.Brand;
                id.CategoryId = item.CategoryId;
                id.Description = item.Description;
                id.ProductCode = item.ProductCode;
                id.ProductName = item.ProductName;
                id.Recupel = item.Recupel;
                id.Reprobel = item.Reprobel;
                id.VATPercId = item.VATPercId;
                id.ProductId = item.ProductId;
                id.VAT = item.VAT;
                db.InvoiceDetails.Add(id);
            }

            db.SaveChanges();
            return RedirectToAction("Index", "Invoices");
        }
        // GET: Invoices
        public ActionResult Index(string searchInvoiceNumber, string currentFilterInvoiceNumber, string searchCustomer, string currentFilterCustomer, string searchDelivery, string currentFilterDelivery, int? page, string sortOrder)
        {
            InvoiceIndexViewModel iivm = new InvoiceIndexViewModel();
            var invoices = from i in db.Invoices select i;
            Invoice invoice = new Invoice();

            //ViewBag.CurrentSort = sortOrder;

            ViewBag.InvoiceSortParm = String.IsNullOrEmpty(sortOrder) ? "invoice_asc" : "";
            ViewBag.CustomerSortParm = sortOrder == "cust" ? "cust_desc" : "cust";

            if (searchCustomer != null || searchInvoiceNumber != null)
            {
                page = 1;
            }
            else
            {
                searchInvoiceNumber = currentFilterInvoiceNumber;
                searchCustomer = currentFilterCustomer;
                searchDelivery = currentFilterDelivery;
            }
            ViewBag.CurrentFilterInvoice = searchInvoiceNumber;
            ViewBag.CurrentFilterCustomer = searchCustomer;
            ViewBag.CurrentFilterDelivery = searchDelivery;

            if (!String.IsNullOrEmpty(searchInvoiceNumber))
            {
                invoices = invoices.Where(i => i.InvoiceNumber.ToString().Contains(searchInvoiceNumber));
            }
            if (!String.IsNullOrEmpty(searchCustomer))
            {
                invoices = invoices.Where(i => i.LastName.ToUpper().Contains(searchCustomer.ToUpper()) || i.FirstName.ToUpper().Contains(searchCustomer.ToUpper()));
            }
            if (!string.IsNullOrEmpty(searchDelivery))
            {
                invoices = invoices.Where(i => i.customerDeliveryAddress.DeliveryAddressInfo.ToUpper().Contains(searchDelivery.ToUpper()) || i.customerDeliveryAddress.StreetName.ToUpper().Contains(searchDelivery.ToUpper()) || i.customerDeliveryAddress.Town.ToUpper().Contains(searchDelivery.ToUpper()));
            }

            switch (sortOrder)
            {
                case "invoice_asc":
                    invoices = invoices.OrderBy(s => s.InvoiceNumber);
                    break;
                case "cust_desc":
                    invoices = invoices.OrderByDescending(s => s.LastName);
                    break;
                case "cust":
                    invoices = invoices.OrderBy(s => s.LastName);
                    break;
                default:
                    invoices = invoices.OrderByDescending(s => s.InvoiceNumber);
                    break;
            }

            var userDefinedInfo = db.UserDefinedSettings.Find(1);
            int pageSize = userDefinedInfo.IndexResultLength;
            int pageNumber = (page ?? 1);

            //ViewBag.Quotations = quotations.ToPagedList(pageNumber, pageSize);
            iivm.invoices = invoices.ToPagedList(pageNumber, pageSize);

            return View(iivm);
        }