public ActionResult AddDailyIncidentsForTournament(DailyIncident dailyIncident) { if (ModelState.IsValid) { if (dailyIncident.EndDate >= dailyIncident.StartDate) { // get list of dates between List <DateTime> ListOfDatesBetween = GetDatesBetween((DateTime)dailyIncident.StartDate, (DateTime)dailyIncident.EndDate); // get list of officer in this tournament List <Officer> officersList = _context.Officers.Where(o => o.IsActive && o.TournamentId == dailyIncident.TournamentId).ToList(); //loop in officer to insert fo everyone a dailyincident foreach (var officer in officersList) { DailyIncident di = new DailyIncident(); di.IsActive = true; di.CreationDate = DateTime.Now.Date; di.Explanation = dailyIncident.Explanation; di.StartDate = dailyIncident.StartDate; di.EndDate = dailyIncident.EndDate; di.TournamentId = dailyIncident.TournamentId; di.OfficerId = officer.Id; di.ReasonOfIncidentId = dailyIncident.ReasonOfIncidentId; // -1 cz i select each day to create daily report for it di.CountDaysOfIncident = dailyIncident.NotCountDayOfIncident ?0 :ListOfDatesBetween.Count - 1; _context.DailyIncidents.Add(di); } //loop in each day and add to daily report foreach (var day in ListOfDatesBetween) { DailyReport dr = new DailyReport(); dr.IsActive = true; dr.CreationDate = DateTime.Now.Date; dr.ReportDate = day.Date; dr.BaseNumber = officersList.Count; dr.ReadyNumber = 0; dr.NotReadyNumber = officersList.Count; dr.TournamentId = dailyIncident.TournamentId; _context.DailyReports.Add(dr); } } else { TempData["message"] = NotificationSystem.AddMessage("يجب التأكد من التاريخ ", NotificationType.Danger.Value); return(View(dailyIncident)); } _context.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult AssignUserToTournament(List <UserTournamentVM> ListUserTournament, int TournamentId) { var tournament = _context.Tournaments.Find(TournamentId); if (tournament == null) { return(View("NotFound")); } List <UserTournament> userTournaments = new List <UserTournament>(); for (int i = 0; i < ListUserTournament.Count; i++) { // check if has users assign already to this tournament if (UserAssigned(ListUserTournament[i].UserId, TournamentId)) { // get this record UserTournament userTournament = _context.UserTournaments.Where(o => o.UserId == ListUserTournament[i].UserId && o.TournamentId == TournamentId).SingleOrDefault(); // if not selected i remove this assignment if (!ListUserTournament[i].IsSelected) { _context.UserTournaments.Remove(userTournament); } } // if user has no assignment to tournament else { // if selected i create object of UserTournament and insert it to UserTournaments table if (ListUserTournament[i].IsSelected) { UserTournament userTournament = new UserTournament(); // this user is never assign to a tournament if (!UserAssigned(ListUserTournament[i].UserId)) { userTournament.UserId = ListUserTournament[i].UserId; userTournament.TournamentId = TournamentId; } else { TempData["message"] = NotificationSystem.AddMessage("لا يمكن تعيين دور لمستخدم لأكثر من دورة", NotificationType.Danger.Value); return(RedirectToAction("AssignUserToTournament", new { id = TournamentId })); } _context.UserTournaments.Add(userTournament); } } } _context.SaveChanges(); return(RedirectToAction("Index", "Tournaments")); }
public async Task <IActionResult> Create(OfficerVM officerVM, int TournamnetId) { // if has an error on post i keep latest tournament var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; // tournament Id assigned to current user Id int TournamentId = _context.UserTournaments.Where(u => u.UserId == userId).Select(t => t.TournamentId).FirstOrDefault(); Tournament tournament = _context.Tournaments.Where(t => t.Id == TournamentId).First(); officerVM.TournamentId = tournament.Id; officerVM.TournamentId = tournament.Id; officerVM.TournamentName = tournament.Name; if (ModelState.IsValid) { using (var transaction = _context.Database.BeginTransaction()) { try { string image = "defaultofficer.png"; _fileManagementController = new FileManagementController(_appEnvironment); #region Image //if we uploaded an image if (officerVM.Profileimage != null) { // #region File Upload Validation bool fileUploadError = _fileManagementController.ValidateFileUploadExtensionAndSize(ModelState, officerVM.Profileimage, FileType.Image, 2); //return error if there is a file upload Error if (fileUploadError) { TempData["message"] = NotificationSystem.AddMessage("لقد حصل خطأ في تحميل الملف", NotificationType.Danger.Value); return(View(officerVM)); } // upload and remove existing image = _fileManagementController.UploadFile(officerVM.Profileimage, "Media/Officer_Profile"); } #endregion // create officer Officer officer = new Officer() { TournamentId = TournamnetId, Name = officerVM.Name, MilitaryNumber = officerVM.MilitaryNumber, PhoneNumber = officerVM.PhoneNumber, Email = officerVM.Email, BirthAddress = officerVM.BirthAddress, BirthDate = officerVM.BirthDate, Nationality = officerVM.Nationality, Address = officerVM.Address, BloodType = officerVM.BloodType, Height = officerVM.Height, PreviousJob = officerVM.PreviousJob, HealthProblem = officerVM.HealthProblem, RatingEnteringYear = officerVM.RatingEnteringYear, RatingEnteringNumber = officerVM.RatingEnteringNumber, Specialization = officerVM.Specialization, RatingGradutionYear = officerVM.RatingGradutionYear, RatingGradutionNumber = officerVM.RatingGradutionNumber, Notes = officerVM.Notes, ProfileImage = image, IsActive = true, CreationDate = DateTime.Now }; _context.Officers.Add(officer); _context.SaveChanges(); // loop each row in educational list then insert to database foreach (EducationalAttainment item in officerVM.EducationalAttainments) { if (!String.IsNullOrEmpty(item.Certificate)) { EducationalAttainment educationalAttainment = new EducationalAttainment() { Certificate = item.Certificate, Source = item.Source, Date = item.Date, Grade = item.Grade, OfficerId = officer.Id }; _context.EducationalAttainments.Add(educationalAttainment); } } // loop each row in Language list then insert to database foreach (Language item in officerVM.Languages) { if (!String.IsNullOrEmpty(item.Name)) { Language language = new Language() { Name = item.Name, Listen = item.Listen, Speak = item.Speak, Read = item.Read, Write = item.Write, OfficerId = officer.Id }; _context.Languages.Add(language); } } // loop each row in Hobby list then insert to database foreach (Hobby item in officerVM.Hobbies) { if (!String.IsNullOrEmpty(item.Name)) { Hobby hobby = new Hobby() { Name = item.Name, Explanation = item.Explanation, Level = item.Level, OfficerId = officer.Id }; _context.Hobbies.Add(hobby); } } await _context.SaveChangesAsync(); transaction.Commit(); TempData["message"] = NotificationSystem.AddMessage("تم اضافة ضابط بنجاح", NotificationType.Success.Value); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { transaction.Rollback(); Console.WriteLine("Error occurred."); } } } TempData["message"] = NotificationSystem.AddMessage("يجب التأكد من حقل الدورة قبل ادخال الضابط", NotificationType.Danger.Value); return(View(officerVM)); }
public ActionResult Create(DailyReportVM drVM, string SubmitButton) { // check why model state is false var errors = ModelState .Where(x => x.Value.Errors.Count > 0) .Select(x => new { x.Key, x.Value.Errors }) .ToArray(); if (ModelState.IsValid) { drVM.ReportDate = drVM.ReportDate != null ? drVM.ReportDate : DateTime.Today.Date; switch (SubmitButton) { case "بحث": return(RedirectToAction("Create", new { currentReportdate = drVM.ReportDate })); case "اضافة": if (drVM.BaseNumber == 0) { TempData["message"] = NotificationSystem.AddMessage("لا يوجد ضباط ", NotificationType.Danger.Value); return(View(drVM)); } // daily report /// check if report exist today cz Report Date only inserted one time DailyReport dr_today = _context.DailyReports.Where(t => t.TournamentId == drVM.TournamentId && (drVM.ReportDate.Date == t.ReportDate.Date)).FirstOrDefault(); if (dr_today == null) { DailyReport dr = new DailyReport() { TournamentId = drVM.TournamentId, IsActive = true, CreationDate = DateTime.Today.Date, ReportDate = drVM.ReportDate, BaseNumber = drVM.BaseNumber, ReadyNumber = drVM.ReadyNumber, NotReadyNumber = drVM.NotReadyNumber }; _context.DailyReports.Add(dr); } else { dr_today.ReadyNumber = drVM.ReadyNumber; dr_today.NotReadyNumber = dr_today.NotReadyNumber; _context.DailyReports.Update(dr_today); } // count of incidents already in database and today int counttodayincidents = 0; // loop in old record incidents if (drVM.TodayDailyIncidents != null && drVM.TodayDailyIncidents.Count > 0) { counttodayincidents = drVM.TodayDailyIncidents.Count; foreach (var item in drVM.TodayDailyIncidents) { // find existed daily incident DailyIncident di = _context.DailyIncidents.Find(item.Id); // if the daily incident of officer is finished , we need to update enddate to today if (item.IsFinish) { if (di == null) { TempData["message"] = NotificationSystem.AddMessage("حصل خطأ في احد وقوعات الضباط ", NotificationType.Danger.Value); return(View(drVM)); } // count day between start date and report date , cz when i finish some incident enddate updated to current report date int countdaybetween = GetCountdaysBetween((DateTime?)item.StartDate, (DateTime?)drVM.ReportDate); // this incident will finish in current date posted di.EndDate = drVM.ReportDate; di.CountDaysOfIncident = countdaybetween; } di.Explanation = item.Explanation; _context.DailyIncidents.Update(di); //di.ReasonOfIncidentId = item.ReasonOfIncidentId; } } // loop in Daily Notes int CountDailyNotePosted = 0; if (drVM.DailyNotes != null) { foreach (var item in drVM.DailyNotes) { if (item.OfficerId != null || !String.IsNullOrEmpty(item.Note)) { DailyNote dn = new DailyNote() { CreationDate = drVM.ReportDate, OfficerId = item.OfficerId, Note = item.Note, IsActive = true, IsImportant = item.IsImportant, IsPositive = item.IsPositive, TournamentId = drVM.TournamentId }; CountDailyNotePosted++; _context.DailyNotes.Add(dn); } } } drVM.CountDailyNotePosted = CountDailyNotePosted; // take the records needed from Basenumber record if (drVM.NotReadyNumber - counttodayincidents > 0) { drVM.DailyIncidents = drVM.DailyIncidents.Take(drVM.NotReadyNumber - counttodayincidents).ToList(); // loop in new Inserted Record of Daily Incident foreach (var item in drVM.DailyIncidents) { if (item.OfficerId != null) { if (item.EndDate >= item.StartDate) { int countdaybetween = GetCountdaysBetween((DateTime?)item.StartDate, (DateTime?)item.EndDate); DailyIncident di = new DailyIncident() { CreationDate = DateTime.Today, ReasonOfIncidentId = item.ReasonOfIncidentId, Explanation = item.Explanation, StartDate = item.StartDate.Value.Date, EndDate = item.EndDate.Value.Date, IsActive = true, OfficerId = item.OfficerId, CountDaysOfIncident = countdaybetween, TournamentId = drVM.TournamentId }; _context.DailyIncidents.Add(di); } else { // if an error exist , i need to call ddlreson cz each user has reson so id need ddlreason data drVM.DDLReason = new SelectList(_context.ReasonOfIncidents.ToList().OrderBy(n => n.Name), "Id", "Name"); TempData["message"] = NotificationSystem.AddMessage("يجب التأكد من التاريخ ", NotificationType.Danger.Value); return(View(drVM)); } } else { // if an error exist , i need to call ddlreson cz each user has reson so id need ddlreason data drVM.DDLReason = new SelectList(_context.ReasonOfIncidents.ToList().OrderBy(n => n.Name), "Id", "Name"); TempData["message"] = NotificationSystem.AddMessage("يوجد وقوعات دون ضابط ", NotificationType.Danger.Value); return(View(drVM)); } } } _context.SaveChanges(); TempData["message"] = NotificationSystem.AddMessage("تم اضافة التقرير اليومي بنجاح", NotificationType.Success.Value); return(RedirectToAction("Index", new { currentreportdate = drVM.ReportDate })); } } TempData["message"] = NotificationSystem.AddMessage("حصل خطأ ما", NotificationType.Danger.Value); return(View(drVM)); }