using Elwig.Helpers; using Microsoft.EntityFrameworkCore; using System; using System.ComponentModel.DataAnnotations.Schema; namespace Elwig.Models.Entities { [Table("member_history"), PrimaryKey("HistNr")] public class MemberHistory { public const string REASON_PURCHASE = "purchase"; // Kauf public const string REASON_CONVERT = "convert"; // Änderung public const string REASON_TRANSFER = "transfer"; // Übertragung public const string REASON_PRESCRIPTION = "prescription"; // Vorschreibung public const string REASON_EXCLUSION = "exclusion"; // Ausschluss public const string REASON_DECEASED = "deceased"; // Tod public const string REASON_RETIREMENT = "retirement"; // Kündigung/stilllegen public const string REASON_PAYOUT = "payout"; // Auszahlung [Column("histnr")] public int HistNr { get; set; } [Column("from_mgnr")] public int? FromMgNr { get; set; } [Column("from_type")] public int? FromTypeValue { get; set; } [NotMapped] public BusinessShareType? FromType { get => (BusinessShareType?)FromTypeValue; set => FromTypeValue = (int?)value; } [Column("to_mgnr")] public int? ToMgNr { get; set; } [Column("to_type")] public int? ToTypeValue { get; set; } [NotMapped] public BusinessShareType? ToType { get => (BusinessShareType?)ToTypeValue; set => ToTypeValue = (int?)value; } [Column("date_notice")] public required string DateNoticeString { get; set; } [NotMapped] public DateOnly DateNotice { get => DateOnly.ParseExact(DateNoticeString, "yyyy-MM-dd"); set => value.ToString("yyyy-MM-dd"); } [Column("date_effective")] public required string DateEffectiveString { get; set; } [NotMapped] public DateOnly DateEffective { get => DateOnly.ParseExact(DateEffectiveString, "yyyy-MM-dd"); set => value.ToString("yyyy-MM-dd"); } [Column("reason")] public required string Reason { get; set; } [NotMapped] public string ReasonPretty => Reason switch { REASON_PURCHASE => "Kauf", REASON_CONVERT => "Änderung", REASON_TRANSFER => "Übertragung", REASON_PRESCRIPTION => "Vorschreibung", REASON_EXCLUSION => "Ausschluss", REASON_DECEASED => "Tod", REASON_RETIREMENT => "Kündigung", REASON_PAYOUT => "Auszahlung", _ => Reason, }; [Column("source")] public required string Source { get; set; } [Column("shares")] public int Shares { get; set; } [Column("signed")] public bool MemberHasSigned { get; set; } [Column("value_per_share")] public long? ValuePerShareValue { get; set; } [NotMapped] public decimal? ValuePerShare { get => ValuePerShareValue != null ? Utils.DecFromDb(ValuePerShareValue.Value, 2) : null; set => ValuePerShareValue = value != null ? Utils.DecToDb(value.Value, 2) : null; } [NotMapped] public decimal? TotalValue => Shares * ValuePerShare; [Column("currency")] public string? CurrencyCode { get; set; } [Column("deduct_year")] public int? DeductYear { get; set; } [Column("comment")] public string? Comment { get; set; } public string FormatReasonPhrase(int mgnr) { if (Reason == REASON_TRANSFER) { if (FromMgNr == mgnr) { return $"{ReasonPretty} an {ToMember?.FullAdministrativeName ?? "Nachfolger"}" + (Comment != null ? $", {Comment}" : ""); } else if (ToMgNr == mgnr) { return $"{ReasonPretty} von {FromMember?.FullAdministrativeName ?? "Vorgänger"}" + (Comment != null ? $", {Comment}" : ""); } } return ReasonPretty + (Comment != null ? $", {Comment}" : ""); } [NotMapped] public int? PoVMgNr { get; set; } [NotMapped] public string PoVReasonPhrase => FormatReasonPhrase(PoVMgNr ?? 0); [ForeignKey("FromMgNr")] public virtual Member? FromMember { get; private set; } = null!; [ForeignKey("ToMgNr")] public virtual Member? ToMember { get; private set; } = null!; [ForeignKey("CurrencyCode")] public virtual Currency Currency { get; private set; } = null!; } }