Files
BeWoPlaner/Service/Plugins/SupportConceptAnalysisCreator.cs
2020-10-22 12:20:35 +02:00

417 lines
18 KiB
C#

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;
namespace BeWo.Service.Plugins
{
public class SupportConceptAnalysisCreator : AbstractIDSpecificDefaultClass<SupportConceptAnalysisCreator>
{
public virtual List<SupportConceptOverviewDC> GetSupportConceptAnalysis(IList<long> c2sOids, long? employeeOid, long? teamOid, DateTime? appointedDate,
int? expiredMonths, bool showArchivedScs)
{
DateTime maxDate = appointedDate.HasValue
? appointedDate.Value.AddDays(1)
: DateTime.MaxValue;
var lResult = new List<SupportConceptOverviewDC>();
foreach (var iCb2Sc in DAOFactory.GenericDAO.GetAllActive<CostBearer2SupportConcept>())
{
bool cont = AddToResult(iCb2Sc);
if (cont)
{
var calc = PluginLoader.FindClass<Calculations>(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<Team>(teamOid.Value);
Dictionary<long, long> teamMemberOidDict = new Dictionary<long, long>();
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 = 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;
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();
DateTime minDate = DateTime.MinValue;
if (iCb2Sc.ApprovedStartDate.HasValue)
{
minDate = iCb2Sc.ApprovedStartDate.Value.Date;
}
else if (iCb2Sc.RequestedStartDate.HasValue)
{
minDate = iCb2Sc.RequestedStartDate.Value.Date;
}
iDC.FLSRecordedTillNow = BerechneGeleisteteStunden(iCb2Sc, minDate, maxDate, 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, appointedDate) ?? 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, appointedDate);
//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 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 decimal BerechneGeleisteteStunden(CostBearer2SupportConcept iCb2Sc, 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) / 60m;
}
public virtual bool AddToResult(CostBearer2SupportConcept c2s)
{
return true;
}
}
}