using BS.Shared.DataContracts; using System; using System.Collections.Generic; using BeWo.ViewModel.ListViewModel; using BS.Shared.DataContracts.Compact; namespace BeWo.ViewModel { public class ArbeitszeitVM : AbstractDCMapperVM { public static string PropertyName_GueltigVon = "GueltigVon"; public static string PropertyName_GueltigBis = "GueltigBis"; public static string PropertyName_Notice = "Notice"; public static string PropertyName_ArbeitszeitEintragListeVM = "ArbeitszeitEintragListeVM"; public static string PropertyName_ArbeitszeitString = "ArbeitszeitString"; public static string PropertyName_CompactCustomer = "CompactCustomer"; public static string PropertyName_Klient = "Klient"; private CompactCustomerDC _Customer; private DateTime? _GueltigVon; private DateTime? _GueltigBis; private string _Notice; private ArbeitszeitEintragListVM _ArbeitszeitEintraege; public ArbeitszeitVM(ArbeitszeitDC pDC) : base(pDC, pDC.ArbeitszeitOid == null) { } public override bool IsDirty { get { FirePropertyChanged(nameof(DetailText)); // Hack um die Aktualisierung des Textes zu erzwingen return base.IsDirty || ArbeitszeitEintraege != null && ArbeitszeitEintraege.IsDirty; } } public CompactCustomerDC Customer { get => _Customer; set { if(AreDifferent(_Customer, value)) { _Customer = value; StoreDirtyInformation(AreDifferent(DataContract.Customer, value), nameof(Customer)); FirePropertyChanged(nameof(Customer)); } } } public DateTime? GueltigVon { get => _GueltigVon; set { if(AreDifferent(_GueltigVon, value)) { _GueltigVon = value; StoreDirtyInformation(AreDifferent(DataContract.GueltigVon, value), nameof(GueltigVon)); FirePropertyChanged(nameof(GueltigVon)); FirePropertyChanged(nameof(ArbeitszeitString)); } } } public DateTime? GueltigBis { get => _GueltigBis; set { if(AreDifferent(_GueltigBis, value)) { _GueltigBis = value; StoreDirtyInformation(AreDifferent(DataContract.GueltigBis, value), nameof(GueltigBis)); FirePropertyChanged(nameof(GueltigBis)); FirePropertyChanged(nameof(ArbeitszeitString)); } } } public string Notice { get => _Notice; set { if(AreDifferent(_Notice, value)) { _Notice = value; StoreDirtyInformation(AreDifferent(DataContract.Notice, value), nameof(Notice)); FirePropertyChanged(nameof(Notice)); } } } public string ArbeitszeitString { get { var text = "Zeitraum: "; if(_GueltigVon.HasValue) { text = $"{_GueltigVon:dd.MM.yyyy}"; if(_GueltigBis.HasValue) { text += $" - {_GueltigBis:dd.MM.yyyy}"; } } else { if(_GueltigBis.HasValue) { text = $"bis {_GueltigBis:dd.MM.yyyy}"; } } if(string.IsNullOrEmpty(text)) { //if (this.IsNew) text = ""; //else // text = "Arbeitszeitplanung"; } return text; } } public String DetailText { get { decimal stunden = 0; foreach (var ae in ArbeitszeitEintraege.VMList) { String dateStr1 = String.Format("{0:dd.MM.yyyy} {1}", DateTime.Now, ae.UhrzeitVon); String dateStr2 = String.Format("{0:dd.MM.yyyy} {1}", DateTime.Now, ae.UhrzeitBis); DateTime date1; DateTime date2; if (DateTime.TryParse(dateStr1, out date1) && DateTime.TryParse(dateStr2, out date2)) { stunden += (decimal)date2.Subtract(date1).TotalHours; } } return String.Format("Gesamtstunden: {0:0.00}", stunden); } } public ArbeitszeitEintragListVM ArbeitszeitEintraege { get => _ArbeitszeitEintraege; set { _ArbeitszeitEintraege = value; FirePropertyChanged(nameof(ArbeitszeitEintraege)); } } protected override void InitByDataContract(ArbeitszeitDC pDataContract) { _GueltigBis = pDataContract.GueltigBis; _GueltigVon = pDataContract.GueltigVon; _Notice = pDataContract.Notice; _Customer = pDataContract.Customer; ArbeitszeitEintraege = VMFactory.CreateArbeitszeitEintragListVm(pDataContract.Eintraege ?? (pDataContract.Eintraege = new List())); } protected override ArbeitszeitDC MapToDataContract(ArbeitszeitDC pDataContract, bool doCommit) { pDataContract.GueltigVon = _GueltigVon; pDataContract.GueltigBis = _GueltigBis; pDataContract.Notice = _Notice; pDataContract.Customer = _Customer; pDataContract.Eintraege = ArbeitszeitEintraege.CopyToDCList(doCommit); return pDataContract; } } }