Files
BeWoPlaner/Report/ReportObjects/RosterRO.cs

452 lines
16 KiB
C#

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<RosterRO>
{
public DateTime ReportDateTime { get; set; }
public string WohnheimName { get; set; }
public List<RosterReportRow> RosterReportRows { get; set; }
public int FirstWeekDay { get; set; }
public List<int> Feiertage { get; set; }
public RosterRO()
{
RosterReportRows = new List<RosterReportRow>();
}
public static RosterRO Create(List<DienstBlockDC> dienstBlockDCs, List<DienstInfoDC> 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<int, DienstInfoDC>()
};
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<DienstEintragDC> dienstEintragDCs, List<DienstvertretungDC> dienstvertretungDCs, List<EmployeeDC> employee, List<DienstInfoDC> dienstInfoDCs, BS.Shared.DataContracts.Compact.CompactWohnheimDC compactWohnheimDC, int year, int month, List<int> feiertage)
{
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
};
// 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<List<DienstEintragDC>> alleEintraegeSortiert = EintraegeSortieren(dienstEintragDCs);
List<List<DienstvertretungDC>> alleVertretungenSortiert = VertretungenSortieren(dienstvertretungDCs);
res = EintraegeEinfuegen(res, alleEintraegeSortiert, employee, infoDict);
res = VertretungenEinfuegen(res, alleVertretungenSortiert, employee, infoDict);
return res;
}
private static List<List<DienstEintragDC>> EintraegeSortieren(List<DienstEintragDC> dienstEintragDCs)
{
List<List<DienstEintragDC>> alleEintraegeSortiert = new List<List<DienstEintragDC>>();
List<long> empOids = new List<long>();
foreach (var eintrag in dienstEintragDCs)
{
if (!empOids.Contains(eintrag.EmployeeOid))
empOids.Add(eintrag.EmployeeOid);
}
foreach (var oid in empOids)
{
List<DienstEintragDC> eintraegeProMitarbeiter = new List<DienstEintragDC>();
foreach (var eintrag in dienstEintragDCs)
{
if (eintrag.EmployeeOid == oid)
eintraegeProMitarbeiter.Add(eintrag);
}
alleEintraegeSortiert.Add(eintraegeProMitarbeiter);
}
return alleEintraegeSortiert;
}
private static List<List<DienstvertretungDC>> VertretungenSortieren(List<DienstvertretungDC> dienstvertetungDCs)
{
List<List<DienstvertretungDC>> allevertretungenSortiert = new List<List<DienstvertretungDC>>();
List<long> empOids = new List<long>();
foreach (var vertretung in dienstvertetungDCs)
{
if (!empOids.Contains(vertretung.EmployeeOid))
empOids.Add(vertretung.EmployeeOid);
}
foreach (var oid in empOids)
{
List<DienstvertretungDC> vertretungenProMitarbeiter = new List<DienstvertretungDC>();
foreach (var vertretung in dienstvertetungDCs)
{
if (vertretung.EmployeeOid == oid)
vertretungenProMitarbeiter.Add(vertretung);
}
allevertretungenSortiert.Add(vertretungenProMitarbeiter);
}
return allevertretungenSortiert;
}
private static RosterRO EintraegeEinfuegen(RosterRO res, List<List<DienstEintragDC>> alleEintraegeSortiert, List<EmployeeDC> employee, Dictionary<long, DienstInfoDC> 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<DienstEintragDC>[] eintraegeNachZeilen = new List<DienstEintragDC>[lastRowIndex + 1];
for (int i = 0; i <= lastRowIndex; i++)
eintraegeNachZeilen[i] = new List<DienstEintragDC>();
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);
}
double monthlyHours = 0;
for (int i = 0; i < eintraegeNachZeilen.Length; i++)
{
var aktuellerBlock = eintraegeNachZeilen[i];
row = new RosterReportRow()
{
Pos = i,
DienstDict = new Dictionary<int, DienstInfoDC>(),
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;
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 = monthlyHours;
}
return res;
}
private static RosterRO VertretungenEinfuegen(RosterRO res, List<List<DienstvertretungDC>> alleVertretungenSortiert, List<EmployeeDC> employee, Dictionary<long, DienstInfoDC> 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<DienstvertretungDC>[] vertretungenNachZeilen = new List<DienstvertretungDC>[lastRowIndex + 1];
for (int i = 0; i <= lastRowIndex; i++)
vertretungenNachZeilen[i] = new List<DienstvertretungDC>();
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<int, DienstInfoDC>(),
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;
}
}
public class RosterReportRow
{
public Dictionary<long, DienstInfoDC> 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<int, DienstInfoDC> 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;
}
}