Files
BeWoPlaner/Report/ReportObjects/SbdStundenaufstellungRO.cs
Christian 7559ca2d75 - Christopherus Haus Schulbegleitung, Leistungsnachweis und Rechnung
- Bugfixes Kalender Tooltip und Anlegen von Serien in Monatsansicht mit korrektem Enddatum
2025-01-31 00:56:43 +01:00

995 lines
37 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using BeWo.Data;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Configuration;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.Plugins;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Services;
namespace BeWo.Report.ReportObjects
{
public class SbdStundenaufstellungRO : AbstractBeWoReportObject<SbdStundenaufstellungRO>
{
private List<SbdServiceDetail> _Services = new List<SbdServiceDetail>();
private IList<FeiertagDC> _feiertage = null;
public long CustomerOid { get; set; }
public string AbsenceTimes { get; set; }
public List<AbsenceTimeDC> Abwesenheiten { get; set; }
public DateTime? ApprovedStartDate { get; set; }
public DateTime? ApprovedEndDate { get; set; }
public decimal ApprovedPerMonth { get; set; }
public decimal ApprovedPerWeek { get; set; }
public decimal ApprovedTotal { get; set; }
public DateTime? CustomerBirthday { get; set; }
public string CustomerCity { get; set; }
public string CustomerFirstName { get; set; }
public string CustomerLastName { get; set; }
public string CustomerName { get; set; }
public string CustomerAlias { get; set; }
public string CustomerPostalCode { get; set; }
public string CustomerStreet { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public long EmployeeOid { get; set; }
public string EmployeeName { get; set; }
public string EmployeePersonalNumber { get; set; }
public string ReferenceNumber { get; set; }
public string Salutation { get; set; }
public CompactOrganisationDC Schule { get; set; }
public SupportConceptCostBearerRelDC SupportConceptCostBearer { get; set; }
public MandatorDC Mandator { get; set; }
public List<SbdServiceDetail> Services
{
get
{
return this._Services;
}
set
{
this._Services = value;
}
}
public static List<SbdStundenaufstellungRO> Create(int pMonth, int pYear, long orgaOid, long empOid, long serviceCategoryOid, int startDay, int endDay, bool checkServiceRecords, string bundesland, bool splitByEmployee = true)
{
var roList = new List<SbdStundenaufstellungRO>();
IList<Customer> customers = null;
ApplicationUser loggedInUser = null;
if (LoggedInUserOperationContextExt.Current != null && LoggedInUserOperationContextExt.Current.User != null)
{
loggedInUser = LoggedInUserOperationContextExt.Current.User;
}
else
{
loggedInUser = SessionFacade.LoggedInUser;
}
if (loggedInUser != null && loggedInUser.Employee != null)
{
if (!HasRight(loggedInUser, UserRightType.CustomerView_View))
{
customers = new List<Customer>();
Dictionary<long, bool> customerOidDict = new Dictionary<long, bool>();
if (HasRight(loggedInUser, UserRightType.Customer_ViewMyTeams))
{
foreach (var t in loggedInUser.Employee.LeadingTeams)
{
var teamCustomers = DAOFactory.SearchDAO.FindCustomerOfTeam(t.Oid.Value);
foreach (var c in teamCustomers)
{
if (!customerOidDict.ContainsKey(c.Oid.Value))
{
customers.Add(c);
customerOidDict.Add(c.Oid.Value, true);
}
}
}
}
else
{
foreach (var e2c in loggedInUser.Employee.Employee2CustomerList)
{
customers.Add(e2c.Customer);
}
}
}
}
if (customers == null)
{
customers = DAOFactory.GenericDAO.GetAllActive<Customer>();
}
if (startDay > endDay)
{
int temp = startDay;
startDay = endDay;
endDay = temp;
}
var pSpan = new DateTimeSpan();
pSpan.StartDateTime = new DateTime(pYear, pMonth, startDay);
pSpan.EndDateTime = pSpan.StartDateTime.AddDays(endDay - startDay + 1).AddTicks(-1);
// IList<Customer> customers = DAOFactory.SearchDAO.GetAllCustomerWithServiceRecordsInSpan(pSpan).OrderBy(ob => ob.Person.LastName + ", " + ob.Person.FirstName).ToList();
foreach (Customer customer in customers)
{
var rolist = Create(customer, pSpan, orgaOid, empOid, serviceCategoryOid, true, checkServiceRecords, bundesland, splitByEmployee);
if (rolist != null)
{
roList.AddRange(rolist);
}
}
return roList.OrderBy(r => r.CustomerLastName + ", " + r.CustomerFirstName).ToList();
// return DAOFactory.SearchDAO.FindSupportConceptsInSpan(pSpan).OrderBy(ob => ob.Customer.Person.LastName + ", " + ob.Customer.Person.FirstName).Select(
// sc => Create(sc.Customer.Oid.Value, pMonth, pYear, true)).ToList();
}
private static bool HasRight(ApplicationUser user, UserRightType right)
{
return GetGrantedRights(user).Contains(right);
}
public static List<UserRightType> GetGrantedRights(ApplicationUser user)
{
var rights = new List<UserRightType>();
foreach (var ug in user.UserGroups)
{
foreach (var rr in ug.Rights)
{
rights.Add(rr.RightType);
}
}
return rights;
}
public static List<SbdStundenaufstellungRO> Create(long pCustomerOid, int pMonth, int pYear, long orgaOid, long empOid, long serviceCategoryOid, int start, int end, bool checkServiceRecords, string bundesland, bool disableEmptySupportConcepts = false, bool splitByEmployee = true)
{
var pSpan = new DateTimeSpan();
if (start > end)
{
int temp = start;
start = end;
end = temp;
}
pSpan.StartDateTime = new DateTime(pYear, pMonth, start);
pSpan.EndDateTime = pSpan.StartDateTime.AddDays(end - start + 1).AddTicks(-1);
var resultList = new List<SbdStundenaufstellungRO>();
Customer c = null;
if (pCustomerOid > 0)
{
c = DAOFactory.GenericDAO.LoadByID<Customer>(pCustomerOid);
resultList.AddRange(Create(c, pSpan, orgaOid, empOid, serviceCategoryOid, disableEmptySupportConcepts, checkServiceRecords, bundesland, splitByEmployee));
}
else if (empOid > 0)
{
var emp = DAOFactory.GenericDAO.LoadByID<Employee>(empOid);
foreach (var e2c in emp.Employee2CustomerList)
{
if (e2c.Customer.IsActive == ActivationTypeId.Active)
{
resultList.AddRange(Create(e2c.Customer, pSpan, orgaOid, empOid, serviceCategoryOid,
disableEmptySupportConcepts, checkServiceRecords, bundesland, splitByEmployee));
}
}
}
return resultList;
}
public static List<SbdStundenaufstellungRO> Create(Customer cust, DateTimeSpan span, long orgaOid, long empOid, long serviceCategoryOid, bool disableEmptySupportConcepts, bool checkServiceRecords, string bundesland, bool splitByEmployee)
{
var resultList = new List<SbdStundenaufstellungRO>();
Dictionary<long, SbdStundenaufstellungRO> employeeOid2Ro = new Dictionary<long, SbdStundenaufstellungRO>();
Dictionary<int, SbdServiceDetail> day2Details = new Dictionary<int, SbdServiceDetail>();
var eintraege = ErstelleStundenplanEintraege(cust, empOid, span.StartDate, span.EndDate, bundesland);
foreach (var sd in eintraege)
{
day2Details.Add(sd.StartDate.Day, sd);
}
var cb2scs = new Dictionary<long, SupportConceptCostBearerRelDC>();
var groupBookingDict = new Dictionary<long, ServiceRecord>();
//bool checkServiceRecords = true;
if (checkServiceRecords)
{
IList<ServiceRecord> lServiceRecords = null;
if (cust != null)
{
lServiceRecords = DAOFactory.SearchDAO.FindCustomerServiceRecords(cust.Oid.Value, span, null, null, false).OrderBy(s => s.Start).ToList();
}
else
{
lServiceRecords = new List<ServiceRecord>();
}
bool deleteEntries = false;
foreach (var iSr in lServiceRecords)
{
bool cont = !(empOid != 0 && iSr.EmployeeOid != null && iSr.EmployeeOid != empOid);
if (cont)
{
cont = day2Details.ContainsKey(iSr.Start.Value.Day);
}
CostBearer2SupportConcept cb2sc = null;
if (cont)
{
cb2sc = iSr.CostBearer2SupportConcept;
}
if (cb2sc == null)
cont = false;
else if (cont && orgaOid != 0 && cb2sc.CostBearer.Organisation != null && cb2sc.CostBearer.Organisation.Oid.Value != orgaOid)
cont = false;
if (cont)
{
if (cb2sc.SupportConcept.IsActive == ActivationTypeId.Deleted)
cont = false;
}
if (cont && iSr.GroupOid != null && groupBookingDict.ContainsKey(iSr.GroupOid.Value))
cont = false;
if (cont && serviceCategoryOid > 0 && iSr.ServiceDescription.ServiceCategory.Oid != serviceCategoryOid)
cont = false;
if (cont)
{
SbdStundenaufstellungRO ro = null;
if (!splitByEmployee && employeeOid2Ro.Count == 1)
{
ro = employeeOid2Ro.Values.ToList()[0];
}
else if (employeeOid2Ro.ContainsKey(iSr.EmployeeOid.Value))
{
ro = employeeOid2Ro[iSr.EmployeeOid.Value];
}
else
{
ro = CreateInitalRo(cust, span, iSr.Employee, 0);
employeeOid2Ro.Add(iSr.EmployeeOid.Value, ro);
resultList.Add(ro);
}
ro.ApprovedStartDate = cb2sc.StartDate;
ro.ApprovedEndDate = cb2sc.EndDate;
if (!cb2scs.ContainsKey(cb2sc.Oid.Value))
{
var cb2scDC = cb2sc.MapToNewDC();
var calc = PluginLoader.FindClass<Calculations>(cb2sc.CostBearer.ID) ?? Calculations.GetInstance(cb2sc.CostBearer.ID);
DateTime sdt = span.StartDateTime;
if (sdt < cb2scDC.StartDate)
sdt = cb2scDC.StartDate.Value;
ro.ApprovedPerWeek = calc.GetApprovedHoursPerWeek(cb2scDC, sdt, false) ?? 0;
ro.ApprovedTotal = calc.GetApprovedHoursTotal(cb2scDC, false) ?? 0;
cb2scs.Add(cb2sc.Oid.Value, cb2scDC);
}
deleteEntries = true;
var sd = day2Details[iSr.Start.Value.Day];
sd.CostBearer = iSr.CostBearer2SupportConcept != null && iSr.CostBearer2SupportConcept.CostBearer.Organisation != null
? iSr.CostBearer2SupportConcept.CostBearer.Organisation.Name : string.Empty;
sd.StartDate = iSr.Start.Value;
sd.ServiceRecordOid = iSr.Oid.Value;
sd.SignatureOid = iSr.SignatureOid;
decimal roundedDur = iSr.RoundedDuration;
decimal prozent = iSr.ServiceDescription.ServiceCategory.ProzentAbrechnung;
if (iSr.ServiceDescription.ProzentAbrechnung.HasValue)
{
prozent = iSr.ServiceDescription.ProzentAbrechnung.Value;
}
roundedDur *= (prozent / 100);
sd.Minutes += Convert.ToDouble(roundedDur);
if (iSr.GroupEmployeeCount != null)
sd.EmployeeCount = iSr.GroupEmployeeCount.Value;
else
sd.EmployeeCount = 1;
if (iSr.GroupPersonCount != null)
sd.CustomerCount = iSr.GroupPersonCount.Value;
else
sd.CustomerCount = 1;
sd.GroupRoundedDuration = iSr.GroupRoundedDuration;
if (iSr.GroupRoundedDuration != null)
sd.GroupMinutes = (double)iSr.GroupRoundedDuration.Value;
else
sd.GroupMinutes = sd.Minutes;
if (iSr.GroupOid != null)
{
sd.IsGroup = true;
groupBookingDict.Add(iSr.GroupOid.Value, iSr);
}
sd.EndDate = sd.StartDate.AddMinutes(Convert.ToDouble(roundedDur));
sd.EndDateNotRounded = iSr.End.Value;
if (sd.StartDate.Second == 0)
{
var timeRange = String.Format("{0}-{1}", sd.StartDate.ToShortTimeString(), sd.EndDate.ToShortTimeString());
if (sd.StundenplanUhrzeiten1 != timeRange && sd.StundenplanUhrzeiten2 != timeRange)
{
if (String.IsNullOrWhiteSpace(sd.IstUhrzeiten2) && String.IsNullOrWhiteSpace(sd.StundenplanUhrzeiten1))
{
sd.IstUhrzeiten2 = String.Format("{0}-{1}", sd.StartDate.ToShortTimeString(), sd.EndDate.ToShortTimeString());
}
else if (!String.IsNullOrWhiteSpace(sd.StundenplanUhrzeiten1))
{
DateTime schuleAus = new DateTime(sd.EndDate.Year, sd.EndDate.Month, sd.EndDate.Day, Convert.ToInt32(sd.StundenplanUhrzeiten1.Substring(6, 2)), Convert.ToInt32(sd.StundenplanUhrzeiten1.Substring(9, 2)), 0);
if (String.IsNullOrWhiteSpace(sd.IstUhrzeiten2) && sd.EndDate > schuleAus)
sd.IstUhrzeiten2 = String.Format("{0}-{1}", sd.StartDate.ToShortTimeString(), sd.EndDate.ToShortTimeString());
else if (String.IsNullOrWhiteSpace(sd.IstUhrzeiten1))
{
sd.IstUhrzeiten1 = String.Format("{0}-{1}", sd.StartDate.ToShortTimeString(), sd.EndDate.ToShortTimeString());
}
else
{
sd.IstUhrzeiten2 += String.Format(", {0}-{1}", sd.StartDate.ToShortTimeString(), sd.EndDate.ToShortTimeString());
}
}
else if (String.IsNullOrWhiteSpace(sd.IstUhrzeiten1))
{
sd.IstUhrzeiten1 = String.Format("{0}-{1}", sd.StartDate.ToShortTimeString(), sd.EndDate.ToShortTimeString());
}
else
{
sd.IstUhrzeiten2 += String.Format(", {0}-{1}", sd.StartDate.ToShortTimeString(), sd.EndDate.ToShortTimeString());
}
}
sd.StartDateMinutes = sd.StartDate;
sd.EndDateMinutes = sd.EndDate;
}
sd.StartDateString = iSr.Start.Value.ToShortDateString();
sd.KW = GetKW(sd.StartDate);
string employeeAbbr = iSr.Employee.Person.Abbreviation;
string employeeFullName = iSr.Employee.Person.FirstName + " " + iSr.Employee.Person.LastName;
if (employeeAbbr == null || employeeAbbr.Length == 0)
employeeAbbr = iSr.Employee.Person.FirstName.Substring(0, 1) + ". " + iSr.Employee.Person.LastName;
sd.EmployeeAbbr = employeeAbbr;
sd.EmployeeFullname = employeeFullName;
sd.EmployeeFirstName = iSr.Employee.Person.FirstName;
sd.EmployeeLastName = iSr.Employee.Person.LastName;
sd.EmployeeOid = iSr.Employee.Oid.Value;
sd.Notice = iSr.Notice;
sd.Notice2 = iSr.Notice2;
sd.Notice3 = iSr.Notice3;
sd.Notice4 = iSr.Notice4;
sd.Notice5 = iSr.Notice5;
if (iSr.ServiceDescription != null)
{
sd.ServiceDescription = iSr.ServiceDescription.Name;
sd.ServiceCategory = iSr.ServiceDescription.ServiceCategory.Name;
sd.ServiceCategoryAbbr = iSr.ServiceDescription.ServiceCategory.Abbreviation;
if (sd.ServiceCategoryAbbr == null || sd.ServiceCategoryAbbr.Length == 0)
sd.ServiceCategoryAbbr = iSr.ServiceDescription.ServiceCategory.Name;
}
sd.IsBillable = iSr.ServiceDescription.ServiceCategory.IsBillable;
}
}
if (deleteEntries)
{
foreach (var key in day2Details.Keys)
{
var dd = day2Details[key];
if (dd.ServiceRecordOid == 0)
{
dd.StundenplanUhrzeiten1 = "";
dd.StundenplanUhrzeiten2 = "";
}
if (cust.AbsenceTimes.Count > 0)
{
foreach (var at in cust.AbsenceTimes)
{
var ad = at.Start.Value;
if (at.Start.HasValue && dd.StartDate >= at.Start.Value && at.End.HasValue && dd.EndDate <= at.End.Value)
{
dd.StundenplanUhrzeiten1 = "";
dd.StundenplanUhrzeiten2 = "";
}
}
}
}
}
}
if (disableEmptySupportConcepts && eintraege.Count == 0)
{
return null;
}
eintraege.Sort((x, y) =>
{
var c = x.StartDate.CompareTo(y.StartDate);
if (c != 0)
{
return c;
}
return x.ServiceRecordOid.CompareTo(y.ServiceRecordOid);
});
Employee hauptBetreuer = null;
if (cust != null)
{
foreach (var item in cust.Employee2CustomerList)
{
if (hauptBetreuer == null)
{
foreach (var valueEntry in item.ValueList)
{
if (valueEntry.Entry.SystemEntryID != null && valueEntry.Entry.SystemEntryID.Value ==
SystemEntryID.EmployeeRoleMainAttendant)
{
hauptBetreuer = item.Employee;
}
}
}
}
}
foreach (var eintrag in eintraege)
{
if (eintrag.EmployeeOid == 0)
{
eintrag.EmployeeOid = empOid;
}
SbdStundenaufstellungRO ro = null;
if (eintrag.EmployeeOid == 0 && hauptBetreuer != null)
{
eintrag.EmployeeOid = hauptBetreuer.Oid.Value;
}
if (!splitByEmployee && employeeOid2Ro.Count == 1)
{
ro = employeeOid2Ro.Values.ToList()[0];
}
else if (employeeOid2Ro.ContainsKey(eintrag.EmployeeOid))
{
ro = employeeOid2Ro[eintrag.EmployeeOid];
}
else
{
ro = CreateInitalRo(cust, span, null, eintrag.EmployeeOid);
employeeOid2Ro.Add(eintrag.EmployeeOid, ro);
resultList.Add(ro);
}
if (ro.Services == null)
{
ro.Services = new List<SbdServiceDetail>();
}
ro.Services.Add(eintrag);
}
var mandator = MapperFactory.MandatorDC_Mandator.MapToNewDC(AppSettings.GetMandatorEntity());
foreach (var ro in resultList)
{
CreateLeerEintraege(ro, span, bundesland);
ro.Mandator = mandator;
if (cb2scs.Count > 0)
{
ro.SupportConceptCostBearer = cb2scs.Values.First();
}
}
resultList.Sort((x, y) =>
{
String xx = x.EmployeeName ?? "";
return xx.CompareTo(y.EmployeeName);
});
return resultList;
}
private static void CreateLeerEintraege(SbdStundenaufstellungRO ro, DateTimeSpan span, string bundesland)
{
var vs = PluginLoader.FindClass<VacationService>();
Dictionary<int, bool> tagDict = new Dictionary<int, bool>();
foreach (var s in ro.Services)
{
if (!tagDict.ContainsKey(s.StartDate.Day))
{
tagDict.Add(s.StartDate.Day , true);
}
}
DateTime start = span.StartDateTime;
while (start <= span.EndDateTime)
{
if (!tagDict.ContainsKey(start.Day))
{
SbdServiceDetail detail = CreateDefaultDetail(vs, start, bundesland);
ro.Services.Add(detail);
}
start = start.AddDays(1);
}
ro.Services.Sort((x, y) =>
{
var c = x.StartDate.CompareTo(y.StartDate);
if (c != 0)
{
return c;
}
return x.ServiceRecordOid.CompareTo(y.ServiceRecordOid);
});
}
private static SbdServiceDetail CreateDefaultDetail(VacationService vs, DateTime start, string bundesland)
{
var detail = new SbdServiceDetail();
detail.StartDate = start;
detail.TimeRangeString = "";
detail.Notice = "";
if (start.DayOfWeek == DayOfWeek.Saturday)
{
detail.IstSamstag = true;
}
if (start.DayOfWeek == DayOfWeek.Sunday)
{
detail.IstSonntag = true;
}
var ferien = vs.GetFerien(start, vs.Bundesland);
if (ferien != null)
{
detail.IstFerien = true;
detail.Notice = ferien.Name;
}
var sbd = new SbdStundenaufstellungRO();
var ft = sbd.GetFeiertag(vs, start);
if (ft != null)
{
detail.IstFeiertag = true;
detail.Notice = ft.Name;
}
return detail;
}
public virtual FeiertagDC GetFeiertag(VacationService vs, DateTime start)
{
if (_feiertage == null)
{
_feiertage = vs.GetFeiertage(start.Year, vs.Bundesland);
}
foreach (var ft in _feiertage)
{
if (start == ft.Datum)
return ft;
}
return null;
}
private static SbdStundenaufstellungRO CreateInitalRo(Customer customer, DateTimeSpan span, Employee employee, long employeeoid)
{
var lResult = new SbdStundenaufstellungRO();
lResult.Abwesenheiten = new List<AbsenceTimeDC>();
lResult.StartDate = span.StartDate;
lResult.EndDate = span.EndDate;
if (customer != null)
{
lResult.CustomerOid = customer.Oid.Value;
lResult.CustomerAlias = customer.CustomerAlias;
lResult.CustomerFirstName = customer.Person.FirstName;
lResult.CustomerLastName = customer.Person.LastName;
lResult.CustomerBirthday = customer.Person.DateOfBirth;
lResult.CustomerName = customer.Person.FirstName + " " + customer.Person.LastName;
if (customer.Person.Address != null)
{
lResult.CustomerStreet = customer.Person.Address.Street;
lResult.CustomerPostalCode = customer.Person.Address.PostalCode;
lResult.CustomerCity = customer.Person.Address.Town;
}
var schule = SucheSchule(customer);
if (schule != null)
{
lResult.Schule = MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(schule);
if (schule.Address != null)
{
lResult.Schule.Street = schule.Address.Street;
lResult.Schule.PostalCode = schule.Address.PostalCode;
lResult.Schule.Town = schule.Address.Town;
}
}
foreach (var at in customer.AbsenceTimes)
{
var atDc = MapperFactory.AbsenceTimeDC_AbsenceTime.MapToNewDC(at);
lResult.Abwesenheiten.Add(atDc);
}
}
if (employee == null && employeeoid > 0)
{
employee = DAOFactory.GenericDAO.LoadByID<Employee>(employeeoid);
}
if (employee != null)
{
lResult.EmployeeOid = employee.Oid.Value;
lResult.EmployeePersonalNumber = employee.PersonnelNumber;
lResult.EmployeeName = employee.Person.FirstName + " " + employee.Person.LastName;
}
//foreach (var item in customer.Employee2CustomerList)
//{
// foreach (var valueEntry in item.ValueList)
// {
// if (valueEntry.Entry.SystemEntryID != null && valueEntry.Entry.SystemEntryID.Value == SystemEntryID.EmployeeRoleMainAttendant)
// {
// lResult.EmployeePersonalNumber = item.Employee.PersonnelNumber;
// var empName = item.Employee.Person.FirstName + " " + item.Employee.Person.LastName;
// if (String.IsNullOrEmpty(lResult.MainAttendant))
// {
// lResult.MainAttendant = empName;
// }
// else
// {
// if (!lResult.MainAttendant.Contains(empName))
// {
// lResult.MainAttendant += ", " + empName;
// }
// }
// }
// }
//}
return lResult;
}
private static List<SbdServiceDetail> ErstelleStundenplanEintraege(Customer customer, long empOid, DateTime start, DateTime ende, string bundesland)
{
var vs = PluginLoader.FindClass<VacationService>();
List<SbdServiceDetail> details = new List<SbdServiceDetail>();
while (start <= ende)
{
var stundenplan = GetStundenplan(customer, start, start);
SbdServiceDetail detail = CreateDefaultDetail(vs, start, vs.Bundesland);
details.Add(detail);
if (!detail.IstSamstag && !detail.IstSonntag && !detail.IstFerien && !detail.IstFeiertag)
{
SetzeStundenplanEintraege(stundenplan, detail, empOid);
}
start = start.AddDays(1);
}
return details;
}
private static Organisation SucheSchule(Customer customer)
{
foreach (var c2o in customer.Customer2OrganisationList)
{
foreach (var v2o in c2o.ValueList)
{
if (v2o.Entry.Value != null && v2o.Entry.Value.Contains("Schule"))
{
return c2o.Organisation;
}
}
}
return null;
}
private static Arbeitszeit GetStundenplan(Customer customer, DateTime start, DateTime ende)
{
if (customer == null)
{
return null;
}
var stundenplan = SucheStundenplan(customer.Arbeitszeiten, start, ende);
if (stundenplan == null)
{
//Kein Stundenplan hinterlegt, suche Stundenplan bei der Organisation (Schule)
var org = SucheSchule(customer);
if (org != null)
{
return SucheStundenplan(org.Arbeitszeiten, start, ende);
}
}
return stundenplan;
}
private static Arbeitszeit SucheStundenplan(IList<Arbeitszeit> arbeitszeiten, DateTime start, DateTime ende)
{
if (arbeitszeiten != null && arbeitszeiten.Count > 0)
{
foreach (var az in arbeitszeiten)
{
if ((!az.GueltigVon.HasValue || az.GueltigVon <= ende) && (!az.GueltigBis.HasValue || az.GueltigBis >= start))
{
return az;
}
}
}
return null;
}
private static void SetzeStundenplanEintraege(Arbeitszeit arbeitszeit, SbdServiceDetail detail, long empOid)
{
if (arbeitszeit != null && arbeitszeit.ArbeitszeitEintraege != null && arbeitszeit.ArbeitszeitEintraege.Count > 0)
{
if (arbeitszeit.GueltigVon <= detail.StartDate && detail.StartDate.Date <= arbeitszeit.GueltigBis)
{
var day = detail.StartDate.DayOfWeek;
foreach (var ae in arbeitszeit.ArbeitszeitEintraege)
{
if (ae.Employee == null || empOid == 0 || empOid == ae.Employee.Oid.Value)
{
if (SindAmGleichenWochentag(day, ae.Tag))
{
if (String.IsNullOrWhiteSpace(detail.StundenplanUhrzeiten1))
{
detail.StundenplanUhrzeiten1 = CreateTimeRangeString(ae.UhrzeitVon, ae.UhrzeitBis);
}
else if (String.IsNullOrWhiteSpace(detail.StundenplanUhrzeiten2))
{
detail.StundenplanUhrzeiten2 = CreateTimeRangeString(ae.UhrzeitVon, ae.UhrzeitBis);
}
else
{
detail.StundenplanUhrzeiten2 += CreateTimeRangeString(ae.UhrzeitVon, ae.UhrzeitBis);
}
if (detail.ArbeitszeitEintraege == null)
{
detail.ArbeitszeitEintraege = new List<ArbeitszeitEintragDC>();
}
detail.ArbeitszeitEintraege.Add(MapperFactory.ArbeitszeitEintragDC_ArbeitszeitEintrag.MapToNewDC(ae));
}
}
}
}
}
}
private static string CreateTimeRangeString(string uhrzeitVon, string uhrzeitBis)
{
var dtStart = SchulbegleitenderDienstService.GetDateTime(uhrzeitVon, DateTime.Now);
var dtEnd = SchulbegleitenderDienstService.GetDateTime(uhrzeitBis, DateTime.Now);
return String.Format("{0:HH:mm}-{1:HH:mm}", dtStart, dtEnd);
}
private static bool SindAmGleichenWochentag(DayOfWeek day, AppointmentDayOfWeek aeTag)
{
if (day == DayOfWeek.Monday && aeTag == AppointmentDayOfWeek.Montag)
{
return true;
}
if (day == DayOfWeek.Tuesday && aeTag == AppointmentDayOfWeek.Dienstag)
{
return true;
}
if (day == DayOfWeek.Wednesday && aeTag == AppointmentDayOfWeek.Mittwoch)
{
return true;
}
if (day == DayOfWeek.Thursday && aeTag == AppointmentDayOfWeek.Donnerstag)
{
return true;
}
if (day == DayOfWeek.Friday && aeTag == AppointmentDayOfWeek.Freitag)
{
return true;
}
if (day == DayOfWeek.Saturday && aeTag == AppointmentDayOfWeek.Samstag)
{
return true;
}
return false;
}
private static int GetKW(DateTime actualDate)
{
Calendar objCal = CultureInfo.CurrentCulture.Calendar;
int weekofyear = objCal.GetWeekOfYear(actualDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekofyear;
}
public class SbdServiceDetail
{
public long ServiceRecordOid { get; set; }
public string CostBearer { get; set; }
public long EmployeeOid { get; set; }
public string EmployeeFirstName { get; set; }
public string EmployeeLastName { get; set; }
public string EmployeeAbbr { get; set; }
public int CustomerCount { get; set; }
public int EmployeeCount { get; set; }
public string EmployeeFullname { get; set; }
public DateTime EndDate { get; set; }
public DateTime? EndDateMinutes { get; set; }
public double GroupMinutes { get; set; }
public decimal? GroupRoundedDuration { get; set; }
public bool IsBillable { get; set; }
public bool IsGroup { get; set; }
public int KW { get; set; }
public double Minutes { get; set; }
public string Notice { get; set; }
public string Notice2 { get; set; }
public string Notice3 { get; set; }
public string Notice4 { get; set; }
public string Notice5 { get; set; }
public string ServiceCategory { get; set; }
public string ServiceCategoryAbbr { get; set; }
public string ServiceDescription { get; set; }
public DateTime StartDate { get; set; }
public DateTime? StartDateMinutes { get; set; }
public string StartDateString { get; set; }
public string TimeRangeString { get; set; }
public DateTime EndDateNotRounded { get; set; }
public bool IstFeiertag { get; set; }
public bool IstFerien { get; set; }
public bool IstSamstag { get; set; }
public bool IstSonntag { get; set; }
public long? SignatureOid { get; set; }
public String Signature { get; set; }
public DateTime? SignatureDate { get; set; }
public string StundenplanUhrzeiten1 { get; set; }
public string StundenplanUhrzeiten2 { get; set; }
public string IstUhrzeiten1 { get; set; }
public string IstUhrzeiten2 { get; set; }
public IList<ArbeitszeitEintragDC> ArbeitszeitEintraege { get; set; }
}
}
}