Files

571 lines
21 KiB
C#
Raw Permalink Normal View History

2020-09-30 10:00:31 +02:00
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; }
2022-12-29 13:57:48 +01:00
public EmployeeDC Creator { get; set; }
2020-09-30 10:00:31 +02:00
public RosterRO()
{
RosterReportRows = new List<RosterReportRow>();
}
2020-09-30 10:00:31 +02:00
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>()
};
2020-09-30 10:00:31 +02:00
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;
}
2021-04-06 08:06:51 +02:00
return res;
}
public static RosterRO CreateNew(List<DienstEintragDC> dienstEintragDCs, List<DienstvertretungDC> dienstvertretungDCs, List<EmployeeDC> employee,
2022-12-29 13:57:48 +01:00
List<DienstInfoDC> dienstInfoDCs, BS.Shared.DataContracts.Compact.CompactWohnheimDC compactWohnheimDC, int year, int month, List<int> feiertage, int sortingIndex, bool aufsteigend, EmployeeDC creator)
2021-04-06 08:06:51 +02:00
{
var dt = new DateTime(year, month, 1);
// Result
var res = new RosterRO()
{
WohnheimName = compactWohnheimDC.WohnheimName,
ReportDateTime = dt,
FirstWeekDay = ((int)dt.DayOfWeek - 1) % 7,
2022-12-29 13:57:48 +01:00
Feiertage = feiertage,
Creator = creator
2021-04-06 08:06:51 +02:00
};
// 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, year, month);
2021-04-06 08:06:51 +02:00
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();
}
2021-04-06 08:06:51 +02:00
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, int year, int month)
2021-04-06 08:06:51 +02:00
{
infoDict = AddAbwesenheitseinträgeToInfoDict(infoDict);
2021-04-06 08:06:51 +02:00
foreach (var block in alleEintraegeSortiert)
{
int lastRowIndex = 0;
foreach (var item in block)
{
if (item.Zeile > lastRowIndex)
lastRowIndex = item.Zeile;
}
2021-04-06 08:06:51 +02:00
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>();
2021-04-06 08:06:51 +02:00
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)
{
2024-01-31 16:45:28 +01:00
if (c.EntryDate.HasValue && c.EntryDate.Value <= reportDate && c.ContractEndDate.Value >= reportDate)
currentContract = c;
}
else
{
2024-01-31 16:45:28 +01:00
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
2023-03-27 10:24:25 +02:00
//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);
// }
// }
//}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2021-04-06 08:06:51 +02:00
double monthlyHours = 0;
for (int i = 0; i < eintraegeNachZeilen.Length; i++)
{
var aktuellerBlock = eintraegeNachZeilen[i];
2021-04-06 08:06:51 +02:00
row = new RosterReportRow()
{
Pos = i,
DienstDict = new Dictionary<int, DienstInfoDC>(),
AlleDienste = infoDict
};
if (currentEmployee.EmployeeOid == null)
2021-04-06 08:06:51 +02:00
{
EmployeeDC emp = new EmployeeDC();
foreach (var e in employee)
2021-04-06 08:06:51 +02:00
{
foreach (var b in aktuellerBlock)
2021-04-06 08:06:51 +02:00
{
if (b.EmployeeOid == e.EmployeeOid)
{
emp = e;
currentEmployee = e;
break;
}
2021-04-06 08:06:51 +02:00
}
if (emp.EmployeeOid != null)
break;
2021-04-06 08:06:51 +02:00
}
}
2021-04-06 08:06:51 +02:00
row.Employee = currentEmployee;
if (i == 0)
row.Title = currentEmployee.LastNameFirstName;
2021-04-06 08:06:51 +02:00
DienstInfoDC dienst = null;
2022-10-14 13:41:53 +02:00
//
List<DienstInfoDC> doppelteDienste = new List<DienstInfoDC>();
//
2021-04-06 08:06:51 +02:00
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;
2022-10-14 13:41:53 +02:00
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);
}
2021-04-06 08:06:51 +02:00
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;
}
2021-04-06 08:06:51 +02:00
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>();
2021-04-06 08:06:51 +02:00
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;
2021-04-06 08:06:51 +02:00
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;
2021-04-06 08:06:51 +02:00
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;
2021-04-06 08:06:51 +02:00
}
res.RosterReportRows = res.RosterReportRows.OrderBy(r => r.Employee.LastNameFirstName).ThenBy(r => r.Pos).ToList();
2020-09-30 10:00:31 +02:00
return res;
}
private static Dictionary<long, DienstInfoDC> AddAbwesenheitseinträgeToInfoDict(Dictionary<long, DienstInfoDC> 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;
}
2020-09-30 10:00:31 +02:00
}
2020-09-30 10:00:31 +02:00
public class RosterReportRow
{
2021-04-06 08:06:51 +02:00
public Dictionary<long, DienstInfoDC> AlleDienste { get; set; }
2020-09-30 10:00:31 +02:00
public string Title { get; set; }
public int Pos { get; set; }
public EmployeeDC Employee { get; set; }
2020-09-30 10:00:31 +02:00
public double[] WeekSum { get; set; }
2021-04-06 08:06:51 +02:00
public double? MonthSum { get; set; }
2020-09-30 10:00:31 +02:00
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];
2020-09-30 10:00:31 +02:00
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;
}
}