public async Task <IActionResult> Apply() { var viewModel = new CongeFormulaire { CongeTypes = await _context.CongeTypes .Where(t => t.Label != Types.Absence) .AsNoTracking() .ToListAsync() }; return(View(viewModel)); }
public async Task <IActionResult> Apply([Bind("DateDebut, DateFin, CongeTypeID")] Conge conge) { var viewModel = new CongeFormulaire { CongeTypes = await _context.CongeTypes .Where(t => t.Label != Types.Absence) .AsNoTracking() .ToListAsync(), Conge = conge }; var currentUserId = _userManager.GetUserId(HttpContext.User); Dictionary <int, int> yearQuota = new Dictionary <int, int>(); yearQuota.Add(conge.DateDebut.Year, 0); if (conge.DateDebut.Year != conge.DateFin.Year) { yearQuota.Add(conge.DateFin.Year, 0); } var conges = await _context.Conges .Where(c => c.CollaborateurID == currentUserId) .Where(c => (c.DateDebut.Year == conge.DateDebut.Year || c.DateDebut.Year == conge.DateFin.Year) || (c.DateFin.Year == conge.DateDebut.Year || c.DateFin.Year == conge.DateFin.Year)) .Include(s => s.Statut) .Where(s => s.Statut.Label == Statuts.Accepte || s.Statut.Label == Statuts.Attente) .Include(t => t.CongeType) .Where(t => t.CongeType.Label == Types.Conge) .AsNoTracking() .ToListAsync(); var congesOwner = await _context.Conges .Where(c => c.CollaborateurID == currentUserId) .Include(s => s.Statut) .Where(s => s.Statut.Label == Statuts.Accepte || s.Statut.Label == Statuts.Attente) .AsNoTracking() .ToListAsync(); conges.Add(conge); foreach (var c in congesOwner) { if (c.DateDebut <= conge.DateFin && c.DateFin >= conge.DateDebut) { ViewData["Error"] = "Vous avez déjà une demande de congé durant cette période !"; return(View(viewModel)); } } var typeConge = await _context.CongeTypes.SingleAsync(t => t.Label == Types.Conge); foreach (var c in conges) { for (DateTime date = c.DateDebut; date <= c.DateFin; date = date.AddDays(1)) { if ((date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday) && (yearQuota.ContainsKey(date.Year))) { yearQuota[date.Year] = yearQuota[date.Year] + 1; if (yearQuota[date.Year] > 22 && c.CongeTypeID == typeConge.CongeTypeID) { ViewData["Error"] = "Vous dépassez votre quota pour l'année " + date.Year + " !"; return(View(viewModel)); } } } } try { conge.CollaborateurID = currentUserId; conge.StatutID = _context.Statuts .Single(s => s.Label == Statuts.Attente) .StatutID; conge.EnvoiDate = DateTime.Now; if (ModelState.IsValid) { _context.Add(conge); await _context.SaveChangesAsync(); return(RedirectToAction("Show")); } } catch (DbUpdateException ex) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(viewModel)); }