99 lines
5.0 KiB
C#
99 lines
5.0 KiB
C#
using Elwig.Helpers;
|
|
using Elwig.Models;
|
|
using Elwig.Models.Entities;
|
|
using Elwig.ViewModels;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Elwig.Services {
|
|
public static class MemberBusinessSharesService {
|
|
|
|
public static async Task InitInputs(this MemberBusinessSharesViewModel vm) {
|
|
using var ctx = new AppDbContext();
|
|
vm.DateNoticeString = $"{Utils.Today:dd.MM.yyyy}";
|
|
vm.ValuePerShare = (await ctx.Seasons.OrderBy(s => s.Year).LastOrDefaultAsync())?.BusinessShareValue;
|
|
}
|
|
|
|
public static void ClearInputs(this MemberBusinessSharesViewModel vm) {
|
|
|
|
}
|
|
|
|
public static void FillInputs(this MemberBusinessSharesViewModel vm, MemberHistory h) {
|
|
var mgnr = vm.FilterMember.MgNr;
|
|
vm.DateNotice = h.DateNotice;
|
|
vm.DateEffective = h.DateEffective;
|
|
vm.Reason = h.Reason + (h.Reason == MemberHistory.REASON_TRANSFER ? $"_{(h.ToMgNr == mgnr ? "from" : "to")}" : "");
|
|
vm.Shares = h.Shares;
|
|
vm.MemberHasSigned = h.MemberHasSigned;
|
|
vm.ValuePerShare = h.ValuePerShare;
|
|
vm.OtherMgNr = h.FromMgNr != null && h.FromMgNr != mgnr ? h.FromMgNr : h.ToMgNr != null && h.ToMgNr != mgnr ? h.ToMgNr : null;
|
|
vm.DeductYear = h.DeductYear ?? h.DateNotice.Year;
|
|
vm.Deduct = h.DeductYear != null;
|
|
vm.Comment = h.Comment;
|
|
}
|
|
|
|
public static async Task<int> UpdateMemberHistory(this MemberBusinessSharesViewModel vm, int? oldHistNr, bool enableTriggers) {
|
|
return await Task.Run(async () => {
|
|
using var ctx = new AppDbContext();
|
|
var tx = await ctx.Database.BeginTransactionAsync();
|
|
await ctx.Database.ExecuteSqlAsync($"UPDATE client_parameter SET value = {(enableTriggers ? "1" : "0")} WHERE param = 'ENABLE_MEMBER_HISTORY_TRIGGERS'");
|
|
|
|
var h = new MemberHistory {
|
|
HistNr = oldHistNr ?? await ctx.NextHistNr(),
|
|
FromMgNr =
|
|
vm.Reason == $"{MemberHistory.REASON_TRANSFER}_from" ? vm.OtherMgNr :
|
|
vm.Reason == MemberHistory.REASON_PURCHASE || vm.Reason == MemberHistory.REASON_PRESCRIPTION ? null :
|
|
vm.FilterMember.MgNr,
|
|
FromType =
|
|
vm.Reason == MemberHistory.REASON_PAYOUT ? BusinessShareType.Dormant :
|
|
vm.Reason == MemberHistory.REASON_PURCHASE || vm.Reason == MemberHistory.REASON_PRESCRIPTION ? null :
|
|
BusinessShareType.Normal,
|
|
ToMgNr =
|
|
vm.Reason == MemberHistory.REASON_PAYOUT || vm.Reason == MemberHistory.REASON_EXCLUSION ? null :
|
|
vm.Reason == $"{MemberHistory.REASON_TRANSFER}_to" ? vm.OtherMgNr :
|
|
vm.FilterMember.MgNr,
|
|
ToType =
|
|
vm.Reason == MemberHistory.REASON_PAYOUT || vm.Reason == MemberHistory.REASON_EXCLUSION ? null :
|
|
vm.Reason == MemberHistory.REASON_RETIREMENT || vm.Reason == MemberHistory.REASON_DECEASED ? BusinessShareType.Dormant :
|
|
BusinessShareType.Normal,
|
|
DateNoticeString = $"{vm.DateNotice:yyyy-MM-dd}",
|
|
DateEffectiveString = $"{vm.DateEffective:yyyy-MM-dd}",
|
|
Reason = vm.Reason?.Split("_")[0] ?? MemberHistory.REASON_CONVERT,
|
|
Source = "manual",
|
|
Shares = vm.Shares ?? 0,
|
|
MemberHasSigned = vm.MemberHasSigned,
|
|
ValuePerShare = vm.ValuePerShare,
|
|
CurrencyCode = vm.ValuePerShare != null ? "EUR" : null,
|
|
DeductYear = vm.Deduct ? vm.DeductYear : null,
|
|
Comment = string.IsNullOrWhiteSpace(vm.Comment) ? null : vm.Comment.Trim(),
|
|
};
|
|
|
|
if (oldHistNr != null) {
|
|
ctx.Update(h);
|
|
} else {
|
|
ctx.Add(h);
|
|
}
|
|
|
|
await ctx.SaveChangesAsync();
|
|
await ctx.Database.ExecuteSqlAsync($"UPDATE client_parameter SET value = '1' WHERE param = 'ENABLE_MEMBER_HISTORY_TRIGGERS'");
|
|
await tx.CommitAsync();
|
|
|
|
return h.HistNr;
|
|
});
|
|
}
|
|
|
|
|
|
public static async Task DeleteMemberHistory(int histnr, bool enableTriggers) {
|
|
await Task.Run(async () => {
|
|
using var ctx = new AppDbContext();
|
|
var tx = await ctx.Database.BeginTransactionAsync();
|
|
await ctx.Database.ExecuteSqlAsync($"UPDATE client_parameter SET value = {(enableTriggers ? "1" : "0")} WHERE param = 'ENABLE_MEMBER_HISTORY_TRIGGERS'");
|
|
await ctx.MemberHistory.Where(h => h.HistNr == histnr).ExecuteDeleteAsync();
|
|
await ctx.Database.ExecuteSqlAsync($"UPDATE client_parameter SET value = '1' WHERE param = 'ENABLE_MEMBER_HISTORY_TRIGGERS'");
|
|
await tx.CommitAsync();
|
|
});
|
|
}
|
|
}
|
|
}
|