Files
BeWoPlaner/BeWo/Scheduler/ViewModel/NewSchedulerViewModel.cs
2017-12-12 17:20:23 -04:00

114 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using BeWo.Core.Service;
using BS.Shared.Core;
using BeWo.Scheduler.View;
using BeWo.ServiceProxy;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using DevExpress.DataProcessing;
using DevExpress.XtraScheduler;
namespace BeWo.Scheduler.ViewModel
{
public class NewSchedulerViewModel
{
public event EventHandler<EventArgs<ISchedulerViewModel>> ViewModelChanged;
private ISchedulerViewModel _activeAppointmentViewModel;
private Dictionary<ValueListEntryDC, List<ResourceDC>> _allCats2ResInDic;
public ISchedulerViewModel ActiveAppointmentViewModel
{
get => _activeAppointmentViewModel;
set
{
if(value != null)
{
_activeAppointmentViewModel = value;
ViewModelChanged?.Invoke(this, new EventArgs<ISchedulerViewModel>(value));
}
}
}
private List<CompactEmployeeDC> _allEmployees = new List<CompactEmployeeDC>();
public void UpdateAppointmentViewModel(DateTime pIntervalStart, DateTime pIntervalEnd, List<long> pSelectedEmployees, List<long> pSelectedCustomer, List<long> pSelectedResources, bool pEmployeesOnly, bool pCustomersOnly, bool pResourcesOnly, bool pPrivateAppointmentsOnly, bool pOnlyMyAppointments, bool pShouldShowAbsenceTimes, Action callBack)
{
if (!BeWoApp.LoggedOnEmployee.EmployeeOid.HasValue)
{
return;
}
var start = pIntervalStart - NewSchedulerView.FetchPadding;
var end = pIntervalEnd + NewSchedulerView.FetchPadding;
Cache.GetInstance().GetAllActiveEmployeesCompact(allEmployees =>
{
ServiceFacade.DoResourceServiceAsync(s => s.GetAllCategories2ResourcesInDictionary(), cats2Res =>
{
ServiceFacade.DoResourceServiceAsync(s2 => s2.LoadFilteredAppointments(BeWoApp.HasLoggedOnUserRight(new[] { UserRightType.KalenderMitarbeitertermineAnsehen }), BeWoApp.LoggedOnEmployee.EmployeeOid.Value, start, end, pSelectedEmployees, pSelectedCustomer, pSelectedResources, pEmployeesOnly, pCustomersOnly, pResourcesOnly, pPrivateAppointmentsOnly, pOnlyMyAppointments),
appointments =>
{
Cache.GetInstance().GetAllActiveCustomersCompact(customers =>
{
if(BeWoApp.LoggedOnUser.HasRight(UserRightType.Customer_ViewMyCustomers) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.CustomerView_View))
{
customers = customers.Where(c => BeWoApp.LoggedOnEmployee.RelatedCustomers.Any(a => a.Customer.Equals(c))).ToList();
}
ServiceFacade.DoResourceServiceAsync(s3 => s3.GetAllActiveAbsenceTimesInInterval(start, end, BeWoApp.LoggedOnEmployee.EmployeeOid.Value, BeWoApp.LoggedOnUser.HasRight(UserRightType.KalenderMitarbeitertermineAnsehen)),
absenceTimes =>
{
var prefilteredAppointments = appointments.Where(w => (w.EmployeeList.Count == 0 && w.Originator.EmployeeOid.Equals(BeWoApp.LoggedOnEmployee.EmployeeOid) ||
w.EmployeeList.Count > 0 && w.EmployeeList.Any(a => a.Employee.EmployeeOid.Equals(BeWoApp.LoggedOnEmployee.EmployeeOid)) ||
BeWoApp.LoggedOnUser.HasRight(UserRightType.KalenderMitarbeitertermineAnsehen)) && (!w.EmployeeList.TrueForAll(e2a => e2a.ParticipationAnswer == ParticipationAnswer.Absage) || (w.EmployeeList == null || w.EmployeeList.Count == 0))).ToList();
var vm = new SchedulerAppointmentListVM(prefilteredAppointments, customers, allEmployees, cats2Res);
if(pShouldShowAbsenceTimes)
{
vm.Appointments.AddRange(ConvertAbsenceTimesToAppointments(absenceTimes, end));
}
// TODO: add absence times!
ActiveAppointmentViewModel = vm;
SchedulerSettings = GetActiveSettings();
SchedulerSettings.StartDate = pIntervalEnd;
callBack.Invoke();
});
});
});
});
});
}
public AbstractAppointmentEditForm GetEditAppointmentForm(DevExpress.Xpf.Scheduler.SchedulerControl control, Appointment appointment)
{
return ActiveAppointmentViewModel?.GetEditAppointmentForm(control, appointment);
}
internal void InitNewAppointment(Appointment appointment)
{
ActiveAppointmentViewModel?.InitNewAppointment(appointment);
}
public SchedulerSettings SchedulerSettings { get; set; }
public SchedulerSettings GetActiveSettings()
{
return ActiveAppointmentViewModel == null ? null : SchedulerSettings ?? ActiveAppointmentViewModel.GetDefaultSchedulerSettings();
}
private IEnumerable<SchedulerAppointmentVM> ConvertAbsenceTimesToAppointments(IEnumerable<AbsenceTimeDC> pAbsenceTimes, DateTime pIntervalEnd)
{
return pAbsenceTimes.Select(abwesenheit => new SchedulerAppointmentVM(true, abwesenheit.Reason.Description, abwesenheit.Start.Value, abwesenheit.End ?? pIntervalEnd, _allEmployees.FirstOrDefault(f => f.EmployeeOid == abwesenheit.EmployeeOid.Value))).ToList();
}
}
}