public ActionResult Edit2(int id)
        {
            try
            {
                SchoolFeePayment theFeetoBeApproved = work.SchoolFeePaymentRepository.GetByID(id);
                theFeetoBeApproved.Approved = true;
                work.SchoolFeePaymentRepository.Update(theFeetoBeApproved);

                SchoolFeesAccount thePayment = new SchoolFeesAccount();

                thePayment.Amount               = theFeetoBeApproved.Paid;
                thePayment.ApprovedBy           = User.Identity.Name;
                thePayment.BankDraftNumber      = theFeetoBeApproved.BankDraftNumber;
                thePayment.ChequeNumber         = theFeetoBeApproved.ChequeNumber;
                thePayment.DateApproved         = DateTime.Now;
                thePayment.EnteredBy            = theFeetoBeApproved.EnteredBy;
                thePayment.PaidDate             = theFeetoBeApproved.DatePaid;
                thePayment.POSTransactionNumber = theFeetoBeApproved.POSTransactionNumber;
                thePayment.StudentID            = theFeetoBeApproved.StudentID;
                thePayment.TellerNumber         = theFeetoBeApproved.TellerNumber;
                thePayment.TransactionMethod    = theFeetoBeApproved.PaymentMethod;
                thePayment.TransactionType      = "Credit";
                thePayment.Session              = theFeetoBeApproved.Session;
                thePayment.Level = theFeetoBeApproved.Level;
                work.SchoolFeesAccountRepository.Insert(thePayment);



                SchoolAccount thePayment1 = new SchoolAccount();

                thePayment1.Amount               = theFeetoBeApproved.Paid;
                thePayment1.ApprovedBy           = User.Identity.Name;
                thePayment1.BankDraftNumber      = theFeetoBeApproved.BankDraftNumber;
                thePayment1.ChequeNumber         = theFeetoBeApproved.ChequeNumber;
                thePayment1.DateApproved         = DateTime.Now;
                thePayment1.EnteredBy            = theFeetoBeApproved.EnteredBy;
                thePayment1.PaidDate             = theFeetoBeApproved.DatePaid;
                thePayment1.POSTransactionNumber = theFeetoBeApproved.POSTransactionNumber;
                thePayment1.StudentID            = theFeetoBeApproved.StudentID;
                thePayment1.TellerNumber         = theFeetoBeApproved.TellerNumber;
                thePayment1.TransactionMethod    = theFeetoBeApproved.PaymentMethod;
                thePayment1.TransactionType      = "Credit";
                thePayment1.Session              = theFeetoBeApproved.Session;
                thePayment1.Level   = theFeetoBeApproved.Level;
                thePayment1.Balance = new SchoolAccountBalanceHelper().getSchoolAccountBalance(thePayment1.Amount);
                work.SchoolAccountRepository.Insert(thePayment1);
                work.Save();
                // TODO: Add update logic here

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
        //Edit Entry
        public ActionResult Edit(SchoolFeePayment model)
        {
            try
            {
                //  model.Owing =
                int paymentID = model.SchoolFeePaymentID;

                UnitOfWork       work2                  = new UnitOfWork();
                SchoolFeePayment yetToBeUpdatedFee      = work2.SchoolFeePaymentRepository.GetByID(paymentID);
                decimal          theDifferenceinPayment = (model.Paid - yetToBeUpdatedFee.Paid);

                //update the owing
                // model.Owing = model.Owing - theDifferenceinPayment;

                model.Owing = (yetToBeUpdatedFee.Paid - model.Paid) + model.Owing;
                work.SchoolFeePaymentRepository.Update(model);
                List <SchoolFeePayment> thePayment = work.SchoolFeePaymentRepository.Get(a => a.DatePaid > model.DatePaid && a.StudentID == model.StudentID).ToList();

                decimal theAmount = model.Owing;
                if (thePayment.Count > 0)
                {
                    foreach (var p in thePayment)
                    {
                        if (!(p == model))
                        {
                            // p.Owing = model.Owing - p.Paid;
                            p.Owing   = theAmount - p.Paid;
                            theAmount = p.Owing;
                            work.SchoolFeePaymentRepository.Update(p);
                        }
                    }
                }
                work2.Save();
                work.Save();
                // TODO: Add update logic here

                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));
            }
        }
        //
        // GET: /ApproveSchoolFeePayment/Edit/5

        public ActionResult Edit(int id)
        {
            SchoolFeePayment thePayment = work.SchoolFeePaymentRepository.GetByID(id);

            return(View(thePayment));
        }
