Files
BeWoPlaner/Service/Plugins/ResourceService.cs
Lyndon Jetten 2a2cf05f5b Quittierungsbeleg für SKF Leverkusen hinzugefügt
MoK erweiterte Ressourcenverfügbarkeitsprüfung hinzugefügt
MoK Darstellung von ServiceRecords mit Marker ist wie im FaC, Anpassung im Formular folgt
2023-07-24 17:57:14 +02:00

111 lines
5.0 KiB
C#

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<ResourceService>
{
public virtual string ValidateSchedulerAppointment(DateTime start, DateTime end, IEnumerable<long> employees, IEnumerable<long> customers, List<long> 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<ResourceDC> CheckResourceAvailability(DateTime start, DateTime end, List<long> resourceOids, long? selectedAppointmentOid)
{
var list = new List<ResourceDC>();
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<string, List<string>> CheckResourceAvailabilityWithBookingEmployees(DateTime start, DateTime end, List<long> resourceOids, long? selectedAppointmentOid)
{
var appointments = DAOFactory.SearchDAO.FindAppointmentsByResourcesInInterval(resourceOids, start, end, selectedAppointmentOid);
var user = LoggedInUserOperationContextExt.Current?.User ?? SessionFacade.LoggedInUser;
var result = new Dictionary<string, List<string>>();
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<Resource, List<SchedulerAppointment>>();
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;
}
}
}