using BeWoPlanerMobil.Models; using BeWoPlanerMobil.Service; using BeWoPlanerMobil.Util; using BeWoPlanerMobil.Util.Constants; using BeWoPlanerMobil.Util.SchedulerUtils; using BeWoPlanerMobil.Views.DevExpressScheduler; using BS.Shared; using BS.Shared.Core; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using BS.Shared.Extensions; using DevExpress.Web.Mvc; using DevExpress.XtraScheduler; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; namespace BeWoPlanerMobil.Controllers { /* * ToDo: * 1. Filterung einbauen (Mitarbeiter, Klienten, Ressourcen, Privat, Aufgaben, Abwesenheiten) * 2. "In die Zeiterfassung übertragen" und "Verfügbare Mitarbeiter prüfen" einbauen * 3. Rechte implementieren! * 4. Ressourcen-Farben einbauen -> Templates wie im DemoCenter benutzen * 5. Übersetzungen */ public class DevExpressSchedulerController : AbstractBaseController { [Authorize] public override string SaveCollapsibleStatus(bool isOpen, string identifier) { return SaveCollapsibleStatusToModel(Model, isOpen, identifier); } private DevExpressSchedulerModel _Model; public DevExpressSchedulerModel Model { get { if(false == MobileSessionFacade.IsUserLoggedIn()) { Logout(); return null; } if(_Model != null) { return _Model; } _Model = new DevExpressSchedulerModel(); InitModel(_Model); InitViewModel(); return _Model; } } private AppointmentModel _AppointmentModel; public AppointmentModel AppointmentModel { get { if(false == MobileSessionFacade.IsUserLoggedIn()) { Logout(); return null; } if(_AppointmentModel != null) { return _AppointmentModel; } _AppointmentModel = new AppointmentModel(); InitModel(_AppointmentModel); InitAppointmentModel(); return _AppointmentModel; } } private void InitAppointmentModel() { AppointmentModel.PossibleEmployees = EmployeeService.GetAllAuthorizedCompactEmployees(); AppointmentModel.PossibleCustomers = CustomerService.GetAllAuthorizedCompactCustomers(); AppointmentModel.PossibleResources = KalenderService.GetAllResources(); } private void InitViewModel() { if(!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer") || Model?.LoggedInEmployee?.EmployeeOid is null) { Logout(); return; } Model.PossibleEmployees = EmployeeService.GetAllAuthorizedCompactEmployees(); Model.PossibleCustomers = CustomerService.GetAllAuthorizedCompactCustomers(); Model.PossibleResources = KalenderService.GetAllResources(); ReloadAppointments(); } private void ReloadAppointments() { if(Model.LoggedInEmployee.EmployeeOid is null) { throw new Exception("Bei DevExpressSchedulerController.ReloadAppointments() ist die Oid des angemeldeten Benutzers ist wider Erwarten NULL!"); } // ToDo: ausgewählte Ansicht laden und speichern! //Model.ShowBetrag = UserSettingsUtils.GetSettingValueAsBool(Model.Mandator.Settings, SettingsKeys.ShowBetrag); var user = LoggedInUser; var userSettings = user?.Settings.FirstOrDefault(f => f.Type.Equals(SettingsType.ApplicationSettings))?.Value; Model.CurrentViewType = (SchedulerViewType) UserSettingsUtils.GetSettingValueAsInteger(userSettings, SettingsKeys.SchedulerViewType); var intervalStart = DateTimeExtensions.GetDayOfWeek(DayOfWeek.Monday); var intervalEnd = intervalStart.AddDays(7); var appointments = KalenderService.LoadFilteredAppointmentsMitAufgaben ( Model.HasRightKalenderMitarbeitertermineAnsehen, Model.LoggedInEmployee.EmployeeOid.Value, intervalStart, intervalEnd, new List(), new List(), new List(), false, false, false, false, false, true ); Model.Appointments = appointments; } [Authorize] public ActionResult DevExpressScheduler() { if(!MobileSessionFacade.IsUserLoggedIn() || Model is null) { return RedirectToActionPermanent("Index", "Login"); } Model.Resources = new List(); return View(Model); } [Authorize] public ActionResult SchedulerPagePartial() { return PartialView("SchedulerPagePartial", Model); } [Authorize] public ActionResult EditAppointment() { UpdateAppointment(); return PartialView("SchedulerPagePartial", Model); } [Authorize] private void UpdateAppointment() { var appointmentsToInsert = SchedulerExtension.GetAppointmentsToInsert( "scheduler", Model.Appointments, SchedulerHelper.DefaultAppointmentStorage, SchedulerHelper.DefaultResourceStorage); var appointmentsToUpdate = SchedulerExtension.GetAppointmentsToUpdate( "scheduler", Model.Appointments, SchedulerHelper.DefaultAppointmentStorage, SchedulerHelper.DefaultResourceStorage ); var appointmentsToRemove = SchedulerExtension.GetAppointmentsToRemove( "scheduler", Model.Appointments, SchedulerHelper.DefaultAppointmentStorage, SchedulerHelper.DefaultResourceStorage ); foreach(var appointment in appointmentsToInsert) { appointment.CustomerList = AppointmentModel.SelectedCustomers; appointment.ResourceList = AppointmentModel.SelectedResources; appointment.EmployeeList = new List(); foreach(var selectedEmployee in AppointmentModel.SelectedEmployees ?? new List()) { appointment.EmployeeList.Add(new Employee2SchedulerAppointmentDC { Employee = selectedEmployee }); } appointment.Originator = Model.LoggedInUser.Employee; KalenderService.InsertSchedulerAppointment(appointment); ViewData["EditableAppointmentModel"] = appointment; } foreach(var appointment in appointmentsToUpdate) { if(AppointmentModel.SelectedCustomers != null) { appointment.CustomerList = AppointmentModel.SelectedCustomers; } if(AppointmentModel.SelectedResources != null) { appointment.ResourceList = AppointmentModel.SelectedResources; } if(AppointmentModel.SelectedEmployees != null) { appointment.EmployeeList = new List(); foreach(var selectedEmployee in AppointmentModel.SelectedEmployees) { appointment.EmployeeList.Add(new Employee2SchedulerAppointmentDC { Employee = selectedEmployee }); } } appointment.ActivationType = ActivationTypeId.Active; KalenderService.UpdateSchedulerAppointments(new List { appointment }); } KalenderService.DeleteSchedulerAppointments( appointmentsToRemove .Where(appointment => appointment.SchedulerAppointmentOid.HasValue && appointment.NewSchedulerAppointmentVersion.HasValue) .ToDictionary(k => k.SchedulerAppointmentOid.Value, v => v.NewSchedulerAppointmentVersion.Value)); ReloadAppointments(); } [HttpPost] public ActionResult CustomerMultiSelectPartial(string value) { var selectedCustomerOids = GridLookupExtension.GetSelectedValues("CustomerGridLookup") ?? Array.Empty(); AppointmentModel.SelectedCustomerOids = selectedCustomerOids.Where(oid => oid.HasValue).Select(oid => oid.Value).ToList(); return PartialView("CustomerMultiSelectPartial", AppointmentModel); } [HttpPost] public ActionResult ResourceGridLookupPartial(string value) { var selectedResourceOids = GridLookupExtension.GetSelectedValues("ResourceGridLookup") ?? Array.Empty(); AppointmentModel.SelectedResourceOids = selectedResourceOids.Where(oid => oid.HasValue).Select(oid => oid.Value).ToList(); return PartialView("ResourceMultiSelectPartial", AppointmentModel); } [HttpPost] public ActionResult EmployeeGridLookupPartial(string value) { /* * Im AppointmentModel müssen die PossibleEmployees bleiben, denn daraus wählt man die Mitarbeiter, die noch nicht mit dem Termin * verknüpft sind und es müssen - zumindest beim Bearbeiten - die bereits mit dem Termin verbundenen Mitarbeiter-zu-Termin-Relationen * zwischengespeichert werden. * */ var selectedEmployeeOids = GridLookupExtension.GetSelectedValues("EmployeeGridLookup") ?? Array.Empty(); AppointmentModel.SelectedEmployeeOids = selectedEmployeeOids.Where(oid => oid.HasValue).Select(oid => oid.Value).ToList(); return PartialView("EmployeeMultiSelectPartial", AppointmentModel); } [Authorize] public string SelectActiveViewType(string viewTypeName) { if(Enum.TryParse(viewTypeName, out var viewType)) { Model.CurrentViewType = viewType; UpdateUserSettingsWithoutReload(SettingsKeys.SchedulerViewType, ((int) viewType).ToString()); } return LeerzeichenFuerGetMethoden; } [Authorize] public ActionResult CustomCallbackAction(string apptID, string actionId, string[] args) { if(actionId == "ToServiceRecord") { long? selectedAppointmentOid; var isRecurring = false; // Serientermin if(apptID.Contains("_")) { var splitApptId = apptID.Split('_'); selectedAppointmentOid = long.Parse(splitApptId[0]); var recurrenceIndex = int.Parse(splitApptId[1]); TempData[TempDataConstants.AppointmentRecurrenceIndexKey] = recurrenceIndex; } else { selectedAppointmentOid = long.Parse(apptID); } var appointment = selectedAppointmentOid.HasValue ? Model.GetAppointmentByOid(selectedAppointmentOid.Value) : null; TempData[TempDataConstants.AppointmentOidKey] = appointment?.SchedulerAppointmentOid; TempData[TempDataConstants.AppointmentStartKey] = appointment?.StartDate; TempData[TempDataConstants.AppointmentEndKey] = appointment?.EndDate; TempData[TempDataConstants.AppointmentSubjectKey] = appointment?.Subject; TempData[TempDataConstants.AppointmentIsAllDayKey] = appointment?.AllDay ?? false; TempData[TempDataConstants.AppointmentCustomerOidsKey] = string.Join(",", appointment?.CustomerList.Select(s => s.CustomerOid) ?? new List()); TempData[TempDataConstants.AppointmentEmployeeOidsKey] = string.Join(",", appointment?.EmployeeList.Select(s => s.Employee.EmployeeOid) ?? new List()); TempData[TempDataConstants.AppointmentIsSerienterminKey] = appointment != null && selectedAppointmentOid is null && appointment.RecurrenceInfo is string; return RedirectToActionPermanent("PrepareServiceRecordInsert", "Main"); } return PartialView("SchedulerPagePartial", Model); } [Authorize] public string GetAppointmentType(long oid) { var appointment = Model.GetAppointmentByOid(oid); var showMenuItem = false; //var isTask = appointment.IsTask; //var isAbsenceTime = appointment. return JsonConvert.SerializeObject(showMenuItem); } [Authorize] public ActionResult CheckEmployeeAvailability(string startString, string endString) { var start = DateTime.Parse(startString); var end = DateTime.Parse(endString); var availabilities = ReportService.GetMitarbeiterverfuegbarkeiten(AppointmentModel.PossibleEmployees, start, end); var employee2Overtime = new EmployeeAvailabilityObject(start, end, availabilities); Model.EmployeeAvailabilityObject = employee2Overtime; ViewData[ViewDataConstants.SchedulerEmployeeAvailabilityConstant] = employee2Overtime; return PartialView("SchedulerPagePartial", Model); } } }