public PilotScreeningViewModel IntialScreeningInfo(int RegNo)
        {
            PilotScreeningViewModel Model = new PilotScreeningViewModel
            {
                PilotRegistrationInfo = _context.ptaPilotRegistrationMasters.Where(p => p.RegistrationNo == RegNo && p.IsActive).Select(i => new PilotRegistrationViewModel
                {
                    Id = i.Id,
                    PilotRegistartionNo = i.ptaRegistrationInfoes.FirstOrDefault().RegistartionNo,
                    RegistartionNo      = i.RegistrationNo,
                    Fname            = i.Fname,
                    Lname            = i.Lname,
                    Email            = i.Email,
                    Mobile           = i.Mobile,
                    CourseName       = i.CourseMaster.CourseName,
                    RegistrationDate = i.RegistrationDate,
                    Gender           = i.ptaRegistrationInfoes.FirstOrDefault().ptaGenderMaster.Name,
                    DOB               = i.DOB,
                    Height            = i.Height,
                    SessionName       = i.SessionMaster.SessionName,
                    TenthCGPA         = i.ptaRegistrationInfoes.FirstOrDefault().ptaEducationDetails.Where(e => e.Class == "10th").FirstOrDefault().PercentageOrCGPA,
                    TwelveCGPA        = i.ptaRegistrationInfoes.FirstOrDefault().ptaEducationDetails.Where(e => e.Class == "12th").FirstOrDefault().PercentageOrCGPA,
                    SourceOfCandidate = i.ptaRegistrationInfoes.FirstOrDefault().ptaLeadSourceMaster.Name,
                    ScreeningInfoId   = _context.ptaScreeningInfoes.Where(p => p.PTARegistrationNo == i.RegistrationNo).Any() ? _context.ptaScreeningInfoes.Where(p => p.PTARegistrationNo == i.RegistrationNo).FirstOrDefault().Id : 0
                }).FirstOrDefault(),

                GetPilotTrainingTestTypeList = _context.ptaScreeningTestTypes.Where(p => p.IsActive).Select(i => new PilotScreeningTestTypeViewModel
                {
                    Id                      = i.Id,
                    Name                    = i.Name,
                    InitialMark             = i.InitialMark,
                    ptaScreenningTestTypeId = i.ptaScreeningTestResults.Any() ? i.ptaScreeningTestResults.FirstOrDefault().PTAScreeningTestTypeId : 0
                }).ToList()
            };

            var data = _context.ptaScreeningInfoes.Where(s => s.IsActive && s.PTARegistrationNo == RegNo).FirstOrDefault();

            if (data != null)
            {
                Model.Remark = data.Remark;
            }
            if (data != null && data.ptaScreeningTestResults.Count() > 0)
            {
                foreach (var item in data.ptaScreeningTestResults)
                {
                    var d = Model.GetPilotTrainingTestTypeList.Where(g => g.Id == item.PTAScreeningTestTypeId).FirstOrDefault();
                    if (d != null)
                    {
                        d.IsSelected = item.IsPassed;
                        d.ObtainMark = item.ObtainMark;
                        if (!item.IsPassed.HasValue)
                        {
                            d.AgainExam = "yes";
                        }
                    }
                }
            }
            return(Model);
        }
 public JsonResult SaveUpdatePilotScreeningInfo(PilotScreeningViewModel Model)
 {
     Model.EnteredBy = Convert.ToInt32(Session["UserId"]);
     return(Json(_registrationService.SaveUpdatePilotScreeningInfo(Model), JsonRequestBehavior.AllowGet));
 }
        public MessageForSucccessful SaveUpdatePilotScreeningInfo(PilotScreeningViewModel Model)
        {
            MessageForSucccessful message = new MessageForSucccessful();
            int screenInfoId = 0;
            var registerData = _context.ptaPilotRegistrationMasters.Where(r => r.IsActive && r.RegistrationNo == Model.PTARegistrationNo).FirstOrDefault();

            if (registerData != null)
            {
                PTAExamLogViewModel failedlog = new PTAExamLogViewModel();
                failedlog.PTAPilotRegistrationMasterId = registerData.Id;
                if (Model.PilotScreeningInfoId == 0)
                {
                    ptaScreeningInfo screeninfo = new ptaScreeningInfo
                    {
                        PTAPilotRegistrationMasterId = registerData.Id,
                        PTARegistrationNo            = Model.PTARegistrationNo,
                        Remark      = Model.Remark,
                        EnteredDate = DateTime.Now,
                        EnteredBy   = Model.EnteredBy,
                        IsActive    = true
                    };
                    _context.ptaScreeningInfoes.Add(screeninfo);

                    if (registerData.ptaScreeningExamFeeInfoes.Count == 1)
                    {
                        registerData.ptaScreeningExamFeeInfoes.FirstOrDefault().PTAScreeningInfoId = screeninfo.Id;
                    }
                    _context.SaveChanges();
                    screenInfoId = screeninfo.Id;
                }
                else
                {
                    screenInfoId = _context.ptaScreeningInfoes.Where(p => p.IsActive && p.PTARegistrationNo == Model.PTARegistrationNo).FirstOrDefault().Id;
                }
                foreach (var Item in Model.GetPilotTrainingTestTypeList)
                {
                    var exist = _context.ptaScreeningInfoes.Where(p => p.IsActive && p.PTARegistrationNo == Model.PTARegistrationNo && p.ptaScreeningTestResults.Where(t => t.PTAScreeningTestTypeId == Item.Id).Any()).FirstOrDefault();
                    if (exist == null)
                    {
                        ptaScreeningTestResult TestResult = new ptaScreeningTestResult
                        {
                            PTAScreeningInfoId     = screenInfoId,
                            PTAScreeningTestTypeId = Item.Id,
                            IsActive   = true,
                            ObtainMark = Item.ObtainMark,
                            IsPassed   = Item.IsPassed
                        };
                        _context.ptaScreeningTestResults.Add(TestResult);
                    }
                    else
                    {
                        var aa = _context.ptaScreeningTestResults.Where(t => t.PTAScreeningTestTypeId == Item.Id && t.PTAScreeningInfoId == screenInfoId).FirstOrDefault();
                        if (aa != null && aa.IsActive)
                        {
                            aa.IsPassed   = Item.IsPassed;
                            aa.ObtainMark = Item.ObtainMark;
                        }
                    }
                    if (!Item.IsPassed)
                    {
                        failedlog.PTAScreeningInfoId        = screenInfoId;
                        failedlog.PTAScreeningExamFeeInfoId = registerData.ptaScreeningExamFeeInfoes.OrderByDescending(o => o.ScreeningAmountTerm).FirstOrDefault().Id;
                        failedlog.PTAScreeningTestTypeId    = Item.Id;
                        failedlog.EnteredBy = Model.EnteredBy;

                        ptaScreeningExamfailedLog log = new ptaScreeningExamfailedLog
                        {
                            PTAPilotRegistrationMasterId = failedlog.PTAPilotRegistrationMasterId,
                            PTAScreeningExamFeeInfoId    = failedlog.PTAScreeningExamFeeInfoId,
                            PTAScreeningInfoId           = failedlog.PTAScreeningInfoId,
                            PTAScreeningTestTypeId       = failedlog.PTAScreeningTestTypeId,
                            EnteredBy   = failedlog.EnteredBy,
                            ExamStatus  = "Failed",
                            EnteredDate = DateTime.Now
                        };
                        _context.ptaScreeningExamfailedLogs.Add(log);
                    }
                }
                _context.SaveChanges();
                if (Model.GetPilotTrainingTestTypeList.Where(g => g.IsPassed == true).Count() == 4)
                {
                    AdmissionPilotService s = new AdmissionPilotService();
                    s.CreatePilotAdmission(registerData, Model.EnteredBy);
                }
                if (Model.GetPilotTrainingTestTypeList.LastOrDefault() != null)
                {
                    int d = Model.GetPilotTrainingTestTypeList.Where(g => g.IsPassed).Count();
                    message.Message = (Model.GetPilotTrainingTestTypeList.LastOrDefault().IsPassed ? (d == 4 ? "Candidate is selected." : "Candidate is selected") : "Candidate is rejected");
                }
                message.IsSuccess = true;
            }
            message.RegNo = Model.PTARegistrationNo.ToString();
            return(message);
        }