예제 #5
0
        public Document FeesPrinting(List <TermRegistration> registration, ref StringWriter sw, ref Document itextDoc)
        {
            PdfPTable table3 = new PdfPTable(2);

            table3.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            iTextSharp.text.Image imgPDF = iTextSharp.text.Image.GetInstance(HttpRuntime.AppDomainAppPath + "\\Images\\wexford.jpg");
            imgPDF.ScaleToFit(128, 100);
            // imgPDF.ScaleToFit(200, 200);
            PdfPCell theImage = new PdfPCell(imgPDF);

            theImage.Border = iTextSharp.text.Rectangle.NO_BORDER;
            table3.AddCell(theImage);

            iTextSharp.text.Font font_heading_1 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, iTextSharp.text.Font.BOLD);

            iTextSharp.text.Font font_heading_2 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 11, iTextSharp.text.Font.BOLD);
            //  iTextSharp.text.Font font_heading_3 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLD);

            PdfPTable theSchoolNameTable = new PdfPTable(1);

            PdfPCell theWexFord = new PdfPCell(new Paragraph("WEXFORD COLLEGE", font_heading_1));

            theWexFord.Border = iTextSharp.text.Rectangle.NO_BORDER;
            // theWexFord.w

            PdfPCell theWexFordFirstGate = new PdfPCell(new Paragraph(new Paragraph("FIRST GATE OBA ILE HOUSING ESTATE, AKURE, AKURE NORTH L.G.A,   ", font_heading_1)));

            theWexFordFirstGate.Border = iTextSharp.text.Rectangle.NO_BORDER;

            PdfPCell theWexFordMoto = new PdfPCell(new Paragraph("MOTTO: INTELLIGENCE, INTEGRITY & INDUSTRY", font_heading_1));

            theWexFordMoto.Border = iTextSharp.text.Rectangle.NO_BORDER;

            theSchoolNameTable.AddCell(theWexFord);
            theSchoolNameTable.AddCell(theWexFordFirstGate);
            theSchoolNameTable.AddCell(theWexFordMoto);
            table3.AddCell(theSchoolNameTable);



            itextDoc.Add(table3);

            itextDoc.Add(new Paragraph("  "));

            itextDoc.Add(new Paragraph("  "));

            iTextSharp.text.Font font_heading_3 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLD);

            PdfPTable table4 = new PdfPTable(8);


            table4.AddCell((new Paragraph("S/N", font_heading_3)));
            table4.AddCell((new Paragraph("Name", font_heading_3)));
            table4.AddCell((new Paragraph("Sex", font_heading_3)));
            table4.AddCell((new Paragraph("Class", font_heading_3)));
            table4.AddCell((new Paragraph("Session", font_heading_3)));
            table4.AddCell((new Paragraph("School Fees Category", font_heading_3)));
            table4.AddCell((new Paragraph("School Fees  (NGN)", font_heading_3)));
            table4.AddCell((new Paragraph("Owing  (NGN)", font_heading_3)));

            int counter = 1;

            foreach (var s in registration)
            {
                // s.

                //PdfPTable table = new PdfPTable(2);
                List <SchoolFeePayment> theStudentPayments = new List <SchoolFeePayment>();
                theStudentPayments = work.SchoolFeePaymentRepository.Get(a => a.StudentID == s.StudentID).ToList();
                SchoolFeePayment theStudentPayment = new SchoolFeePayment();
                // theStudentPayment.Owing = 0;
                if (theStudentPayments.Count > 0)
                {
                    theStudentPayment = theStudentPayments.OrderByDescending(a => a.DatePaid).First();
                    // theStudentPayment.Owing = 0;
                }
                table4.DefaultCell.Border = iTextSharp.text.Rectangle.BOX;
                decimal toTalDeduction = 0;
                /// toTalDeduction = Convert.ToDecimal(s.TotalLatenessDeduction) + toTalDeduction;
                // toTalDeduction = Convert.ToDecimal(s.TotalLoan) + toTalDeduction;
                // toTalDeduction = Convert.ToDecimal(s.TotalAbscentDeduction) + toTalDeduction;

                table4.AddCell(new Paragraph(Convert.ToString(counter), font_heading_3));

                table4.AddCell(new Paragraph(s.LastName + " " + s.FirstName, font_heading_3));
                table4.AddCell(new Paragraph(s.Sex, font_heading_2));
                table4.AddCell(new Paragraph(s.Level, font_heading_2));
                table4.AddCell(new Paragraph(s.Session, font_heading_2));
                table4.AddCell(new Paragraph(s.SchoolFeesKind, font_heading_2));
                table4.AddCell(new Paragraph(Convert.ToString(s.Cost), font_heading_2));
                table4.AddCell(new Paragraph(Convert.ToString(theStudentPayment.Owing), font_heading_2));


                counter = counter + 1;
            }
            itextDoc.Add(table4);
            return(itextDoc);
        }
