using System; using System.Collections.Generic; using System.Linq; using BeWo.Data; 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.Extensions; using NHibernate.Util; namespace BeWo.Service.Plugins { public class ResourceService : AbstractIDSpecificDefaultClass { public virtual string ValidateSchedulerAppointment(DateTime start, DateTime end, IEnumerable employees, IEnumerable customers, List resources, long originator, long? appointmentOid, Guid? recurrenceId, int occurrenceIndex = 0, bool shouldSkipResourceAvailability = false) { return DAOFactory.SearchDAO.ValidateSchedulerAppointment(start, end, employees, customers, resources, originator, appointmentOid, recurrenceId, occurrenceIndex); } public virtual List CheckResourceAvailability(DateTime start, DateTime end, List resourceOids, long? selectedAppointmentOid) { var list = new List(); var resources = DAOFactory.SearchDAO.CheckAvailabilityOfResources(resourceOids, start, end, selectedAppointmentOid); foreach(var resource in resources) { list.AddIfNotIn(MapperFactory.ResourceDC_Resource.MapToNewDC(resource)); } return list; } public virtual Dictionary> CheckResourceAvailabilityWithBookingEmployees(DateTime start, DateTime end, List resourceOids, long? selectedAppointmentOid) { var appointments = DAOFactory.SearchDAO.FindAppointmentsByResourcesInInterval(resourceOids, start, end, selectedAppointmentOid); var user = LoggedInUserOperationContextExt.Current?.User ?? SessionFacade.LoggedInUser; var result = new Dictionary>(); if(user is null) { return result; } var loggedInEmployee = user.Employee; var hasRightToViewEmployeeAppointments = user.CheckForRight(UserRightType.KalenderMitarbeitertermineAnsehen); var hasRightToViewResourceAppointments = user.CheckForRight(UserRightType.KalenderRessourcentermineAnsehen); var dictionary = new Dictionary>(); foreach(var appointment in appointments) { foreach(var resource in appointment.ResourceList) { dictionary.AddOrUpdateValueInDictionary(resource, appointment); } } foreach(var resource2Appointments in dictionary) { foreach(var appointment in resource2Appointments.Value) { var originator = appointment.Originator; var employee2Appointments = appointment.EmployeeList; var isOwnAppointment = (originator?.Equals(loggedInEmployee) ?? false) || employee2Appointments.ToList().Any(e2a => e2a.Employee.Equals(loggedInEmployee)); // Wenn isOwnAppointment true ist, darf der Benutzer die Uhrzeiten und die Mitarbeiter sehen. Ansonsten müssen die Rechte geprüft werden. if(isOwnAppointment || hasRightToViewEmployeeAppointments) // Es werden Zeit und Mitarbeiter angezeigt { result.AddOrUpdateValueInDictionary(resource2Appointments.Key.Name, $"{CreateDateTimeInfoFromAppointment(appointment.StartDate, appointment.EndDate, appointment.AllDay)} von {originator}"); } else if(hasRightToViewResourceAppointments && !hasRightToViewEmployeeAppointments) // Nur die Uhrzeit anzeigen { result.AddOrUpdateValueInDictionary(resource2Appointments.Key.Name, $"{CreateDateTimeInfoFromAppointment(appointment.StartDate, appointment.EndDate, appointment.AllDay)}"); } else if(!hasRightToViewEmployeeAppointments) // Es werden weder Zeit noch Mitarbeiter angezeigt { result.AddOrUpdateValueInDictionary(resource2Appointments.Key.Name, string.Empty); } } } return result; } private string CreateDateTimeInfoFromAppointment(DateTime? start, DateTime? end, bool allDay) { if(start.HasValue && end.HasValue) { return start.Value.Date == end.Value.Date ? $"{start.Value.ToShortTimeString()} - {end.Value.ToShortTimeString()} " : $"{start.Value:dd.MM.yyyy HH:mm} - {end.Value:dd.MM.yyyy HH:mm} "; } if(allDay && start.HasValue && end.HasValue) { return start.Value.Date == end.Value.Date ? " " : $"{start.Value.ToShortDateString()} - {end.Value.ToShortDateString()} "; } return string.Empty; } } }