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> ViewModelChanged; private ISchedulerViewModel mActiveAppointmentViewModel; private Dictionary> mAllCats2ResInDic; public ISchedulerViewModel ActiveAppointmentViewModel { get { return mActiveAppointmentViewModel; } set { if(value != null) { mActiveAppointmentViewModel = value; if (ViewModelChanged != null) { ViewModelChanged.Invoke(this, new EventArgs(value)); } } } } private List allEmployees = new List(); public void UpdateAppointmentViewModel(DateTime pIntervalStart, DateTime pIntervalEnd) { if(!BeWoApp.LoggedOnEmployee.EmployeeOid.HasValue) { return; } var appointments = new List(); var allCust = new List(); var abwesenheiten = new List(); allEmployees = Cache.GetInstance().GetAllActiveEmployeesCompactSync(); if(!BeWoApp.LoggedOnUser.HasRight(UserRightType.KalenderMitarbeitertermineAnsehen)) { ServiceFacade.DoEmployeeServiceSync(es => allEmployees = new List { es.LoadCompactEmployee(BeWoApp.LoggedOnEmployee.EmployeeOid.Value) }); } ServiceFacade.DoResourceServiceSync(s => mAllCats2ResInDic = s.GetAllCategories2ResourcesInDictionary()); var start = pIntervalStart - NewSchedulerView.FetchPadding; var end = pIntervalEnd + NewSchedulerView.FetchPadding; ServiceFacade.DoResourceServiceSync(rs => appointments = rs.GetAllActiveAppointmentsForEmployeeInInterval2(start, end, BeWoApp.LoggedOnEmployee.EmployeeOid.Value, BeWoApp.LoggedOnUser.HasRight(UserRightType.KalenderMitarbeitertermineAnsehen))); ServiceFacade.DoResourceServiceSync(rs => abwesenheiten = rs.GetAllActiveAbsenceTimesInInterval(start, end, BeWoApp.LoggedOnEmployee.EmployeeOid.Value, BeWoApp.LoggedOnUser.HasRight(UserRightType.KalenderMitarbeitertermineAnsehen))); allCust = Cache.GetInstance().GetAllActiveCustomersCompactSync(); if(BeWoApp.LoggedOnUser.HasRight(UserRightType.Customer_ViewMyCustomers) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.CustomerView_View)) { allCust = allCust.Where(c => BeWoApp.LoggedOnEmployee.RelatedCustomers.Any(a => a.Customer.Equals(c))).ToList(); } 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))); //###cb testen :w.EmployeeList.IsEmpty geht nicht mehr var vm = new SchedulerAppointmentListVM(prefilteredAppointments.ToList(), allCust, allEmployees, mAllCats2ResInDic); vm.VMList.AddRange(ConvertAbsenceTimesToAppointments(abwesenheiten, end)); ActiveAppointmentViewModel = vm; SchedulerSettings = GetActiveSettings(); SchedulerSettings.StartDate = pIntervalStart; } public AbstractAppointmentEditForm GetEditAppointmentForm(DevExpress.Xpf.Scheduler.SchedulerControl control, Appointment appointment) { return ActiveAppointmentViewModel != null ? ActiveAppointmentViewModel.GetEditAppointmentForm(control, appointment) : null; } internal void InitNewAppointment(Appointment appointment) { if(ActiveAppointmentViewModel != null) { ActiveAppointmentViewModel.InitNewAppointment(appointment); } } public SchedulerSettings SchedulerSettings { get; set; } public SchedulerSettings GetActiveSettings() { return ActiveAppointmentViewModel == null ? null : SchedulerSettings ?? ActiveAppointmentViewModel.GetDefaultSchedulerSettings(); } private IEnumerable ConvertAbsenceTimesToAppointments(IEnumerable pAbsenceTimes, DateTime pIntervalEnd) { var result = new List(); foreach(var abwesenheit in pAbsenceTimes) { result.Add(new SchedulerAppointmentVM(true, abwesenheit.Reason.Description, abwesenheit.Start.Value, abwesenheit.End ?? pIntervalEnd, allEmployees.FirstOrDefault(f => f.EmployeeOid == abwesenheit.EmployeeOid.Value))); } return result; } } }