public ActionResult AddEditProject(int clientID, int projectID, string projectName)
        {
            try{
            var db = new ExpenseDb();

            if (projectID == 0)
            {
                // add a new project
                var project = new Project { ClientID = clientID, ProjectName = projectName };

                db.Project.Add(project);
                db.SaveChanges();
            }
            else// this is an edit of an existing Client
            {
               // db.Clients.Where(c => c.Id == intClientID).FirstOrDefault().ClientName = clientName;
                db.Project.Where(p => p.Id == projectID).FirstOrDefault().ProjectName = projectName;
                //db change tracker
                db.SaveChanges();

            }

            // get list of Projects
            var projects = getProjects(clientID);

            return PartialView("_ProjectTable", projects);
               }
               catch (Exception ex)
               {

               return ThrowJSONError(ex);
               }
        }
        public ActionResult AddEditReport(ReportViewModel model)
        {
            if (ModelState.IsValid)
            {
                // do good stuff
            }
            else
            {
                // make sure you return the bad model to the view... otherwise the model errors get wiped out
                // and your validation helpers don't work
            }
            var db = new ExpenseDb();
            if (model.ReportID > 0)
            {
                // this is an update
                //db.EstablishmentTBL.Where(e => e.Id == establishmentID).FirstOrDefault().EstablishmentName = establishmentName;
               var report= db.Reports.Where(e => e.Id == model.ReportID).FirstOrDefault();
               report.MonthYear = model.MonthYearInput;
               report.Billable = model.BillableInput;
               report.EmpName = model.EmpNameInput;
               report.EmpTitle = model.EmpTitleInput;
               report.ErNumber = model.ErNumberInput != null ? model.ErNumberInput : -1;
               report.ProjectID = model.SelectedProjectID;

                db.SaveChanges();

            }
            else {
                // this is a new report
                var report = new Report { Billable = model.BillableInput, EmpName = model.EmpNameInput, EmpTitle = model.EmpTitleInput, MonthYear = (DateTime)model.MonthYearInput, ErNumber = model.ErNumberInput != null ? model.ErNumberInput : 0, ProjectID = model.SelectedProjectID };
                db.Reports.Add(report);
                db.SaveChanges();

            }

            // add a new client
            // var client = new Client { ClientName = clientName };
            // db.Clients.Add(client);

            var reports = GetReports(db);

            model.Reports = reports;

            return PartialView("_ReportsTable", model);
        }
        public ActionResult AddEditClient(string clientID, string clientName)
        {
            try
            {
                int intClientID = Convert.ToInt32(clientID);
                var db = new ExpenseDb();

                if (intClientID <= 0)
                {
                    // add a new client
                    var client = new Client { ClientName = clientName };
                    db.Clients.Add(client);
                    db.SaveChanges();
                }
                else// this is an edit of an existing Client
                {
                    db.Clients.Where(c => c.Id == intClientID).FirstOrDefault().ClientName = clientName;
                    //db change tracker
                    db.SaveChanges();

                    // how to update using Fluent syntax
                   // db.Clients.Add(clinet);
                   // db.SaveChanges();

                }

                // get list of Clients
                var clients = GetClients();

                return PartialView("_ClientTable", clients);

            }
            catch (Exception ex)
            {

                return ThrowJSONError(ex);
            }
        }
        /// <summary>
        /// This action will be used to send a new/eddited establishment to the DB
        /// </summary>
        /// <param name="establishmentName"></param>
        /// <param name="establishmentID"></param>
        /// <returns></returns>
        public ActionResult AddEditEstablishment(string establishmentName, int establishmentID)
        {
            var db = new ExpenseDb();

            if (establishmentID <= 0) /// we add a new est to the DB
            {
                var establishment = new Establishment { EstablishmentName = establishmentName };
                db.EstablishmentTBL.Add(establishment);
                db.SaveChanges();

            }
            else // we edit the Est on the DB
            {
                db.EstablishmentTBL.Where(e => e.Id == establishmentID).FirstOrDefault().EstablishmentName = establishmentName;
                db.SaveChanges();

            }

            // here we return an updated list of the Establishments
            EstablishmentViewModel model = GetEstablishments(db);

            return PartialView("_EstablishmentTable", model);
        }
        public ActionResult DeleteClient(int clientID)
        {
            var db = new ExpenseDb();
            var client = db.Clients.Find(clientID);
            db.Clients.Remove(client);
            db.SaveChanges();

            // get list of Clients
            var clients = GetClients();

            return PartialView("_ClientTable", clients);
        }
        public ActionResult DeleteProject(int projectID, int clientID)
        {
            var db = new ExpenseDb();
            var project = db.Project.Find(projectID);
            db.Project.Remove(project);
            db.SaveChanges();

            // get list of Clients
            var projects = getProjects(clientID);

            return PartialView("_ProjectTable", projects);
        }
        /// <summary>
        /// This action will remove a given Est. from the DB 
        /// </summary>
        /// <param name="establishmentID"></param>
        /// <returns> an updated list of the EST. </returns>
        public ActionResult DeleteEstablishment(int establishmentID)
        {
            var db = new ExpenseDb();

            var establishment = db.EstablishmentTBL.Find(establishmentID);
            db.EstablishmentTBL.Remove(establishment);
            db.SaveChanges();

            // here we return an updated list of the Establishments
            EstablishmentViewModel model = GetEstablishments(db);

            return PartialView("_EstablishmentTable", model);
        }
        /// <summary>
        /// this is the method removes a given report from the DB
        /// </summary>
        /// <param name="ReportID"></param>
        /// <returns></returns>
        public ActionResult DeleteReport(int reportID)
        {
            var model = new ReportViewModel();
            var db = new ExpenseDb();

            //delete the give report by report ID
            var report = db.Reports.Find(reportID);
            db.Reports.Remove(report);
            db.SaveChanges();

            var reports = GetReports(db);

            model.Reports = reports;
            return PartialView("_ReportsTable", model);
        }
        ///////------------------------------------------- Establishment Actions : 
        public ActionResult AddEstablishment(string establishmentName)
        {
            // in this instance we dont have Access to the Id's so we will only compare the stirngs client side
            // so we will always be creating a new Establishment

            var db = new ExpenseDb();

                var establishment = new Establishment { EstablishmentName = establishmentName };
                db.EstablishmentTBL.Add(establishment);

                db.SaveChanges();

            // here we return an updated list of the Establishments
            var establishments = GetEstablishments(db);

            var viewModel = new ExpenseDetailsViewModel();
            viewModel.Establishments = establishments;

            return PartialView("_EstablishmentTxt",viewModel);
        }
        public ActionResult AddEditExpense(ExpenseDetailsViewModel viewModel)
        {
            var db = new ExpenseDb();
            // get expense ID if < 0 then it is a new report
            if (viewModel.ExpenseInput.ExpenseID <= 0)
            {
                // this is a new expense
                var expense = new ExpenseDetail
                {
                    DateIncurred = viewModel.ExpenseInput.DateIncurred,
                    ReceiptEntry = viewModel.ExpenseInput.ReceiptEntry,
                    NumberOfGuest = viewModel.ExpenseInput.NumberOfGuest,
                    GuestNames = viewModel.ExpenseInput.GuestNames,
                    Establishment = viewModel.ExpenseInput.Establishment,
                    Expensecategory = viewModel.ExpenseInput.Expensecategory,
                    Reason = viewModel.ExpenseInput.Reason,
                    Amount = viewModel.ExpenseInput.Amount,
                    AmountPerPerson = viewModel.ExpenseInput.AmountPerPerson,
                    ReportID = viewModel.ExpenseInput.ReportID,
                    Notes =viewModel.ExpenseInput.Notes

                };
                db.ExpenseDetail.Add(expense);
                db.SaveChanges();

            }
            else // it is an edit to an existing report
            {
                // update an expense
                //db.ExpenseDetail.Where(e => e.Id == viewModel.ExpenseInput.ExpenseID).FirstOrDefault()
               // var expense = db.ExpenseDetail.Find(viewModel.ExpenseInput.ExpenseID);

               // db.Clients.Where(c => c.Id == intClientID).FirstOrDefault()
                var expense = db.ExpenseDetail.Where(e => e.Id == viewModel.ExpenseInput.ExpenseID).FirstOrDefault();
                expense.DateIncurred = viewModel.ExpenseInput.DateIncurred;
                expense.ReceiptEntry = viewModel.ExpenseInput.ReceiptEntry;
                expense.NumberOfGuest = viewModel.ExpenseInput.NumberOfGuest;
                expense.GuestNames = viewModel.ExpenseInput.GuestNames;
                expense.Establishment = viewModel.ExpenseInput.Establishment;
                expense.Expensecategory = viewModel.ExpenseInput.Expensecategory;
                expense.Reason = viewModel.ExpenseInput.Reason;
                expense.Notes = viewModel.ExpenseInput.Notes;
                expense.Amount = viewModel.ExpenseInput.Amount;
                expense.AmountPerPerson = viewModel.ExpenseInput.AmountPerPerson;
                expense.ReportID = viewModel.ExpenseInput.ReportID;
                //db.ExpenseDetail.Add(expense); when doing an update no need to do the add

                db.SaveChanges();// just by doing the saveChanges it knows it is an update

                    //.ClientName = clientName;
                //db change tracker
                db.SaveChanges();

                //db.Clients.Where(c => c.Id == intClientID).FirstOrDefault().ClientName = clientName;
                //db change tracker
                db.SaveChanges();

            }

            var establishments = GetEstablishments(db);// gets a list of Establishemnts

            var report = GetReportDetails(viewModel.ExpenseInput.ReportID, db);// gets the details of a single reports

            var expenses = GetReportExpenses(viewModel.ExpenseInput.ReportID, db);

            // ViewBag.ListOfEst = establishments;// this is putting a list of establishments in the View bag to then be retrieved from the JS

            viewModel.Establishments = establishments;
            viewModel.Report = report;
            viewModel.ListOfExpenses = expenses;
            if (report == null)
            {
                return View("Error");
            }
            else
            {
                return PartialView("_ExpenseDetails", viewModel);
                //return View("Index",viewModel);
            }
        }
        public ActionResult DeleteExpense(int expenseID, int reportID)
        {
            var viewModel = new ExpenseDetailsViewModel();

            var db = new ExpenseDb();

            // delete expense
            var expense = db.ExpenseDetail.Find(expenseID);
            db.ExpenseDetail.Remove(expense);
            db.SaveChanges();

            // re-render Partial
            var establishments = GetEstablishments(db);// gets a list of Establishemnts

            var report = GetReportDetails(reportID, db);// gets the details of a single reports

            var expenses = GetReportExpenses(reportID, db);

            // ViewBag.ListOfEst = establishments;// this is putting a list of establishments in the View bag to then be retrieved from the JS

            viewModel.Establishments = establishments;
            viewModel.Report = report;
            viewModel.ListOfExpenses = expenses;
            if (report == null)
            {
                return View("Error");
            }
            else {
                return PartialView("_ExpenseDetails", viewModel);
                //return View("Index", viewModel);
            }
        }