/// <summary>
        /// Add an item to the patient's invoice.
        /// </summary>
        /// <returns>The Jason object so it can be immediately displayed.</returns>
        public JsonResult AddInvoiceItem()
        {
            try
            {
                //Build Line Item objects
                InvoiceItem lineItem = new InvoiceItem();

                //Get patient object
                int patientID = int.Parse(Request.Form["patientID"]);
                PatientRepository patientRepo = new PatientRepository();
                var patient = patientRepo.Get(patientID);

                //Get current open patient checkin
                var query = from checkin in patient.PatientCheckIns
                            where checkin.CheckOutTime == DateTime.MinValue
                            select checkin;
                PatientCheckIn openCheckIn = query.First<PatientCheckIn>();

                //Invoice Repository
                InvoiceRepository invoiceRepo = new InvoiceRepository();

                //Product Repository
                ProductRepository productRepo = new ProductRepository();

                //Service Repository
                ServiceRepository serviceRepo = new ServiceRepository();

                //Quantity
                if (Request.Form["quantity"] != "")
                {
                    lineItem.Quantity = int.Parse(Request.Form["quantity"]);
                    lineItem.Invoice = openCheckIn.Invoice;
                    lineItem.IsActive = true;

                    //Product
                    if (Request.Form["product"] != "")
                    {
                        lineItem.Product = productRepo.Get(int.Parse(Request.Form["product"]));
                        lineItem.Service = null;
                        invoiceRepo.AddLineItem(lineItem);
                        UnitOfWork.CurrentSession.Flush();
                        return Json(new
                        {
                            error = "false",
                            lineItem.Product.Name,
                            lineItem.Quantity

                        });
                    } //Service
                    else if (Request.Form["service"] != "")
                    {
                        lineItem.Service = serviceRepo.Get(int.Parse(Request.Form["service"]));
                        lineItem.Product = null;
                        invoiceRepo.AddLineItem(lineItem);
                        UnitOfWork.CurrentSession.Flush();
                        return Json(new
                        {
                            error = "false",
                            lineItem.Service.Name,
                            lineItem.Quantity
                        });
                    }
                }

                return Json(new
                {
                    error = "false"
                });
            }
            catch (Exception e)
            {
                return Json(new
                {
                    error = "true",
                    status = e.Message
                });
            }
        }
        private ActionResult Top25()
        {
            var invoices = new InvoiceRepository().GetTop25();
            var billings = new List<BillingViewModel>();

            foreach (Invoice invoice in invoices)
            {
                billings.Add(new BillingViewModel(invoice.Id));
            }

            return View(billings);
        }
        public RedirectResult SaveLineItem(int itemId, int productId, int serviceId, int quantity)
        {
            InvoiceItem lineItem = new InvoiceRepository().GetItem(itemId);
            if (serviceId == 0)
            {
                lineItem.Product = new ProductRepository().Get(productId);
            }
            if (productId == 0)
            {
                lineItem.Service = new ServiceRepository().Get(serviceId);
            }
            lineItem.Quantity = quantity;
            new InvoiceRepository().AddLineItem(lineItem);

            return new RedirectResult("/Billing/Edit/" + lineItem.Invoice.Id);
        }
        public ActionResult Service(int invoiceId)
        {
            var lineItem = new InvoiceItem();
            var repo = new InvoiceRepository();
            lineItem.Service = new ServiceRepository().Get(1);
            lineItem.Invoice = repo.Get(invoiceId);
            lineItem.IsActive = true;
            repo.AddLineItem(lineItem);

            return View(lineItem);
        }
        public ActionResult Index(FormCollection values)
        {
            string searchCriteria = values["BillingSearchTextBox"];    //Get the value entered in the 'Search' field

            //If the search field is empty then return the top 25 default results
            if (string.IsNullOrEmpty(searchCriteria))
                return Top25();

            string[] criteriaItems = searchCriteria.Split('[');
            string[] criteriaItems2 = criteriaItems[1].Split(']');

            //Get a list of invoices for this person
            IList<Invoice> invoices = new InvoiceRepository().FindByPatientId(Convert.ToInt32(criteriaItems2[0]));

            var billings = new List<BillingViewModel>();

            foreach (Invoice invoice in invoices)
            {
                billings.Add(new BillingViewModel(invoice.Id, "test"));
            }

            return View(billings);
        }