예제 #6
0
        public Document FeesPrinting(List<TermRegistration> registration, ref StringWriter sw, ref Document itextDoc)
        {
            PdfPTable table3 = new PdfPTable(2);
            table3.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            iTextSharp.text.Image imgPDF = iTextSharp.text.Image.GetInstance(HttpRuntime.AppDomainAppPath + "\\Images\\wexford.jpg");
            imgPDF.ScaleToFit(128, 100);
            // imgPDF.ScaleToFit(200, 200);
            PdfPCell theImage = new PdfPCell(imgPDF);
            theImage.Border = iTextSharp.text.Rectangle.NO_BORDER;
            table3.AddCell(theImage);

            iTextSharp.text.Font font_heading_1 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, iTextSharp.text.Font.BOLD);

            iTextSharp.text.Font font_heading_2 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 11, iTextSharp.text.Font.BOLD);
            //  iTextSharp.text.Font font_heading_3 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLD);

            PdfPTable theSchoolNameTable = new PdfPTable(1);

            PdfPCell theWexFord = new PdfPCell(new Paragraph("WEXFORD COLLEGE", font_heading_1));
            theWexFord.Border = iTextSharp.text.Rectangle.NO_BORDER;
            // theWexFord.w

            PdfPCell theWexFordFirstGate = new PdfPCell(new Paragraph(new Paragraph("FIRST GATE OBA ILE HOUSING ESTATE, AKURE, AKURE NORTH L.G.A,   ", font_heading_1)));
            theWexFordFirstGate.Border = iTextSharp.text.Rectangle.NO_BORDER;

            PdfPCell theWexFordMoto = new PdfPCell(new Paragraph("MOTTO: INTELLIGENCE, INTEGRITY & INDUSTRY", font_heading_1));
            theWexFordMoto.Border = iTextSharp.text.Rectangle.NO_BORDER;

            theSchoolNameTable.AddCell(theWexFord);
            theSchoolNameTable.AddCell(theWexFordFirstGate);
            theSchoolNameTable.AddCell(theWexFordMoto);
            table3.AddCell(theSchoolNameTable);

            itextDoc.Add(table3);

            itextDoc.Add(new Paragraph("  "));

            itextDoc.Add(new Paragraph("  "));

            iTextSharp.text.Font font_heading_3 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLD);

             PdfPTable table4 = new PdfPTable(8);

            table4.AddCell((new Paragraph("S/N", font_heading_3)));
            table4.AddCell((new Paragraph("Name", font_heading_3)));
            table4.AddCell((new Paragraph("Sex", font_heading_3)));
            table4.AddCell((new Paragraph("Class", font_heading_3)));
            table4.AddCell((new Paragraph("Session", font_heading_3)));
            table4.AddCell((new Paragraph("School Fees Category", font_heading_3)));
            table4.AddCell((new Paragraph("School Fees  (NGN)", font_heading_3)));
            table4.AddCell((new Paragraph("Owing  (NGN)", font_heading_3)));

            int counter = 1;

            foreach (var s in registration)
            {
              // s.

                //PdfPTable table = new PdfPTable(2);
                List<SchoolFeePayment> theStudentPayments = new List<SchoolFeePayment>();
               theStudentPayments = work.SchoolFeePaymentRepository.Get(a => a.StudentID == s.StudentID).ToList() ;
               SchoolFeePayment theStudentPayment = new SchoolFeePayment();
              // theStudentPayment.Owing = 0;
               if (theStudentPayments.Count > 0)
               {
                 theStudentPayment =  theStudentPayments.OrderByDescending(a => a.DatePaid).First();
                // theStudentPayment.Owing = 0;
               }
                table4.DefaultCell.Border = iTextSharp.text.Rectangle.BOX;
                decimal toTalDeduction = 0;
               /// toTalDeduction = Convert.ToDecimal(s.TotalLatenessDeduction) + toTalDeduction;
               // toTalDeduction = Convert.ToDecimal(s.TotalLoan) + toTalDeduction;
               // toTalDeduction = Convert.ToDecimal(s.TotalAbscentDeduction) + toTalDeduction;

                table4.AddCell(new Paragraph(Convert.ToString(counter), font_heading_3));

                table4.AddCell(new Paragraph(s.LastName + " " + s.FirstName, font_heading_3));
                table4.AddCell(new Paragraph(s.Sex, font_heading_2));
                table4.AddCell(new Paragraph(s.Level, font_heading_2));
                table4.AddCell(new Paragraph(s.Session, font_heading_2));
                table4.AddCell(new Paragraph(s.SchoolFeesKind, font_heading_2));
                table4.AddCell(new Paragraph(Convert.ToString(s.Cost), font_heading_2));
                table4.AddCell(new Paragraph(Convert.ToString(theStudentPayment.Owing), font_heading_2));

                counter = counter + 1;
            }
            itextDoc.Add(table4);
            return itextDoc;
        }
        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 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 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));
            // }
        }