Files
BeWoPlaner/Report/ReportObjects/UrlaubskontoRO.cs
Christian a896a7bfec cb
2019-09-17 17:44:57 +02:00

230 lines
7.5 KiB
C#

using System;
using System.Collections.Generic;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.Extensions;
namespace BeWo.Report.ReportObjects
{
public class UrlaubskontoRO : AbstractBeWoReportObject<UrlaubskontoRO>
{
List<EmployeeDetail> _EmployeeDetailList = new List<EmployeeDetail>();
List<TeamDetail> _TeamDetailList = new List<TeamDetail>();
public DateTime ReportStartDate { get; set; }
public DateTime ReportEndDate { get; set; }
public List<EmployeeDetail> EmployeeDetailList
{
get { return this._EmployeeDetailList; }
set { this._EmployeeDetailList = value; }
}
public List<TeamDetail> TeamDetailList
{
get { return this._TeamDetailList; }
set { this._TeamDetailList = value; }
}
public static UrlaubskontoRO Create(long? employeeOid, IList<long> teamOids, DateTime startDate, DateTime endDate, IEnumerable<long> employeeOids)
{
Team team = null;
Dictionary<long, bool> empOidDict = new Dictionary<long, bool>();
var ro = new UrlaubskontoRO { ReportStartDate = startDate, ReportEndDate = endDate };
IList<Employee> employees = null;
if (employeeOid.HasValue)
{
var emp = DAOFactory.GenericDAO.GetByID<Employee>(employeeOid.Value);
employees = new List<Employee> { emp };
}
else if (teamOids != null)
{
foreach (var teamOid in teamOids)
{
team = DAOFactory.GenericDAO.GetByID<Team>(teamOid);
if (employees == null)
{
employees = team.MemberList;
}
else
{
employees.AddRange(team.MemberList);
team = null;
}
}
}
else if (employeeOids != null)
{
employees = DAOFactory.GenericDAO.LoadByIDs<Employee>(employeeOids);
}
else
{
employees = DAOFactory.GenericDAO.GetAllActiveAndArchived<Employee>();
}
if (employees == null)
{
employees = new List<Employee>();
}
foreach (var emp in employees)
{
if (!empOidDict.ContainsKey(emp.Oid.Value))
{
var empDetail = new EmployeeDetail();
bool add = true;
if (emp.IsActive == ActivationTypeId.Archived)
{
Contract c = emp.GetValidContractForDate(ro.ReportStartDate);
if (c == null) // Kein Vertrag vom 1. gefunden, gucken, ob es einen gibt, der mitten im Monat beginnt.
{
var c2 = GetNextValidContractForDate(ro.ReportStartDate, emp);
if (c2 != null && c2.EntryDate.HasValue && c2.EntryDate.Value < ro.ReportEndDate)
c = c2;
}
if (c == null)
{
add = false;
}
}
if (add)
{
ro.EmployeeDetailList.Add(empDetail);
empDetail.Teams = team == null
? DAOFactory.SearchDAO.FindTeamsOfEmployee(emp.Oid.Value)
: new List<Team> { team };
empDetail.Employee = emp;
empDetail.LastName = emp.Person.LastName;
empDetail.FirstName = emp.Person.FirstName;
empDetail.FullName = String.Format("{0}, {1}", empDetail.LastName, empDetail.FirstName);
}
if (!empOidDict.ContainsKey(emp.Oid.Value))
{
empOidDict.Add(emp.Oid.Value, true);
}
}
}
var teamOid2Details = new Dictionary<long, TeamDetail>();
foreach (var employee in ro.EmployeeDetailList)
{
if (employee.Teams.Count > 0)
{
foreach (var t in employee.Teams)
{
TeamDetail td;
if (!teamOid2Details.ContainsKey(t.Oid.Value))
{
td = new TeamDetail { TeamName = t.Name, TeamLeader = t.Leader };
ro.TeamDetailList.Add(td);
teamOid2Details.Add(t.Oid.Value, td);
}
else
{
td = teamOid2Details[t.Oid.Value];
}
td.EmployeeDetailList.Add(employee);
}
}
}
ro.TeamDetailList.Sort((t1, t2) => t1.TeamName.CompareTo(t2.TeamName));
foreach (var teamDetail in ro.TeamDetailList)
{
if (teamDetail.EmployeeDetailList != null)
{
teamDetail.EmployeeDetailList.Sort((e1, e2) => e1.FullName.CompareTo(e2.FullName));
}
}
ro.EmployeeDetailList.Sort((e1, e2) => e1.FullName.CompareTo(e2.FullName));
return ro;
}
private static Contract GetNextValidContractForDate(DateTime dt, Employee emp)
{
Contract contractNextToStartDate = emp.GetValidContractForDate(dt);
if (contractNextToStartDate == null)
{
foreach (var item in emp.Contracts)
{
if (item.EntryDate.HasValue && item.EntryDate.Value >= dt)
{
if (contractNextToStartDate == null || item.EntryDate.Value < contractNextToStartDate.EntryDate.Value)
{
contractNextToStartDate = item;
}
}
}
}
return contractNextToStartDate;
}
public class TeamDetail
{
private List<EmployeeDetail> _EmployeeDetailList = new List<EmployeeDetail>();
public string TeamName { get; set; }
public Employee TeamLeader { get; set; }
public List<EmployeeDetail> EmployeeDetailList
{
get
{
return this._EmployeeDetailList;
}
set
{
this._EmployeeDetailList = value;
}
}
}
public class EmployeeDetail
{
public IList<Team> Teams { get; set; }
public Employee Employee { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public bool HasContract { get; set; }
public Dictionary<string, decimal> TagZuUrlaub { get; set; }
public Dictionary<string, decimal> TagZuKrankheit { get; set; }
public List<AbsenceTime> AbsenceTimesInTimeSpan { get; set; }
public IList<AbsenceTime> AllAbsenceTimes { get; set; }
}
}
}