public getStudentPromotionsList getStudentPromotionsList()
        {
            var model = new getStudentPromotionsList();

            try
            {
                var StudentPromotionsList = _StudentPromotionsRepo.GetAll().ToList();
                var studentsList          = _studentsRepo.GetAll().ToList();
                var classList             = _classesRepo.GetAll().ToList();
                var query = (from _StudentPromotion in StudentPromotionsList
                             join _Class in classList on _StudentPromotion?.ClassId equals _Class?.Id
                             join _student in studentsList on _StudentPromotion?.StudentId equals _student?.Id
                             select new { _StudentPromotion, _Class, _student });
                var list = new List <getStudentPromotionsList_StudentPromotions>();
                foreach (var item in query)
                {
                    var temp = new getStudentPromotionsList_StudentPromotions()
                    {
                        Id             = item._StudentPromotion.Id,
                        ClassId        = item._StudentPromotion.ClassId,
                        ClassName      = item._Class.Name,
                        StudentId      = item._StudentPromotion.StudentId,
                        RegistrationNo = item._student.RegistrationNo,
                        RollNo         = item._StudentPromotion.RollNo,
                        ClassYear      = item._StudentPromotion.ClassYear,
                        IsActive       = item._StudentPromotion.IsActive,
                        AddedBy        = item._StudentPromotion.AddedBy,
                        AddedDate      = item._StudentPromotion.AddedDate,
                        ModifiedBy     = item._StudentPromotion.ModifiedBy,
                        ModifiedDate   = item._StudentPromotion.ModifiedDate,
                        DataType       = item._StudentPromotion.DataType
                    };
                    list.Add(temp);
                }
                ;


                model = new getStudentPromotionsList()
                {
                    _StudentPromotions = list,
                };
            }
            catch (Exception)
            {
            }
            return(model);
        }
        public SearchPaymentDetailById SearchPaymentDetailById(SearchPaymentDetailById obj)
        {
            var model = new SearchPaymentDetailById();

            try
            {
                var StudentPaymentsList = _StudentPaymentsRepo.GetAll().ToList();
                var studentsList        = _studentsRepo.GetAll().ToList();
                var FeeTypesList        = _FeeTypesRepo.GetAll().ToList();
                var classes             = _classesRepo.GetAll().ToList();
                var feeStructure        = _FeeStructures.GetAll().ToList();
                var studentPromotion    = _StudentPromotions.GetAll().ToList();
                var studentDiscount     = _StudentDiscounts.GetAll().ToList();
                var feeTermDescription  = _FeeTermDescriptions.GetAll().ToList();
                var StudentDetails      = (from c in studentsList
                                           where c.Id == obj.StudentId
                                           select c).SingleOrDefault();
                //[Note: Step 1- Get Student from promotion table for all class ]
                var query = (from sp in studentPromotion
                             where sp.StudentId == obj.StudentId
                             join s in studentsList on sp.StudentId equals s.Id
                             join c in classes on sp.ClassId equals c.Id
                             join f in FeeTypesList on c?.Id equals f?.ClassId into allFeeTypes
                             from feeTypes in allFeeTypes.DefaultIfEmpty()
                             join fs in feeStructure on feeTypes?.Id equals fs?.FeeTypeId into allfeeStructure
                             from feeStucture in allfeeStructure.DefaultIfEmpty()
                             select new
                {
                    Id = sp.Id,
                    StudentId = obj.StudentId,
                    StuClassId = sp.ClassId,
                    StuFeeYear = sp?.ClassYear,
                    StuFeeType = feeStucture.FeeTypeId,
                    StuClassName = c.Name,
                    FeeTypeName = feeTypes?.Name,
                    FeeAmount = feeStucture?.Amount,
                    StuPaymentDate = (dynamic)null,
                    StuPaidAmount = 0,
                    StuFine = 0,
                    StuRemarks = "",
                    StuDiscount = 0
                }).ToList();

                //[NOTE:Step 2- get discount of the student if exist]
                studentDiscount = studentDiscount.Where(s => s.StudentId == obj.StudentId).ToList();
                if (query != null)
                {
                    var     list           = new List <SearchPaymentDetailById_StudentPayments>();
                    decimal?totalPaid      = 0;
                    decimal?totalDiscount  = 0;
                    decimal?totalFeeAmount = 0;
                    decimal?totalFine      = 0;
                    foreach (var item in query)
                    {
                        var getFeeTerm = from ft in feeTermDescription
                                         join fs in feeStructure on ft.FeeStructureId equals fs.Id
                                         //[NOTE: temporary solution bellow. Need to investigate comment line]
                                         //where fs.ClassId == item.StuClassId && fs.FeeTypeId == item.StuFeeType && fs.StartingYear <= item.StuFeeYear && fs.EndingYear >= item.StuFeeYear
                                         where fs.ClassId == item.StuClassId && fs.FeeTypeId == item.StuFeeType

                                         select new { ft, fs };
                        //[NOTE:Step 3- find feetype discount if exist]
                        decimal?currentTypeDiscountAmount = 0;
                        if (studentDiscount.Count() > 0)
                        {
                            currentTypeDiscountAmount = (from s in studentDiscount
                                                         where s.FeeTypeId == item.StuFeeType
                                                         select s.DiscountAmout).FirstOrDefault();
                        }
                        foreach (var item2 in getFeeTerm)
                        {
                            //[NOTE: get existing payment of the student if exist]
                            var GetPaymentListOfTheStudent = (from p in StudentPaymentsList
                                                              where p.StudentId == obj.StudentId &&
                                                              p.ClassId == item2.fs.ClassId &&
                                                              p.FeeTermDescriptionId == item2.ft.Id &&
                                                              p.FeeTypeId == item2.fs.FeeTypeId
                                                              //&& (p.FeeYearDate.Value.Year >= item2.fs.StartingYear.Value.Year && p.FeeYearDate.Value.Year <= item2.fs.EndingYear.Value.Year)
                                                              select p).FirstOrDefault();
                            var temp = new SearchPaymentDetailById_StudentPayments()
                            {
                                Id          = item.Id,
                                ClassId     = item.StuClassId,
                                ClassName   = item.StuClassName,
                                FeeYearDate = item.StuFeeYear,
                                PaidAmount  = GetPaymentListOfTheStudent != null ? GetPaymentListOfTheStudent.PaidAmount : 0,
                                FeeType     = item.FeeTypeName,
                                FeeTypeId   = item.StuFeeType,
                                TermNo      = item2.ft.TermNo,
                                TermName    = item2.ft.TermName,
                                FeeAmount   = item.FeeAmount != null ? item.FeeAmount : 0,
                                PaymentDate = GetPaymentListOfTheStudent != null ? GetPaymentListOfTheStudent.PaymentDate : null,
                                Remarks     = item.StuRemarks,
                                Fine        = GetPaymentListOfTheStudent != null ? GetPaymentListOfTheStudent.Fine : 0,
                                Discount    = currentTypeDiscountAmount
                            };
                            list.Add(temp);
                            //[NOTE: total fine ]
                            totalFine += GetPaymentListOfTheStudent != null ? (GetPaymentListOfTheStudent.Fine != null ? GetPaymentListOfTheStudent.Fine : 0) : 0;
                            totalPaid += GetPaymentListOfTheStudent != null ? (GetPaymentListOfTheStudent.PaidAmount != null ? GetPaymentListOfTheStudent.PaidAmount : 0) : 0;
                        }
                        totalDiscount  += currentTypeDiscountAmount;
                        totalFeeAmount += item.FeeAmount != null ? item.FeeAmount : 0;
                    }
                    ;
                    model = new SearchPaymentDetailById()
                    {
                        TotalFine         = totalFine,
                        TotalPaid         = totalPaid,
                        TotalHavetoPay    = totalFeeAmount - totalDiscount,
                        ClassId           = StudentDetails.ClassId,
                        _StudentPayments  = list,
                        StudentSearchName = StudentDetails.FirstName + " " + StudentDetails.LastName
                    };
                }
                else
                {
                    model = new SearchPaymentDetailById()
                    {
                        TotalFine         = null,
                        TotalPaid         = null,
                        TotalHavetoPay    = null,
                        ClassId           = 0,
                        _StudentPayments  = null,
                        StudentSearchName = null
                    };
                }
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }
            return(model);
        }
        public GetFeeStructuresByClass GetFeeStructuresByClass(long StudentId, long ClassId, long ClassYear)
        {
            var model = new GetFeeStructuresByClass();

            try
            {
                var StudentPaymentsList = _StudentPaymentsRepo.GetAll().ToList();
                var studentsList        = _studentsRepo.GetAll().ToList();
                var FeeTypesList        = _FeeTypesRepo.GetAll().ToList();
                var classes             = _classesRepo.GetAll().ToList();
                var feeStructure        = _FeeStructuresRepo.GetAll().ToList();
                var studentPromotion    = _StudentPromotionsRepo.GetAll().ToList();
                var studentDiscount     = _StudentDiscountsRepo.GetAll().ToList();
                var feeTermDescription  = _FeeTermDescriptions.GetAll().ToList();
                var StudentDetails      = (from c in studentsList
                                           where c.Id == StudentId
                                           select c).SingleOrDefault();
                var getClass = (from cls in classes
                                where cls.Id == ClassId
                                select cls).SingleOrDefault();
                var getStuFeeYear = (from sp in studentPromotion
                                     where sp.StudentId == StudentId && sp.ClassId == ClassId
                                     select sp).SingleOrDefault();

                //[Note: Step 1- Get Student from promotion table for all class ]
                var query = (from sp in studentPromotion
                             where sp.StudentId == StudentId
                             join s in studentsList on sp.StudentId equals s.Id
                             join c in classes on sp.ClassId equals c.Id
                             join f in FeeTypesList on c?.Id equals f?.ClassId into allFeeTypes
                             from feeTypes in allFeeTypes.DefaultIfEmpty()
                             join fs in feeStructure on feeTypes?.Id equals fs?.FeeTypeId into allfeeStructure
                             from feeStucture in allfeeStructure.DefaultIfEmpty()
                             select new
                {
                    Id = sp.Id,
                    StudentId = StudentId,
                    StuClassId = sp.ClassId,
                    StuFeeYear = sp?.ClassYear,
                    StuFeeType = feeStucture.FeeTypeId,
                    StuClassName = c.Name,
                    FeeTypeName = feeTypes?.Name,
                    FeeAmount = feeStucture?.Amount,
                    StuPaymentDate = (dynamic)null,
                    StuPaidAmount = 0,
                    StuFine = 0,
                    StuRemarks = "",
                    StuDiscount = 0
                }).ToList();

                //[NOTE:Step 2- get discount of the student if exist]
                studentDiscount = studentDiscount.Where(s => s.StudentId == StudentId).ToList();
                if (query != null)
                {
                    //var list = new List<SearchPaymentDetailById_StudentPayments>();
                    var list = new List <GetFeeStructuresByClass_FeeStructures>();
                    //decimal? totalPaid = 0;
                    //decimal? totalDiscount = 0;
                    //decimal? totalFeeAmount = 0;
                    //decimal? totalFine = 0;

                    foreach (var item in query)
                    {
                        var getFeeTerm = from ft in feeTermDescription
                                         join fs in feeStructure on ft.FeeStructureId equals fs.Id
                                         //where fs.ClassId == ClassId && fs.FeeTypeId == item.StuFeeType && fs.StartingYear <= item.StuFeeYear && fs.EndingYear >= item.StuFeeYear
                                         where fs.ClassId == ClassId && fs.FeeTypeId == item.StuFeeType
                                         select new { ft, fs };

                        //[NOTE:Step 3- find feetype discount if exist]
                        decimal?currentTypeDiscountAmount = 0;
                        if (studentDiscount.Count() > 0)
                        {
                            currentTypeDiscountAmount = (from s in studentDiscount
                                                         where s.FeeTypeId == item.StuFeeType
                                                         select s.DiscountAmout).FirstOrDefault();
                        }
                        foreach (var item2 in getFeeTerm)
                        {
                            //[NOTE: get existing payment of the student if exist]
                            var GetPaymentListOfTheStudent = (from p in StudentPaymentsList
                                                              where p.StudentId == StudentId &&
                                                              p.ClassId == item2.fs.ClassId &&
                                                              p.FeeTermDescriptionId == item2.ft.Id &&
                                                              p.FeeTypeId == item2.fs.FeeTypeId &&
                                                              (p.FeeYearDate.Value.Year == ClassYear)
                                                              select p).FirstOrDefault();
                            var temp = new GetFeeStructuresByClass_FeeStructures()
                            {
                                Id                   = item.Id,
                                ClassId              = item.StuClassId,
                                FeeTypeId            = item.StuFeeType,
                                FeeType              = item.FeeTypeName,
                                FeeTypeMood          = item2.ft.TermNo,
                                Amount               = item.FeeAmount != null ? (dynamic)item.FeeAmount : 0,
                                Fine                 = GetPaymentListOfTheStudent != null ? GetPaymentListOfTheStudent.Fine : 0,
                                FeeTermDescriptionId = item2.ft.Id,
                                TermNo               = item2.ft.TermNo,
                                TermName             = item2.ft.TermName,
                                PaidAmount           = GetPaymentListOfTheStudent != null ? GetPaymentListOfTheStudent.PaidAmount : 0,
                                Remarks              = item.StuRemarks,
                                StartingYear         = item2.fs.StartingYear,
                                EndingYear           = item2.fs.EndingYear,
                                DiscountAmount       = (dynamic)currentTypeDiscountAmount,
                                IsActive             = item2.fs.IsActive
                            };
                            list.Add(temp);
                        }
                    }
                    ;
                    model = new GetFeeStructuresByClass()
                    {
                        ClassName      = getClass.Name,
                        FeeYear        = getStuFeeYear.ClassYear.Value.Year,
                        _FeeStructures = list
                    };
                }
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }
            return(model);
        }
        public getStudentsList.getStudentsList getStudentList(getStudentsList.getStudentsList obj)
        {
            var model = (dynamic)null;

            try
            {
                var studentsList          = _studentsRepo.GetAll().ToList();
                var StudentPromotionsList = _StudentPromotionsRepo.GetAll().ToList();
                var thisYear    = DateTime.Now.Year;
                var ClassList   = _classesRepo.GetAll().ToList();
                var GendersList = _GendersRepo.GetAll().ToList();
                var query       = (from _students in studentsList
                                   join _Classes in ClassList on _students?.ClassId equals _Classes?.Id
                                   join _Genders in GendersList on _students?.GenderId equals _Genders?.Id
                                   join _studentPromotion in StudentPromotionsList on _students.Id equals _studentPromotion.StudentId
                                   join _promotionClass in ClassList on _studentPromotion.ClassId equals _promotionClass.Id
                                   where _studentPromotion.StudentId == _students.Id && _studentPromotion.ClassYear.Value.Year == thisYear
                                   orderby _students.ClassId, _students.FirstName ascending
                                   select new { _students, _Classes, _Genders, _studentPromotion, _promotionClass });
                var list = new List <getStudentsList.getStudentsList_Students>();
                foreach (var item in query)
                {
                    string path = string.IsNullOrEmpty(item._students.IP300X200) ? "" : item._students.IP300X200;
                    var    temp = new getStudentsList.getStudentsList_Students()
                    {
                        Id               = item._students.Id,
                        ClassId          = item._students.ClassId,
                        RollNO           = item._studentPromotion.RollNo,
                        CurrentClassId   = item._studentPromotion.ClassId,
                        CurrentYear      = item._studentPromotion.ClassYear,
                        ClassName        = item._Classes.Name,
                        GenderId         = item._students.GenderId,
                        RegistrationNo   = item._students.RegistrationNo,
                        GenderName       = item._Genders.Name,
                        FirstName        = item._students.FirstName,
                        LastName         = item._students.LastName,
                        StudentName      = item._students.FirstName + "  " + item._students.LastName,
                        IP300X200        = _commonServ.CommImage_WrapperDefaultImage(path, obj.WebRootPath, item._students.GenderId),
                        AdmittedYear     = item._students.AdmittedYear,
                        PresentAddress   = item._students.PresentAddress,
                        PermanentAddress = item._students.PermanentAddress,
                        DOB              = item._students.DOB,
                        IsActive         = item._students.IsActive,
                        AddedBy          = 0,
                        AddedDate        = DateTime.Now,
                        ModifiedBy       = 0,
                        ModifiedDate     = DateTime.Now,
                        DataType         = null
                    };
                    list.Add(temp);
                }
                ;
                model = new getStudentsList.getStudentsList()
                {
                    _Students = list
                };
            }
            catch (Exception)
            {
            }
            return(model);
        }