Files
BeWoPlaner/BeWo/ViewModel/PreisVM.cs
2021-08-17 17:12:52 +02:00

218 lines
5.3 KiB
C#

using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using BeWo.Validation;
using BeWo.ViewModel.ListViewModel;
namespace BeWo.ViewModel
{
public class PreisVM : AbstractDCMapperVM<PreisDC>
{
private long? _PreisOid;
private long? _PreisVersion;
private SystemEntryID? _PreisSystemEntryID;
private string _Name;
public static string PropertyName_CostRatePeriods = "CostRatePeriods";
public static string PropertyName_CurrentAmount = "CurrentAmount";
public static string PropertyName_IsDefault = "IsDefault";
public static string PropertyName_DoNotCheckOverlapping = "DoNotCheckOverlapping";
private bool _IsDeleteable = true;
private bool _IsDefault;
private bool _DoNotCheckOverlapping;
private CostRatePeriodListVM _CostRatePeriods;
public PreisVM(PreisDC preisDC) : base(preisDC, preisDC.PreisOid == null)
{ }
public long? PreisOid
{
get { return _PreisOid; }
set
{
_PreisOid = value;
StoreDirtyInformation(AreDifferent(DataContract.PreisOid, value), nameof(_PreisOid));
FirePropertyChanged(nameof(_PreisOid));
}
}
public long? PreisVersion
{
get { return _PreisVersion; }
set
{
_PreisVersion = value;
StoreDirtyInformation(AreDifferent(DataContract.PreisVersion, value), nameof(_PreisVersion));
FirePropertyChanged(nameof(_PreisVersion));
}
}
public SystemEntryID? PreisSystemEntryID
{
get { return _PreisSystemEntryID; }
set
{
_PreisSystemEntryID = value;
StoreDirtyInformation(AreDifferent(DataContract.PreisSystemEntryID, value), nameof(_PreisSystemEntryID));
FirePropertyChanged(nameof(_PreisSystemEntryID));
}
}
public string Name
{
get { return _Name; }
set
{
_Name = value;
StoreDirtyInformation(AreDifferent(DataContract.Name, value), nameof(_Name));
FirePropertyChanged(nameof(_Name));
}
}
public bool IsDeleteable
{
get { return _IsDeleteable; }
}
public CostRatePeriodListVM CostRatePeriods
{
get { return _CostRatePeriods; }
set
{
if (AreDifferent(_CostRatePeriods, value))
{
_CostRatePeriods = value;
if ( _CostRatePeriods != null && _CostRatePeriods.VMList.Count > 0)
FirePropertyChanged(PropertyName_CostRatePeriods);
}
}
}
public bool IsDefault
{
get { return this._IsDefault; }
set
{
if (this.AreDifferent(this._IsDefault, value))
{
this._IsDefault = value;
this.StoreDirtyInformation(this.AreDifferent(this.DataContract.IsDefault, value), PropertyName_IsDefault);
this.FirePropertyChanged(PropertyName_IsDefault);
}
}
}
public bool DoNotCheckOverlapping
{
get { return this._DoNotCheckOverlapping; }
set
{
if (this.AreDifferent(this._DoNotCheckOverlapping, value))
{
this._DoNotCheckOverlapping = value;
this.StoreDirtyInformation(this.AreDifferent(this.DataContract.IsDefault, value), PropertyName_DoNotCheckOverlapping);
this.FirePropertyChanged(PropertyName_DoNotCheckOverlapping);
}
}
}
public CostRatePeriodVM CurrentAmount
{
get { return CostRatePeriods.GetCurrentlyValidRate(CostRatePeriodType.AmountOfMoney); }
}
public override bool IsDirty
{
get
{
if (_CostRatePeriods == null)
{
return false;
}
return base.IsDirty || _CostRatePeriods.IsDirty;
}
}
protected override void InitByDataContract(PreisDC bDataContract)
{
if (!IsNew)
{
_IsDeleteable = !bDataContract.IsSystemEntry;
_IsDefault = bDataContract.IsDefault;
_DoNotCheckOverlapping = bDataContract.DoNotCheckOverlapping;
_PreisOid = bDataContract.PreisOid;
_PreisVersion = bDataContract.PreisVersion;
_PreisSystemEntryID = bDataContract.PreisSystemEntryID;
Name = bDataContract.Name;
}
if (bDataContract.CostRatePeriods == null)
{
bDataContract.CostRatePeriods = new List<CostRatePeriodDC>();
}
Utils.GetAllEnumValues<CostRatePeriodType>().ToList().ForEach(
t =>
{
if (t == CostRatePeriodType.AmountOfMoney && !bDataContract.CostRatePeriods.Exists(i => i.CostRateType == t))
{
bDataContract.CostRatePeriods.Add(
new CostRatePeriodDC
{
CostRateType = t
});
}
});
_CostRatePeriods = new CostRatePeriodListVM(bDataContract.CostRatePeriods);
_CostRatePeriods.VMList.ListChanged += (s, e) =>
{
if (e.ListChangedType != ListChangedType.ItemChanged)
{
FirePropertyChanged(PropertyName_CostRatePeriods);
}
FirePropertyChanged(PropertyName_CurrentAmount);
};
}
protected override PreisDC MapToDataContract(PreisDC bDataContract, bool doCommit)
{
bDataContract.IsDefault = _IsDefault;
bDataContract.DoNotCheckOverlapping = _DoNotCheckOverlapping;
bDataContract.PreisOid = _PreisOid;
bDataContract.PreisVersion = _PreisVersion;
bDataContract.PreisSystemEntryID = _PreisSystemEntryID;
bDataContract.Name = _Name;
bDataContract.CostRatePeriods = new List<CostRatePeriodDC>();
List<CostRatePeriodDC> crpList = _CostRatePeriods.CopyToDCList(doCommit);
foreach (var costRatePeriodDc in crpList)
{
if (costRatePeriodDc.CostRateValue.HasValue && costRatePeriodDc.CostRateValue.Value > 0)
bDataContract.CostRatePeriods.Add(costRatePeriodDc);
}
return bDataContract;
}
}
}