using System; using System.Collections.Generic; using BeWo.Data.Entities; using BeWo.Data.Access; using System.Linq; using BS.Shared.DataContracts; namespace BeWo.Report.ReportObjects { public class RosterRO : AbstractBeWoReportObject { public DateTime ReportDateTime { get; set; } public string WohnheimName { get; set; } public List RosterReportRows { get; set; } public int FirstWeekDay { get; set; } public List Feiertage { get; set; } public EmployeeDC Creator { get; set; } public RosterRO() { RosterReportRows = new List(); } public static RosterRO Create(List dienstBlockDCs, List dienstInfoDCs, BS.Shared.DataContracts.Compact.CompactWohnheimDC compactWohnheimDC, int year, int month) { var dt = new DateTime(year, month, 1); // Result var res = new RosterRO() { WohnheimName = compactWohnheimDC.WohnheimName, ReportDateTime = dt, FirstWeekDay = ((int)dt.DayOfWeek - 1) % 7 }; // WD V F Formel // Mo 1 0 (tag - 1) / 7 // Di 2 1 (tag + 0) / 7 // Mi 3 2 (tag + 1) / 7 // Do 4 3 (tag + 2) / 7 // Fr 5 4 (tag + 3) / 7 // Sa 6 5 (tag + 4) / 7 // So 0 6 (tag + 5) / 7 // // int week = (day + res.FirstWeekDay - 1) / 7; var infoDict = dienstInfoDCs.ToDictionary(x => x.Oid.Value, x => x); foreach (var block in dienstBlockDCs) { RosterReportRow row = null; double[] sum = new double[6]; for (int i = 0; i < block.DienstDictArray.Length; i++) { var dict = block.DienstDictArray[i]; row = new RosterReportRow() { Pos = i, Title = i == 0 ? block.Employee.LastNameFirstName : null, DienstDict = new Dictionary() }; foreach (var kv in dict) { var dienst = infoDict[kv.Value]; var day = kv.Key; row.DienstDict.Add(day, dienst); int week = (day + res.FirstWeekDay - 1) / 7; sum[week] += dienst.DurationHours; } res.RosterReportRows.Add(row); } row.WeekSum = sum; } return res; } public static RosterRO CreateNew(List dienstEintragDCs, List dienstvertretungDCs, List employee, List dienstInfoDCs, BS.Shared.DataContracts.Compact.CompactWohnheimDC compactWohnheimDC, int year, int month, List feiertage, int sortingIndex, bool aufsteigend, EmployeeDC creator) { var dt = new DateTime(year, month, 1); // Result var res = new RosterRO() { WohnheimName = compactWohnheimDC.WohnheimName, ReportDateTime = dt, FirstWeekDay = ((int)dt.DayOfWeek - 1) % 7, Feiertage = feiertage, Creator = creator }; // WD V F Formel // Mo 1 0 (tag - 1) / 7 // Di 2 1 (tag + 0) / 7 // Mi 3 2 (tag + 1) / 7 // Do 4 3 (tag + 2) / 7 // Fr 5 4 (tag + 3) / 7 // Sa 6 5 (tag + 4) / 7 // So 0 6 (tag + 5) / 7 // // int week = (day + res.FirstWeekDay - 1) / 7; var infoDict = dienstInfoDCs.ToDictionary(x => x.Oid.Value, x => x); //alle Dienste List> alleEintraegeSortiert = EintraegeSortieren(dienstEintragDCs); List> alleVertretungenSortiert = VertretungenSortieren(dienstvertretungDCs); res = EintraegeEinfuegen(res, alleEintraegeSortiert, employee, infoDict, year, month); res = VertretungenEinfuegen(res, alleVertretungenSortiert, employee, infoDict); // Hier sortieren nach DienstDict-Count. Sortier-Art aus Parameterliste nehmen if (sortingIndex == 0) // Alphabetisch { if (aufsteigend) res.RosterReportRows = res.RosterReportRows.OrderBy(r => r.Employee.LastName).ToList(); else res.RosterReportRows = res.RosterReportRows.OrderByDescending(r => r.Employee.LastName).ToList(); } else if (sortingIndex == 1) // Menge der Dienste { if (aufsteigend) res.RosterReportRows = res.RosterReportRows.OrderBy(r => r.DienstDict.Count).ToList(); else res.RosterReportRows = res.RosterReportRows.OrderByDescending(r => r.DienstDict.Count).ToList(); } return res; } private static List> EintraegeSortieren(List dienstEintragDCs) { List> alleEintraegeSortiert = new List>(); List empOids = new List(); foreach (var eintrag in dienstEintragDCs) { if (!empOids.Contains(eintrag.EmployeeOid)) empOids.Add(eintrag.EmployeeOid); } foreach (var oid in empOids) { List eintraegeProMitarbeiter = new List(); foreach (var eintrag in dienstEintragDCs) { if (eintrag.EmployeeOid == oid) eintraegeProMitarbeiter.Add(eintrag); } alleEintraegeSortiert.Add(eintraegeProMitarbeiter); } return alleEintraegeSortiert; } private static List> VertretungenSortieren(List dienstvertetungDCs) { List> allevertretungenSortiert = new List>(); List empOids = new List(); foreach (var vertretung in dienstvertetungDCs) { if (!empOids.Contains(vertretung.EmployeeOid)) empOids.Add(vertretung.EmployeeOid); } foreach (var oid in empOids) { List vertretungenProMitarbeiter = new List(); foreach (var vertretung in dienstvertetungDCs) { if (vertretung.EmployeeOid == oid) vertretungenProMitarbeiter.Add(vertretung); } allevertretungenSortiert.Add(vertretungenProMitarbeiter); } return allevertretungenSortiert; } private static RosterRO EintraegeEinfuegen(RosterRO res, List> alleEintraegeSortiert, List employee, Dictionary infoDict, int year, int month) { infoDict = AddAbwesenheitseinträgeToInfoDict(infoDict); foreach (var block in alleEintraegeSortiert) { int lastRowIndex = 0; foreach (var item in block) { if (item.Zeile > lastRowIndex) lastRowIndex = item.Zeile; } RosterReportRow row = null; double[] sum = new double[6]; List[] eintraegeNachZeilen = new List[lastRowIndex + 1]; for (int i = 0; i <= lastRowIndex; i++) eintraegeNachZeilen[i] = new List(); foreach (var b in block) eintraegeNachZeilen[b.Zeile].Add(b); EmployeeDC currentEmployee = new EmployeeDC(); foreach (var zeile in eintraegeNachZeilen) { if (zeile.Count > 0) currentEmployee = employee.First(e => e.EmployeeOid == zeile[0].EmployeeOid); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ContractDC currentContract = null; var reportDate = new DateTime(year, month, 1); foreach (var c in currentEmployee.Contracts) { if (c.ContractEndDate.HasValue) { if (c.EntryDate.HasValue && c.EntryDate.Value <= reportDate && c.ContractEndDate.Value >= reportDate) currentContract = c; } else { if (c.EntryDate.HasValue && c.EntryDate.Value <= reportDate) currentContract = c; } } // HIER URLAUB UND KRANKHEIT ALS DIENST ANLEGEN UND IN INFODICT EINFÜGEN, UM DIENSTEINTRÄGE MIT ENTSPRECHENDEM ABWESENHEITSDIENST ZU ERSTELLEN //foreach (var abwesenheit in currentEmployee.AbsenceTimes) //{ // long absOid = 0; // if (abwesenheit.Reason.Description.ToLower().Contains("urlaub")) // absOid = -1; // else if (abwesenheit.Reason.Description.ToLower().Contains("krank")) // absOid = -2; // else if (abwesenheit.Reason.Description.ToLower().Contains("krank")) // absOid = -3; // if (abwesenheit.Start.Value.Month == month && abwesenheit.Start.Value.Year == year || abwesenheit.End.Value.Month == month && abwesenheit.End.Value.Year == year) // { // var start = abwesenheit.Start.Value; // while (start <= abwesenheit.End.Value) // { // DienstEintragDC absEintrag = new DienstEintragDC() // { // Datum = start, // DauerInStunden = currentContract != null ? Convert.ToDouble(currentContract.WeeklyTotalHours / currentContract.WeeklyDays) : 8, // EmployeeOid = currentEmployee.EmployeeOid.Value, // Notice = abwesenheit.Notice, // Zeile = 0, // DienstOid = absOid // }; // eintraegeNachZeilen[0].Add(absEintrag); // start = start.AddDays(1); // } // } //} //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// double monthlyHours = 0; for (int i = 0; i < eintraegeNachZeilen.Length; i++) { var aktuellerBlock = eintraegeNachZeilen[i]; row = new RosterReportRow() { Pos = i, DienstDict = new Dictionary(), AlleDienste = infoDict }; if (currentEmployee.EmployeeOid == null) { EmployeeDC emp = new EmployeeDC(); foreach (var e in employee) { foreach (var b in aktuellerBlock) { if (b.EmployeeOid == e.EmployeeOid) { emp = e; currentEmployee = e; break; } } if (emp.EmployeeOid != null) break; } } row.Employee = currentEmployee; if (i == 0) row.Title = currentEmployee.LastNameFirstName; DienstInfoDC dienst = null; // List doppelteDienste = new List(); // foreach (var b in aktuellerBlock) { foreach (var d in infoDict) { if (d.Value.Oid == b.DienstOid) { dienst = d.Value; break; } } var day = b.Datum.Day; if (!row.DienstDict.ContainsKey(day)) row.DienstDict.Add(day, dienst); else { row.DienstDict[day] = dienst; //string s = currentEmployee.LastNameFirstName + ", Tag " + day + ": " + dienst.Start + " - " + dienst.Ende; //doppelteDienste.Add(dienst); } int week = (day + res.FirstWeekDay - 1) / 7; sum[week] += b.DauerInStunden; monthlyHours += b.DauerInStunden; } res.RosterReportRows.Add(row); } row.WeekSum = sum; row.MonthSum = monthlyHours; } return res; } private static RosterRO VertretungenEinfuegen(RosterRO res, List> alleVertretungenSortiert, List employee, Dictionary infoDict) { foreach (var block in alleVertretungenSortiert) { int lastRowIndex = 0; foreach (var item in block) { if (item.Zeile > lastRowIndex) lastRowIndex = item.Zeile; } RosterReportRow row = null; double[] sum = new double[6]; List[] vertretungenNachZeilen = new List[lastRowIndex + 1]; for (int i = 0; i <= lastRowIndex; i++) vertretungenNachZeilen[i] = new List(); foreach (var b in block) vertretungenNachZeilen[b.Zeile].Add(b); EmployeeDC currentEmployee = new EmployeeDC(); foreach (var zeile in vertretungenNachZeilen) { if (zeile.Count > 0) currentEmployee = employee.FirstOrDefault(e => e.EmployeeOid == zeile[0].EmployeeOid); } if (currentEmployee == null) continue; double monthlyHours = 0; for (int i = 0; i < vertretungenNachZeilen.Length; i++) { var aktuellerBlock = vertretungenNachZeilen[i]; row = new RosterReportRow() { Pos = i, DienstDict = new Dictionary(), AlleDienste = infoDict }; EmployeeDC emp = new EmployeeDC(); foreach (var e in employee) { foreach (var b in aktuellerBlock) { if (b.EmployeeOid == e.EmployeeOid) { emp = e; currentEmployee = e; break; } } if (emp.EmployeeOid != null) break; } row.Employee = currentEmployee; if (i == 0) row.Title = currentEmployee.LastNameFirstName; DienstInfoDC dienst = null; foreach (var b in aktuellerBlock) { foreach (var d in infoDict) { if (d.Value.Oid == b.DienstOid) { dienst = d.Value; break; } } var day = b.Datum.Day; row.DienstDict.Add(day, dienst); int week = (day + res.FirstWeekDay - 1) / 7; sum[week] += b.DauerInStunden; monthlyHours += b.DauerInStunden; } res.RosterReportRows.Add(row); } row.WeekSum = sum; row.MonthSum = (double)monthlyHours; } res.RosterReportRows = res.RosterReportRows.OrderBy(r => r.Employee.LastNameFirstName).ThenBy(r => r.Pos).ToList(); return res; } private static Dictionary AddAbwesenheitseinträgeToInfoDict(Dictionary infoDict) { infoDict.Add(-1, new DienstInfoDC() { //Dauer = Convert.ToDouble(currentContract.WeeklyTotalHours / currentContract.WeeklyDays), DienstName = "Urlaub", Farbe = "#FFFFFF", Kurz = "U", Oid = -1 }); infoDict.Add(-2, new DienstInfoDC() { //Dauer = Convert.ToDouble(currentContract.WeeklyTotalHours / currentContract.WeeklyDays), DienstName = "Krankheit", Farbe = "#FFFFFF", Kurz = "K", Oid = -2 }); infoDict.Add(-3, new DienstInfoDC() { //Dauer = Convert.ToDouble(currentContract.WeeklyTotalHours / currentContract.WeeklyDays), DienstName = "Fortbildung", Farbe = "#FFFFFF", Kurz = "F", Oid = -3 }); return infoDict; } } public class RosterReportRow { public Dictionary AlleDienste { get; set; } public string Title { get; set; } public int Pos { get; set; } public EmployeeDC Employee { get; set; } public double[] WeekSum { get; set; } public double? MonthSum { get; set; } public Dictionary DienstDict { get; set; } public double? Week1 => WeekSum?[0]; public double? Week2 => WeekSum?[1]; public double? Week3 => WeekSum?[2]; public double? Week4 => WeekSum?[3]; public double? Week5 => WeekSum?[4]; public double? Week6 => WeekSum?[5]; public DienstInfoDC Day1 => GetDienst(1); public DienstInfoDC Day2 => GetDienst(2); public DienstInfoDC Day3 => GetDienst(3); public DienstInfoDC Day4 => GetDienst(4); public DienstInfoDC Day5 => GetDienst(5); public DienstInfoDC Day6 => GetDienst(6); public DienstInfoDC Day7 => GetDienst(7); public DienstInfoDC Day8 => GetDienst(8); public DienstInfoDC Day9 => GetDienst(9); public DienstInfoDC Day10 => GetDienst(10); public DienstInfoDC Day11 => GetDienst(11); public DienstInfoDC Day12 => GetDienst(12); public DienstInfoDC Day13 => GetDienst(13); public DienstInfoDC Day14 => GetDienst(14); public DienstInfoDC Day15 => GetDienst(15); public DienstInfoDC Day16 => GetDienst(16); public DienstInfoDC Day17 => GetDienst(17); public DienstInfoDC Day18 => GetDienst(18); public DienstInfoDC Day19 => GetDienst(19); public DienstInfoDC Day20 => GetDienst(20); public DienstInfoDC Day21 => GetDienst(21); public DienstInfoDC Day22 => GetDienst(22); public DienstInfoDC Day23 => GetDienst(23); public DienstInfoDC Day24 => GetDienst(24); public DienstInfoDC Day25 => GetDienst(25); public DienstInfoDC Day26 => GetDienst(26); public DienstInfoDC Day27 => GetDienst(27); public DienstInfoDC Day28 => GetDienst(28); public DienstInfoDC Day29 => GetDienst(29); public DienstInfoDC Day30 => GetDienst(30); public DienstInfoDC Day31 => GetDienst(31); public DienstInfoDC GetDienst(int i) => DienstDict.ContainsKey(i) ? DienstDict[i] : null; } }