Files
BeWoPlaner/BeWoPlanerMobil/Controllers/DevExpressSchedulerController.cs

362 lines
13 KiB
C#

using BeWoPlanerMobil.Models;
using BeWoPlanerMobil.Service;
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;
using BeWoPlanerMobil.Util.Constants;
using BeWoPlanerMobil.Util.SchedulerUtils;
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<long>(),
new List<long>(),
new List<long>(),
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<object>();
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<SchedulerAppointmentDC>(
"scheduler",
Model.Appointments,
SchedulerHelper.DefaultAppointmentStorage,
SchedulerHelper.DefaultResourceStorage);
var appointmentsToUpdate = SchedulerExtension.GetAppointmentsToUpdate<SchedulerAppointmentDC>(
"scheduler",
Model.Appointments,
SchedulerHelper.DefaultAppointmentStorage,
SchedulerHelper.DefaultResourceStorage
);
var appointmentsToRemove = SchedulerExtension.GetAppointmentsToRemove<SchedulerAppointmentDC>(
"scheduler",
Model.Appointments,
SchedulerHelper.DefaultAppointmentStorage,
SchedulerHelper.DefaultResourceStorage
);
foreach(var appointment in appointmentsToInsert)
{
appointment.CustomerList = AppointmentModel.SelectedCustomers;
appointment.ResourceList = AppointmentModel.SelectedResources;
appointment.EmployeeList = new List<Employee2SchedulerAppointmentDC>();
foreach(var selectedEmployee in AppointmentModel.SelectedEmployees ?? new List<CompactEmployeeDC>())
{
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<Employee2SchedulerAppointmentDC>();
foreach(var selectedEmployee in AppointmentModel.SelectedEmployees)
{
appointment.EmployeeList.Add(new Employee2SchedulerAppointmentDC { Employee = selectedEmployee });
}
}
appointment.ActivationType = ActivationTypeId.Active;
KalenderService.UpdateSchedulerAppointments(new List<SchedulerAppointmentDC> { 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<long?>("CustomerGridLookup") ?? Array.Empty<long?>();
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<long?>("ResourceGridLookup") ?? Array.Empty<long?>();
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<long?>("EmployeeGridLookup") ?? Array.Empty<long?>();
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<SchedulerViewType>(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")
{
// Serientermin
if(apptID.Contains("_"))
{
var splitApptId = apptID.Split('_');
var selectedAppointmentOid = long.Parse(splitApptId[0]);
var recurrenceIndex = int.Parse(splitApptId[1]);
// Hier dann Ausnahme bilden mit ServiceRecord?
var appointment = GetAppointmentFromModelByOid(selectedAppointmentOid);
var test = appointment is null;
}
else
{
if(long.TryParse(apptID, out var selectedAppointmentOid))
{
var appointment = GetAppointmentFromModelByOid(selectedAppointmentOid);
var test = appointment is null;
}
}
return RedirectToActionPermanent("Main", "Main");
}
if(actionId == "ShowAvailableEmployees")
{
var startString = args[0];
var endString = args[1];
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);
}
return PartialView("SchedulerPagePartial", Model);
}
private SchedulerAppointmentDC GetAppointmentFromModelByOid(long oid)
{
foreach(SchedulerAppointmentDC appointment in Model.Appointments)
{
if(appointment.SchedulerAppointmentOid == oid)
{
return appointment;
}
}
return null;
}
}
}