public ActionResult <FeeVM> Get(int id) { if (id <= 0) { return(BadRequest()); } var result = _feeSer.Get(id); if (result == null) { return(NotFound()); } return(result); }
public async Task <ActionResult> GetFees([FromBody] FeeFilterView model) { try { var schoolId = GetMySchoolId(); FeeStudentModelView FeeStudentView = new FeeStudentModelView(); //Get School // var school = await _schoolService.GetSchoolById(schoolId.ToString()); if (school == null) { return(BadRequest("School not valid")); } /// Get Active session from school var sessionToUse = school.GetActiveSession(); /// Update Current Session if user choose other session from filter if (model.SessionId.HasSomething()) { sessionToUse = school.Sessions.FirstOrDefault(x => x.Id == model.SessionId.AsObjectId()); } if (sessionToUse != null) { model.ActiveSessionName = sessionToUse.Name; var allClassesForSchool = await _schoolClassService.Get(x => x.SchoolId == school.Id && x.SessionId == sessionToUse.Id && x.DeactivateDate == null); var activeClassIds = allClassesForSchool.Select(x => x.Id); var student = await _studentService.Get( x => activeClassIds.Contains(x.ClassId) && x.DeactivateDate == null && x.SchoolId == school.Id && x.DeletedAt == null); if (!school.FeeCycles.Any()) { return(BadRequest("School has no feecylces")); } IEnumerable <Student> allstudent; if (model.ClassId != null) { allstudent = student.Where(x => x.ClassId == model.ClassId.AsObjectId()).ToList(); } else { allstudent = student; } if (model.StudentId != null) { allstudent = allstudent.Where(x => x.Id == model.StudentId.AsObjectId()); } if (model.StudentFeeFrequency != null && !model.StudentFeeFrequency.Contains("All")) { allstudent = allstudent.Where(x => x.FeeFrequency.ToString() == model.StudentFeeFrequency); } var allstudentIds = allstudent.Select(x => x.Id); var allCalculatedFee = new List <Fee>(); if (model.FeeCycleId != null) { allCalculatedFee = await _feeService.Get( x => allstudentIds.Contains(x.StudentId) && x.FeeCycleId == model.FeeCycleId.AsObjectId()); } else { var feeCycleIdsforSchool = school.FeeCycles.Where(x => x.SessionId == sessionToUse.Id).Select(x => x.Id); allCalculatedFee = await _feeService.Get( x => allstudentIds.Contains(x.StudentId) && feeCycleIdsforSchool.Contains(x.FeeCycleId)); } var students = student.Where(x => allstudentIds.Contains(x.Id)); var schoolComponents = school.SchoolFeeComponents; var allfeeCycle = school.FeeCycles; if (model.FeeStatus != null && !model.FeeStatus.Contains("All")) { allCalculatedFee = allCalculatedFee.Where(x => model.FeeStatus.Contains(x.FeeStatus.ToString())).ToList(); } if (allCalculatedFee.Count > 0) { var sortedFees = allCalculatedFee.OrderByDescending(x => x.CreatedAt) .ThenBy(x => x.FeeStatus == FeeStatus.PendingApproval); var classDict = allClassesForSchool.OrderBy(x => x.Name) .ToDictionary(x => x.Id.ToString(), x => x.Name); foreach (var fee in sortedFees) { var singleFeeModel = new FeeStudentModel { StudentId = fee.StudentId.ToString(), FeeId = fee.Id.ToString(), FeeStatus = EnumHelper.DisplayName(typeof(FeeStatus), fee.FeeStatus.ToString()), FeeStatusEnum = fee.FeeStatus, Remark = fee.Remark, CreateAt = fee.CreatedAt, Checked = true }; var singleStudent = students.FirstOrDefault(x => x.Id == fee.StudentId); singleFeeModel.ClassName = allClassesForSchool.First(x => x.Id == singleStudent.ClassId).Name; var feeCycleForFee = allfeeCycle.FirstOrDefault(x => x.Id == fee.FeeCycleId); var totalPayable = Math.Round(fee.GetPendingFee(DateTime.Now, feeCycleForFee, school, singleStudent.JoiningDate), 2); singleFeeModel.TotalPayable = totalPayable > 0 ? Math.Round(fee.GetPendingFee(DateTime.Now, feeCycleForFee, school, singleStudent.JoiningDate), 2) : 0; singleFeeModel.TotalPaid = fee.Transactions.Sum(x => x.Amount); singleFeeModel.FeeCycle = _mapperService.MapFeeCycleToFeeCycleSingleModel(feeCycleForFee); //var singleStudent = students.FirstOrDefault(x => x.Id == fee.StudentId); if (singleStudent != null) { singleFeeModel.StudentName = singleStudent.Name; singleFeeModel.AdmNumber = singleStudent.Identifier; singleFeeModel.StudentMotherName = singleStudent.MotherName; singleFeeModel.StudentClass = classDict.ContainsKey(singleStudent.ClassId.ToString()) ? classDict[singleStudent.ClassId.ToString()] : string.Empty; singleFeeModel.StudentFeeFrequency = EnumHelper.DisplayName( typeof(StudentFeeFrequency), singleStudent.FeeFrequency.ToString()); } if (Convert.ToDateTime(singleStudent.JoiningDate).ToLocalTime().Date > feeCycleForFee.LastDueDate.ToLocalTime().Date) { singleFeeModel.LateFee = Math.Round(fee.GetTotalLateFeesOfLateJoinedStudent(school, DateTime.Today, feeCycleForFee, Convert.ToDateTime(singleStudent.JoiningDate).ToLocalTime().Date), 2); } else { singleFeeModel.LateFee = Math.Round(fee.GetTotalLateFees(school, DateTime.Today, feeCycleForFee), 2); } //singleFeeModel.LateFee = fee.GetTotalLateFees(school, DateTime.Today, feeCycleForFee); if (fee.ApprovedBy != null || fee.CancelledBy != null) { singleFeeModel.ApprovedById = fee.ApprovedBy; singleFeeModel.CancelledById = fee.CancelledBy; } else { model.ToCheckUnapprovedStudents = model.ToCheckUnapprovedStudents + 1; } foreach (var component in fee.Components) { var singleComponentModel = new FeeModel { ComponentValue = component.Value, SchoolFeeComponentId = component.ComponetId.ToString() }; singleFeeModel.TotalFeeForStudent = Math.Round(singleFeeModel.TotalFeeForStudent + singleComponentModel.ComponentValue, 2); var singlecomponent = schoolComponents.FirstOrDefault(x => x.Id == component.ComponetId); if (singlecomponent != null) { singleComponentModel.ComponentName = singlecomponent.Name; } singleFeeModel.StudentAllFee = singleFeeModel.StudentAllFee != null ? singleFeeModel.StudentAllFee.Concat(new[] { singleComponentModel }).ToArray() : new[] { singleComponentModel }; singleFeeModel.ComponetDict = singleFeeModel.StudentAllFee.GroupBy( x => x.SchoolFeeComponentId.ToString()).ToDictionary( x => x.Key, x => x.First().ComponentValue); } FeeStudentView.FeeStudents = FeeStudentView.FeeStudents != null ? FeeStudentView.FeeStudents.Concat(new[] { singleFeeModel }).ToArray() : new[] { singleFeeModel }; } FeeStudentView.Maxdata = FeeStudentView.FeeStudents.OrderByDescending(x => x.StudentAllFee.Length).FirstOrDefault(); if (FeeStudentView.Maxdata != null) { FeeStudentView.FeeComponentWithDictinctValueDict = FeeStudentView.Maxdata.StudentAllFee.GroupBy(x => x.SchoolFeeComponentId) .ToDictionary(x => x.Key, x => x.First().ComponentValue ); } } } if (FeeStudentView.FeeStudents != null && FeeStudentView.FeeStudents.Length > 0) { FeeStudentView.FeeStudents = FeeStudentView.FeeStudents.OrderBy(x => x.FeeCycle.LastDueDate).ThenBy(x => x.StudentName).ToArray(); } if (FeeStudentView.FeeStudents != null) { foreach (var fee in FeeStudentView.FeeStudents) { if (fee.FeeCycle.LastDueDate.ToLocalTime().Date < DateTime.Today.Date) { fee.LateFee = fee.LateFee; } else { fee.LateFee = null; } } } return(Ok(FeeStudentView)); } catch (ArgumentNullException argNullEx) { return(BadRequest(argNullEx.Message)); } catch (ArgumentException argEx) { return(BadRequest(argEx.Message)); } catch (Exception ex) { return(StatusCode(500, ex)); } }