using System; using System.Collections.Generic; using System.Linq; using BeWo.Data.Access; using BeWo.Data.Entities; using BeWo.Service.DCEntityMapper; using BS.Shared; using BS.Shared.DataContracts; using BS.Shared.Extensions; namespace BeWo.Report.ReportObjects { public class FLSSollIstReportRO { public DateTime? ReportEndDate { get; set; } public DateTime? ReportStartDate { get; set; } public List RootObjectList { get; set; } = new List(); public class FLSSollIstRootRO { public string Name { get; set; } public long ObjectOid { get; set; } public TableID ObjectTid { get; set; } public List Groups { get; set; } = new List(); public decimal SollGesamt { get; set; } public decimal IstGesamt { get; set; } public decimal DifferenzSollIstGesamt { get; set; } public String CustomValue1 { get; set; } public String CustomValue2 { get; set; } public String CustomValue3 { get; set; } public String CustomValue4 { get; set; } public String CustomValue5 { get; set; } public List ServiceRecords { get; internal set; } = new List(); } public class FLSSollIstReportGroupRO { public string Name { get; set; } public long ObjectOid { get; set; } public TableID ObjectTid { get; set; } public List CostBearer2SupportConceptList { get; set; } = new List(); public List WochenSummen { get; set; } = new List(); public decimal SollGesamt { get; set; } public decimal IstGesamt { get; set; } public decimal DifferenzSollIstGesamt { get; set; } public String CustomValue1 { get; set; } public String CustomValue2 { get; set; } public String CustomValue3 { get; set; } public String CustomValue4 { get; set; } public String CustomValue5 { get; set; } public List ServiceRecords { get; internal set; } = new List(); } public class FLSSollIstWochenSummen { public decimal? SollStunden { get; set; } public decimal? IstStunden { get; set; } public decimal? Differenz { get { if (IstStunden.HasValue && SollStunden.HasValue) { return IstStunden - SollStunden; } if (SollStunden.HasValue) { return SollStunden * -1; } return IstStunden; } } public DateTime WocheStart { get; set; } public DateTime WocheEnde { get; set; } public int KalenderWoche { get { var ci = System.Globalization.CultureInfo.InvariantCulture; return ci.Calendar.GetWeekOfYear( WocheStart, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); } } public List ServiceRecords { get; internal set; } = new List(); } public static FLSSollIstReportRO Create(List dcList, FLSAnalysisConfigDC config) { var ro = new FLSSollIstReportRO(); ro.ReportStartDate = config.StartDate; ro.ReportEndDate = config.EndDate; foreach (var root in dcList.OrderBy(a => a.DisplayName)) { var rootRo = new FLSSollIstRootRO(); rootRo.Name = root.DisplayName; rootRo.SollGesamt = 0; //root.SollFLSTotal ?; if (root.Employee != null) { rootRo.ObjectOid = root.Employee.EmployeeOid; rootRo.ObjectTid = TableID.Employee; } else if (root.Customer != null) { rootRo.ObjectOid = root.Customer.CustomerOid; rootRo.ObjectTid = TableID.Customer; } foreach (var item in root.Groups.OrderBy(a => a.DisplayName)) { var grpRo = new FLSSollIstReportGroupRO(); grpRo.Name = item.DisplayName; grpRo.WochenSummen = ErstelleWochensummen(config.StartDate, config.EndDate, null); if (item.Employee != null) { grpRo.ObjectOid = item.Employee.EmployeeOid; grpRo.ObjectTid = TableID.Employee; } else if (item.Customer != null) { grpRo.ObjectOid = item.Customer.CustomerOid; grpRo.ObjectTid = TableID.Customer; } foreach (var sc in item.SupportConcepts) { grpRo.CostBearer2SupportConceptList.Add(sc.SupportConcept2CostBearer); foreach (var sr in sc.ServiceRecords) { rootRo.ServiceRecords.Add(sr); grpRo.ServiceRecords.Add(sr); var ws = grpRo.WochenSummen.FirstOrDefault(w => w.WocheStart <= sr.Start && sr.Start < w.WocheEnde.AddDays(1)); if (ws != null) { ws.ServiceRecords.Add(sr); if (!ws.IstStunden.HasValue) { ws.IstStunden = 0; } ws.IstStunden += sr.RoundedDuration / 60; if (!ws.SollStunden.HasValue) { ws.SollStunden = sr.SollFLSPerWeek; } } grpRo.IstGesamt += sr.RoundedDuration; } } foreach (var ws in grpRo.WochenSummen) { if (!ws.SollStunden.HasValue) { ws.SollStunden = root.SollFLSPerWeek; } } grpRo.IstGesamt = grpRo.IstGesamt / 60; rootRo.IstGesamt += grpRo.IstGesamt; rootRo.SollGesamt = grpRo.WochenSummen.Sum(w => w.SollStunden) ?? 0; rootRo.Groups.Add(grpRo); } rootRo.DifferenzSollIstGesamt = rootRo.IstGesamt - rootRo.SollGesamt; if (rootRo.Groups.Count > 0) { ro.RootObjectList.Add(rootRo); } } return ro; } private static List ErstelleWochensummen(DateTime? startDate, DateTime? endDate, decimal? defaultSoll) { var liste = new List(); if (startDate.HasValue && endDate.HasValue) { DateTime dt = startDate.Value; while (dt <= endDate.Value) { int days = ((int)DayOfWeek.Sunday - (int)dt.DayOfWeek + 7) % 7; var sonntag = dt.AddDays(days); var ws = new FLSSollIstWochenSummen(); ws.WocheStart = dt; //Ermittele den nächsten Sonntag ws.WocheEnde = sonntag; if (ws.WocheEnde > endDate) { ws.WocheEnde = endDate.Value; } ws.SollStunden = defaultSoll; liste.Add(ws); dt = sonntag.AddDays(1); } } return liste; } } }