Files
BeWoPlaner/BeWoPlanerMobil/Controllers/SchedulerController.cs
2019-07-29 19:40:14 +02:00

275 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using BeWoPlanerMobil.Models;
using BeWoPlanerMobil.Service;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Compatibility;
using static BeWoPlanerMobil.Util.MobileUtils;
namespace BeWoPlanerMobil.Controllers
{
public class SchedulerController : AbstractBaseController
{
private const string SchedulerModelSessionKey = "SchedulerModel";
private SchedulerModel _Model;
public SchedulerModel Model
{
get
{
if (!MobileSessionFacade.IsUserLoggedIn())
{
RedirectToActionPermanent("Index", "Login");
return null;
}
if (((SchedulerModel) Session[SchedulerModelSessionKey])?.Employee == null)
{
_Model = new SchedulerModel { Employee = MobileSessionFacade.LoggedInEmployee };
Session[SchedulerModelSessionKey] = _Model;
}
else
{
_Model = (SchedulerModel) Session[SchedulerModelSessionKey];
}
return _Model;
}
}
[Authorize]
public ActionResult Scheduler()
{
if (!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer") || !AbstractModel.IsAllowedToSeeScheduler)
{
return RedirectToActionPermanent("Index", "Login");
}
return View(Model);
}
[Authorize]
public string LoadAppointments(string newtoday)
{
try
{
if (Model?.Employee.EmployeeOid == null)
{
RedirectToActionPermanent("Index", "Login");
return _LeerzeichenFuerGetMethoden;
}
var date = DateTime.ParseExact(newtoday, "dd.MM.yyyy", null);
var appointments = KalenderService.LoadFilteredAppointments(AbstractModel.HasRightToSeeAllEmployeeAppointments, Model.Employee.EmployeeOid.Value, date.Date, date.Date.AddDays(1), new List<long>(), new List<long>(), new List<long>(), false, false, false, false, true).ToList();
// Nur eigene Kliententermine ansehen
if (!AbstractModel.HasRightToSeeAllCustomerAppointments)
{
appointments = appointments.Where(a => a.CustomerList.All(c => MobileSessionFacade.LoggedInEmployee.RelatedCustomers.Any(rc => rc.Customer.Equals(c)))).ToList();
}
if(!AbstractModel.HasRightToSeeAllResourceAppointments)
{
appointments = appointments.Where(a => a.ResourceList.Count == 0).ToList();
}
if(!AbstractModel.IsAllowedToSeeCustomers)
{
appointments = appointments.Where(a => a.CustomerList.Count == 0).ToList();
}
var holySweetFlyingFuck = appointments.Where(w =>
w.StartDate.HasValue && w.EndDate.HasValue &&
((date.InBetween(w.StartDate.Value, w.EndDate.Value, false) ||
date.CompareShortDates(w.StartDate.Value)) && w.RecurrenceInfo == null ||
w.RecurrenceInfo != null && (w.Type == 3 || w.Type == 4) &&
(date.InBetween(w.StartDate.Value, w.EndDate.Value, false) ||
date.CompareShortDates(w.StartDate.Value)))).ToList();
var deletedOccurrences = holySweetFlyingFuck.Where(w => w.Type == 4).Select(s => GetOccurrenceId(s.RecurrenceInfo)).ToList();
var changedOccurrences = holySweetFlyingFuck.Where(w => w.Type == 3).Select(s => GetOccurrenceId(s.RecurrenceInfo)).ToList();
holySweetFlyingFuck = holySweetFlyingFuck.Where(w => w.Type != 4).ToList();
foreach (var appointment in appointments)
{
if (appointment.RecurrenceInfo != null && appointment.Type == 1)
{
var recurrenceInfo = new RecurrenceInfo();
recurrenceInfo.FromXml(appointment.RecurrenceInfo);
var occurrenceCalculator = OccurrenceCalculator.CreateInstance(recurrenceInfo);
var pattern = StaticAppointmentFactory.CreateAppointment(AppointmentType.Pattern);
pattern.RecurrenceInfo.FromXml(appointment.RecurrenceInfo);
pattern.Start = pattern.RecurrenceInfo.Start;
pattern.End = pattern.RecurrenceInfo.End;
var patternId = pattern.RecurrenceInfo.Id.ToString();
var interval = new TimeInterval(date, date.AddDays(1));
var occurrences = occurrenceCalculator.CalcOccurrences(interval, pattern);
if (occurrences.Count > 0)
{
foreach (var termin in occurrences.GetAppointments(interval))
{
var index = termin.RecurrenceIndex;
var duration = (appointment.EndDate.Value - appointment.StartDate.Value).TotalMinutes;
if (!date.InBetween(termin.Start.GetShortDateTime(), termin.Start.AddMinutes(duration),
true) || changedOccurrences.Any(a =>
a.PatternId.Equals(patternId) && a.Index == index) ||
deletedOccurrences.Any(a => a.PatternId.Equals(patternId) && a.Index == index))
{
continue;
}
holySweetFlyingFuck.AddIfNotIn(new SchedulerAppointmentDC
{
AllDay = appointment.AllDay,
CustomerList = appointment.CustomerList,
Description = appointment.Description,
EmployeeList = appointment.EmployeeList,
EndDate = termin.Start.AddMinutes(duration),
FormerBookingSequenceOid = appointment.FormerBookingSequenceOid,
IsPrivate = appointment.IsPrivate,
LabelId = appointment.LabelId,
Location = appointment.Location,
Originator = appointment.Originator,
RecurrenceInfo = termin.RecurrenceInfo.ToXml(),
ReminderInfo = appointment.ReminderInfo,
ResourceList = appointment.ResourceList,
StartDate = termin.Start,
Subject = appointment.Subject ?? "",
Type = appointment.Type
});
}
}
}
}
return SerializeObject(holySweetFlyingFuck.OrderBy(x => x.StartDate.Value.Hour));
}
catch (Exception e)
{
log.Error(e.Message, e);
return SerializeObject("Fehler 510");
}
}
[Authorize]
public string UpdateAppointment(long oid, string betreff, string notice, string startZeit, string endZeit, string day, string endDate, string pLocation)
{
try
{
if (Model == null)
{
RedirectToActionPermanent("Index", "Login");
return _LeerzeichenFuerGetMethoden;
}
var date = DateTime.Parse(day);
var end = DateTime.Parse(endDate);
var zeit = ConvertRecordTimesWithEndDate(startZeit, endZeit, date, end, 0);
var b1 = KalenderService.GetSchedulerAppointmentByid(oid);
b1.EndDate = zeit[1];
b1.StartDate = zeit[0];
b1.Subject = betreff;
b1.Description = notice;
b1.Location = pLocation;
KalenderService.UpdateSchedulerAppointments(new List<SchedulerAppointmentDC> { b1 });
return _LeerzeichenFuerGetMethoden;
}
catch (Exception e)
{
log.Error(e.Message, e);
return SerializeObject("Fehler 550: Internal Server Error");
}
}
[Authorize]
public void InsertAppointment(string pStartDate, string pStartTime, string pEndTime, string pSubject, string pNotice, string pEndDate, string pLocation)
{
try
{
if (Model == null)
{
RedirectToActionPermanent("Index", "Login");
return;
}
var date = DateTime.Parse(pStartDate);
var end = DateTime.Parse(pEndDate);
var zeit = ConvertRecordTimesWithEndDate(pStartTime, pEndTime, date, end, 0);
var newApp = new SchedulerAppointmentDC
{
EmployeeList = new List<Employee2SchedulerAppointmentDC>(),
CustomerList = new List<CompactCustomerDC>(),
ResourceList = new List<ResourceDC>(),
SupportConceptList = new List<CompactSupportConceptDC>(),
EndDate = zeit[1],
StartDate = zeit[0],
Subject = pSubject,
Description = pNotice,
Originator = MobileSessionFacade.LoggedInCompactEmployee,
Location = pLocation
};
var list = new List<SchedulerAppointmentDC> { newApp };
KalenderService.InsertSchedulerAppointments(list);
}
catch (Exception e)
{
log.Error(e.Message, e);
SerializeObject("Fehler 520");
}
}
[Authorize]
public string LoadBeWoTasks(string newtoday)
{
try
{
if (Model == null)
{
RedirectToActionPermanent("Index", "Login");
return _LeerzeichenFuerGetMethoden;
}
var date = DateTime.ParseExact(newtoday, "dd.MM.yyyy", null);
var tasks = AnalysisService.GetTasksForEmployeeByDate(Model.Employee.EmployeeOid.Value, date);
return SerializeObject(tasks);
}
catch (Exception e)
{
log.Error(e.Message, e);
return SerializeObject("Fehler 530");
}
}
}
}