diff --git a/ucalc/Data/BillingImporter.cs b/ucalc/Data/BillingImporter.cs new file mode 100644 index 0000000..4be2133 --- /dev/null +++ b/ucalc/Data/BillingImporter.cs @@ -0,0 +1,49 @@ +using System; +using System.Text; + +namespace UCalc.Data +{ + public static class BillingImporter + { + private static void RemoveDepartedTenants(Billing billing, StringBuilder details) + { + for (var i = 0; i < billing.Tenants.Count;) + { + var tenant = billing.Tenants[i]; + + if (tenant.DepartureDate != null && tenant.DepartureDate.Value < billing.StartDate) + { + details.Append("- Mieter \""); + details.Append(tenant.Name); + details.Append("\" wurde entfernt, da er ausgezogen ist.\n"); + + billing.Tenants.RemoveAt(i); + continue; + } + + if (tenant.EntryDate != null && tenant.EntryDate.Value <= billing.StartDate) + { + details.Append("- Das Einzugsdatum von Mieter \""); + details.Append(tenant.Name); + details.Append("\" wurde entfernt.\n"); + + tenant.EntryDate = null; + } + + ++i; + } + } + + public static Billing Import(string path, DateTime startDate, DateTime endDate, StringBuilder details) + { + var billing = BillingLoader.Load(path); + + billing.StartDate = startDate; + billing.EndDate = endDate; + + RemoveDepartedTenants(billing, details); + + return billing; + } + } +} \ No newline at end of file diff --git a/ucalc/Models/BillingProperty.cs b/ucalc/Models/BillingProperty.cs index d30ddb5..4f509fe 100644 --- a/ucalc/Models/BillingProperty.cs +++ b/ucalc/Models/BillingProperty.cs @@ -1,17 +1,23 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using UCalc.Data; namespace UCalc.Models { public class BillingProperty : NestedProperty { + public DateTime StartDate { get; } + public DateTime EndDate { get; } public LandlordProperty Landlord { get; } public HouseProperty House { get; } public TenantsProperty Tenants { get; } public CostsProperty Costs { get; } - public BillingProperty(Model model, Property parent, Billing data) : base(model, parent) + public BillingProperty(DateTime startDate, DateTime endDate, Model model, Property parent, Billing data) : base( + model, parent) { + StartDate = startDate; + EndDate = endDate; Landlord = Add(new LandlordProperty(model, this, data.Landlord)); House = Add(new HouseProperty(model, this, data.House)); diff --git a/ucalc/Models/CostEntryProperty.cs b/ucalc/Models/CostEntryProperty.cs index 94fc6bd..b798520 100644 --- a/ucalc/Models/CostEntryProperty.cs +++ b/ucalc/Models/CostEntryProperty.cs @@ -58,6 +58,13 @@ namespace UCalc.Models return $"{Name}: Dieser Zeitraum überschneidet sich mit einem anderen."; } } + + var billing = (BillingProperty) Parent.Parent.Parent.Parent.Parent; + if (!Value.Value.IsBetween(billing.StartDate, billing.EndDate) && + !startDate.Value.IsBetween(billing.StartDate, billing.EndDate)) + { + return $"{Name}: Dieser Zeitraum befindet sich außerhalb des Abrechnungszeitraums."; + } } return ""; diff --git a/ucalc/Models/Model.cs b/ucalc/Models/Model.cs index 1e2a370..dd3bfbf 100644 --- a/ucalc/Models/Model.cs +++ b/ucalc/Models/Model.cs @@ -159,18 +159,14 @@ namespace UCalc.Models } private readonly Validator _validator; - public DateTime StartDate { get; } - public DateTime EndDate { get; } public BillingProperty Root { get; } public Model(Billing billing) { _validator = new Validator(this); - StartDate = billing.StartDate; - EndDate = billing.EndDate; using var validator = BeginValidation(true); - Root = new BillingProperty(this, null, billing); + Root = new BillingProperty(billing.StartDate, billing.EndDate, this, null, billing); } public Validator BeginValidation(bool postPone = false) @@ -183,8 +179,8 @@ namespace UCalc.Models { var billing = new Billing { - StartDate = StartDate, - EndDate = EndDate, + StartDate = Root.StartDate, + EndDate = Root.EndDate, Landlord = { Salutation = (Salutation) Root.Landlord.Salutation.Value, @@ -255,8 +251,8 @@ namespace UCalc.Models new HashSet(cost.AffectedFlats.Select(rentedFlat => flatPropertyToFlat[rentedFlat])), Entries = cost.Entries.Select(entry => new CostEntry { - StartDate = entry.StartDate.Value ?? StartDate, - EndDate = entry.EndDate.Value ?? EndDate, + StartDate = entry.StartDate.Value ?? Root.StartDate, + EndDate = entry.EndDate.Value ?? Root.EndDate, Amount = entry.Amount.ConvertedValue ?? 0, Details = new CostEntryDetails { diff --git a/ucalc/NewWindow.xaml.cs b/ucalc/NewWindow.xaml.cs index 6b684c5..0129bc7 100644 --- a/ucalc/NewWindow.xaml.cs +++ b/ucalc/NewWindow.xaml.cs @@ -1,5 +1,6 @@ -using System; -using System.Globalization; +using System.Globalization; +using System.IO; +using System.Text; using System.Windows; using System.Windows.Input; using Microsoft.Win32; @@ -9,7 +10,6 @@ namespace UCalc { public partial class NewWindow { - public Billing SavedBilling { get; private set; } public Billing Billing { get; private set; } public NewWindow() @@ -28,7 +28,7 @@ namespace UCalc } } - private void OnCreateClick(object sender, RoutedEventArgs e) + private void OnCreateClick(object sender, RoutedEventArgs args) { if (StartCalendar.SelectedDate == null) { @@ -60,52 +60,30 @@ namespace UCalc return; } - throw new NotImplementedException(); - /*billing = new Billing(); - string fileName = (string) ((ComboBoxItem) LoadCombobox.SelectedItem).Tag; - if (!billing.LoadFromFile(fileName)) + var path = ((RecentlyOpenedItem) ReuseDataComboBox.SelectedItem).Path; + try { - MessageBox.Show("Die Datei \"" + fileName + "\" konnte nicht geladen werden!", "Fehler!", - MessageBoxButton.OK, MessageBoxImage.Error); - billing = null; + var details = new StringBuilder(); + Billing = BillingImporter.Import(path, StartCalendar.SelectedDate.Value, + EndCalendar.SelectedDate.Value, details); + + if (details.Length > 0) + { + MessageBox.Show( + $"Die folgenden Änderungen wurden beim Übernehmen vorgenommen:\n{details}", + "Information", MessageBoxButton.OK, MessageBoxImage.Information); + } + } + catch (IOException e) + { + MessageBox.Show( + $"Beim Laden der Datei \"{path}\" ist ein Fehler aufgetreten!\n\nDetails: {e.Message}", + "Fehler!", MessageBoxButton.OK, MessageBoxImage.Error); return; } - - // Remove departured renters and costs that were only paid once - string summary = ""; - billing.StartDate = (DateTime) StartCalendar.SelectedDate; - billing.EndDate = (DateTime) EndCalendar.SelectedDate; - - for (int i = 0; i < billing.Renters.Count;) - { - Renter renter = billing.Renters[i]; - if (renter.DepartureDate != null && renter.DepartureDate < billing.StartDate) - { - summary += "- Mieter \"" + renter.Name + "\" wurde entfernt, da er ausgezogen ist.\n"; - foreach (Cost cost in billing.Costs) - { - cost.AffectedRenters.Remove(renter); - } - - billing.Renters.Remove(renter); - continue; - } - else if (renter.EntryDate != null && renter.EntryDate <= billing.StartDate) - { - summary += "- Das Einzugsdatum von Mieter \"" + renter.Name + "\" wurde entfernt.\n"; - renter.EntryDate = null; - } - - ++i; - } - - if (summary != "") - MessageBox.Show("Die folgenden Änderungen wurden beim Übernehmen vorgenommen:\n" + summary, - "Information", MessageBoxButton.OK, MessageBoxImage.Information);*/ } else { - SavedBilling = new Billing(); Billing = new Billing { StartDate = StartCalendar.SelectedDate.Value,