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 AddDailyIncidentsForTournament(int id)
        {
            Tournament tournament = _context.Tournaments.Find(id);

            DailyIncident di = new DailyIncident();

            ViewBag.DDLReason = new SelectList(_context.ReasonOfIncidents.ToList().OrderBy(n => n.Name), "Id", "Name");

            if (tournament == null)
            {
                TempData["message"] = NotificationSystem.AddMessage("لا يوجد دورة", NotificationType.Danger.Value);
                return(View(di));
            }
            ViewBag.TournamnetName = tournament.Name;
            di.TournamentId        = id;
            return(View(di));
        }
Example #3
0
        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));
        }