diff --git a/ucalc/Data/BillingLoader.cs b/ucalc/Data/BillingLoader.cs index 7553604..9f076b0 100644 --- a/ucalc/Data/BillingLoader.cs +++ b/ucalc/Data/BillingLoader.cs @@ -71,11 +71,11 @@ namespace UCalc.Data public static class BillingLoader { - private static string AsString(JObject parent, string name, bool optional = false) + private static string AsString(JObject parent, string name, string defaultValue = null) { - if (optional && (parent[name]?.Type ?? JTokenType.Null) == JTokenType.Null) + if (defaultValue != null && (parent[name]?.Type ?? JTokenType.Null) == JTokenType.Null) { - return ""; + return defaultValue; } return parent[name]?.Value() ?? @@ -89,7 +89,7 @@ namespace UCalc.Data private static DateTime? AsDateOptional(JObject parent, string name) { - var str = AsString(parent, name, true); + var str = AsString(parent, name, ""); if (string.IsNullOrEmpty(str)) { return null; @@ -104,8 +104,13 @@ namespace UCalc.Data throw new JsonException($"Cannot read {parent.Path}.{name} as integer"); } - private static bool AsBool(JObject parent, string name) + private static bool AsBool(JObject parent, string name, bool? defaultValue = null) { + if (defaultValue != null && (parent[name]?.Type ?? JTokenType.Null) == JTokenType.Null) + { + return defaultValue.Value; + } + return parent[name]?.Value() ?? throw new JsonException($"Cannot read {parent.Path}.{name} as bool"); } @@ -118,7 +123,7 @@ namespace UCalc.Data private static decimal? AsDecimalOptional(JObject parent, string name) { - var str = AsString(parent, name, true); + var str = AsString(parent, name, ""); if (string.IsNullOrEmpty(str)) { return null; @@ -187,7 +192,7 @@ namespace UCalc.Data billing.Landlord.Salutation = (Salutation) AsInt(landlord, "salutation"); billing.Landlord.Name = AsString(landlord, "name"); billing.Landlord.Phone = AsString(landlord, "phone"); - billing.Landlord.MailAddress = AsString(landlord, "mail"); + billing.Landlord.MailAddress = AsString(landlord, "mail", ""); LoadAddress(landlord, "address", billing.Landlord.Address); LoadBankAccount(landlord, "account", billing.Landlord.BankAccount); @@ -207,8 +212,8 @@ namespace UCalc.Data EntryDate = AsDateOptional(tenant, "entry_date"), DepartureDate = AsDateOptional(tenant, "departure_date"), PaidRent = AsDecimal(tenant, "paid_rent"), - CustomMessage1 = AsString(tenant, "message1", true), - CustomMessage2 = AsString(tenant, "message2", true) + CustomMessage1 = AsString(tenant, "message1", ""), + CustomMessage2 = AsString(tenant, "message2", "") }; LoadBankAccount(tenant, "account", targetTenant.BankAccount); @@ -266,7 +271,7 @@ namespace UCalc.Data Details = details }; }).ToList(), - DisplayInBill = AsBool(cost, "display") + DisplayInBill = AsBool(cost, "display", false) }; billing.Costs.Add(targetCost); diff --git a/ucalc/Models/BankAccountProperty.cs b/ucalc/Models/BankAccountProperty.cs index dfac11c..e00c4d4 100644 --- a/ucalc/Models/BankAccountProperty.cs +++ b/ucalc/Models/BankAccountProperty.cs @@ -4,15 +4,25 @@ namespace UCalc.Models { public class BankAccountProperty : NestedProperty { - public NotEmptyStringProperty Iban { get; } - public NotEmptyStringProperty Bic { get; } - public NotEmptyStringProperty BankName { get; } + public ValueProperty Iban { get; } + public ValueProperty Bic { get; } + public ValueProperty BankName { get; } - public BankAccountProperty(Model model, Property parent, BankAccount data) : base(model, parent) + public BankAccountProperty(Model model, Property parent, BankAccount data, bool belongsToTenant) : base(model, + parent) { - Iban = Add(new NotEmptyStringProperty(model, this, "IBAN", data.Iban)); - Bic = Add(new NotEmptyStringProperty(model, this, "BIC", data.Bic)); - BankName = Add(new NotEmptyStringProperty(model, this, "Name der Bank", data.BankName)); + if (belongsToTenant) + { + Iban = Add(new AlwaysValidProperty(model, this, "IBAN", data.Iban)); + Bic = Add(new AlwaysValidProperty(model, this, "BIC", data.Bic)); + BankName = Add(new AlwaysValidProperty(model, this, "Name der Bank", data.BankName)); + } + else + { + Iban = Add(new NotEmptyStringProperty(model, this, "IBAN", data.Iban)); + Bic = Add(new NotEmptyStringProperty(model, this, "BIC", data.Bic)); + BankName = Add(new NotEmptyStringProperty(model, this, "Name der Bank", data.BankName)); + } } } } \ No newline at end of file diff --git a/ucalc/Models/LandlordProperty.cs b/ucalc/Models/LandlordProperty.cs index ea22858..aca7e93 100644 --- a/ucalc/Models/LandlordProperty.cs +++ b/ucalc/Models/LandlordProperty.cs @@ -6,7 +6,7 @@ namespace UCalc.Models { public SalutationProperty Salutation { get; } public NotEmptyStringProperty Name { get; } - public NotEmptyStringProperty MailAddress { get; } + public AlwaysValidProperty MailAddress { get; } public NotEmptyStringProperty Phone { get; } public AddressProperty Address { get; } public BankAccountProperty BankAccount { get; } @@ -15,10 +15,10 @@ namespace UCalc.Models { Salutation = Add(new SalutationProperty(model, this, "Anrede", data.Salutation)); Name = Add(new NotEmptyStringProperty(model, this, "Name", data.Name)); - MailAddress = Add(new NotEmptyStringProperty(model, this, "Email Adresse", data.MailAddress)); + MailAddress = Add(new AlwaysValidProperty(model, this, "Email Adresse", data.MailAddress)); Phone = Add(new NotEmptyStringProperty(model, this, "Telefonnummer", data.Phone)); Address = Add(new AddressProperty(model, this, data.Address)); - BankAccount = Add(new BankAccountProperty(model, this, data.BankAccount)); + BankAccount = Add(new BankAccountProperty(model, this, data.BankAccount, false)); } } } \ No newline at end of file diff --git a/ucalc/Models/TenantProperty.cs b/ucalc/Models/TenantProperty.cs index 0f38a57..08fc205 100644 --- a/ucalc/Models/TenantProperty.cs +++ b/ucalc/Models/TenantProperty.cs @@ -212,7 +212,7 @@ namespace UCalc.Models Salutation = Add(new SalutationProperty(model, this, "Anrede", data.Salutation)); Name = Add(new NotEmptyStringProperty(model, this, "Name", data.Name)); PersonCount = Add(new NaturalNumberProperty(model, this, "Personenanzahl", data.PersonCount)); - BankAccount = Add(new BankAccountProperty(model, this, data.BankAccount)); + BankAccount = Add(new BankAccountProperty(model, this, data.BankAccount, true)); EntryDate = Add(new EntryDateProperty(model, this, "Einzugsdatum", data.EntryDate)); DepartureDate = Add(new DepatureDateProperty(model, this, "Auszugsdatum", data.DepartureDate)); RentedFlats = Add(new RentedFlatsProperty(model, this, data.RentedFlats, flatToProperty)); diff --git a/ucalc/PrintWindow.xaml.cs b/ucalc/PrintWindow.xaml.cs index feb2615..ad92fd3 100644 --- a/ucalc/PrintWindow.xaml.cs +++ b/ucalc/PrintWindow.xaml.cs @@ -188,7 +188,12 @@ namespace UCalc } AddText( - $"{Billing.Landlord.Salutation.AsString()} {Billing.Landlord.Name}\n{DateTime.Now.ToString(Constants.DateFormat)}\n\n{Billing.Landlord.Address.Street} {Billing.Landlord.Address.HouseNumber}\n{Billing.Landlord.Address.Postcode} {Billing.Landlord.Address.City}\nTelefon: {Billing.Landlord.Phone}\nEmail: {Billing.Landlord.MailAddress}"); + $"{Billing.Landlord.Salutation.AsString()} {Billing.Landlord.Name}\n{DateTime.Now.ToString(Constants.DateFormat)}\n\n{Billing.Landlord.Address.Street} {Billing.Landlord.Address.HouseNumber}\n{Billing.Landlord.Address.Postcode} {Billing.Landlord.Address.City}\nTelefon: {Billing.Landlord.Phone}"); + + if (!string.IsNullOrEmpty(Billing.Landlord.MailAddress)) + { + AddText($"Email: {Billing.Landlord.MailAddress}"); + } AddLineBreaks(2);