public async Task <IActionResult> Benevole(int id, int?year, int?month, bool partial = false)
        {
            var now = DateTime.Now;

            if (year == null)
            {
                year = now.Year;
            }

            if (month == null)
            {
                month = now.Month;
            }

            var benevole = await _context.Benevoles.Include(b => b.Adresses).ThenInclude(a => a.Centre)
                           .SingleOrDefaultAsync(b => b.ID == id);

            if (benevole == null)
            {
                return(NotFound());
            }

            var centreGere = GetCurrentUser().Centre;

            if (centreGere != null && !benevole.Adresses.Any(a => a.CentreID == centreGere.ID))
            {
                return(Forbid());
            }

            var model = new PointagesBenevoleModel
            {
                Centre    = centreGere,
                Benevole  = benevole,
                MonthDate = new DateTime(year.Value, month.Value, 1)
            };

            var dateFrom = new DateTime(year.Value, month.Value, 1);

            int dayOfWeekStartMonday = (int)dateFrom.DayOfWeek - 1;

            if (dayOfWeekStartMonday == -1)
            {
                dayOfWeekStartMonday = 6; // dimanche
            }
            dateFrom = dateFrom.AddDays(0 - dayOfWeekStartMonday);

            var dateTo = dateFrom.AddDays(PointagesBenevoleModel.CALENDAR_ROW_COUNT * PointagesBenevoleModel.CALENDAR_DAY_COUNT);

            var pointages = _context.Pointages
                            .Where(p => p.BenevoleID == id)
                            .Where(p => p.Date >= dateFrom && p.Date < dateTo)
                            .OrderBy(p => p.Date)
                            .ToDictionary(p => p.Date);

            var currentDate = dateFrom;

            var adresses = benevole.Adresses
                           .Where(a => a.DateChangement < dateTo)
                           .OrderBy(a => a.DateChangement)
                           .ToList();

            int addressIndex = 0;

            DateTime?nextChangeAddressDate = null;

            if (addressIndex + 1 < adresses.Count)
            {
                nextChangeAddressDate = adresses[addressIndex + 1].DateChangement;
            }

            for (int r = 0; r < PointagesBenevoleModel.CALENDAR_ROW_COUNT; r++)
            {
                var row = new CalendarRow();

                for (int i = 0; i < PointagesBenevoleModel.CALENDAR_DAY_COUNT; i++)
                {
                    while (nextChangeAddressDate != null && currentDate.Date >= nextChangeAddressDate)
                    {
                        addressIndex++;

                        if (addressIndex + 1 < adresses.Count)
                        {
                            nextChangeAddressDate = adresses[addressIndex + 1].DateChangement;
                        }
                        else
                        {
                            nextChangeAddressDate = null;
                        }
                    }

                    var item = new CalendarItem
                    {
                        Date           = currentDate,
                        Pointage       = pointages.GetValueOrDefault(currentDate),
                        IsCurrentMonth = (currentDate.Month == month && currentDate.Year == year),
                        RowIndex       = r,
                        ColumnIndex    = i,
                        Distance       = adresses[addressIndex].DistanceCentre,
                    };

                    if (centreGere != null && adresses[addressIndex].CentreID != centreGere.ID)
                    {
                        item.DisabledByCenter = true;
                    }

                    row.Items.Add(item);

                    currentDate = currentDate.AddDays(1);
                }

                model.CalendarRows.Add(row);
            }

            string viewName = "Benevole";

            if (partial)
            {
                viewName = "_BenevoleCalendarContent";
            }

            return(View(viewName, model));
        }
Exemple #2
0
 public static void ClickOnCell(this CalendarRow row, int idx) => row.CalendarCell.ElementAt(idx).Click();