public IActionResult SchoolCalculation(SchoolCalculationsViewModel model)
        {
            SchoolCalculationsViewModel result = _schoolService
                                                 .GetSchoolDetail(model.SchoolId, model.SelectedCourseId, model.Month, model.SelectedNavId, model.SelectedTeacherId);

            if (model == null)
            {
                return(RedirectToAction(nameof(Index), new { redirect = "SchoolCalculation" }));
            }
            return(View(result));
        }
        public IActionResult SchoolCalculation(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(RedirectToAction(nameof(Index), new { redirect = "SchoolCalculation" }));
            }
            CheckSchool checkSchool = new CheckSchool(_schoolService, HttpContext.Session);

            if (!checkSchool.IsSchoolAllowed(id))
            {
                return(RedirectToAction(Common.UsersConstants.redirectPayPageAction, Common.UsersConstants.redirectPayPageController, new { schoolId = id }));
            }
            SchoolCalculationsViewModel model = _schoolService.GetSchoolDetail(id, "", DateTime.Now, "", "");

            if (model == null)
            {
                return(RedirectToAction(nameof(Index), new { redirect = "SchoolCalculation" }));
            }
            return(View(model));
        }
        public SchoolCalculationsViewModel GetSchoolDetail(string schoolId, string courseId,
                                                           DateTime month, string selectedNavId, string selectedTeacherId)
        {
            if (string.IsNullOrEmpty(schoolId))
            {
                return(new SchoolCalculationsViewModel());
            }
            IEnumerable <Course> courses = _context.Courses
                                           .Include(cour => cour.School)
                                           .Include(cour => cour.CourseAppUsers).ThenInclude(capu => capu.AppUser)
                                           .Include(cour => cour.Subscriptions).ThenInclude(sub => sub.AppUser)
                                           .Include(cour => cour.Subscriptions).ThenInclude(sub => sub.Payments).ThenInclude(pay => pay.PaymentType)
                                           .Where(cour => cour.SchoolId == schoolId)
                                           .OrderBy(cour => cour.Name)
                                           .ToList();
            List <SchoolCourse> schoolCourses = new List <SchoolCourse>();

            if (courses != null && courses.Count() > 0)
            {
                schoolCourses = courses.Select(course => new SchoolCourse
                {
                    Id       = course.Id,
                    Name     = course.Name,
                    IsActive = course.IsActive,
                    Sum      = course.Subscriptions
                               .Where(sub => sub.Period.Month == month.Month && sub.Period.Year == month.Year)
                               .Select(sub => sub.Payments.Select(pay => pay.Price).Sum()).Sum(),
                }).ToList();
            }

            var sumByPaymentType = new Dictionary <string, int>();
            var subs             = courses.SelectMany(entry => entry.Subscriptions)
                                   .Where(sub => sub.Period.Month == month.Month && sub.Period.Year == month.Year).ToList();
            List <Subscription> subscriptions = new List <Subscription>();

            if (!string.IsNullOrEmpty(courseId) && courses.FirstOrDefault(cour => cour.Id == courseId) != null)
            {
                subscriptions = courses.FirstOrDefault(cour => cour.Id == courseId)
                                .Subscriptions
                                .Where(sub => sub.Period.Month == month.Month && sub.Period.Year == month.Year).ToList();
            }
            IEnumerable <SchoolTeacher> schoolTeachers = courses.SelectMany(cour => cour.CourseAppUsers.Select(capu => capu.AppUser))
                                                         .Distinct()
                                                         .Select(usr => new SchoolTeacher
            {
                Id       = usr.Id,
                FullName = usr.FullName,
            })
                                                         .ToList();
            IEnumerable <Subscription> teacherSubscriptions = new List <Subscription>();

            if (!string.IsNullOrEmpty(selectedTeacherId))
            {
                IEnumerable <Course> teacherCourses = courses.Where(cour => cour.CourseAppUsers.Any(cap => cap.AppUserId == selectedTeacherId));
                teacherSubscriptions = teacherCourses.SelectMany(cour => cour.Subscriptions)
                                       .Where(sub => sub.Period.Month == month.Month && sub.Period.Year == month.Year)
                                       .ToList();
            }
            SchoolCalculationsViewModel model = new SchoolCalculationsViewModel
            {
                SchoolId                      = schoolId,
                SelectedCourseId              = courseId,
                SelectedTeacherId             = selectedTeacherId,
                Month                         = month,
                SelectedNavId                 = selectedNavId,
                SchoolCourses                 = schoolCourses,
                SchoolCoursesByPaymentTypeSum = sumByPaymentType,
                CourseSubscriptions           = subscriptions,
                SchoolTeachers                = schoolTeachers,
                TeacherSubscriptions          = teacherSubscriptions,
            };

            return(model);
        }