using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using BeWo.Scheduler.Utils; using BeWo.ViewModel; using BS.Shared; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using BS.Shared.Extensions; using DevExpress.XtraScheduler; namespace BeWo.Scheduler.ViewModel { public class SchedulerAppointmentVM : AbstractDCMapperVM, IBeWoAppointment { public static string PropertyName_Type = "Type"; public static string PropertyName_Start = "Start"; public static string PropertyName_End = "End"; public static string PropertyName_AllDay = "AllDay"; public static string PropertyName_Subject = "Subject"; public static string PropertyName_Location = "Location"; public static string PropertyName_Status = "Status"; public static string PropertyName_RecurrenceInfo = "RecurrenceInfo"; public static string PropertyName_Description = "Description"; public static string PropertyName_EmployeeList = "EmployeeList"; public static string PropertyName_CustomerList = "CustomerList"; public static string PropertyName_ResourceList = "ResourceList"; public static string PropertyName_IsPrivate = "IsPrivate"; public static string PropertyName_Originator = "Originator"; public static string PropertyName_IsAbsenceTime = "IsAbsenceTime"; public static string PropertyName_IsTask = nameof(IsTask); public static string PropertyName_DueDate = nameof(DueDate); public static string PropertyName_CompletedDate = nameof(CompletedDate); public static string PropertyName_CompletedNotice = nameof(CompletedNotice); public static string PropertyName_CompletedUser = nameof(CompletedUser); public static string PropertyName_TaskDescription = nameof(TaskDescription); private bool _AllDay; private string _Description; private DateTime _End; private string _Location; private string _RecurrenceInfo; private DateTime _Start; private int _Status; private string _Subject; private int _Type; private Dictionary _CustomFields; private bool _IsPrivate; private ObservableCollection _EmployeeList; private List _CustomerList; private List _ResourceList; private List _SupportConceptList; private CompactEmployeeDC _Originator; private bool _IsTask; private string _TaskDescription; private string _CompletedNotice; private string _CompletedUser; private DateTime? _CompletedDate; private DateTime? _DueDate; private DateTime? _DueTime; private bool _HasServiceRecordEntry; private bool _IsArchived; private long? _AbsenceTimeOid; private bool _IsCustomerAbsenceTime; private List _ServiceRecordList; public bool HasServiceRecordEntry { get => _HasServiceRecordEntry; set { if(AreDifferent(_HasServiceRecordEntry, value)) { _HasServiceRecordEntry = value; StoreDirtyInformation(AreDifferent(DataContract.HasServiceRecordEntry, value), nameof(HasServiceRecordEntry)); FirePropertyChanged(nameof(HasServiceRecordEntry)); } } } public bool IsArchived { get => _IsArchived; set { if(AreDifferent(_IsArchived, value)) { _IsArchived = value; StoreDirtyInformation((value && DataContract.ActivationType == ActivationTypeId.Active) || (!value && DataContract.ActivationType == ActivationTypeId.Archived), nameof(IsArchived)); FirePropertyChanged(nameof(IsArchived)); } } } public DateTime? AbsenceTimeStart { get; set; } public DateTime? AbsenceTimeEnd { get; set; } public long? AbsenceTimeOid { get; set; } public bool IsCustomerAbsenceTime { get; set; } public object Id { get; set; } public Dictionary CustomFields { get => _CustomFields ?? (_CustomFields = new Dictionary()); set { if (!AreDifferent(_CustomFields, value)) { return; } _CustomFields = value; var customFieldStorage = (CustomFieldStorage) value[nameof(CustomFieldStorage)]; EmployeeList = customFieldStorage.EmployeeList; CustomerList = customFieldStorage.CustomerList; ResourceList = customFieldStorage.ResourceList; Originator = customFieldStorage.Originator; IsPrivate = customFieldStorage.IsPrivate; IsTask = customFieldStorage.IsTask; DueDate = customFieldStorage.DueDate; TaskDescription = customFieldStorage.TaskDescription; CompletedDate = customFieldStorage.CompletedDate; CompletedNotice = customFieldStorage.CompletedNotice; CompletedUser = customFieldStorage.CompletedUser; SupportConceptList = customFieldStorage.SupportConceptList; AbsenceTimeOid = customFieldStorage.AbsenceTimeOid; IsCustomerAbsenceTime = customFieldStorage.IsCustomerAbsenceTime; } } public int EventType { get => _Type; set { if (AreDifferent(_Type, value)) { _Type = value; StoreDirtyInformation(AreDifferent(DataContract.Type, value), PropertyName_Type); FirePropertyChanged(PropertyName_Type); } } } public DateTime End { get => _End; set { if (AreDifferent(_End, value)) { _End = value; if (IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.EndDate, value), PropertyName_End); FirePropertyChanged(PropertyName_End); } } } public DateTime Start { get => _Start; set { if (AreDifferent(_Start, value)) { _Start = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.StartDate, value), PropertyName_Start); FirePropertyChanged(PropertyName_Start); } } } public bool AllDay { get => _AllDay; set { if (AreDifferent(_AllDay, value)) { _AllDay = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.AllDay, value), PropertyName_AllDay); FirePropertyChanged(PropertyName_AllDay); } } } public string Subject { get => _Subject; set { if(AreDifferent(_Subject, value)) { _Subject = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.Subject, value), PropertyName_Subject); FirePropertyChanged(PropertyName_Subject); } } } public string Location { get => _Location; set { if (AreDifferent(_Location, value)) { _Location = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.Location, value), PropertyName_Location); FirePropertyChanged(PropertyName_Location); } } } public int Status { get => _Status; set { if (AreDifferent(_Status, value)) { _Status = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.Status, value), PropertyName_Status); FirePropertyChanged(PropertyName_Status); } } } public bool IsTeilnahmeBestaetigung { get; } public string RecurrenceInfo { get => _RecurrenceInfo; set { if (AreDifferent(_RecurrenceInfo, value)) { _RecurrenceInfo = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.RecurrenceInfo, value), PropertyName_RecurrenceInfo); FirePropertyChanged(PropertyName_RecurrenceInfo); } } } public string Description { get => _Description; set { if (AreDifferent(_Description, value)) { _Description = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.Description, value), PropertyName_Description); FirePropertyChanged(PropertyName_Description); } } } public ObservableCollection EmployeeList { get => _EmployeeList ?? (_EmployeeList = new ObservableCollection()); set { if (AreDifferent(_EmployeeList, value)) { _EmployeeList = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.EmployeeList, value), PropertyName_EmployeeList); FirePropertyChanged(PropertyName_EmployeeList); } } } public List CustomerList { get => _CustomerList ?? (_CustomerList = new List()); set { if (AreDifferent(_CustomerList, value)) { _CustomerList = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.CustomerList, value), PropertyName_CustomerList); FirePropertyChanged(PropertyName_CustomerList); } } } public List ResourceList { get => _ResourceList ?? (ResourceList = new List()); set { if (AreDifferent(_ResourceList, value)) { _ResourceList = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.ResourceList, value), PropertyName_ResourceList); FirePropertyChanged(PropertyName_ResourceList); } } } public List SupportConceptList { get => _SupportConceptList ?? (SupportConceptList = new List()); set { if(AreDifferent(_SupportConceptList, value)) { _SupportConceptList = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.SupportConceptList, value), nameof(SupportConceptList)); FirePropertyChanged(nameof(SupportConceptList)); } } } public bool IsPrivate { get => _IsPrivate; set { if (AreDifferent(_IsPrivate, value)) { _IsPrivate = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.IsPrivate,value), PropertyName_IsPrivate); FirePropertyChanged(PropertyName_IsPrivate); } } } public CompactEmployeeDC Originator { get => _Originator; set { if (AreDifferent(_Originator, value)) { _Originator = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.Originator, value), PropertyName_Originator); FirePropertyChanged(PropertyName_Originator); } } } public virtual TimeSpan EndTime => End.TimeOfDay; public virtual TimeSpan StartTime => Start.TimeOfDay; public virtual bool IsAbsenceTime { get; } public bool IsTask { get => _IsTask; set { if (AreDifferent(_IsTask, value)) { _IsTask = value; if (IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.IsTask, value), nameof(IsTask)); FirePropertyChanged(nameof(IsTask)); } } } public string TaskDescription { get => _TaskDescription; set { if(AreDifferent(_TaskDescription, value)) { _TaskDescription = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.TaskDescription, value), nameof(TaskDescription)); FirePropertyChanged(nameof(TaskDescription)); } } } public string CompletedNotice { get => _CompletedNotice; set { if(AreDifferent(_CompletedNotice, value)) { _CompletedNotice = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.CompletedNotice, value), nameof(CompletedNotice)); FirePropertyChanged(nameof(CompletedNotice)); } } } public string CompletedUser { get => _CompletedUser; set { if (AreDifferent(_CompletedUser, value)) { _CompletedUser = value; if (IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.CompletedUser, value), nameof(CompletedUser)); FirePropertyChanged(nameof(CompletedUser)); } } } public DateTime? CompletedDate { get => _CompletedDate; set { if(AreDifferent(_CompletedDate, value)) { _CompletedDate = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.CompletedDate, value), nameof(CompletedDate)); FirePropertyChanged(nameof(CompletedDate)); } } } public DateTime? DueDate { get => _DueDate; set { if(AreDifferent(_DueDate, value)) { _DueDate = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.DueDate, value), nameof(DueDate)); FirePropertyChanged(nameof(DueDate)); } } } public DateTime? DueTime { get => _DueTime; set { if(AreDifferent(_DueTime, value)) { _DueTime = value; StoreDirtyInformation(AreDifferent(DataContract.DueDate, value), nameof(DueTime)); FirePropertyChanged(nameof(DueTime)); } } } public Guid? RecurrenceId => BS.Shared.Core.Utils.GetRecurrenceIdFromRecurrenceInfo(RecurrenceInfo); public int RecurrenceIndex => BS.Shared.Core.Utils.GetRecurrenceIndexFromRecurrenceInfo(RecurrenceInfo); public bool CanBeEdited { get; set; } = true; public SchedulerAppointmentVM(SchedulerAppointmentDC pDC) : base(pDC, pDC.SchedulerAppointmentOid == null) { IsTeilnahmeBestaetigung = pDC.IsTeilnahmeBestaetigung; CanBeEdited = pDC.CanBeEdited; } // Wird für die Druckfunktion von Serienterminen benutzt: public SchedulerAppointmentVM(Appointment appointment) : base(new SchedulerAppointmentDC(), true) { _AllDay = appointment.AllDay; _Start = appointment.Start; _End = appointment.End; _Location = appointment.Location; _Description = appointment.Description; _Subject = appointment.Subject; _Type = (int) appointment.Type; _IsPrivate = appointment.CF_IsPrivate(); _EmployeeList = appointment.CF_EmployeeList(); _CustomerList = appointment.CF_CustomerList(); _ResourceList = appointment.CF_ResourceList(); _Originator = appointment.CF_Originator(); _AbsenceTimeOid = appointment.CF_AbsenceTimeOid(); _IsCustomerAbsenceTime = appointment.CF_IsCustomerAbsenceTime(); if(!(appointment.RecurrenceInfo is null) && appointment.Type == AppointmentType.Occurrence) { _RecurrenceInfo = $""; } } // Wird für Abwesenheiten genutzt public SchedulerAppointmentVM(bool pAllDay, string pSubject, DateTime pStart, DateTime pEnd, CompactEmployeeDC pOriginator, DateTime? absenceTimeStart, DateTime? absenceTimeEnd, long absenceTimeOid, bool isCustomerAbsenceTime) { _AllDay = pAllDay; _Subject = pSubject; _Start = pStart; _End = pEnd; _Originator = pOriginator; AbsenceTimeStart = absenceTimeStart; AbsenceTimeEnd = absenceTimeEnd; IsAbsenceTime = true; AbsenceTimeOid = absenceTimeOid; IsCustomerAbsenceTime = isCustomerAbsenceTime; UpdateCustomFields(); } public void UpdateCustomFields() { CustomFields = new Dictionary { {nameof(CustomFieldStorage), new CustomFieldStorage(this)} }; } protected override void InitByDataContract(SchedulerAppointmentDC pDataContract) { _AllDay = pDataContract.AllDay; _Description = pDataContract.Description; _End = pDataContract.EndDate ?? DateTime.Now; _Location = pDataContract.Location; _RecurrenceInfo = pDataContract.RecurrenceInfo; _Start = pDataContract.StartDate ?? DateTime.Now; _Status = pDataContract.Status; _Subject = pDataContract.Subject; _Type = pDataContract.Type; _IsPrivate = pDataContract.IsPrivate; _Originator = pDataContract.Originator; _CustomerList = pDataContract.CustomerList ?? new List(); _EmployeeList = new ObservableCollection(pDataContract.EmployeeList ?? new List()); _ResourceList = pDataContract.ResourceList ?? new List(); _IsTask = pDataContract.IsTask; _TaskDescription = pDataContract.TaskDescription; _CompletedNotice = pDataContract.CompletedNotice; _CompletedUser = pDataContract.CompletedUser; _CompletedDate = pDataContract.CompletedDate; _SupportConceptList = pDataContract.SupportConceptList ?? new List(); _HasServiceRecordEntry = pDataContract.HasServiceRecordEntry; _ServiceRecordList = pDataContract.ServiceRecordList ?? new List(); _IsArchived = pDataContract.ActivationType == ActivationTypeId.Archived; CanBeEdited = pDataContract.CanBeEdited; if(pDataContract.DueDate.HasValue) { _DueDate = pDataContract.DueDate; _DueTime = pDataContract.DueDate; } } protected override SchedulerAppointmentDC MapToDataContract(SchedulerAppointmentDC pDataContract, bool doCommit) { pDataContract.AllDay = _AllDay; pDataContract.Description = _Description; pDataContract.EndDate = _End; pDataContract.Location = _Location; pDataContract.RecurrenceInfo = _RecurrenceInfo; pDataContract.StartDate = _Start; pDataContract.Status = _Status; pDataContract.Subject = _Subject; pDataContract.Type = _Type; pDataContract.IsPrivate = _IsPrivate; pDataContract.Originator = _Originator; pDataContract.CustomerList = _CustomerList; pDataContract.EmployeeList = _EmployeeList.ToList(); pDataContract.ResourceList = _ResourceList; pDataContract.IsTask = _IsTask; pDataContract.TaskDescription = _TaskDescription; pDataContract.CompletedNotice = _CompletedNotice; pDataContract.CompletedUser = _CompletedUser; pDataContract.CompletedDate = _CompletedDate; pDataContract.SupportConceptList = _SupportConceptList; pDataContract.HasServiceRecordEntry = _HasServiceRecordEntry; pDataContract.ActivationType = _IsArchived ? ActivationTypeId.Archived : ActivationTypeId.Active; pDataContract.ServiceRecordList = _ServiceRecordList; if(_DueDate.HasValue && _DueTime.HasValue) { pDataContract.DueDate = new DateTime(_DueDate.Value.Year, _DueDate.Value.Month, _DueDate.Value.Day, _DueTime.Value.Hour, _DueTime.Value.Minute, 0); } else { pDataContract.DueDate = _DueDate; } pDataContract.CanBeEdited = CanBeEdited; return pDataContract; } public override bool Equals(object pObj) { if (!(pObj is SchedulerAppointmentVM)) { return false; } var obj = (SchedulerAppointmentVM) pObj; var customFieldsAreEqual = false; if(CustomFields.Count == 0 && obj.CustomFields.Count == 0) { customFieldsAreEqual = CustomFields.Equals(obj.CustomFields); } else if(CustomFields.Count == obj.CustomFields.Count) { for (var i = 0; i < CustomFields.Count; i++) { switch (i) { case 0: var customFieldStorage1 = (CustomFieldStorage) CustomFields.ElementAt(i).Value; var objCustomFieldStorage = (CustomFieldStorage) obj.CustomFields.ElementAt(i).Value; customFieldsAreEqual = customFieldStorage1 == null && objCustomFieldStorage == null || customFieldStorage1 != null && Equals(customFieldStorage1, objCustomFieldStorage); break; } } } var areEqual = Equals(Subject, obj.Subject) && LabelKey == obj.LabelKey && AllDay == obj.AllDay && IsPrivate == obj.IsPrivate && IsDirty == obj.IsDirty && IsNew == obj.IsNew && IsTask == obj.IsTask && TaskDescription == obj.TaskDescription && Equals(CompletedDate, obj.CompletedDate) && Equals(DueDate, obj.DueDate) && Equals(CompletedNotice, obj.CompletedNotice) && Equals(CompletedUser, obj.CompletedUser) && customFieldsAreEqual && CustomerList.AreEqual(obj.CustomerList) && Equals(Description, obj.Description) && EmployeeList.AreEqual(obj.EmployeeList) && Equals(End, obj.End) && Equals(EndTime, obj.EndTime) && Equals(Location, obj.Location) && ResourceList.AreEqual(obj.ResourceList) && Equals(RecurrenceInfo, obj.RecurrenceInfo) && HasServiceRecordEntry == obj.HasServiceRecordEntry ; return areEqual; } public override int GetHashCode() { return DataContract.SchedulerAppointmentOid.HasValue ? GetType().Name.GetHashCode() ^ DataContract.SchedulerAppointmentOid.Value.GetHashCode() : base.GetHashCode(); } public override bool IsDirty { get { return (!IsAbsenceTime && base.IsDirty) || EmployeesChanged(); } } private bool EmployeesChanged() { if (_EmployeeList != null && this.DataContract.EmployeeList != null && _EmployeeList.Count != this.DataContract.EmployeeList.Count) { return true; } return false; } public new bool IsNew { get => !IsAbsenceTime && base.IsNew; set => base.IsNew = value; } public long? LabelKey { get; set; } public List ServiceRecordList { get => _ServiceRecordList ?? (ServiceRecordList = new List()); set { if(!AreDifferent(_ServiceRecordList, value)) { return; } _ServiceRecordList = value; if(IsAbsenceTime) { return; } StoreDirtyInformation(AreDifferent(DataContract.ServiceRecordList, value), nameof(ServiceRecordList)); FirePropertyChanged(nameof(ServiceRecordList)); } } } }