public ActionResult Edit(TermRegistrationViewModel model, string level1, string Term2, string Session3)
        //public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                foreach (TermRegistration m in model.TheTermRegistration)
                {
                    if (m.Register == true && !string.IsNullOrEmpty(m.SchoolFeesKind))
                    {
                        string         theSchoolFeeKind = m.SchoolFeesKind;
                        SchoolFeesType theFeeCost       = work.SchoolFeesTypeRepository.Get(a => a.SchoolFeesKind == theSchoolFeeKind).First();
                        m.Cost    = theFeeCost.Amount;
                        m.Session = Session3;
                        m.Term    = Term2;
                        List <TermRegistration> theReg = work2.TermRegistrationRepository.Get(a => a.StudentID == m.StudentID && a.Session == m.Session && a.Term == m.Term).ToList();

                        if (theReg.Count() > 0)
                        {
                            TermRegistration theRegi = theReg[0];
                            theRegi.Cost           = m.Cost;
                            theRegi.Register       = m.Register;
                            theRegi.SchoolFeesKind = m.SchoolFeesKind;
                            //  theRegi.
                            work2.TermRegistrationRepository.Update(theRegi);
                            // work2.TermRegistrationRepository.Delete(theRegi);
                        }
                        else
                        {
                            m.DateRegistered = DateTime.Now;
                            work.TermRegistrationRepository.Insert(m);
                        }
                    }
                }
                work2.Save();
                work.Save();
                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
        //
        // GET: /TermRegistration/Edit/5

        public ActionResult Edit(string level, string Term, string Session)
        {
            //ViewData["level"] = theItem;
            if (!string.IsNullOrEmpty(level) && !string.IsNullOrEmpty(Term) && !string.IsNullOrEmpty(Session))
            {
                List <PrimarySchoolStudent> theStudents                  = new List <PrimarySchoolStudent>();
                List <TermRegistration>     theTermRegistration          = new List <TermRegistration>();
                TermRegistrationViewModel   theStudentsTermRegistration  = new TermRegistrationViewModel();
                List <TermRegistration>     normalizedRegisteredStudents = new List <TermRegistration>();

                //   var selectedCoursesHS = new HashSet<string>(selectedCourses);
                //  var instructorCourses = new HashSet<int>(instructorToUpdate.Subjects.Select(c => c.SubjectID));

                List <TermRegistration> termRegistrations = work.TermRegistrationRepository.Get(a => a.Term == Term && a.Level == level && a.Session == Session).ToList();

                HashSet <TermRegistration> reg = new HashSet <TermRegistration>(termRegistrations);
                //    var instructorCourses = new HashSet<int>(instructor.Subjects.Select(c => c.SubjectID));

                if (!(string.IsNullOrEmpty(level)))
                {
                    theStudents = work.PrimarySchoolStudentRepository.Get(a => a.PresentLevel == level).OrderByDescending(a => a.LastName).ToList();
                }
                if (theStudents.Count() > 0)
                {
                    foreach (PrimarySchoolStudent p in theStudents)
                    {
                        bool foundValue = false;
                        foreach (TermRegistration t in reg)
                        {
                            if (t.StudentID == p.UserID)
                            {
                                foundValue = true;
                                break;
                            }
                        }
                        //check if the user is in the termregistration database
                        if (foundValue == false)
                        {
                            reg.Add(new TermRegistration {
                                StudentID = p.UserID, FirstName = p.FirstName, Level = p.PresentLevel, LastName = p.LastName, Sex = p.Sex, Owing = 0
                            });
                        }
                    }


                    foreach (TermRegistration t in reg)
                    {
                        List <SchoolFeePayment> theStudentFeesPayment = new List <SchoolFeePayment>();
                        theStudentFeesPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == t.StudentID).OrderByDescending(a => a.DatePaid).ToList();
                        decimal totalOwing = 0;
                        if (theStudentFeesPayment.Count > 0)
                        {
                            SchoolFeePayment thePayment = theStudentFeesPayment[0];
                            //check if its this current term we only wana disable for future registration
                            // string s =  thePayment.Term + thePayment.Session;
                            // if (thePayment.Term != Term && thePayment.Session != Session)

                            double paymentRepository = SessionHelper.FindLager(thePayment.Session, thePayment.Term);

                            double thisTermPayment = SessionHelper.FindLager(Session, Term);
                            if (thePayment.Term != Term && paymentRepository < thisTermPayment)
                            {
                                totalOwing = 0;
                                // foreach (SchoolFeePayment s in theStudentFeesPayment)
                                // if (theStudentFeesPayment.Count > 0)
                                // {
                                // SchoolFeePayment sf = theStudentFeesPayment.OrderByDescending(a => a.DatePaid).First();
                                // totalOwing = sf.Owing;
                                totalOwing = thePayment.Owing;
                                //}
                            }
                        }

                        //get the last registration to find out if there where payments
                        string                  lastTerm                      = null;
                        string                  lastSession                   = null;
                        string                  lastLevel                     = null;
                        TermRegistration        theLastRegisteredTerm         = new TermRegistration();
                        List <TermRegistration> pastRegistrationForCurrentGuy = new List <TermRegistration>();
                        // decimal lastTermCost = 0;

                        // that means he has registered for this current term if CurrentRegistrationForCurrentGuy > 0
                        List <TermRegistration> currentRegistrationForCurrentGuy = work.TermRegistrationRepository.Get(a => a.Term == Term && a.Level == level && a.Session == Session && a.StudentID == t.StudentID && t.Register == true).OrderByDescending(a => a.DateRegistered).ToList();
                        if (currentRegistrationForCurrentGuy.Count > 0)
                        {
                            TermRegistration theCurrentReg = currentRegistrationForCurrentGuy[0];
                            pastRegistrationForCurrentGuy = work.TermRegistrationRepository.Get(a => a.StudentID == t.StudentID && a.DateRegistered < theCurrentReg.DateRegistered).OrderByDescending(a => a.DateRegistered).ToList();
                        }
                        else
                        {
                            pastRegistrationForCurrentGuy = work.TermRegistrationRepository.Get(a => a.StudentID == t.StudentID).OrderByDescending(a => a.DateRegistered).ToList();
                        }


                        if (pastRegistrationForCurrentGuy.Count() > 0)
                        {
                            theLastRegisteredTerm = pastRegistrationForCurrentGuy[0];
                            lastTerm    = theLastRegisteredTerm.Term;
                            lastSession = theLastRegisteredTerm.Session;
                            lastLevel   = theLastRegisteredTerm.Level;
                            // lastTermCost = theLastRegisteredTerm.Cost;


                            //get if there were payments for last term
                            List <SchoolFeePayment> theLastPayments = new List <SchoolFeePayment>();
                            theLastPayments = work.SchoolFeePaymentRepository.Get(a => a.Term == lastTerm && a.StudentID == t.StudentID && a.Session == lastSession && a.Level == lastLevel).ToList();

                            if (theLastPayments.Count() > 0)
                            {
                            }
                            else
                            {
                                //no payment for last term
                                totalOwing = theLastRegisteredTerm.Cost;
                            }
                        }
                        //(string session, string term)
                        //  int sessionToQuery = Models.SessionHelper.FindLager(lastTerm, lastTerm);

                        //(string level, string Term, string Session)

                        // int currentSession = Models.SessionHelper.FindLager(Term, Session);

                        normalizedRegisteredStudents.Add(new TermRegistration {
                            StudentID = t.StudentID, FirstName = t.FirstName, Level = t.Level, LastName = t.LastName, Sex = t.Sex, Owing = totalOwing, SchoolFeesKind = t.SchoolFeesKind, Cost = t.Cost, Session = t.Session, Term = t.Term, DateRegistered = t.DateRegistered, Register = t.Register
                        });
                    }

                    // theTermRegistration.Add(new TermRegistration { StudentID = p.UserID, FirstName = p.FirstName, Level = p.PresentLevel, LastName = p.LastName, Sex = p.Sex });
                    // }
                }

                theStudentsTermRegistration.TheTermRegistration = normalizedRegisteredStudents;// = theTermRegistration;


                // return View();
                return(View("Edit", theStudentsTermRegistration));
            }
            else
            {
                TermRegistrationViewModel theStudentsTermRegistration = new TermRegistrationViewModel();
                return(View("Edit", theStudentsTermRegistration));
            }
        }
        public ActionResult ViewYourFees(string id, string term, string TheSchoolFeesType, string session)
        {
            string theTerm = "";

            if (term == "1")
            {
                theTerm      = "First";
                ViewBag.Term = "First";
            }
            if (term == "2")
            {
                theTerm      = "Second";
                ViewBag.Term = "Second";
            }

            if (term == "3")
            {
                theTerm      = "Third";
                ViewBag.Term = "Third";
            }

            ViewBag.session = session;

            ViewBag.ParentStudent = "ParentStudent";

            Int32                   stuentID             = Convert.ToInt32(Models.Encript.DecryptString(id, true));
            TermRegistration        theTermRegistration  = new TermRegistration();
            List <TermRegistration> theTermRegistrations = work.TermRegistrationRepository.Get(a => a.Session == session && a.Term == theTerm && a.StudentID == stuentID).ToList();

            //  List<TermRegistration> theTermRegistrations = work.TermRegistrationRepository.Get(a => a.Session == session && a.Term == theTerm && a.StudentID == stuentID).ToList();
            List <SchoolFeePayment> theStudentFeesPaymentLast10Transactions = work.SchoolFeePaymentRepository.Get(a => a.StudentID == stuentID).OrderByDescending(a => a.DatePaid).Take(10).OrderBy(a => a.DatePaid).ToList();

            ViewBag.Last10Transactions = theStudentFeesPaymentLast10Transactions;
            if (theTermRegistrations.Count() > 0)
            {
                theTermRegistration      = theTermRegistrations[0];
                ViewBag.TermRegistration = theTermRegistration;

                ViewBag.TheSchoolFeesType = theTermRegistration.SchoolFeesKind;
                ViewBag.session           = theTermRegistration.Session;
                //theTermRegistration.Cost =

                PrimarySchoolStudent theStudent = work.PrimarySchoolStudentRepository.Get(a => a.UserID == stuentID).First();
                ViewBag.Class = theStudent.LevelType;
                ViewBag.Name  = theStudent.LastName.ToUpper();
                //List<SchoolFeePayment> theStudentFeesPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == theStudent.UserID && a.Level.Equals(theStudent.PresentLevel) && a.Term.Equals(term)).ToList();

                List <SchoolFeePayment> theStudentFeesPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == theStudent.UserID).OrderBy(a => a.DatePaid).Take(20).ToList();

                List <SchoolFeePayment> theStudentFeesForThisStudentPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == theStudent.UserID && a.Session == session && a.Term == theTerm).OrderBy(a => a.DatePaid).Take(10).ToList();

                //check if the student is owing for current term
                List <SchoolFeePayment> theStudentFeesForStudentPaymentForThisTerm = work.SchoolFeePaymentRepository.Get(a => a.StudentID == theStudent.UserID && a.Session == session && a.Term == theTerm).OrderByDescending(a => a.DatePaid).ToList();

                // List<TermRegistration> theLastStudentRegistration = work.TermRegistrationRepository.Get(a=>a.)

                //ViewData["SchoolFeesKind"] = theItem;
                List <SchoolFeePayment> owingPayment = new List <SchoolFeePayment>();

                decimal totalOwing = 0;
                // foreach (SchoolFeePayment s in theStudentFeesPayment)
                // if (theStudentFeesPayment.Count > 0)
                if (theStudentFeesForStudentPaymentForThisTerm.Count > 0)
                {
                    //  SchoolFeePayment p = theStudentFeesPayment.OrderByDescending(a => a.DatePaid).First();
                    SchoolFeePayment p = theStudentFeesForStudentPaymentForThisTerm.OrderByDescending(a => a.DatePaid).First();
                    totalOwing = p.Owing;
                    if (totalOwing > 0)
                    {
                        owingPayment.Add(p);
                    }
                }


                ViewBag.totalOwing            = totalOwing;
                ViewBag.Last10Transaction     = theStudentFeesForThisStudentPayment;
                ViewBag.theCurrentTermPayment = theStudentFeesForThisStudentPayment.Count();
                ViewBag.Cost  = theTermRegistration.Cost;// theType.Amount;
                ViewBag.Owing = owingPayment;
                return(View("Edit", new SchoolFeePayment()
                {
                    Level = theStudent.PresentLevel, StudentID = theStudent.UserID, Term = term
                }));
            }
            else
            {
                return(View("Edit"));
            }
        }
        public ActionResult Edit(SchoolFeePayment model, decimal cost, string TheSchoolFeesType, string term, string session)
        {
            try
            {
                //  Int32 stuentID = Convert.ToInt32(Models.Encript.DecryptString(id, true));
                string theTerm = "";
                model.DatePaid          = DateTime.Now;
                model.TheSchoolFeesType = TheSchoolFeesType;
                model.EnteredBy         = User.Identity.Name;
                if (term == "1")
                {
                    theTerm    = "First";
                    model.Term = "First";
                }
                if (term == "2")
                {
                    theTerm    = "Second";
                    model.Term = "Second";
                }

                if (term == "3")
                {
                    theTerm    = "Second";
                    model.Term = "Third";
                }
                TermRegistration theTermRegistration = work2.TermRegistrationRepository.Get(a => a.Session == session && a.Term == theTerm && a.StudentID == model.StudentID).First();
                model.TheSchoolFeesType = theTermRegistration.SchoolFeesKind;

                if (ModelState.IsValid)
                {
                    int theStudentID = model.StudentID;
                    List <SchoolFeePayment> theStudentFeesPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == theStudentID).OrderByDescending(a => a.DatePaid).ToList();

                    if (theStudentFeesPayment.Count > 0)
                    {
                        //take out the very last payment
                        SchoolFeePayment p = theStudentFeesPayment.First();

                        if (p.Owing == 0)
                        {
                            model.Owing = (model.Cost - model.Paid);
                        }
                        if (p.Owing < 0)
                        {
                            model.Owing = (p.Owing + model.Cost) - (model.Paid);
                        }
                        if (p.Owing > 0)
                        {
                            model.Owing = (p.Owing) - (model.Paid);
                        }
                        //totalOwing = p.Owing;
                    }
                    else
                    {
                        model.Owing = model.Cost - model.Paid;
                    }

                    work.SchoolFeePaymentRepository.Insert(model);
                    work.Save();
                }
                // TODO: Add update logic here
                //  model.

                if (User.Identity.Name != "5000001")
                {
                    AuditTrail audit = new AuditTrail {
                        Date = DateTime.Now, Action = "Updated school Fees for User ID -:" + model.StudentID, UserID = User.Identity.Name
                    };
                    work.AuditTrailRepository.Insert(audit);
                    work.Save();
                }

                return(RedirectToAction("Index", "SchoolFeePayment"));
            }
            catch
            {
                return(View("Edit"));
            }
        }
        //public ActionResult Index(string levelString, string session, string arm, string DateFrom, string DateTo, string ApprovedString, string searchString, string SexString, string LevelString, string StudentIDString, int? page
        //    , string paidschoolfess, string term, bool? printQuery)
        //{
        public ActionResult Index(string levelString, string session, string arm, string DateFrom, string DateTo, string ApprovedString, string searchString, string SexString, string LevelString, string StudentIDString, int?page
                                  , string paidschoolfess, string term)
        {
            bool?thePrintQuery = null;

            //  thePrintQuery = printQuery;
            if (User.IsInRole("Parent"))
            {
                List <TermRegistration> studentList = new List <TermRegistration>();
                int    theParentUserName            = Convert.ToInt32(User.Identity.Name);
                Parent theParent = work.ParentRepository.Get(a => a.UserID == theParentUserName).First();


                List <PrimarySchoolStudent> theStudents = work.PrimarySchoolStudentRepository.Get(a => a.ParentID == theParent.ParentID).ToList();

                int counter = 0;
                foreach (PrimarySchoolStudent s in theStudents)
                {
                    if (counter == 0)
                    {
                        theParent.StudentIDOne = Convert.ToString(s.UserID);
                    }
                    if (counter == 1)
                    {
                        theParent.StudentIDTwo = Convert.ToString(s.UserID);
                    }

                    if (counter == 2)
                    {
                        theParent.StudentIDThree = Convert.ToString(s.UserID);
                    }
                    if (counter == 3)
                    {
                        theParent.StudentIDFour = Convert.ToString(s.UserID);
                    }
                    if (counter == 4)
                    {
                        theParent.StudentIDFive = Convert.ToString(s.UserID);
                    }
                    counter = counter + 1;
                }



                if (!(string.IsNullOrEmpty(theParent.StudentIDOne)))
                {
                    int theStudentID = Convert.ToInt32(theParent.StudentIDOne);
                    //List<TermRegistration> theList = work.TermRegistrationRepository.Get(a => a.StudentID == theStudentID && a.Term == term && a.Session == session).ToList();
                    List <TermRegistration> theList = work.TermRegistrationRepository.Get(a => a.StudentID == theStudentID).ToList();
                    if (theList.Count > 0)
                    {
                        studentList.Add(theList[0]);
                    }
                }
                if (!(string.IsNullOrEmpty(theParent.StudentIDTwo)))
                {
                    int theStudentID = Convert.ToInt32(theParent.StudentIDTwo);
                    List <TermRegistration> theList = work.TermRegistrationRepository.Get(a => a.StudentID == theStudentID).ToList();
                    if (theList.Count > 0)
                    {
                        studentList.Add(theList[0]);
                    }
                }
                if (!(string.IsNullOrEmpty(theParent.StudentIDThree)))
                {
                    int theStudentID = Convert.ToInt32(theParent.StudentIDThree);
                    List <TermRegistration> theList = work.TermRegistrationRepository.Get(a => a.StudentID == theStudentID).ToList();
                    if (theList.Count > 0)
                    {
                        studentList.Add(theList[0]);
                    }
                }

                if (!(string.IsNullOrEmpty(theParent.StudentIDFour)))
                {
                    int theStudentID = Convert.ToInt32(theParent.StudentIDFour);
                    List <TermRegistration> theList = work.TermRegistrationRepository.Get(a => a.StudentID == theStudentID).ToList();
                    if (theList.Count > 0)
                    {
                        studentList.Add(theList[0]);
                    }
                }

                if (!(string.IsNullOrEmpty(theParent.StudentIDFive)))
                {
                    int theStudentID = Convert.ToInt32(theParent.StudentIDFive);
                    List <TermRegistration> theList = work.TermRegistrationRepository.Get(a => a.StudentID == theStudentID).ToList();
                    if (theList.Count > 0)
                    {
                        studentList.Add(theList[0]);
                    }
                }

                List <SelectListItem> theItem = new List <SelectListItem>();
                theItem.Add(new SelectListItem()
                {
                    Text = "None", Value = ""
                });

                foreach (var level in studentList)
                {
                    theItem.Add(new SelectListItem()
                    {
                        Text = level.StudentID.ToString(), Value = level.StudentID.ToString()
                    });
                }

                ViewData["studentIds"] = theItem;
                //  students = studentList;
            }



            if (term == "First")
            {
                ViewBag.Term = "1";
            }
            if (term == "Second")
            {
                ViewBag.Term = "2";
            }

            if (term == "Third")
            {
                ViewBag.Term = "3";
            }


            decimal totalAmount = 0;

            if (Request.HttpMethod == "GET")
            {
                // searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var students = from s in work.TermRegistrationRepository.Get(a => a.Term == term)
                           select s;

            // var students = from s in work.TermRegistrationRepository.Get()
            //               select s;

            if (!String.IsNullOrEmpty(term))
            {
                // students = from s in work.TermRegistrationRepository.Get()
                // select s;
                students = students.Where(s => s.Term.Equals(term));
            }

            if (!String.IsNullOrEmpty(session))
            {
                students = students.Where(s => s.Session.Equals(session));
            }

            if (!String.IsNullOrEmpty(LevelString))
            {
                students = students.Where(s => s.Level == LevelString);
            }


            if (!String.IsNullOrEmpty(StudentIDString))
            {
                int theID = Convert.ToInt32(StudentIDString);
                students = students.Where(s => s.StudentID == theID);
            }



            int pageSize   = 50;
            int pageNumber = (page ?? 1);

            if (!String.IsNullOrEmpty(paidschoolfess))
            {
                if (paidschoolfess == "Owing")
                {
                    var payments = from s in work.SchoolFeePaymentRepository.Get()
                                   select s;
                    if (!String.IsNullOrEmpty(term))
                    {
                        payments = payments.Where(s => s.Term.Equals(term));
                    }

                    if (!String.IsNullOrEmpty(session))
                    {
                        payments = payments.Where(s => s.Session.Equals(session));
                    }

                    if (!String.IsNullOrEmpty(LevelString))
                    {
                        payments = payments.Where(s => s.Level == LevelString);
                    }


                    if (!String.IsNullOrEmpty(StudentIDString))
                    {
                        int theID = Convert.ToInt32(StudentIDString);
                        payments = payments.Where(s => s.StudentID == theID);
                    }

                    // payments = payments.Where(a => a.Owing > 0).OrderBy(a => a.StudentID);//.OrderBy(a => a.DatePaid);
                    TermRegistration theStudentWhoRegistered = new TermRegistration();

                    List <TermRegistration> theOwingStudents = new List <TermRegistration>();
                    List <SchoolFeePayment> theRegisteredSchoolFeePyament = new List <SchoolFeePayment>();

                    SchoolFeePayment thePayment = new SchoolFeePayment();
                    TermRegistration tR         = new TermRegistration();
                    SchoolFeePayment sF         = new SchoolFeePayment();

                    List <TermRegistration> theRegisteredStudents = new List <TermRegistration>();

                    theRegisteredStudents = students.ToList();



                    foreach (TermRegistration t in theRegisteredStudents)
                    {
                        List <SchoolFeePayment> thisGuyPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == t.StudentID && a.Term == term).OrderByDescending(a => a.DatePaid).ToList();
                        if (thisGuyPayment.Count() > 0)
                        {
                            SchoolFeePayment theCurrentPayment = thisGuyPayment[0];
                            if (theCurrentPayment.Owing > 0)
                            {
                                totalAmount += theCurrentPayment.Owing;
                                theOwingStudents.Add(new TermRegistration {
                                    DateRegistered = t.DateRegistered, Owing = t.Owing, TermRegistrationID = t.TermRegistrationID, Register = t.Register, Sex = t.Sex, LastName = t.LastName, FirstName = t.FirstName, SchoolFeesKind = theCurrentPayment.TheSchoolFeesType, Cost = theCurrentPayment.Cost, Level = theCurrentPayment.Level, Session = theCurrentPayment.Session, Term = theCurrentPayment.Term, StudentID = theCurrentPayment.StudentID,
                                });
                            }
                        }
                    }


                    students = theOwingStudents;
                }
                else
                {
                    if (Convert.ToBoolean(paidschoolfess) == true)
                    {
                        List <SchoolFeePayment> listOfPayments = work.SchoolFeePaymentRepository.Get().Where(a => a.Owing <= 0).OrderBy(a => a.DatePaid).ToList();
                        List <TermRegistration> theRegisteredStuentWhoHavePaid = new List <TermRegistration>();
                        foreach (TermRegistration r in students)
                        {
                            foreach (SchoolFeePayment l in listOfPayments)
                            {
                                if (r.StudentID == l.StudentID)
                                {
                                    totalAmount += l.Cost;
                                    theRegisteredStuentWhoHavePaid.Add(r);
                                    break;
                                }
                            }
                            // if(s.StudentID ==)theRegisteredStuentWhoHavePaid
                        }
                        students = theRegisteredStuentWhoHavePaid;
                    }
                    else
                    {
                        List <TermRegistration> theNeverPaidStuddentStudents = new List <TermRegistration>();
                        List <TermRegistration> theRegisteredStudents        = new List <TermRegistration>();

                        theRegisteredStudents = students.ToList();
                        theRegisteredStudents = students.ToList();



                        foreach (TermRegistration t in theRegisteredStudents)
                        {
                            List <SchoolFeePayment> thisGuyPayment = new List <SchoolFeePayment>();
                            //   List<SchoolFeePayment> thisGuyPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == t.StudentID && a.Term == term).OrderByDescending(a => a.DatePaid).ToList();
                            if (string.IsNullOrEmpty(session))
                            {
                                thisGuyPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == t.StudentID && a.Term == term).OrderByDescending(a => a.DatePaid).ToList();
                            }

                            if (!(string.IsNullOrEmpty(session)))
                            {
                                thisGuyPayment = work.SchoolFeePaymentRepository.Get(a => a.StudentID == t.StudentID && a.Session == session && a.Term == term).OrderByDescending(a => a.DatePaid).ToList();
                            }

                            if (thisGuyPayment.Count() == 0)
                            {
                                totalAmount += t.Cost;
                                theNeverPaidStuddentStudents.Add(new TermRegistration {
                                    LastName = t.LastName, FirstName = t.FirstName, SchoolFeesKind = t.SchoolFeesKind, Cost = t.Cost, Level = t.Level, Session = t.Session, Term = t.Term, StudentID = t.StudentID
                                });
                            }
                        }
                        students = theNeverPaidStuddentStudents;
                    }
                }
            }

            if (User.IsInRole("Parent"))
            {
                //ViewBag.Term = "1";
                int    theParentUserName = Convert.ToInt32(User.Identity.Name);
                Parent theParent         = work.ParentRepository.Get(a => a.UserID == theParentUserName).First();

                List <TermRegistration> studentList = new List <TermRegistration>();

                int theStudentID = 0;
                if (!(string.IsNullOrEmpty(StudentIDString)))
                {
                    theStudentID = Convert.ToInt32(StudentIDString);
                }
                List <TermRegistration> theList = new List <TermRegistration>();
                theList  = work.TermRegistrationRepository.Get(a => a.StudentID == theStudentID && a.Term == term && a.Session == session && a.Level == LevelString).ToList();
                students = theList;
            }

            if (User.IsInRole("Student"))
            {
                //  ViewBag.Term = "1";
                int theStudentUserName = Convert.ToInt32(User.Identity.Name);
                List <PrimarySchoolStudent> theStudent = new List <PrimarySchoolStudent>();
                List <TermRegistration>     theList    = new List <TermRegistration>();
                theStudent = work.PrimarySchoolStudentRepository.Get(a => a.UserID == theStudentUserName).ToList();

                //  List<TermRegistration> studentList = new List<TermRegistration>();
                if ((theStudent.Count > 0))
                {
                    int theStudentID = Convert.ToInt32(theStudent[0].UserID);
                    theList = work.TermRegistrationRepository.Get(a => a.StudentID == theStudentID && a.Level == LevelString && a.Term == term && a.Session == session).ToList();
                    //if (theList.Count > 0)
                    //{
                    //   theList.Add(theList[0]);
                    //  }
                }
                students = theList;
            }

            if (thePrintQuery != null && students.Count() > 0)
            {
                thePrintQuery = null;
                StringWriter oStringWriter1 = new StringWriter();
                Document     itextDoc       = new Document(PageSize.LETTER);
                itextDoc.Open();
                Response.ContentType = "application/pdf";
                PrintResult print = new PrintResult();
                // Set up the document and the MS to write it to and create the PDF writer instance
                MemoryStream ms = new MemoryStream();
                //Document document = new Document(PageSize.A3.Rotate());
                Document  document = new Document(PageSize.A4);
                PdfWriter writer   = PdfWriter.GetInstance(document, ms);

                // Open the PDF document
                document.Open();

                Document thedoc = new SchoolFeesPrinting().FeesPrinting(students.ToList(), ref oStringWriter1, ref document);
                iTextSharp.text.pdf.draw.VerticalPositionMark seperator = new iTextSharp.text.pdf.draw.LineSeparator();
                seperator.Offset = -6f;

                document.Close();

                // Hat tip to David for his code on stackoverflow for this bit
                // http://stackoverflow.com/questions/779430/asp-net-mvc-how-to-get-view-to-generate-pdf
                byte[]       file   = ms.ToArray();
                MemoryStream output = new MemoryStream();
                output.Write(file, 0, file.Length);
                output.Position = 0;

                HttpContext.Response.AddHeader("content-disposition", "attachment; filename=SchoolFees.pdf");
                return(new FileStreamResult(output, "application/pdf")); //new FileStreamResult(output, "application/pdf");

                // return View(new FileStreamResult(output, "application/pdf"));
            }


            ViewBag.TotalAmount = totalAmount;
            ViewBag.Count       = students.Count();
            return(View(students.ToPagedList(pageNumber, pageSize)));
            // }
            // else
            // {
            //   return View("Index3", students.ToPagedList(pageNumber, pageSize));
            // }
        }