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

            try
            {
                var FeeTermDescriptionsList = _FeeTermDescriptionsRepo.GetAll().ToList();
                var FeeStructures           = _FeeStructuresRepo.GetAll().ToList();
                var FeeTypes  = _FeeTypesRepo.GetAll().ToList();
                var classList = _classesRepo.GetAll().ToList();

                var query = (from _FeeTermDescriptions in FeeTermDescriptionsList
                             join feeStucture in FeeStructures on _FeeTermDescriptions.FeeStructureId equals feeStucture.Id
                             join feeTypes in FeeTypes on feeStucture.FeeTypeId equals feeTypes.Id
                             join cls in classList on feeStucture.ClassId equals cls.Id

                             select new { _FeeTermDescriptions, feeStucture, feeTypes, cls });

                var list = new List <GetFeeTermDescriptionsList_FeeTermDescriptions>();
                foreach (var item in query)
                {
                    var temp = new GetFeeTermDescriptionsList_FeeTermDescriptions()
                    {
                        Id             = item._FeeTermDescriptions.Id,
                        FeeStructureId = item._FeeTermDescriptions.FeeStructureId,
                        TermNo         = item._FeeTermDescriptions.TermNo,
                        TermName       = item._FeeTermDescriptions.TermName,
                        ClassName      = item.cls.Name,
                        FeeType        = item.feeTypes.Name,
                        ClassId        = item.feeStucture.ClassId,
                        FeeTypeId      = item.feeStucture.FeeTypeId
                    };
                    list.Add(temp);
                }
                ;


                model = new GetFeeTermDescriptionsList()
                {
                    _FeeTermDescriptions = list
                };
            }
            catch (Exception)
            {
            }
            return(model);
        }
        public getFeeStructuresList getFeeStructuresList()
        {
            var model = new getFeeStructuresList();

            try
            {
                var FeeStructuresList = _FeeStructuresRepo.GetAll().ToList();
                var FeeTypesList      = _FeeTypesRepo.GetAll().ToList();
                var classList         = _classesRepo.GetAll().ToList();
                var query             = (from _FeeStructure in FeeStructuresList
                                         join _FeeTypes in FeeTypesList on _FeeStructure?.FeeTypeId equals _FeeTypes?.Id
                                         join _class in classList on _FeeStructure?.ClassId equals _class?.Id
                                         select new { _FeeStructure, _FeeTypes, _class });
                var list = new List <getFeeStructuresList_FeeStructures>();
                foreach (var item in query)
                {
                    var temp = new getFeeStructuresList_FeeStructures()
                    {
                        Id           = item._FeeStructure.Id,
                        ClassId      = item._FeeStructure.ClassId,
                        Class        = item._class.Name,
                        FeeTypeId    = item._FeeStructure.FeeTypeId,
                        FeeType      = item._FeeTypes.Name,
                        FeeTypeMood  = item._FeeStructure.YearlyTermNo,
                        Amount       = item._FeeStructure.Amount,
                        StartingYear = item._FeeStructure.StartingYear,
                        EndingYear   = item._FeeStructure.EndingYear,
                        IsActive     = item._FeeStructure.IsActive
                    };
                    list.Add(temp);
                }
                ;
                model = new getFeeStructuresList()
                {
                    _FeeStructures = 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);
        }