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.Core; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using BS.Shared.Extensions; using BS.Shared.Services; using BeWo.Service.Configuration; using System.Text; namespace BeWo.Service.Plugins { public class SupportConceptAnalysisCreator : AbstractIDSpecificDefaultClass { private Dictionary> _cs2ServiceRecords = null; public virtual List GetSupportConceptAnalysis(IList c2sOids, long? employeeOid, long? teamOid, DateTime? appointedDate, int? expiredMonths, bool showArchivedScs) { return GetSupportConceptAnalysis2(c2sOids, employeeOid, teamOid, null, null, appointedDate, expiredMonths, showArchivedScs); } public virtual List GetSupportConceptAnalysisItems(IList c2sOids, long? employeeOid, long? teamOid, long? catOid, DateTime? startDate, DateTime? endDate, int? expiredMonths, bool showArchivedScs) { //return GetSupportConceptAnalysis2Alt(c2sOids, employeeOid, teamOid, catOid, startDate, endDate, expiredMonths, showArchivedScs); var lResult = new List(); long starttime = DateTime.Now.Ticks; if (catOid.HasValue && catOid.Value > 0) { //return GetSupportConceptAnalysisWithCategory(c2sOids, employeeOid, teamOid, catOid.Value, startDate, endDate, expiredMonths, showArchivedScs); } DateTime filterStartDate = startDate.HasValue ? startDate.Value : DateTime.MinValue; DateTime filterEndDate = endDate.HasValue ? endDate.Value.AddDays(1) : DateTime.MaxValue; List alleItems = new List(); var scList = DAOFactory.SearchDAO.FindSupportConceptsInDateRange(showArchivedScs, filterStartDate, filterEndDate); //Filter alle Hilfepläne, die überhaupt durch die Parameter infrage kommen StringBuilder oidList = new StringBuilder(); foreach (var sc in scList) { if (sc.Customer.Person.LastName == "Arndt" || sc.Customer.Person.LastName == "Arsovska") { int test = 0; } //Wenn TeamOids gesetzt ist, prüfe ob Klient zu einem der Teams gehört if (teamOid != null) { bool isRelatedToTeam = false; if (sc.Customer.Team2CustomerList.Count == 0) { var team = DAOFactory.GenericDAO.LoadByID(teamOid.Value); Dictionary teamMemberOidDict = new Dictionary(); foreach (var member in team.MemberList) { if (!teamMemberOidDict.ContainsKey(member.Oid.Value)) { teamMemberOidDict.Add(member.Oid.Value, member.Oid.Value); } //if (team.Leader != null && !teamMemberOidDict.ContainsKey(team.Leader.Oid.Value)) //{ // teamMemberOidDict.Add(team.Leader.Oid.Value, team.Leader.Oid.Value); //} } foreach (var e2c in sc.Customer.Employee2CustomerList) { if (!isRelatedToTeam) { if (teamMemberOidDict.ContainsKey(e2c.EmployeeOid.Value)) { isRelatedToTeam = true; } } } } else { foreach (var team2Customer in sc.Customer.Team2CustomerList) { if (team2Customer.TeamOid == teamOid) { isRelatedToTeam = true; } } } if (!isRelatedToTeam) { //Gehört nicht zum Team also überspringen continue; } } // Wenn Mitarbeiter gesetzt ist prüfe ob Klient von Mitarbeiter betreut wird if (employeeOid != null && teamOid == null) { bool isRelatedToEmployee = false; foreach (var e2c in sc.Customer.Employee2CustomerList) { if (!isRelatedToEmployee) { if (employeeOid.Value == e2c.EmployeeOid.Value) { isRelatedToEmployee = true; } } } if (!isRelatedToEmployee) { //Wird nicht betreut also überspringen continue; } } foreach (var c2s in sc.CostBearer2SupportConceptList) { bool cont = AddToResult(c2s); if (cont) { if (c2sOids != null) { if (!c2sOids.Contains(c2s.Oid.Value)) { cont = false; } } else { if (expiredMonths.HasValue) { cont = c2s.EndDate.HasValue && c2s.EndDate.Value < DateTime.Now.AddMonths(expiredMonths.Value); } } DateTime minDt = filterStartDate; if (cont) { cont = !c2s.EndDate.HasValue || c2s.EndDate.Value >= minDt; } if (cont) { var item = new SupportConceptOverviewItem(); item.SupportConcept = sc; item.CostBearer2SupportConcept = c2s; alleItems.Add(item); if (oidList.Length > 0) { oidList.Append(","); } oidList.Append(c2s.Oid); } } } } var allServiceDescs = DAOFactory.GenericDAO.GetAll(); Dictionary serviceDict = new Dictionary(); foreach (var item in allServiceDescs) { serviceDict.Add(item.Oid.Value, MapperFactory.ServiceDescriptionDC_ServiceDescription.MapToNewDC(item)); } _cs2ServiceRecords = new Dictionary>(); if (alleItems.Count > 0) { String sql = "SELECT Oid, Version, StartDate, EndDate, DistanceInMeter, CostBearer2SupportConceptOid, Betrag, ServiceDescriptionOid, RoundedDuration, GroupEmployeeCount, GroupPersonCount, GroupRoundedDuration, GroupOid, oid as 'test' FROM servicerecord"; sql += String.Format(" WHERE CostBearer2SupportConceptOid in ({0})", oidList.ToString()); //sql += " WHERE CostBearer2SupportConceptOid is not null" var result = DAOFactory.SearchDAO.GetSqlResult(sql); // //update customer set debitornumber = '5' + debitornumber where debitornumber is not null foreach (object[] srRow in result) { var sr = CreateServiceRecordDC2(srRow, serviceDict); if (sr.CostBearer2SupportConceptOid.HasValue) { if (!_cs2ServiceRecords.ContainsKey(sr.CostBearer2SupportConceptOid.Value)) { _cs2ServiceRecords.Add(sr.CostBearer2SupportConceptOid.Value, new List()); } _cs2ServiceRecords[sr.CostBearer2SupportConceptOid.Value].Add(sr); } } } foreach (var item in alleItems) { var iDC = new SupportConceptOverviewDC(); var sc = item.SupportConcept; var c2s = item.CostBearer2SupportConcept; var scDC = MapperFactory.CompactSupportConceptDC_SupportConcept.MapToNewDC(sc); var calc = PluginLoader.FindClass(c2s.CostBearer.ID) ?? Calculations.GetInstance(c2s.CostBearer.ID); iDC.ActivationType = sc.IsActive; if (iDC.ActivationType == ActivationTypeId.Active && sc.Customer.IsActive != ActivationTypeId.Active) { iDC.ActivationType = sc.Customer.IsActive; } DateTime minDt = filterStartDate; iDC.CalculationDate = DateTime.Now.Date; iDC.RequestedStartDate = c2s.RequestedStartDate; iDC.RequestedEndDate = c2s.RequestedEndDate; iDC.SupportConceptOid = sc.Oid.Value; iDC.CostBearer2SupportConceptOid = c2s.Oid.Value; iDC.CostBearer = c2s.CostBearer.Organisation != null ? c2s.CostBearer.Organisation.Name : string.Empty; iDC.CostBearerId = c2s.CostBearer.ID; iDC.CustomerOid = sc.Customer.Oid.Value; iDC.CustomerFirstName = sc.Customer.Person.FirstName; iDC.CustomerLastName = sc.Customer.Person.LastName; var costRates = c2s.CostBearer.CostRatePeriods.MapToNewDCs(); iDC.ServiceUnit = costRates.GetCurrentlyValidRate(CostRatePeriodType.MinutesPerServiceUnit); if (iDC.ServiceUnit == null) { iDC.ServiceUnit = new CostRatePeriodDC(); iDC.ServiceUnit.CostRateType = CostRatePeriodType.MinutesPerServiceUnit; iDC.ServiceUnit.UnitName = "FLS"; iDC.ServiceUnit.CostRateValue = 60; } iDC.CurrentHourlyRate = costRates.GetCurrentlyValidRateValue(CostRatePeriodType.HourlyRate) ?? 0m; iDC.CurrentRateFactor = costRates.GetCurrentlyValidRateValue(CostRatePeriodType.RateFactor) ?? 0m; var relDC = CreateCostbearer2SupportConceptDC(c2s, scDC); if (c2s.ApprovedStartDate.HasValue && c2s.ApprovedStartDate.Value > minDt) { minDt = c2s.ApprovedStartDate.Value.Date; } else if (c2s.RequestedStartDate.HasValue && c2s.RequestedStartDate.Value > minDt) { minDt = c2s.RequestedStartDate.Value.Date; } var records = GetServiceRecordDCs(c2s, minDt, filterEndDate); iDC.ServiceRecords = records; iDC.FLSRecordedTillNow = BerechneGeleisteteStunden(records, calc); iDC.FLSBilledTillNow = iDC.FLSRecordedTillNow; decimal lDurationInWeeksTillNow = calc.GetDurationInWeeksTillNow(relDC, endDate) ?? 0; decimal lDurationInWeeks = calc.GetDurationInWeeks(relDC) ?? 0; if (c2s.Status == CostBearer2SupportConceptStatus.Approved) { iDC.IsApproved = true; if (c2s.ApprovedStartDate != null && c2s.ApprovedEndDate != null) { iDC.ApprovedStartDate = c2s.ApprovedStartDate.Value; iDC.ApprovedEndDate = c2s.ApprovedEndDate.Value; } } else { iDC.IsApproved = false; } BerechneBewilligteStunden(calc, iDC, relDC, endDate); if (iDC.FLSApprovedSum <= 0) { iDC.FLSRecordedTillNowInPercent = 0; } else { iDC.FLSRecordedTillNowInPercent = iDC.FLSRecordedTillNow / iDC.FLSApprovedSum * 100; } iDC.FLSDifferenceApprovedRecordedTillNow = iDC.FLSRecordedTillNow - iDC.FLSApprovedTillNow; if (iDC.FLSApprovedTillNow <= 0) { iDC.FLSDifferenceApprovedRecordedTillNowInPercent = 0; } else { iDC.FLSDifferenceApprovedRecordedTillNowInPercent = iDC.FLSRecordedTillNow / iDC.FLSApprovedTillNow * 100; } iDC.FLSOpen = iDC.FLSApprovedSum - iDC.FLSRecordedTillNow; iDC.OpenWeeks = lDurationInWeeks - lDurationInWeeksTillNow; //TODO kostenträger spezifisch!? in Calculations verlagern! iDC.OpenMonths = iDC.OpenWeeks / 4.34m; if (iDC.OpenWeeks <= 0) { iDC.FLSOpenPerWeek = 0; } else { iDC.FLSOpenPerWeek = iDC.FLSOpen / iDC.OpenWeeks; } iDC.WeeksTotal = lDurationInWeeks; if (iDC.WeeksTotal != 0) { iDC.DurationPercentage = (lDurationInWeeksTillNow / iDC.WeeksTotal) * 100; } lResult.Add(iDC); iDC.BEApprovedPerWeek = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedPerWeek); iDC.BEApprovedSum = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedSum); iDC.BEApprovedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedTillNow); iDC.BEBilledTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSBilledTillNow); iDC.BEDifferenceApprovedRecordedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSDifferenceApprovedRecordedTillNow); iDC.BEDifferenceApprovedRecordedTillNowInPercent = iDC.ServiceUnit.GetHoursInBE(iDC.FLSDifferenceApprovedRecordedTillNowInPercent); iDC.BEOpen = iDC.ServiceUnit.GetHoursInBE(iDC.FLSOpen); iDC.BEOpenPerWeek = iDC.ServiceUnit.GetHoursInBE(iDC.FLSOpenPerWeek); if (iDC.BEOpenPerWeek > iDC.BEOpen) iDC.BEOpenPerWeek = iDC.BEOpen; iDC.BERecordedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSRecordedTillNow); iDC.BERecordedTillNowInPercent = iDC.ServiceUnit.GetHoursInBE(iDC.FLSRecordedTillNowInPercent); SetKorridorStatus(iDC); var customerTeams = ""; foreach (var t2c in sc.Customer.Team2CustomerList) { var team = t2c.Team; if (String.IsNullOrEmpty(iDC.Team)) customerTeams = team.Name; else { if (!iDC.Team.Contains(team.Name)) { customerTeams += ", " + team.Name; } } } iDC.CustomerTeams = customerTeams; } long endtime = DateTime.Now.Ticks; long diff = endtime - starttime; double secs = diff / (double)10000000; return lResult; } public virtual List GetSupportConceptAnalysis2(IList c2sOids, long? employeeOid, long? teamOid, long? catOid, DateTime? startDate, DateTime? endDate, int? expiredMonths, bool showArchivedScs) { //return GetSupportConceptAnalysis2Alt(c2sOids, employeeOid, teamOid, catOid, startDate, endDate, expiredMonths, showArchivedScs); if (catOid.HasValue && catOid.Value > 0) { return GetSupportConceptAnalysisWithCategory(c2sOids, employeeOid, teamOid, catOid.Value, startDate, endDate, expiredMonths, showArchivedScs); } var lResult = new List(); long starttime = DateTime.Now.Ticks; DateTime filterStartDate = startDate.HasValue ? startDate.Value : DateTime.MinValue; DateTime filterEndDate = endDate.HasValue ? endDate.Value.AddDays(1) : DateTime.MaxValue; List alleItems = new List(); var scList = DAOFactory.SearchDAO.FindSupportConceptsInDateRange(showArchivedScs, filterStartDate, filterEndDate); //Filter alle Hilfepläne, die überhaupt durch die Parameter infrage kommen StringBuilder oidList = new StringBuilder(); foreach (var sc in scList) { if (sc.Customer.Person.LastName == "Arndt" || sc.Customer.Person.LastName == "Arsovska") { int test = 0; } //Wenn TeamOids gesetzt ist, prüfe ob Klient zu einem der Teams gehört if (teamOid != null) { bool isRelatedToTeam = false; if (sc.Customer.Team2CustomerList.Count == 0) { var team = DAOFactory.GenericDAO.LoadByID(teamOid.Value); Dictionary teamMemberOidDict = new Dictionary(); foreach (var member in team.MemberList) { if (!teamMemberOidDict.ContainsKey(member.Oid.Value)) { teamMemberOidDict.Add(member.Oid.Value, member.Oid.Value); } //if (team.Leader != null && !teamMemberOidDict.ContainsKey(team.Leader.Oid.Value)) //{ // teamMemberOidDict.Add(team.Leader.Oid.Value, team.Leader.Oid.Value); //} } foreach (var e2c in sc.Customer.Employee2CustomerList) { if (!isRelatedToTeam) { if (teamMemberOidDict.ContainsKey(e2c.EmployeeOid.Value)) { isRelatedToTeam = true; } } } } else { foreach (var team2Customer in sc.Customer.Team2CustomerList) { if (team2Customer.TeamOid == teamOid) { isRelatedToTeam = true; } } } if (!isRelatedToTeam) { //Gehört nicht zum Team also überspringen continue; } } // Wenn Mitarbeiter gesetzt ist prüfe ob Klient von Mitarbeiter betreut wird if (employeeOid != null && teamOid == null) { bool isRelatedToEmployee = false; foreach (var e2c in sc.Customer.Employee2CustomerList) { if (!isRelatedToEmployee) { if (employeeOid.Value == e2c.EmployeeOid.Value) { isRelatedToEmployee = true; } } } if (!isRelatedToEmployee) { //Wird nicht betreut also überspringen continue; } } foreach (var c2s in sc.CostBearer2SupportConceptList) { bool cont = AddToResult(c2s); if (cont) { if (c2sOids != null) { if (!c2sOids.Contains(c2s.Oid.Value)) { cont = false; } } else { if (expiredMonths.HasValue) { cont = c2s.EndDate.HasValue && c2s.EndDate.Value < DateTime.Now.AddMonths(expiredMonths.Value); } } DateTime minDt = filterStartDate; if (cont) { cont = !c2s.EndDate.HasValue || c2s.EndDate.Value >= minDt; } if (cont) { var item = new SupportConceptOverviewItem(); item.SupportConcept = sc; item.CostBearer2SupportConcept = c2s; alleItems.Add(item); if (oidList.Length > 0) { oidList.Append(","); } oidList.Append(c2s.Oid); } } } } var allServiceDescs = DAOFactory.GenericDAO.GetAll(); Dictionary serviceDict = new Dictionary(); foreach (var item in allServiceDescs) { serviceDict.Add(item.Oid.Value, MapperFactory.ServiceDescriptionDC_ServiceDescription.MapToNewDC(item)); } _cs2ServiceRecords = new Dictionary>(); if (alleItems.Count > 0) { String sql = "SELECT Oid, Version, StartDate, EndDate, DistanceInMeter, CostBearer2SupportConceptOid, Betrag, ServiceDescriptionOid, RoundedDuration, GroupEmployeeCount, GroupPersonCount, GroupRoundedDuration, GroupOid FROM servicerecord"; sql += String.Format(" WHERE CostBearer2SupportConceptOid in ({0})", oidList.ToString()); //sql += " WHERE CostBearer2SupportConceptOid is not null" var result = DAOFactory.SearchDAO.GetSqlResult(sql); // //update customer set debitornumber = '5' + debitornumber where debitornumber is not null foreach (object[] srRow in result) { var sr = CreateServiceRecordDC2(srRow, serviceDict); if (sr.CostBearer2SupportConceptOid.HasValue) { if (!_cs2ServiceRecords.ContainsKey(sr.CostBearer2SupportConceptOid.Value)) { _cs2ServiceRecords.Add(sr.CostBearer2SupportConceptOid.Value, new List()); } _cs2ServiceRecords[sr.CostBearer2SupportConceptOid.Value].Add(sr); } } } // //update customer set debitornumber = '5' + debitornumber where debitornumber is not null foreach (var item in alleItems) { var iDC = new SupportConceptOverviewDC(); var sc = item.SupportConcept; var c2s = item.CostBearer2SupportConcept; var scDC = MapperFactory.CompactSupportConceptDC_SupportConcept.MapToNewDC(sc); var calc = PluginLoader.FindClass(c2s.CostBearer.ID) ?? Calculations.GetInstance(c2s.CostBearer.ID); iDC.ActivationType = sc.IsActive; if (iDC.ActivationType == ActivationTypeId.Active && sc.Customer.IsActive != ActivationTypeId.Active) { iDC.ActivationType = sc.Customer.IsActive; } DateTime minDt = filterStartDate; iDC.CalculationDate = DateTime.Now.Date; iDC.RequestedStartDate = c2s.RequestedStartDate; iDC.RequestedEndDate = c2s.RequestedEndDate; iDC.SupportConceptOid = sc.Oid.Value; iDC.CostBearer2SupportConceptOid = c2s.Oid.Value; iDC.CostBearer = c2s.CostBearer.Organisation != null ? c2s.CostBearer.Organisation.Name : string.Empty; iDC.CostBearerId = c2s.CostBearer.ID; iDC.CustomerOid = sc.Customer.Oid.Value; iDC.CustomerFirstName = sc.Customer.Person.FirstName; iDC.CustomerLastName = sc.Customer.Person.LastName; var costRates = c2s.CostBearer.CostRatePeriods.MapToNewDCs(); iDC.ServiceUnit = costRates.GetCurrentlyValidRate(CostRatePeriodType.MinutesPerServiceUnit); if (iDC.ServiceUnit == null) { iDC.ServiceUnit = new CostRatePeriodDC(); iDC.ServiceUnit.CostRateType = CostRatePeriodType.MinutesPerServiceUnit; iDC.ServiceUnit.UnitName = "FLS"; iDC.ServiceUnit.CostRateValue = 60; } iDC.CurrentHourlyRate = costRates.GetCurrentlyValidRateValue(CostRatePeriodType.HourlyRate) ?? 0m; iDC.CurrentRateFactor = costRates.GetCurrentlyValidRateValue(CostRatePeriodType.RateFactor) ?? 0m; var relDC = CreateCostbearer2SupportConceptDC(c2s, scDC); if (c2s.ApprovedStartDate.HasValue && c2s.ApprovedStartDate.Value > minDt) { minDt = c2s.ApprovedStartDate.Value.Date; } else if (c2s.RequestedStartDate.HasValue && c2s.RequestedStartDate.Value > minDt) { minDt = c2s.RequestedStartDate.Value.Date; } var records = GetServiceRecordDCs(c2s, minDt, filterEndDate); iDC.ServiceRecords = records; iDC.FLSRecordedTillNow = BerechneGeleisteteStunden(records, calc); iDC.FLSBilledTillNow = iDC.FLSRecordedTillNow; decimal lDurationInWeeksTillNow = calc.GetDurationInWeeksTillNow(relDC, endDate) ?? 0; decimal lDurationInWeeks = calc.GetDurationInWeeks(relDC) ?? 0; if (c2s.Status == CostBearer2SupportConceptStatus.Approved) { iDC.IsApproved = true; if (c2s.ApprovedStartDate != null && c2s.ApprovedEndDate != null) { iDC.ApprovedStartDate = c2s.ApprovedStartDate.Value; iDC.ApprovedEndDate = c2s.ApprovedEndDate.Value; } } else { iDC.IsApproved = false; } BerechneBewilligteStunden(calc, iDC, relDC, endDate); if (iDC.FLSApprovedSum <= 0) { iDC.FLSRecordedTillNowInPercent = 0; } else { iDC.FLSRecordedTillNowInPercent = iDC.FLSRecordedTillNow / iDC.FLSApprovedSum * 100; } iDC.FLSDifferenceApprovedRecordedTillNow = iDC.FLSRecordedTillNow - iDC.FLSApprovedTillNow; if (iDC.FLSApprovedTillNow <= 0) { iDC.FLSDifferenceApprovedRecordedTillNowInPercent = 0; } else { iDC.FLSDifferenceApprovedRecordedTillNowInPercent = iDC.FLSRecordedTillNow / iDC.FLSApprovedTillNow * 100; } iDC.FLSOpen = iDC.FLSApprovedSum - iDC.FLSRecordedTillNow; iDC.OpenWeeks = lDurationInWeeks - lDurationInWeeksTillNow; //TODO kostenträger spezifisch!? in Calculations verlagern! iDC.OpenMonths = iDC.OpenWeeks / 4.34m; if (iDC.OpenWeeks <= 0) { iDC.FLSOpenPerWeek = 0; } else { iDC.FLSOpenPerWeek = iDC.FLSOpen / iDC.OpenWeeks; } iDC.WeeksTotal = lDurationInWeeks; if (iDC.WeeksTotal != 0) { iDC.DurationPercentage = (lDurationInWeeksTillNow / iDC.WeeksTotal) * 100; } lResult.Add(iDC); iDC.BEApprovedPerWeek = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedPerWeek); iDC.BEApprovedSum = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedSum); iDC.BEApprovedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedTillNow); iDC.BEBilledTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSBilledTillNow); iDC.BEDifferenceApprovedRecordedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSDifferenceApprovedRecordedTillNow); iDC.BEDifferenceApprovedRecordedTillNowInPercent = iDC.ServiceUnit.GetHoursInBE(iDC.FLSDifferenceApprovedRecordedTillNowInPercent); iDC.BEOpen = iDC.ServiceUnit.GetHoursInBE(iDC.FLSOpen); iDC.BEOpenPerWeek = iDC.ServiceUnit.GetHoursInBE(iDC.FLSOpenPerWeek); if (iDC.BEOpenPerWeek > iDC.BEOpen) iDC.BEOpenPerWeek = iDC.BEOpen; iDC.BERecordedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSRecordedTillNow); iDC.BERecordedTillNowInPercent = iDC.ServiceUnit.GetHoursInBE(iDC.FLSRecordedTillNowInPercent); SetKorridorStatus(iDC); } long endtime = DateTime.Now.Ticks; long diff = endtime - starttime; double secs = diff / (double)10000000; return lResult; } public virtual SupportConceptCostBearerRelDC CreateCostbearer2SupportConceptDC(CostBearer2SupportConcept pEntity, CompactSupportConceptDC scDc) { SupportConceptCostBearerRelDC dc = new SupportConceptCostBearerRelDC(); if (pEntity.CostBearer.Organisation != null) { dc.CostBearer = MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(pEntity.CostBearer.Organisation); } dc.CostBearer2SupportConceptOid = pEntity.Oid; dc.CostBearer2SupportConceptVersion = pEntity.Version; dc.Notice = pEntity.Notice; dc.Status = pEntity.Status; dc.SupportConcept = scDc; // pDataContract.NoRelationExists = pEntity.ServiceRecords.Count == 0; dc.RequestedStartDate = pEntity.RequestedStartDate; dc.RequestedEndDate = pEntity.RequestedEndDate; dc.ApprovedStartDate = pEntity.ApprovedStartDate; dc.ApprovedEndDate = pEntity.ApprovedEndDate; dc.CustomerReferenceNumber = pEntity.CustomerReferenceNumber; dc.AuswahlBezeichnung = pEntity.AuswahlBezeichnung; dc.MonthlyPayment = pEntity.MonthlyPayment; dc.ApprovalPeriodList = MapperFactory.SupportConceptApprovalPeriodDC_SupportConceptApprovalPeriod.MapToNewDCs(pEntity.ApprovalPeriodList); if (dc.ApprovalPeriodList != null) { foreach (SupportConceptApprovalPeriodDC item in dc.ApprovalPeriodList) { if (!item.StartDate.HasValue) { item.StartDate = pEntity.ApprovedStartDate.HasValue ? pEntity.ApprovedStartDate : pEntity.RequestedStartDate; } if (!item.EndDate.HasValue) { item.EndDate = pEntity.ApprovedEndDate.HasValue ? pEntity.ApprovedEndDate : pEntity.RequestedEndDate; } } } return dc; } public virtual List GetSupportConceptAnalysis2Alt(IList c2sOids, long? employeeOid, long? teamOid, long? catOid, DateTime? startDate, DateTime? endDate, int? expiredMonths, bool showArchivedScs) { long starttime = DateTime.Now.Ticks; if (catOid.HasValue && catOid.Value > 0) { return GetSupportConceptAnalysisWithCategory(c2sOids, employeeOid, teamOid, catOid.Value, startDate, endDate, expiredMonths, showArchivedScs); } DateTime filterStartDate = startDate.HasValue ? startDate.Value : DateTime.MinValue; DateTime filterEndDate = endDate.HasValue ? endDate.Value.AddDays(1) : DateTime.MaxValue; var lResult = new List(); var c2sList = DAOFactory.GenericDAO.GetAllActive(); //var c2sList2 = DAOFactory.SearchDAO.FindCostBearer2SupportConceptInDateRange(showArchivedScs, filterStartDate, filterEndDate); foreach (var iCb2Sc in c2sList) { if (iCb2Sc.SupportConcept.Customer.Person.LastName == "Arndt" || iCb2Sc.SupportConcept.Customer.Person.LastName == "Arsovska") { int test = 0; } bool cont = AddToResult(iCb2Sc); if (cont) { var calc = PluginLoader.FindClass(iCb2Sc.CostBearer.ID) ?? Calculations.GetInstance(iCb2Sc.CostBearer.ID); var iDC = new SupportConceptOverviewDC(); iDC.ActivationType = iCb2Sc.SupportConcept.IsActive; if (iDC.ActivationType == ActivationTypeId.Active && iCb2Sc.SupportConcept.Customer.IsActive != ActivationTypeId.Active) { iDC.ActivationType = iCb2Sc.SupportConcept.Customer.IsActive; } if (c2sOids != null) { if (!c2sOids.Contains(iCb2Sc.Oid.Value)) { cont = false; } } else { cont = (showArchivedScs && iDC.ActivationType != ActivationTypeId.Deleted) || iDC.ActivationType == ActivationTypeId.Active; if (cont && teamOid != null) { bool isRelatedToTeam = false; if (iCb2Sc.SupportConcept.Customer.Team2CustomerList.Count == 0) { var team = DAOFactory.GenericDAO.LoadByID(teamOid.Value); Dictionary teamMemberOidDict = new Dictionary(); foreach (var member in team.MemberList) { if (!teamMemberOidDict.ContainsKey(member.Oid.Value)) { teamMemberOidDict.Add(member.Oid.Value, member.Oid.Value); } //if (team.Leader != null && !teamMemberOidDict.ContainsKey(team.Leader.Oid.Value)) //{ // teamMemberOidDict.Add(team.Leader.Oid.Value, team.Leader.Oid.Value); //} } foreach (var e2c in iCb2Sc.SupportConcept.Customer.Employee2CustomerList) { if (!isRelatedToTeam) { if (teamMemberOidDict.ContainsKey(e2c.EmployeeOid.Value)) { isRelatedToTeam = true; } } } } else { foreach (var team2Customer in iCb2Sc.SupportConcept.Customer.Team2CustomerList) { if (team2Customer.TeamOid == teamOid) { isRelatedToTeam = true; } } } if (!isRelatedToTeam) { cont = false; } } if (cont && employeeOid != null && teamOid == null) { bool isRelatedToEmployee = false; foreach (var e2c in iCb2Sc.SupportConcept.Customer.Employee2CustomerList) { if (!isRelatedToEmployee) { if (employeeOid.Value == e2c.EmployeeOid.Value) { isRelatedToEmployee = true; } } } if (!isRelatedToEmployee) { cont = false; } } if (cont && expiredMonths.HasValue) { cont = iCb2Sc.EndDate.HasValue && iCb2Sc.EndDate.Value < DateTime.Now.AddMonths(expiredMonths.Value); } } DateTime minDt = filterStartDate; if (cont) { cont = !iCb2Sc.EndDate.HasValue || iCb2Sc.EndDate.Value >= minDt; } if (cont) { iDC.CalculationDate = DateTime.Now.Date; iDC.RequestedStartDate = iCb2Sc.RequestedStartDate; iDC.RequestedEndDate = iCb2Sc.RequestedEndDate; iDC.SupportConceptOid = iCb2Sc.SupportConcept.Oid.Value; iDC.CostBearer2SupportConceptOid = iCb2Sc.Oid.Value; iDC.CostBearer = iCb2Sc.CostBearer.Organisation != null ? iCb2Sc.CostBearer.Organisation.Name : string.Empty; iDC.CostBearerId = iCb2Sc.CostBearer.ID; iDC.CustomerOid = iCb2Sc.SupportConcept.Customer.Oid.Value; iDC.CustomerFirstName = iCb2Sc.SupportConcept.Customer.Person.FirstName; iDC.CustomerLastName = iCb2Sc.SupportConcept.Customer.Person.LastName; if (iDC.CustomerLastName == "Böhm") { int test = 0; } var costRates = iCb2Sc.CostBearer.CostRatePeriods.MapToNewDCs(); iDC.ServiceUnit = costRates.GetCurrentlyValidRate(CostRatePeriodType.MinutesPerServiceUnit); if (iDC.ServiceUnit == null) { iDC.ServiceUnit = new CostRatePeriodDC(); iDC.ServiceUnit.CostRateType = CostRatePeriodType.MinutesPerServiceUnit; iDC.ServiceUnit.UnitName = "FLS"; iDC.ServiceUnit.CostRateValue = 60; } iDC.CurrentHourlyRate = costRates.GetCurrentlyValidRateValue(CostRatePeriodType.HourlyRate) ?? 0m; iDC.CurrentRateFactor = costRates.GetCurrentlyValidRateValue(CostRatePeriodType.RateFactor) ?? 0m; var relDC = iCb2Sc.MapToNewDC(); if (iCb2Sc.ApprovedStartDate.HasValue) { minDt = iCb2Sc.ApprovedStartDate.Value.Date; } else if (iCb2Sc.RequestedStartDate.HasValue) { minDt = iCb2Sc.RequestedStartDate.Value.Date; } var records = GetServiceRecordDCs(iCb2Sc, minDt, filterEndDate); iDC.ServiceRecords = records; iDC.FLSRecordedTillNow = BerechneGeleisteteStunden(records, calc); //var notBillableAbsenceTimes = calc.GetHoursNotBillableDueToAbsenceTimes(relDC, recordDCs, // iCb2Sc.SupportConcept.Customer.GetAbsencesTimesNotBillableWholeWeeks(), false, // false); //var notBillableMoreThanApproved = calc.GetHoursNotBillableDueToMoreThanApproved(relDC, recordDCs, // false, true); iDC.FLSBilledTillNow = iDC.FLSRecordedTillNow; //- notBillableAbsenceTimes //- notBillableMoreThanApproved; decimal lDurationInWeeksTillNow = calc.GetDurationInWeeksTillNow(relDC, endDate) ?? 0; decimal lDurationInWeeks = calc.GetDurationInWeeks(relDC) ?? 0; if (iCb2Sc.Status == CostBearer2SupportConceptStatus.Approved) { iDC.IsApproved = true; if (iCb2Sc.ApprovedStartDate != null && iCb2Sc.ApprovedEndDate != null) { iDC.ApprovedStartDate = iCb2Sc.ApprovedStartDate.Value; iDC.ApprovedEndDate = iCb2Sc.ApprovedEndDate.Value; //lDurationInWeeks = Convert.ToDecimal((iDC.ApprovedEndDate.Value - iDC.ApprovedStartDate.Value).Days + 1) / 7; //lCalcDate = lCalcDate > iCb2Sc.ApprovedEndDate // ? iCb2Sc.ApprovedEndDate.Value.Date.AddDays(1) // : lCalcDate; //lDurationInWeeksTillNow = Convert.ToDecimal((lCalcDate - iCb2Sc.ApprovedStartDate.Value).Days) / 7m; } } else { iDC.IsApproved = false; //if (iCb2Sc.RequestedStartDate != null && iCb2Sc.RequestedEndDate != null) //{ //lDurationInWeeks = Convert.ToDecimal((iCb2Sc.RequestedEndDate.Value - iCb2Sc.RequestedStartDate.Value).Days + 1) / 7; //lCalcDate = lCalcDate > iCb2Sc.RequestedEndDate // ? iCb2Sc.RequestedEndDate.Value.Date // : lCalcDate; //lDurationInWeeksTillNow = Convert.ToDecimal((lCalcDate - iCb2Sc.RequestedStartDate.Value).Days) / 7m; //} } BerechneBewilligteStunden(calc, iDC, relDC, endDate); //if (flsTotal.HasValue && lDurationInWeeks != 0) //{ // iDC.FLSApprovedTillNow = flsTotal.Value * ((lDurationInWeeksTillNow + (1m / 7m)) / lDurationInWeeks); //} //else //{ // iDC.FLSApprovedTillNow = iDC.FLSApprovedPerWeek * (lDurationInWeeksTillNow + (1m / 7m)); //} if (iDC.FLSApprovedSum <= 0) { iDC.FLSRecordedTillNowInPercent = 0; } else { iDC.FLSRecordedTillNowInPercent = iDC.FLSRecordedTillNow / iDC.FLSApprovedSum * 100; } iDC.FLSDifferenceApprovedRecordedTillNow = iDC.FLSRecordedTillNow - iDC.FLSApprovedTillNow; if (iDC.FLSApprovedTillNow <= 0) { iDC.FLSDifferenceApprovedRecordedTillNowInPercent = 0; } else { iDC.FLSDifferenceApprovedRecordedTillNowInPercent = iDC.FLSRecordedTillNow / iDC.FLSApprovedTillNow * 100; } iDC.FLSOpen = iDC.FLSApprovedSum - iDC.FLSRecordedTillNow; iDC.OpenWeeks = lDurationInWeeks - lDurationInWeeksTillNow; //TODO kostenträger spezifisch!? in Calculations verlagern! iDC.OpenMonths = iDC.OpenWeeks / 4.34m; if (iDC.OpenWeeks <= 0) { iDC.FLSOpenPerWeek = 0; } else { iDC.FLSOpenPerWeek = iDC.FLSOpen / iDC.OpenWeeks; } iDC.WeeksTotal = lDurationInWeeks; if (iDC.WeeksTotal != 0) { iDC.DurationPercentage = (lDurationInWeeksTillNow / iDC.WeeksTotal) * 100; } lResult.Add(iDC); iDC.BEApprovedPerWeek = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedPerWeek); iDC.BEApprovedSum = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedSum); iDC.BEApprovedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedTillNow); iDC.BEBilledTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSBilledTillNow); iDC.BEDifferenceApprovedRecordedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSDifferenceApprovedRecordedTillNow); iDC.BEDifferenceApprovedRecordedTillNowInPercent = iDC.ServiceUnit.GetHoursInBE(iDC.FLSDifferenceApprovedRecordedTillNowInPercent); iDC.BEOpen = iDC.ServiceUnit.GetHoursInBE(iDC.FLSOpen); iDC.BEOpenPerWeek = iDC.ServiceUnit.GetHoursInBE(iDC.FLSOpenPerWeek); if (iDC.BEOpenPerWeek > iDC.BEOpen) iDC.BEOpenPerWeek = iDC.BEOpen; iDC.BERecordedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSRecordedTillNow); iDC.BERecordedTillNowInPercent = iDC.ServiceUnit.GetHoursInBE(iDC.FLSRecordedTillNowInPercent); SetKorridorStatus(iDC); } } } long endtime = DateTime.Now.Ticks; long diff = endtime - starttime; double secs = diff / (double)10000000; return lResult; } private List GetSupportConceptAnalysisWithCategory(IList c2sOids, long? employeeOid, long? teamOid, long catOid, DateTime? startDate, DateTime? endDate, int? expiredMonths, bool showArchivedScs) { DateTime minDt = startDate.HasValue ? startDate.Value : DateTime.MinValue; DateTime maxDt = endDate.HasValue ? endDate.Value.AddDays(1) : DateTime.MaxValue; var lResult = new List(); foreach (var iCb2Sc in DAOFactory.GenericDAO.GetAllActive()) { var calc = PluginLoader.FindClass(iCb2Sc.CostBearer.ID) ?? Calculations.GetInstance(iCb2Sc.CostBearer.ID); foreach (var scap in iCb2Sc.ApprovalPeriodList) { bool cont = AddToResult(scap, catOid); if (cont) { var iDC = new SupportConceptOverviewDC(); iDC.ActivationType = iCb2Sc.SupportConcept.IsActive; if (iDC.ActivationType == ActivationTypeId.Active && iCb2Sc.SupportConcept.Customer.IsActive != ActivationTypeId.Active) { iDC.ActivationType = iCb2Sc.SupportConcept.Customer.IsActive; } if (c2sOids != null) { if (!c2sOids.Contains(iCb2Sc.Oid.Value)) { cont = false; } } else { cont = (showArchivedScs && iDC.ActivationType != ActivationTypeId.Deleted) || iDC.ActivationType == ActivationTypeId.Active; if (cont && teamOid != null) { bool isRelatedToTeam = false; if (iCb2Sc.SupportConcept.Customer.Team2CustomerList.Count == 0) { var team = DAOFactory.GenericDAO.LoadByID(teamOid.Value); Dictionary teamMemberOidDict = new Dictionary(); foreach (var member in team.MemberList) { if (!teamMemberOidDict.ContainsKey(member.Oid.Value)) { teamMemberOidDict.Add(member.Oid.Value, member.Oid.Value); } //if (team.Leader != null && !teamMemberOidDict.ContainsKey(team.Leader.Oid.Value)) //{ // teamMemberOidDict.Add(team.Leader.Oid.Value, team.Leader.Oid.Value); //} } foreach (var e2c in iCb2Sc.SupportConcept.Customer.Employee2CustomerList) { if (!isRelatedToTeam) { if (teamMemberOidDict.ContainsKey(e2c.EmployeeOid.Value)) { isRelatedToTeam = true; } } } } else { foreach (var team2Customer in iCb2Sc.SupportConcept.Customer.Team2CustomerList) { if (team2Customer.TeamOid == teamOid) { isRelatedToTeam = true; } } } if (!isRelatedToTeam) { cont = false; } } if (cont && employeeOid != null && teamOid == null) { bool isRelatedToEmployee = false; foreach (var e2c in iCb2Sc.SupportConcept.Customer.Employee2CustomerList) { if (!isRelatedToEmployee) { if (employeeOid.Value == e2c.EmployeeOid.Value) { isRelatedToEmployee = true; } } } if (!isRelatedToEmployee) { cont = false; } } if (cont && expiredMonths.HasValue) { cont = iCb2Sc.EndDate.HasValue && iCb2Sc.EndDate.Value < DateTime.Now.AddMonths(expiredMonths.Value); } } if (cont) { iDC.CalculationDate = DateTime.Now.Date; iDC.RequestedStartDate = scap.StartDate; iDC.RequestedEndDate = scap.EndDate; iDC.SupportConceptOid = iCb2Sc.SupportConcept.Oid.Value; iDC.CostBearer2SupportConceptOid = iCb2Sc.Oid.Value; iDC.CostBearer = iCb2Sc.CostBearer.Organisation != null ? iCb2Sc.CostBearer.Organisation.Name : string.Empty; iDC.CostBearerId = iCb2Sc.CostBearer.ID; iDC.CustomerOid = iCb2Sc.SupportConcept.Customer.Oid.Value; iDC.CustomerFirstName = iCb2Sc.SupportConcept.Customer.Person.FirstName; iDC.CustomerLastName = iCb2Sc.SupportConcept.Customer.Person.LastName; var costRates = iCb2Sc.CostBearer.CostRatePeriods.MapToNewDCs(); iDC.ServiceUnit = costRates.GetCurrentlyValidRate(CostRatePeriodType.MinutesPerServiceUnit); if (iDC.ServiceUnit == null) { iDC.ServiceUnit = new CostRatePeriodDC(); iDC.ServiceUnit.CostRateType = CostRatePeriodType.MinutesPerServiceUnit; iDC.ServiceUnit.UnitName = "FLS"; iDC.ServiceUnit.CostRateValue = 60; } iDC.CurrentHourlyRate = costRates.GetCurrentlyValidRateValue(CostRatePeriodType.HourlyRate) ?? 0m; iDC.CurrentRateFactor = costRates.GetCurrentlyValidRateValue(CostRatePeriodType.RateFactor) ?? 0m; var c2sDc = iCb2Sc.MapToNewDC(); var scapDc = scap.MapToNewDC(); if (scap.StartDate.HasValue && scap.StartDate.Value > minDt) { minDt = scap.StartDate.Value.Date; } else if (scap.EndDate.HasValue && scap.EndDate.Value > minDt) { minDt = scap.EndDate.Value.Date; } iDC.FLSRecordedTillNow = BerechneGeleisteteStunden(iCb2Sc, catOid, minDt, maxDt, calc); //var notBillableAbsenceTimes = calc.GetHoursNotBillableDueToAbsenceTimes(relDC, recordDCs, // iCb2Sc.SupportConcept.Customer.GetAbsencesTimesNotBillableWholeWeeks(), false, // false); //var notBillableMoreThanApproved = calc.GetHoursNotBillableDueToMoreThanApproved(relDC, recordDCs, // false, true); iDC.FLSBilledTillNow = iDC.FLSRecordedTillNow; //- notBillableAbsenceTimes //- notBillableMoreThanApproved; decimal lDurationInWeeksTillNow = GetDurationInWeeksTillNow(scap.StartDate, scap.EndDate, endDate) ?? 0; decimal lDurationInWeeks = GetDurationInWeeks(scap.StartDate, scap.EndDate) ?? 0; if (iCb2Sc.Status == CostBearer2SupportConceptStatus.Approved) { iDC.IsApproved = true; if (iCb2Sc.ApprovedStartDate != null && iCb2Sc.ApprovedEndDate != null) { iDC.ApprovedStartDate = iCb2Sc.ApprovedStartDate.Value; iDC.ApprovedEndDate = iCb2Sc.ApprovedEndDate.Value; //lDurationInWeeks = Convert.ToDecimal((iDC.ApprovedEndDate.Value - iDC.ApprovedStartDate.Value).Days + 1) / 7; //lCalcDate = lCalcDate > iCb2Sc.ApprovedEndDate // ? iCb2Sc.ApprovedEndDate.Value.Date.AddDays(1) // : lCalcDate; //lDurationInWeeksTillNow = Convert.ToDecimal((lCalcDate - iCb2Sc.ApprovedStartDate.Value).Days) / 7m; } } else { iDC.IsApproved = false; //if (iCb2Sc.RequestedStartDate != null && iCb2Sc.RequestedEndDate != null) //{ //lDurationInWeeks = Convert.ToDecimal((iCb2Sc.RequestedEndDate.Value - iCb2Sc.RequestedStartDate.Value).Days + 1) / 7; //lCalcDate = lCalcDate > iCb2Sc.RequestedEndDate // ? iCb2Sc.RequestedEndDate.Value.Date // : lCalcDate; //lDurationInWeeksTillNow = Convert.ToDecimal((lCalcDate - iCb2Sc.RequestedStartDate.Value).Days) / 7m; //} } BerechneBewilligteStunden(calc, iDC, c2sDc, scapDc, endDate); //if (flsTotal.HasValue && lDurationInWeeks != 0) //{ // iDC.FLSApprovedTillNow = flsTotal.Value * ((lDurationInWeeksTillNow + (1m / 7m)) / lDurationInWeeks); //} //else //{ // iDC.FLSApprovedTillNow = iDC.FLSApprovedPerWeek * (lDurationInWeeksTillNow + (1m / 7m)); //} if (iDC.FLSApprovedSum <= 0) { iDC.FLSRecordedTillNowInPercent = 0; } else { iDC.FLSRecordedTillNowInPercent = iDC.FLSRecordedTillNow / iDC.FLSApprovedSum * 100; } iDC.FLSDifferenceApprovedRecordedTillNow = iDC.FLSRecordedTillNow - iDC.FLSApprovedTillNow; if (iDC.FLSApprovedTillNow <= 0) { iDC.FLSDifferenceApprovedRecordedTillNowInPercent = 0; } else { iDC.FLSDifferenceApprovedRecordedTillNowInPercent = iDC.FLSRecordedTillNow / iDC.FLSApprovedTillNow * 100; } iDC.FLSOpen = iDC.FLSApprovedSum - iDC.FLSRecordedTillNow; iDC.OpenWeeks = lDurationInWeeks - lDurationInWeeksTillNow; //TODO kostenträger spezifisch!? in Calculations verlagern! iDC.OpenMonths = iDC.OpenWeeks / 4.34m; if (iDC.OpenWeeks <= 0) { iDC.FLSOpenPerWeek = 0; } else { iDC.FLSOpenPerWeek = iDC.FLSOpen / iDC.OpenWeeks; } iDC.WeeksTotal = lDurationInWeeks; if (iDC.WeeksTotal != 0) { iDC.DurationPercentage = (lDurationInWeeksTillNow / iDC.WeeksTotal) * 100; } lResult.Add(iDC); iDC.BEApprovedPerWeek = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedPerWeek); iDC.BEApprovedSum = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedSum); iDC.BEApprovedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSApprovedTillNow); iDC.BEBilledTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSBilledTillNow); iDC.BEDifferenceApprovedRecordedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSDifferenceApprovedRecordedTillNow); iDC.BEDifferenceApprovedRecordedTillNowInPercent = iDC.ServiceUnit.GetHoursInBE(iDC.FLSDifferenceApprovedRecordedTillNowInPercent); iDC.BEOpen = iDC.ServiceUnit.GetHoursInBE(iDC.FLSOpen); iDC.BEOpenPerWeek = iDC.ServiceUnit.GetHoursInBE(iDC.FLSOpenPerWeek); if (iDC.BEOpenPerWeek > iDC.BEOpen) iDC.BEOpenPerWeek = iDC.BEOpen; iDC.BERecordedTillNow = iDC.ServiceUnit.GetHoursInBE(iDC.FLSRecordedTillNow); iDC.BERecordedTillNowInPercent = iDC.ServiceUnit.GetHoursInBE(iDC.FLSRecordedTillNowInPercent); SetKorridorStatus(iDC); } } } } return lResult; } public virtual decimal? GetDurationInWeeksTillNow(DateTime? start, DateTime? end, DateTime? appointedDate) { if (start != null && end != null) { DateTime lCalcDate = (appointedDate.HasValue && appointedDate.Value < DateTime.Now.Date) ? appointedDate.Value : DateTime.Now.Date; lCalcDate = lCalcDate > end ? end.Value.Date.AddDays(1) : lCalcDate; return Convert.ToDecimal((lCalcDate - start.Value).Days) / 7m; } return null; } public virtual decimal? GetDurationInWeeks(DateTime? start, DateTime? end) { if (start != null && end != null) { return Convert.ToDecimal((end.Value - start.Value).Days + 1) / 7; } return null; } public virtual void SetKorridorStatus(SupportConceptOverviewDC iDc) { iDc.KorridorStatus = null; decimal minpercent = -1; decimal maxpercent = -1; decimal sumPerYear = GetAppovedBEPerYear(iDc); sumPerYear = Math.Round(sumPerYear); if (sumPerYear == 99) { minpercent = 8900m / 99m; maxpercent = 10900m / 99m; } else if (sumPerYear == 120) { minpercent = 11000m / 120m; maxpercent = 13300m / 120m; } else if (sumPerYear == 147) { minpercent = 13400m / 147m; maxpercent = 17200m / 147m; } else if (sumPerYear == 198) { minpercent = 17300m / 198m; maxpercent = 24300m / 198m; } else if (sumPerYear == 288) { minpercent = 24400m / 288m; maxpercent = 31500m / 288m; } else if (sumPerYear == 343) { minpercent = 31600m / 343m; maxpercent = 39600m / 343m; } if (minpercent > 0) { if (minpercent <= iDc.BEDifferenceApprovedRecordedTillNowInPercent && iDc.BEDifferenceApprovedRecordedTillNowInPercent <= maxpercent) { iDc.KorridorStatus = "Innerhalb des Korridors"; iDc.KorridorStatusFarbe = "ForestGreen"; } else { iDc.KorridorStatus = "Außerhalb des Korridors"; iDc.KorridorStatusFarbe = "Red"; } } } public virtual decimal GetAppovedBEPerYear(SupportConceptOverviewDC iDc) { decimal total = iDc.BEApprovedSum; DateTime? start = iDc.ApprovedStartDate; if (!start.HasValue) start = iDc.RequestedStartDate; DateTime? end = iDc.ApprovedEndDate; if (!end.HasValue) end = iDc.RequestedEndDate; if (start.HasValue && end.HasValue) { decimal days = (decimal)end.Value.Subtract(start.Value).Days + 1; decimal years = days / 365m; if (years != 0) { return total / years; } } return 0; } public virtual void BerechneBewilligteStunden(Calculations calc, SupportConceptOverviewDC iDc, SupportConceptCostBearerRelDC relDc, DateTime? appointedDate) { iDc.FLSApprovedPerWeek = calc.GetApprovedHoursPerWeekAverage(relDc, true) ?? 0m; decimal? flsTotal = calc.GetApprovedHoursTotal(relDc, true); iDc.FLSApprovedSum = flsTotal ?? 0m; var approvedTillNow = calc.GetApprovedHoursTillNow(relDc, appointedDate); iDc.FLSApprovedTillNow = approvedTillNow ?? 0; } public virtual void BerechneBewilligteStunden(Calculations calc, SupportConceptOverviewDC iDc, SupportConceptCostBearerRelDC relDc, SupportConceptApprovalPeriodDC scap, DateTime? appointedDate) { iDc.FLSApprovedPerWeek = GetApprovedHoursPerWeek(calc, relDc, scap, true) ?? 0m; decimal? flsTotal = calc.GetApprovedHoursTotal(relDc, true); iDc.FLSApprovedSum = flsTotal ?? 0m; var approvedTillNow = calc.GetApprovedHoursTillNow(relDc, appointedDate); iDc.FLSApprovedTillNow = approvedTillNow ?? 0; } public virtual decimal? GetApprovedHoursPerWeek(Calculations calc, SupportConceptCostBearerRelDC relDC, SupportConceptApprovalPeriodDC scap, bool useIsCalculationWithFactor) { if (scap.StartDate.HasValue && scap.EndDate.HasValue) { decimal? hoursPerWeek = calc.GetApprovedHoursPerWeek(scap, relDC.CostBearer.CostRatePeriods); if (useIsCalculationWithFactor) hoursPerWeek = calc.ApplyIsCalculationWithFactor(relDC, hoursPerWeek); return hoursPerWeek; } return null; } public virtual decimal BerechneGeleisteteStunden(CostBearer2SupportConcept c2s, DateTime minDate, DateTime maxDate, Calculations calc) { var recordDCs = GetServiceRecordDCs(c2s, minDate, maxDate); return calc.GetBillableDurationInMinutes(recordDCs) / 60m; } public virtual decimal BerechneGeleisteteStunden(List recordDCs, Calculations calc) { return calc.GetBillableDurationInMinutes(recordDCs) / 60m; } public virtual List GetServiceRecordDCs(CostBearer2SupportConcept c2s, DateTime minDate, DateTime maxDate) { if (_cs2ServiceRecords != null && _cs2ServiceRecords.Count > 0) { if (_cs2ServiceRecords.ContainsKey(c2s.Oid.Value)) { var list = _cs2ServiceRecords[c2s.Oid.Value]; return list.Where(s => s.Start >= minDate && s.Start < maxDate).ToList(); } return new List(); } //var serviceRecordsInSpan = c2s.ServiceRecords.Where(s => s.Start >= minDate && s.Start < maxDate); var serviceRecordsInSpan = c2s.ServiceRecords.Where(s => s.Start >= minDate && s.Start < maxDate); var dcList = new List(); foreach (var sr in serviceRecordsInSpan) { dcList.Add(CreateServiceRecordDC(sr)); } return dcList; } public virtual ServiceRecordDC CreateServiceRecordDC(ServiceRecord sr) { var dc = new ServiceRecordDC(); dc.ServiceRecordOid = sr.Oid; dc.ServiceRecordVersion = sr.Version; dc.Start = sr.Start; dc.End = sr.End; dc.DistanceInMeter = (int?)sr.DistanceInMeter; dc.DistanceInMeterDecimal = sr.DistanceInMeter; dc.Relevance = sr.Relevance; dc.Betrag = sr.Betrag; if (sr.ServiceDescription != null) { dc.ServiceDescription = MapperFactory.ServiceDescriptionDC_ServiceDescription.MapToNewDC(sr.ServiceDescription); } dc.RoundedDuration = sr.RoundedDuration; dc.GroupEmployeeCount = sr.GroupEmployeeCount; dc.GroupPersonCount = sr.GroupPersonCount; dc.GroupRoundedDuration = sr.GroupRoundedDuration; dc.GroupOid = sr.GroupOid; return dc; } public virtual ServiceRecordDC CreateServiceRecordDC2(object[] sqlRow, Dictionary serviceDict) { try { var dc = new ServiceRecordDC(); //DAOFactory.SearchDAO.GetSqlResult("SELECT Oid, Version, Start, End, DistanceInMeter, CostBearer2SupportConceptOid, Betrag, ServiceDescriptionOid, RoundedDuration, GroupEmployeeCount, GroupPersonCount, GroupRoundedDuration, GroupOid FROM servicerecord"); dc.ServiceRecordOid = (long?)sqlRow[0]; dc.ServiceRecordVersion = (long?)sqlRow[1]; dc.Start = (DateTime?)sqlRow[2]; dc.End = (DateTime?)sqlRow[3]; dc.DistanceInMeterDecimal = (decimal?)sqlRow[4]; if (dc.DistanceInMeterDecimal.HasValue) { dc.DistanceInMeter = (int)dc.DistanceInMeterDecimal; } dc.CostBearer2SupportConceptOid = (long?)sqlRow[5]; dc.Betrag = (decimal?)sqlRow[6]; long? sdOid = (long?)sqlRow[7]; if (sdOid.HasValue && serviceDict.ContainsKey(sdOid.Value)) { dc.ServiceDescription = serviceDict[sdOid.Value]; } dc.RoundedDuration = (decimal)sqlRow[8]; dc.GroupEmployeeCount = (int?)sqlRow[9]; dc.GroupPersonCount = (int?)sqlRow[10]; dc.GroupRoundedDuration = (decimal?)sqlRow[11]; dc.GroupOid = (long?)sqlRow[12]; return dc; } catch (Exception e) { throw; } } public virtual decimal BerechneGeleisteteStunden(CostBearer2SupportConcept iCb2Sc, long catOid, DateTime minDate, DateTime maxDate, Calculations calc) { var recordDCs = (from sr in iCb2Sc.ServiceRecords where (sr.Start >= minDate && sr.Start < maxDate) select MapperFactory.ServiceRecordDC_ServiceRecord.MapToNewDC(sr)).ToList(); return calc.GetBillableDurationInMinutes(recordDCs.Where(r => r.ServiceDescription.Category.ServiceCategoryOid == catOid).ToList()) / 60m; } public virtual bool AddToResult(CostBearer2SupportConcept c2s) { return true; } public virtual bool AddToResult(SupportConceptApprovalPeriod scap, long? catOid) { if (!catOid.HasValue || scap.ServiceCategory == null) { return true; } if (scap.ServiceCategory.Oid != catOid) { return false; } return true; } } public class SupportConceptOverviewItem { public CostBearer2SupportConcept CostBearer2SupportConcept { get; set; } public SupportConcept SupportConcept { get; set; } } }