using Elwig.Helpers.Billing; using Elwig.Models; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace Elwig.Helpers.Export { public class Ebics : IBankingExporter { public static string FileExtension => "xml"; private readonly StreamWriter _writer; private readonly DateOnly _date; private readonly int _year; private readonly string _name; private readonly int _nr; public Ebics(PaymentVar variant, string filename) { _writer = new(filename, false, Utils.UTF8); _date = variant.TransferDate ?? DateOnly.Parse("2021-01-10"); //throw new ArgumentException("TransferDate has to be set in PaymentVar"); _year = variant.Year; _name = variant.Name; _nr = variant.AvNr; } public void Dispose() { GC.SuppressFinalize(this); _writer.Dispose(); } public ValueTask DisposeAsync() { GC.SuppressFinalize(this); return _writer.DisposeAsync(); } public void Export(IEnumerable transactions, IProgress? progress = null) { ExportAsync(transactions, progress).GetAwaiter().GetResult(); } public async Task ExportAsync(IEnumerable transactions, IProgress? progress = null) { progress?.Report(0.0); var nbOfTxs = transactions.Count(); int count = nbOfTxs + 2, i = 0; var ctrlSum = Transaction.FormatAmountCent(transactions.Sum(tx => tx.AmountCent)); var msgId = $"ELWIG-{App.Client.NameToken}-{_year}-AV{_nr:00}"; var pmtInfId = $"{msgId}-1"; await _writer.WriteLineAsync($""" {msgId} {DateTime.UtcNow:o} {nbOfTxs} {ctrlSum} {App.Client.NameFull} {pmtInfId} TRF {nbOfTxs} {ctrlSum}
{_date:yyyy-MM-dd}
{App.Client.NameFull} {App.Client.Iban?.Replace(" ", "")} {App.Client.Bic ?? "NOTPROVIDED"} """); progress?.Report(100.0 * ++i / count); foreach (var tx in transactions) { var a = (IAddress?)tx.Member.BillingAddress ?? tx.Member; var (a1, a2) = Utils.SplitAddress(a.Address); var id = $"ELWIG-{App.Client.NameToken}-{_year}-TG{tx.Nr:0000}"; var info = $"{_name} - Traubengutschrift {_year}/{tx.Nr:000}"; await _writer.WriteLineAsync($""" {id} {Transaction.FormatAmountCent(tx.AmountCent)} {a.Name} {a1}{a2} {a.PostalDest.AtPlz?.Plz}{a.PostalDest.AtPlz?.Ort.Name} {a.PostalDest.Country.Alpha2} {tx.Member.Iban} {tx.Member.Bic ?? "NOTPROVIDED"} {info} """); progress?.Report(100.0 * ++i / count); } await _writer.WriteLineAsync("""
"""); await _writer.FlushAsync(); progress?.Report(100.0); } } }