245 lines
9.0 KiB
C#
245 lines
9.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using BeWo.Data.Access;
|
|
using BeWo.Data.Entities;
|
|
using BeWo.Report;
|
|
using BeWo.Report.ReportObjects;
|
|
using BeWo.Service.DCEntityMapper;
|
|
using BS.Shared;
|
|
using BS.Shared.DataContracts;
|
|
using BS.Shared.DataContracts.Compact;
|
|
using BS.Shared.Extensions;
|
|
using BS.Shared.Services;
|
|
|
|
namespace BeWoDarmstadt
|
|
{
|
|
public partial class Teamuebersicht : DevExpress.XtraReports.UI.XtraReport//, IBeWoReport<FinanceRO>
|
|
{
|
|
public Teamuebersicht()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
public void SetReportDataSource(FinanceRO pRO)
|
|
{
|
|
DateTime start = pRO.ReportStartDate.Value;
|
|
DateTime end = pRO.ReportEndDate.Value;
|
|
DateTime monthStart = new DateTime(pRO.ReportEndDate.Value.Year, pRO.ReportEndDate.Value.Month, 1);
|
|
|
|
// ServiceDescriptions einmal vorladen, falls QA-Einträge vorhanden sind
|
|
Dictionary<long, ServiceDescriptionDC> serviceDictCache = null;
|
|
if (pRO.SupportConceptFinanceList != null && pRO.SupportConceptFinanceList.Any(sc => sc.Leistungskategorie != null && sc.Leistungskategorie.IndexOf("qualifiziert", StringComparison.OrdinalIgnoreCase) >= 0))
|
|
{
|
|
serviceDictCache = LoadServiceDescriptionDict();
|
|
}
|
|
|
|
if (pRO.SupportConceptFinanceList != null)
|
|
{
|
|
foreach (var scRo in pRO.SupportConceptFinanceList)
|
|
{
|
|
String team = GetTeams(scRo);
|
|
if (String.IsNullOrEmpty(team))
|
|
{
|
|
scRo.Custom1 = "";
|
|
}
|
|
else
|
|
{
|
|
scRo.Custom1 = team;
|
|
}
|
|
if (scRo.Costbearer2Supportconcept.SupportConcept.Customer.CustomerAlias == null)
|
|
{
|
|
scRo.Costbearer2Supportconcept.SupportConcept.Customer.CustomerAlias = scRo.Costbearer2Supportconcept.SupportConcept.CustomerFullName;
|
|
}
|
|
//if (scRo.Costbearer2Supportconcept.SupportConcept.Customer.CustomerAlias == "160")
|
|
// {
|
|
// var Test = 1;
|
|
// }
|
|
|
|
//Hole zusätzlich Werte nur für einzelnen Monat
|
|
var cb2sc = scRo.Costbearer2Supportconcept;
|
|
var costRateDcs = cb2sc.CostBearer.CostRatePeriods;
|
|
var calc = BS.Shared.Services.Calculations.GetInstance(cb2sc.CostBearer.CostBearerID);
|
|
scRo.ApprovedHours = GetApprovedHoursOhneFahrzeitForPeriod(cb2sc, monthStart, end, scRo.SupportConceptApprovalPeriod);
|
|
scRo.BillableHours = 0;
|
|
scRo.BillableHoursInReportTimeSpan = 0;
|
|
|
|
scRo.AmountPaid = 0;
|
|
scRo.AmountTotal = 0;
|
|
|
|
// Bei qualifizierter Assistenz: alle abrechenbaren, nicht-kompensatorischen Records des Hilfeplanis hinzufügen, die noch nicht in ServiceRecords enthalten sind
|
|
// die dokumentieren teilweise völlig beliebig bei Gruppenbuchungen
|
|
if (scRo.Leistungskategorie != null && scRo.Leistungskategorie.IndexOf("qualifiziert", StringComparison.OrdinalIgnoreCase) >= 0 && serviceDictCache != null)
|
|
{
|
|
var existingOids = new HashSet<long>((scRo.ServiceRecords ?? new List<ServiceRecordDC>()).Where(r => r.ServiceRecordOid.HasValue).Select(r => r.ServiceRecordOid.Value));
|
|
var additionalRecords = LoadBillableNonKompensatorischRecords(cb2sc, scRo, existingOids, serviceDictCache);
|
|
if (scRo.ServiceRecords == null)
|
|
{
|
|
scRo.ServiceRecords = additionalRecords;
|
|
}
|
|
else
|
|
{
|
|
scRo.ServiceRecords.AddRange(additionalRecords);
|
|
}
|
|
}
|
|
|
|
if (scRo.ServiceRecords != null)
|
|
{
|
|
foreach (var sr in scRo.ServiceRecords)
|
|
{
|
|
if (sr.GroupRoundedDuration != null && sr.GroupRoundedDuration != 0 && sr.Start >= pRO.ReportStartDate.Value && sr.Start.Value.Date <= end)
|
|
scRo.AmountPaid += sr.RoundedDuration / 60;
|
|
if (sr.ServiceDescription != null && sr.ServiceDescription.Name == "ZwR" && sr.Start >= pRO.ReportStartDate.Value && sr.Start.Value.Date <= end)
|
|
scRo.AmountTotal += sr.RoundedDuration / 60;
|
|
if (sr.Start >= monthStart && sr.Start.Value.Date <= end)
|
|
{
|
|
scRo.BillableHours += calc.GetBillableDurationInMinutes(sr, false) / 60;
|
|
}
|
|
if (sr.Start >= start && sr.Start.Value.Date <= end)
|
|
{
|
|
scRo.BillableHoursInReportTimeSpan += calc.GetBillableDurationInMinutes(sr, false) / 60;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pRO.CalculateBillableAmountInReportTimeSpan();
|
|
|
|
this.bindingSource1.DataSource = pRO;
|
|
}
|
|
|
|
private Dictionary<long, ServiceDescriptionDC> LoadServiceDescriptionDict()
|
|
{
|
|
var allServiceDescs = DAOFactory.GenericDAO.GetAll<ServiceDescription>();
|
|
var dict = new Dictionary<long, ServiceDescriptionDC>();
|
|
foreach (var sd in allServiceDescs)
|
|
{
|
|
if (sd.Oid.HasValue)
|
|
{
|
|
dict[sd.Oid.Value] = MapperFactory.ServiceDescriptionDC_ServiceDescription.MapToNewDC(sd);
|
|
}
|
|
}
|
|
return dict;
|
|
}
|
|
|
|
private List<ServiceRecordDC> LoadBillableNonKompensatorischRecords(
|
|
SupportConceptCostBearerRelDC cb2sc,
|
|
FinanceRO.SupportConceptFinanceRO scRo,
|
|
HashSet<long> existingOids,
|
|
Dictionary<long, ServiceDescriptionDC> serviceDict)
|
|
{
|
|
var result = new List<ServiceRecordDC>();
|
|
|
|
string sql = string.Format(
|
|
"SELECT Oid, Version, StartDate, EndDate, DistanceInMeter, CostBearer2SupportConceptOid, Betrag, ServiceDescriptionOid, RoundedDuration, GroupEmployeeCount, GroupPersonCount, GroupRoundedDuration, GroupOid " +
|
|
"FROM servicerecord WHERE CostBearer2SupportConceptOid = {0}", cb2sc.CostBearer2SupportConceptOid.Value);
|
|
|
|
var sqlresult = DAOFactory.SearchDAO.GetSqlResult(sql);
|
|
|
|
foreach (object[] row in sqlresult)
|
|
{
|
|
long? oid = (long?)row[0];
|
|
if (!oid.HasValue || existingOids.Contains(oid.Value))
|
|
continue;
|
|
|
|
long? sdOid = (long?)row[7];
|
|
if (!sdOid.HasValue || !serviceDict.ContainsKey(sdOid.Value))
|
|
continue;
|
|
|
|
var sd = serviceDict[sdOid.Value];
|
|
if (sd.Category == null ||
|
|
!sd.Category.IsBillable ||
|
|
(sd.Category.Name ?? "").IndexOf("kompensatorisch", StringComparison.OrdinalIgnoreCase) >= 0)
|
|
continue;
|
|
|
|
var sr = new ServiceRecordDC();
|
|
sr.ServiceRecordOid = oid;
|
|
sr.ServiceRecordVersion = (long?)row[1];
|
|
sr.Start = (DateTime?)row[2];
|
|
sr.End = (DateTime?)row[3];
|
|
sr.CostBearer2SupportConceptOid = (long?)row[5];
|
|
sr.Betrag = (decimal?)row[6];
|
|
sr.ServiceDescription = sd;
|
|
sr.RoundedDuration = (decimal)row[8];
|
|
sr.GroupEmployeeCount = (int?)row[9];
|
|
sr.GroupPersonCount = (int?)row[10];
|
|
sr.GroupRoundedDuration = (decimal?)row[11];
|
|
sr.GroupOid = (long?)row[12];
|
|
// Pflichtfelder für GetBillableDurationInMinutes (analog zu FinanceRO.Create)
|
|
sr.Employee = new CompactEmployeeDC { ValueListEntries = new Dictionary<long, ValueListEntryType>() };
|
|
sr.CostBearer = cb2sc.CostBearer;
|
|
sr.SupportConcept = scRo.Costbearer2Supportconcept.SupportConcept;
|
|
sr.Customer = scRo.Costbearer2Supportconcept.SupportConcept.Customer;
|
|
|
|
result.Add(sr);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private string GetTeams(FinanceRO.SupportConceptFinanceRO scRo)
|
|
{
|
|
var c = DAOFactory.GenericDAO.GetByID<Customer>(scRo.Costbearer2Supportconcept.SupportConcept.Customer.CustomerOid);
|
|
if (c.Team2CustomerList.Count > 0)
|
|
{
|
|
scRo.Costbearer2Supportconcept.SupportConcept.Customer.RelatedTeams = c.Team2CustomerList[0].Team.Name;
|
|
}
|
|
return scRo.Costbearer2Supportconcept.SupportConcept.Customer.RelatedTeams;
|
|
}
|
|
|
|
private decimal GetApprovedHoursOhneFahrzeitForPeriod(SupportConceptCostBearerRelDC relDC, DateTime pStart, DateTime pEnd, SupportConceptApprovalPeriodDC filterScap = null)
|
|
{
|
|
var costRatePeriods = relDC.CostBearer.CostRatePeriods;
|
|
decimal approvedHours = 0;
|
|
DateTime start = pStart;
|
|
DateTime end = pEnd;
|
|
|
|
IEnumerable<SupportConceptApprovalPeriodDC> periodsToCheck = relDC.ApprovalPeriodList;
|
|
if (filterScap?.ServiceCategory != null)
|
|
{
|
|
periodsToCheck = relDC.ApprovalPeriodList.Where(p => p.ServiceCategory?.ServiceCategoryOid == filterScap.ServiceCategory.ServiceCategoryOid);
|
|
}
|
|
|
|
foreach (var scap in periodsToCheck)
|
|
{
|
|
if ((start.InBetween(scap.Span, true) || end.InBetween(scap.Span, true)) && scap.ServiceCategory != null && !scap.ServiceCategory.Name.ToLower().Contains("fahrzeit"))
|
|
{
|
|
if (scap.StartDate.HasValue && scap.StartDate.Value > start)
|
|
start = scap.StartDate.Value;
|
|
while (start <= end)
|
|
{
|
|
if (scap != null)
|
|
{
|
|
if (scap.EndDate.HasValue && scap.EndDate.Value < end)
|
|
end = scap.EndDate.Value;
|
|
|
|
var periodInDays = end.Subtract(start).Days + 1;
|
|
var calc = BS.Shared.Services.Calculations.GetInstance(relDC.CostBearer.CostBearerID);
|
|
approvedHours += (calc.GetApprovedHoursPerDay(scap, costRatePeriods) ?? 0) * periodInDays;
|
|
}
|
|
else
|
|
{
|
|
DateTime minStart = DateTime.MaxValue;
|
|
foreach (var period in relDC.ApprovalPeriodList)
|
|
{
|
|
if (period.StartDate.HasValue && period.EndDate.HasValue)
|
|
{
|
|
if (period.StartDate.Value > start && period.StartDate.Value <= pEnd && period.StartDate.Value < minStart)
|
|
{
|
|
minStart = period.StartDate.Value;
|
|
}
|
|
}
|
|
}
|
|
end = minStart.AddDays(-1);
|
|
}
|
|
|
|
start = end.AddDays(1);
|
|
}
|
|
}
|
|
}
|
|
return approvedHours;
|
|
}
|
|
}
|
|
}
|