using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web.Mvc; using BeWoPlanerMobil.Service; using BeWoPlanerMobil.Util; using BS.Shared; using BS.Shared.Core; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using BS.Shared.Extensions; namespace BeWoPlanerMobil.Models { public class SchedulerModel : AbstractModel { public List TeamMemberCustomerOids { get; set; } public DateTime SelectedDate { get; set; } = DateTime.Today; public SchedulerAppointmentDC SelectedAppointment { get; set; } private List _Appointments; public List Appointments { get => _Appointments ?? (_Appointments = new List()); set { var oldAppointments = AppointmentListItems.Select(li => li.SchedulerAppointment).ToList(); _Appointments = value ?? new List(); if(_Appointments.AreListEqual(oldAppointments)) { return; } _AppointmentListItems = new List(); foreach(var appointment in _Appointments) { var listItem = new AppointmentListItem(appointment); listItem.GenerateIdentifier(_AppointmentListItems.Select(a => a.Identifier).ToList()); _AppointmentListItems.Add(listItem); } } } private List _AppointmentListItems; public List AppointmentListItems => _AppointmentListItems ?? (_AppointmentListItems = new List()); public bool HasRightToEditAppointment(Guid identifier) { var appointment = AppointmentListItems.FirstOrDefault(app => app.Identifier.Equals(identifier))?.SchedulerAppointment; //Appointments.FirstOrDefault(app => app.SchedulerAppointmentOid.HasValue && app.SchedulerAppointmentOid.Value == appointmentOid); return appointment != null && Utils.CheckSchedulerRights(appointment.CustomerList, appointment.ResourceList, appointment.EmployeeList, appointment.Originator, appointment.SchedulerAppointmentOid is null, SchedulerRightsCheckType.Edit, MobileSessionFacade.LoggedInUserDC, TeamMemberCustomerOids ?? new List()); } public string DescriptionToEdit => SelectedAppointment?.Description ?? string.Empty; public string SubjectToEdit => SelectedAppointment?.Subject ?? string.Empty; public string StartDateToEdit => SelectedAppointment?.StartDate?.ToShortDateString() ?? SelectedDate.ToShortDateString(); public string EndDateToEdit => SelectedAppointment?.EndDate?.ToShortDateString() ?? SelectedDate.ToShortDateString(); public string StartTimeToEdit => SelectedAppointment?.StartDate?.ToShortTimeString() ?? string.Empty; public string EndTimeToEdit => SelectedAppointment?.EndDate?.ToShortTimeString() ?? string.Empty; public string LocationToEdit => SelectedAppointment?.Location ?? string.Empty; public List AllResources { get { var allResources = new List(); foreach(var cat2res in ResourceCategories2Resources) { allResources.AddRangeIfElementsNotIn(cat2res.Value); } return allResources; } } public List SelectedResources { get; set; } = new List(); public Dictionary> ResourceCategories2Resources { get; set; } = new Dictionary>(); [Display(Name = "Kalenderansicht")] public string SelectedSchedulerView { get; set; } = "1"; public List SchedulerViewList { get { var result = new List { new SelectListItem { Text = "1", Value = "1" }, new SelectListItem { Text = "5", Value = "5" }, new SelectListItem { Text = "7", Value = "7" } }; return result; } } public WeekViewObject WeekViewObject { get; set; } public List AllEmployees { get; set; } = new List(); public List SelectedEmployees { get; set; } = new List(); public List AllCustomers { get; set; } = new List(); public List SelectedCustomers { get; set; } = new List(); public bool IsInEditMode => SelectedAppointment != null; // ToDo: Überarbeiten? Es kam das Ansehen hinzu public bool HasResourcesEmployeesOrCustomers { get { var isOwnAppointment = !IsInEditMode || SelectedAppointment.Originator.EmployeeOid == Employee.EmployeeOid; var v1 = HasRightToInsertEmployeeAppointments || IsInEditMode && HasRightToEditEmployeeAppointments; var v2 = HasRightToInsertRessourceAppointments || IsInEditMode && HasRightToEditResourceAppointments; if (IsInEditMode && !isOwnAppointment) { v2 = HasRightToEditOthersResourceAppointments; } var v3 = HasRightToInsertCustomerAppointments || IsInEditMode && HasRightToEditCustomerAppointments; return v1 || v2 || v3; } } public bool ShowEmployeeButton => IsInEditMode ? HasRightToInsertEmployeeAppointments : HasRightToEditEmployeeAppointments; public bool ShowCustomerButton => IsInEditMode ? HasRightToInsertCustomerAppointments : HasRightToEditCustomerAppointments; //IntervalFinder public List SelectedEmployeesForIntervalFinder { get; set; } = new List(); public List SelectedCustomersForIntervalFinder { get; set; } = new List(); public List SelectedResourcesForIntervalFinder { get; set; } = new List(); public Dictionary> FreeIntervals { get; set; } = new Dictionary>(); public bool IsInIntervalFinderMode { get; set; } public DateTime? IntervalStartDate { get; set; } public DateTime? IntervalEndDate { get; set; } public string IntervalStartDateStr => IntervalStartDate.HasValue ? $"{IntervalStartDate:dd.MM.yyyy}" : string.Empty; public string IntervalEndDateStr => IntervalEndDate.HasValue ? $"{IntervalEndDate:dd.MM.yyyy}" : string.Empty; public string IntervalStartTimeStr => IntervalStartDate.HasValue ? $"{IntervalStartDate:HH:mm}" : string.Empty; public string IntervalEndTimeStr => IntervalEndDate.HasValue ? $"{IntervalEndDate:HH:mm}" : string.Empty; public int? IntervalDuration { get; set; } public string IntervalDurationStr => IntervalDuration.HasValue ? $"{IntervalDuration}" : string.Empty; [Display(Name = "Ganztägig")] public bool IsAllDay { get; set; } [Display(Name = "Privat")] public bool IsPrivate { get; set; } public List MyTeamEmployees { get; set; } public List SelectedResourcesForFiltering { get; set; } = new List(); public List SelectedEmployeesForFiltering { get; set; } = new List(); public List SelectedCustomersForFiltering { get; set; } = new List(); public List MyTeams { get; set; } = new List(); public bool ShouldLoadTasks { get; set; } } public class WeekViewObject { public KeyValuePair> MondayAppointments { get; set; } public KeyValuePair> TuesdayAppointments { get; set; } public KeyValuePair> WednesdayAppointments { get; set; } public KeyValuePair> ThursdayAppointments { get; set; } public KeyValuePair> FridayAppointments { get; set; } public KeyValuePair> SaturdayAppointments { get; set; } public KeyValuePair> SundayAppointments { get; set; } public WeekViewObject(IEnumerable appointments, DateTime monday, EmployeeDC loggedInEmployee) { var first = monday.Date; var last = first.AddDays(6); var tuesday = first.AddDays(1); var wednesday = first.AddDays(2); var thursday = first.AddDays(3); var friday = first.AddDays(4); var saturday = first.AddDays(5); var blubbtest = appointments.Where(a => a.StartDate >= first || a.StartDate <= last).ToList(); var mondays = blubbtest.Where(a => a.StartDate.HasValue && a.StartDate.Value.DayOfWeek == DayOfWeek.Monday); var tuesdays = blubbtest.Where(a => a.StartDate.HasValue && a.StartDate.Value.DayOfWeek == DayOfWeek.Tuesday); var wednesdays = blubbtest.Where(a => a.StartDate.HasValue && a.StartDate.Value.DayOfWeek == DayOfWeek.Wednesday); var thursdays = blubbtest.Where(a => a.StartDate.HasValue && a.StartDate.Value.DayOfWeek == DayOfWeek.Thursday); var fridays = blubbtest.Where(a => a.StartDate.HasValue && a.StartDate.Value.DayOfWeek == DayOfWeek.Friday); var saturdays = blubbtest.Where(a => a.StartDate.HasValue && a.StartDate.Value.DayOfWeek == DayOfWeek.Saturday); var sundays = blubbtest.Where(a => a.StartDate.HasValue && a.StartDate.Value.DayOfWeek == DayOfWeek.Sunday); MondayAppointments = new KeyValuePair>(first, new List()); TuesdayAppointments = new KeyValuePair>(tuesday, new List()); WednesdayAppointments = new KeyValuePair>(wednesday, new List()); ThursdayAppointments = new KeyValuePair>(thursday, new List()); FridayAppointments = new KeyValuePair>(friday, new List()); SaturdayAppointments = new KeyValuePair>(saturday, new List()); SundayAppointments = new KeyValuePair>(last, new List()); mondays.DoForEach(app => MondayAppointments.Value.Add(new AppointmentListItem(app))); tuesdays.DoForEach(app => TuesdayAppointments.Value.Add(new AppointmentListItem(app))); wednesdays.DoForEach(app => WednesdayAppointments.Value.Add(new AppointmentListItem(app))); thursdays.DoForEach(app => ThursdayAppointments.Value.Add(new AppointmentListItem(app))); fridays.DoForEach(app => FridayAppointments.Value.Add(new AppointmentListItem(app))); saturdays.DoForEach(app => SaturdayAppointments.Value.Add(new AppointmentListItem(app))); sundays.DoForEach(app => SundayAppointments.Value.Add(new AppointmentListItem(app))); } } }