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 DateTime SelectedDate { get; set; } = DateTime.Today; public SchedulerAppointmentDC SelectedAppointment { get; set; } public List Appointments { get; set; } = new List(); public List AppointmentListItems { get { var result = new List(); Appointments.DoForEach(app => result.AddIfNotIn(new AppointmentListItem(app))); return result; } } public bool HasRightToEditAppointment(long? appointmentOid) { if (appointmentOid == null) { return false; } var appointment = Appointments.FirstOrDefault(app => app.SchedulerAppointmentOid.HasValue && app.SchedulerAppointmentOid.Value == appointmentOid); if (appointment == null) { return false; } var hasCustomers = appointment.CustomerList.Any(); var hasEmployees = appointment.EmployeeList.Any(); var hasResources = appointment.ResourceList.Any(); var result = true; if(hasEmployees) { result = hasResources ? MobileSessionFacade.CheckForUserRight(UserRightType.KalenderRessourcentermineAndererAendern) : MobileSessionFacade.CheckForUserRight(UserRightType.KalenderMitarbeitertermineAendern); if (appointment.CustomerList.Any()) { result = MobileSessionFacade.CheckForUserRight(UserRightType.KalenderKliententermineAendern) && result; } } else { if(hasCustomers && hasResources) { result = MobileSessionFacade.CheckForUserRight(UserRightType.KalenderKliententermineAendern) && MobileSessionFacade.CheckForUserRight(UserRightType.KalenderRessourcentermineAendern); } else if(hasCustomers) { result = MobileSessionFacade.CheckForUserRight(UserRightType.KalenderKliententermineAendern); } else if(hasResources) { result = MobileSessionFacade.CheckForUserRight(UserRightType.KalenderRessourcentermineAendern); } } return result; } 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; set; } = new List(); 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; 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 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) { 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))); } } }