Files
BeWoPlaner/BeWo/ViewModel/WohnheimVM.cs
2026-07-14 14:40:39 +02:00

489 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using BeWo.Core;
using BeWo.Validation;
using BeWo.ViewModel.ListViewModel;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
using System.Windows.Forms;
using BS.Shared.DataContracts.Compact;
namespace BeWo.ViewModel
{
public class WohnheimVM : AbstractDCMapperVM<WohnheimDC>
{
#region Property Names
public static string PropertyName_WohnheimEmployeeRelations = "WohnheimEmployeeRelations";
public static string PropertyName_WohnheimCustomerRelations = "WohnheimCustomerRelations";
public static string PropertyName_WohnheimTeamRelations = "WohnheimTeamRelations";
public static string PropertyName_IsArchived = "IsArchived";
//##################### Erstellte Variablen #######################
public static string PropertyName_WohnheimName = "WohnheimName";
public static string PropertyName_PLZ = "PLZ";
public static string PropertyName_Ort = "Ort";
public static string PropertyName_Bemerkung = "Bemerkung";
public static string PropertyName_Street = "Strasse";
public static string PropertyName_PflichtbesetzungStart = "PflichtbesetzungStart";
public static string PropertyName_PflichtbesetzungEnde = "PflichtbesetzungEnde";
public static string PropertyName_PflichtbesetzungEinblenden = "PflichtbesetzungEinblenden";
public static string PropertyName_StandardZeilenanzahl = "StandardZeilenanzahl";
public static string PropertyName_Sortierungsart = "Sortierungsart";
public static string PropertyName_AbsteigendeSortierung = "AbsteigendeSortierung";
public static string PropertyName_VarFields = "VarFields";
//##################################################################
#endregion
//##################### Erstellte Variablen #######################
private string _WohnheimName;
private string _Strasse;
private string _PLZ;
private string _Ort;
private string _Bemerkung;
private string _PflichtbesetzungStart;
private string _PflichtbesetzungEnde;
private int _PflichtbesetzungEinblenden;
private int _StandardZeilenanzahl;
private int _Sortierungsart;
private int _AbsteigendeSortierung;
private bool _IsArchived;
private VarFieldListVM _VarFields;
//##################################################################
private WohnheimEmployeeRelationListVM _WohnheimEmployeeRelations;
private WohnheimCustomerRelationListVM _WohnheimCustomerRelations;
private WohneinheitListVM _WohneinheitListVM;
private WohneinheitBelegungListVM _WohneinheitBelegungListVM;
private CompactTeamDC _WohnheimTeamRelations;
public WohnheimVM()
{
_WohnheimName = "Design Test";
PermissionWohneinheitView = true;
PermissionWohneinheitBelegungView = true;
var vms = new System.ComponentModel.BindingList<WohneinheitVM>()
{
new WohneinheitVM(),
new WohneinheitVM()
};
WohneinheitListVM = new WohneinheitListVM();
WohneinheitListVM.VMList = vms;
}
public WohnheimVM(WohnheimDC pWohnheimDC, List<CompactCustomerDC> customers)
: base(pWohnheimDC, pWohnheimDC.WohnheimOid == null)
{
Customers = customers;
}
public override bool IsDirty
{
get
{
return base.IsDirty || WohnheimEmployeeRelations.IsDirty
|| WohnheimCustomerRelations.IsDirty
|| this._VarFields.IsDirty
|| WohneinheitListVM.IsDirty
|| WohneinheitBelegungListVM.IsDirty;
}
}
public List<CompactCustomerDC> Customers { get; set; }
public TimeSpan IntervalRange { get; set; }
public bool IntervalChanging { get; set; }
public bool PermissionWohneinheitTabitemView => PermissionWohneinheitView || PermissionWohneinheitBelegungView;
public bool PermissionWohneinheitView { get; } = BeWoApp.HasLoggedOnUserRight(UserRightType.WohnheimView_Wohneinheit_View);
public bool PermissionWohneinheitBelegungView { get; } = BeWoApp.HasLoggedOnUserRight(UserRightType.WohnheimView_Wohneinheit_Belegung_View);
public bool IsArchived
{
get { return _IsArchived; }
set
{
if (AreDifferent(_IsArchived, value))
{
_IsArchived = value;
StoreDirtyInformation(
(value && DataContract.ActivationType == ActivationTypeId.Active) ||
(!value && DataContract.ActivationType == ActivationTypeId.Archived), PropertyName_IsArchived);
FirePropertyChanged(PropertyName_IsArchived);
}
}
}
//Setter und Getter
public String WohnheimName
{
get
{
return _WohnheimName;
}
set
{
_WohnheimName = value;
StoreDirtyInformation(AreDifferent(DataContract.WohnheimName, value), PropertyName_WohnheimName);
FirePropertyChanged(PropertyName_WohnheimName);
}
}
public String Strasse
{
get
{
return _Strasse;
}
set
{
_Strasse = value;
StoreDirtyInformation(AreDifferent(DataContract.Strasse, value), PropertyName_Street);
FirePropertyChanged(PropertyName_Street);
}
}
public String PLZ
{
get
{
return _PLZ;
}
set
{
_PLZ = value;
StoreDirtyInformation(AreDifferent(DataContract.PLZ, value), PropertyName_PLZ);
FirePropertyChanged(PropertyName_PLZ);
}
}
public String Ort
{
get
{
return _Ort;
}
set
{
_Ort = value;
StoreDirtyInformation(AreDifferent(DataContract.Ort, value), PropertyName_Ort);
FirePropertyChanged(PropertyName_Ort);
}
}
public String Bemerkung
{
get
{
return _Bemerkung;
}
set
{
_Bemerkung = value;
StoreDirtyInformation(AreDifferent(DataContract.Bemerkung, value), PropertyName_Bemerkung);
FirePropertyChanged(PropertyName_Bemerkung);
}
}
public String PflichtbesetzungStart
{
get
{
return _PflichtbesetzungStart;
}
set
{
_PflichtbesetzungStart = value;
StoreDirtyInformation(AreDifferent(DataContract.PflichtbesetzungStart, value), PropertyName_PflichtbesetzungStart);
FirePropertyChanged(PropertyName_PflichtbesetzungStart);
}
}
public String PflichtbesetzungEnde
{
get
{
return _PflichtbesetzungEnde;
}
set
{
_PflichtbesetzungEnde = value;
StoreDirtyInformation(AreDifferent(DataContract.PflichtbesetzungEnde, value), PropertyName_PflichtbesetzungEnde);
FirePropertyChanged(PropertyName_PflichtbesetzungEnde);
}
}
public int PflichtbesetzungEinblenden
{
get
{
return _PflichtbesetzungEinblenden;
}
set
{
_PflichtbesetzungEinblenden = value;
StoreDirtyInformation(AreDifferent(DataContract.PflichtbesetzungEinblenden, value), PropertyName_PflichtbesetzungEinblenden);
FirePropertyChanged(PropertyName_PflichtbesetzungEinblenden);
}
}
public int StandardZeilenanzahl
{
get
{
return _StandardZeilenanzahl;
}
set
{
_StandardZeilenanzahl = value;
StoreDirtyInformation(AreDifferent(DataContract.StandardZeilenanzahl, value), PropertyName_StandardZeilenanzahl);
FirePropertyChanged(PropertyName_StandardZeilenanzahl);
}
}
public int Sortierungsart
{
get
{
return _Sortierungsart;
}
set
{
_Sortierungsart = value;
StoreDirtyInformation(AreDifferent(DataContract.Sortierungsart, value), PropertyName_Sortierungsart);
FirePropertyChanged(PropertyName_Sortierungsart);
}
}
public int AbsteigendeSortierung
{
get
{
return _AbsteigendeSortierung;
}
set
{
_AbsteigendeSortierung = value;
StoreDirtyInformation(AreDifferent(DataContract.AbsteigendeSortierung, value), PropertyName_AbsteigendeSortierung);
FirePropertyChanged(PropertyName_AbsteigendeSortierung);
}
}
public WohnheimEmployeeRelationListVM WohnheimEmployeeRelations
{
get { return _WohnheimEmployeeRelations; }
set
{
_WohnheimEmployeeRelations = value;
FirePropertyChanged(PropertyName_WohnheimEmployeeRelations);
}
}
public WohnheimCustomerRelationListVM WohnheimCustomerRelations
{
get { return _WohnheimCustomerRelations; }
set
{
_WohnheimCustomerRelations = value;
FirePropertyChanged(PropertyName_WohnheimCustomerRelations);
}
}
public WohneinheitListVM WohneinheitListVM
{
get { return _WohneinheitListVM; }
set
{
if (_WohneinheitListVM == value)
return;
_WohneinheitListVM = value;
FirePropertyChanged(nameof(WohneinheitListVM));
}
}
public WohneinheitBelegungListVM WohneinheitBelegungListVM
{
get { return _WohneinheitBelegungListVM; }
set
{
if (_WohneinheitBelegungListVM == value)
return;
_WohneinheitBelegungListVM = value;
FirePropertyChanged(nameof(WohneinheitBelegungListVM));
}
}
public CompactTeamDC WohnheimTeamRelations
{
get { return _WohnheimTeamRelations; }
set
{
_WohnheimTeamRelations = value;
FirePropertyChanged(PropertyName_WohnheimTeamRelations);
}
}
public VarFieldListVM VarFields
{
get { return this._VarFields; }
set
{
if (this.AreDifferent(this._VarFields, value))
{
this._VarFields = value;
this.FirePropertyChanged(PropertyName_VarFields);
}
}
}
public bool AddBelegung(object wohneinheit_oid = null, DateTime? start = null)
{
if (!WohneinheitBelegungListVM.PermissionWohneinheitBelegungManage)
return false;
var beleg_list_vm = WohneinheitBelegungListVM;
var vm = beleg_list_vm.NewVM;
var today = DateTime.Today;
var first_next_month = new DateTime(today.Year, today.Month, 1);
vm.StartDate = first_next_month;
if(wohneinheit_oid is long oid)
{
vm.Wohneinheit = WohneinheitListVM.VMList.First(x => x.Oid == oid);
}
if(start is DateTime startDate)
{
vm.StartDate = startDate;
}
beleg_list_vm.CurrentVM = vm;
return true;
}
public bool EditBelegung(WohneinheitBelegungVM vm)
{
if (vm is null || !WohneinheitBelegungListVM.VMList.Contains(vm))
return false;
if (!WohneinheitBelegungListVM.PermissionWohneinheitBelegungManage)
return false;
WohneinheitBelegungListVM.EditVM = vm;
WohneinheitBelegungListVM.CurrentVM = vm;
return true;
}
protected override void InitByDataContract(WohnheimDC pDataContract)
{
this._VarFields = new VarFieldListVM(pDataContract.VarFields);
_WohnheimName = pDataContract.WohnheimName;
_Strasse = pDataContract.Strasse;
_PLZ = pDataContract.PLZ;
_Ort = pDataContract.Ort;
_Bemerkung = pDataContract.Bemerkung;
_PflichtbesetzungStart = pDataContract.PflichtbesetzungStart;
_PflichtbesetzungEnde = pDataContract.PflichtbesetzungEnde;
_PflichtbesetzungEinblenden = pDataContract.PflichtbesetzungEinblenden;
_StandardZeilenanzahl = pDataContract.StandardZeilenanzahl;
_Sortierungsart = pDataContract.Sortierungsart;
_AbsteigendeSortierung = pDataContract.AbsteigendeSortierung;
_IsArchived = pDataContract.ActivationType == ActivationTypeId.Archived;
WohnheimEmployeeRelations = VMFactory.CreateWohnheimEmployeeRelationListVM(pDataContract.RelatedEmployees ?? (pDataContract.RelatedEmployees = new List<WohnheimEmployeeRelationDC>()));
WohnheimCustomerRelations = VMFactory.CreateWohnheimCustomerRelationListVM(pDataContract.RelatedCustomers ?? (pDataContract.RelatedCustomers = new List<WohnheimCustomerRelationDC>()));
if (pDataContract.Wohneinheiten is null)
pDataContract.Wohneinheiten = new List<WohneinheitDC>();
VMFactory.CreateWohneinheitListVMs(pDataContract.Wohneinheiten, out var einheit, out var beleg);
WohneinheitListVM = einheit;
WohneinheitBelegungListVM = beleg;
}
protected override WohnheimDC MapToDataContract(WohnheimDC pDataContract, bool doCommit)
{
pDataContract.WohnheimName = _WohnheimName;
pDataContract.Strasse = _Strasse;
pDataContract.PLZ = _PLZ;
pDataContract.Ort = _Ort;
pDataContract.Bemerkung = _Bemerkung;
pDataContract.PflichtbesetzungStart = _PflichtbesetzungStart;
pDataContract.PflichtbesetzungEnde = _PflichtbesetzungEnde;
pDataContract.PflichtbesetzungEinblenden = _PflichtbesetzungEinblenden;
pDataContract.StandardZeilenanzahl = _StandardZeilenanzahl;
pDataContract.Sortierungsart = _Sortierungsart;
pDataContract.AbsteigendeSortierung = _AbsteigendeSortierung;
pDataContract.ActivationType = _IsArchived ? ActivationTypeId.Archived : ActivationTypeId.Active;
pDataContract.RelatedEmployees = _WohnheimEmployeeRelations.CopyToDCList(doCommit);
pDataContract.RelatedCustomers = _WohnheimCustomerRelations.CopyToDCList(doCommit);
//pDataContract.Wohneinheiten = _WohneinheitListVM.CopyToDCList(doCommit);
if (this._VarFields != null)
{
pDataContract.VarFields = this._VarFields.CopyToDCList(doCommit);
}
pDataContract.Wohneinheiten = null;
var einheit_vm2dc = new Dictionary<WohneinheitVM, WohneinheitDC>();
foreach (var vm in _WohneinheitListVM.VMList)
{
var dc = doCommit ? vm.CommitToDataContract() : vm.CopyToEmptyDataContract();
if (pDataContract.Wohneinheiten is null)
pDataContract.Wohneinheiten = new List<WohneinheitDC>();
einheit_vm2dc.Add(vm, dc);
pDataContract.Wohneinheiten.Add(dc);
}
foreach (var vm in _WohneinheitBelegungListVM.VMList)
{
var dc = doCommit ? vm.CommitToDataContract() : vm.CopyToEmptyDataContract();
if (einheit_vm2dc[vm.Wohneinheit].Belegungen is null)
einheit_vm2dc[vm.Wohneinheit].Belegungen = new List<WohneinheitBelegungDC>();
if (!einheit_vm2dc[vm.Wohneinheit].Belegungen.Contains(dc))
einheit_vm2dc[vm.Wohneinheit].Belegungen.Add(dc);
}
return pDataContract;
}
}
}