private static void FillNewTache(IXLWorksheet sheet, Tache tache, JourEvenement jour, ref int line, Planning p, IEnumerable <Affectation> affectations, bool readableExport) { IXLCell c = sheet.Cell("A" + line); c.Value = tache.Id; c = sheet.Cell("B" + line); c.Value = tache.Nom; c.Style.Font.FontSize = 20; c.Style.Font.Bold = true; line++; IXLRow r = sheet.Row(line); int j = FIRST_PLAN_COLUMN; foreach (var def in jour.CreneauDefs.OrderBy(s => s.NoCreneau)) { c = r.Cell(j); c.Value = "'" + def.Debut.ToString("HH:mm", CultureInfo.InvariantCulture); j++; } line++; int maxB = tache.GetMaxBenevoleByDay(jour.Id); for (int i = 0; i < maxB; i++) { r = sheet.Row(line); FIllNewRow(r, jour, tache, p, i == 0, i == maxB - 1, i, affectations, readableExport); line++; } }
private static void ImportTache(IXLWorksheet sheet, ref int line, Tache t, JourEvenement jour, Planning p, FestivArtsContext ctx) { IXLRow timeRow = sheet.Row(line); line++; int maxB = t.GetMaxBenevoleByDay(jour.Id); Regex regex = new Regex("^([0-9]+)"); var aAjouter = new Dictionary <int, HashSet <int> >(); for (int l = 0; l < maxB; l++) { IXLRow r = sheet.Row(line); int i = ExcelUtils.FIRST_PLAN_COLUMN; foreach (CreneauDef d in jour.CreneauDefs.OrderBy(s => s.NoCreneau)) { var cell = r.Cell(i); string benStr = cell.Value.ToString(); if (!string.IsNullOrWhiteSpace(benStr)) { var m = regex.Match(benStr.Trim()); if (m.Success) { int id = int.Parse(m.Groups[0].Captures[0].Value); var b = ctx.Benevoles.FirstOrDefault(s => s.Id == id); if (b == null) { throw new ImportException(string.Format("Cell ({0}) Tache {1} : n° de bénévole introuvable : {2}'", cell.Address.ToStringRelative(true), t.Nom, benStr)); } var c = t.Creneaux.FirstOrDefault(s => s.CreneauDefId == d.Id); if (c == null) { throw new ImportException(string.Format("Cell ({0}) Tache {1} : creneau introuvable. Creneau def {2}'", cell.Address.ToStringRelative(true), t.Nom, d.Id)); } if (!aAjouter.ContainsKey(b.Id)) { aAjouter.Add(b.Id, new HashSet <int>()); } if (!aAjouter[b.Id].Contains(c.Id)) { aAjouter[b.Id].Add(c.Id); } } else { throw new ImportException(string.Format("Cell ({0}) ne correspond pas a un n° de bénévole : {1}'", cell.Address.ToStringRelative(true), benStr)); } } i++; } line++; } if (aAjouter.Count > 0) { var anciensA = ctx.Affectations.Where(s => s.Creneau.CreneauDef.JourId == jour.Id && s.PlanningId == p.Id && s.Creneau.TacheId == t.Id).ToList(); foreach (Affectation a in anciensA) { if (aAjouter.ContainsKey(a.BenevoleId) && aAjouter[a.BenevoleId].Contains(a.CreneauId)) { //L'affectation existait déjà, on ne fait rien aAjouter[a.BenevoleId].Remove(a.CreneauId); } else { //N'existe plus, on la supprime; ctx.Affectations.Remove(a); } } foreach (var kv in aAjouter.Where(s => s.Value.Count > 0).ToList()) { foreach (var v in kv.Value) { Affectation a = new Affectation() { BenevoleId = kv.Key, CreneauId = v, PlanningId = p.Id }; ctx.Affectations.Add(a); } } ctx.SaveChanges(); } }