Files
BeWoPlaner/BeWo/ViewModel/WohneinheitBelegungVM.cs
2026-06-18 11:05:29 +02:00

195 lines
5.7 KiB
C#

using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace BeWo.ViewModel
{
public class WohneinheitBelegungVM : AbstractDCMapperVM<WohneinheitBelegungDC>
{
private WohneinheitVM _Wohneinheit;
private long? _Oid;
private CompactCustomerDC _Customer;
private DateTime _StartDate;
private DateTime? _EndDate;
private string _Kommentar;
public bool SoftDelete { get; set; }
public WohneinheitBelegungVM() : base(new WohneinheitBelegungDC(), true)
{
}
public WohneinheitBelegungVM(WohneinheitBelegungDC dc) : base(dc, dc.Oid == null)
{
}
public long? Oid => _Oid;
public bool CanSave
=> Wohneinheit is object && Customer is object;
public bool AllDay => true;
public WohneinheitVM Wohneinheit
{
get { return _Wohneinheit; }
set
{
if (!AreDifferent(Wohneinheit, value))
return;
if (_Wohneinheit is object)
_Wohneinheit.PropertyChanged -= FirePropertyChanged;
_Wohneinheit = value;
if (_Wohneinheit is object)
_Wohneinheit.PropertyChanged += FirePropertyChanged;
FirePropertyChanged(nameof(Wohneinheit));
FirePropertyChanged(nameof(CanSave));
}
}
public void SetNewWohneinheit(WohneinheitVM wh)
{
if (wh != null && Wohneinheit != null)
{
if (Wohneinheit.Oid != wh.Oid)
{
StoreDirtyInformation(true, nameof(Wohneinheit));
}
}
this.Wohneinheit = wh;
}
public CompactCustomerDC Customer
{
get { return _Customer; }
set
{
if (!AreDifferent(Customer, value))
return;
_Customer = value;
FirePropertyChanged(nameof(Customer));
FirePropertyChanged(nameof(CanSave));
}
}
public DateTime StartDate
{
get { return _StartDate; }
set
{
if (!AreDifferent(StartDate, value))
return;
_StartDate = value;
StoreDirtyInformation(AreDifferent(DataContract.StartDate, value), nameof(StartDate));
FirePropertyChanged(nameof(StartDate));
if (EndDate.HasValue && EndDate.Value < StartDate)
{
EndDate = value.Date;
}
}
}
public DateTime? EndDate
{
get { return _EndDate; }
set
{
if (!AreDifferent(EndDate, value))
return;
_EndDate = value;
StoreDirtyInformation(AreDifferent(DataContract.EndDate, value), nameof(EndDate));
FirePropertyChanged(nameof(EndDate));
FirePropertyChanged(nameof(EndDateDisplay));
if (value.HasValue && value.Value < StartDate)
{
StartDate = value.Value.Date;
}
}
}
public string Kommentar
{
get { return _Kommentar; }
set
{
if (!AreDifferent(Kommentar, value))
return;
_Kommentar = value;
StoreDirtyInformation(AreDifferent(DataContract.Kommentar, value), nameof(Kommentar));
FirePropertyChanged(nameof(Kommentar));
}
}
public DateTime EndDateDisplay => EndDate ?? new DateTime(DateTime.MaxValue.Year, 1, 1);
public WohneinheitBelegungVM ViewModel => this;
public override bool Equals(object obj)
{
return Equals(obj as WohneinheitBelegungVM);
}
public bool Equals(WohneinheitBelegungVM other)
{
var equ = other != null &&
Oid == other.Oid &&
Wohneinheit == other.Wohneinheit &&
StartDate == other.StartDate &&
EndDate == other.EndDate &&
Kommentar == other.Kommentar;
if (!equ)
return false;
if (Customer == null && other.Customer == null)
return true;
if (Customer == null && other.Customer != null)
return false;
return Customer.Equals(other.Customer);
}
protected override void InitByDataContract(WohneinheitBelegungDC pDataContract)
{
_Oid = pDataContract.Oid;
_Customer = pDataContract.Customer;
_EndDate = pDataContract.EndDate;
_Kommentar = pDataContract.Kommentar;
_StartDate = pDataContract.StartDate;
if (pDataContract.Wohneinheit != null)
{
_Wohneinheit = new WohneinheitVM(pDataContract.Wohneinheit);
}
}
protected override WohneinheitBelegungDC MapToDataContract(WohneinheitBelegungDC pDataContract, bool doCommit)
{
pDataContract.Oid = _Oid;
pDataContract.Customer = Customer;
pDataContract.EndDate = EndDate;
pDataContract.Kommentar = Kommentar;
pDataContract.StartDate = StartDate;
return pDataContract;
}
}
}