Fix reading transaction details
This commit is contained in:
parent
779962eb05
commit
80956a6965
3 changed files with 88 additions and 18 deletions
|
|
@ -37,6 +37,10 @@ namespace Schaad.Finance.Api.AccountStatements
|
|||
|
||||
public string Text { get; set; }
|
||||
|
||||
public string Debtor { get; set; }
|
||||
|
||||
public string Creditor { get; set; }
|
||||
|
||||
private string GetHashString(string inputString)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
|
|
|||
|
|
@ -25,7 +25,8 @@ namespace Schaad.Finance.Tests
|
|||
[Test]
|
||||
public void ReadFile_CAMT053()
|
||||
{
|
||||
var statements = service.ReadFile(@"Data\camt053.xml", System.Text.Encoding.UTF8);
|
||||
//var statements = service.ReadFile(@"Data\camt053.xml", System.Text.Encoding.UTF8);
|
||||
var statements = service.ReadFile(@"C:\temp\camt053.xml", System.Text.Encoding.UTF8);
|
||||
|
||||
Assert.That(statements.TrueForAll(s => s.IsSuccess));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,20 +93,14 @@ namespace Schaad.Finance.Formats.AccountStatements
|
|||
{
|
||||
foreach (var entry in stmt.Ntry)
|
||||
{
|
||||
(var text, var debtor, var creditor) = ParseEntry(entry);
|
||||
|
||||
var transaction = new Transaction();
|
||||
accountStatement.Transactions.Add(transaction);
|
||||
|
||||
// var amount = entry.Amt.Value;
|
||||
// var currency = entry.Amt.Ccy;
|
||||
// var valueDate = entry.ValDt.Item;
|
||||
// var bookingDate = entry.BookgDt.Item;
|
||||
//var creditDepit = entry.CdtDbtInd;
|
||||
// var trxId = entry.NtryRef;
|
||||
// var text = entry.AddtlNtryInf;
|
||||
|
||||
transaction.Id = stmt.Id + entry.NtryRef;
|
||||
transaction.BookingDate = entry.BookgDt.Item;
|
||||
transaction.Text = ParseEntry(entry);
|
||||
transaction.Text = text;
|
||||
transaction.Debtor = debtor;
|
||||
transaction.Creditor = creditor;
|
||||
transaction.Value = (double)entry.Amt.Value;
|
||||
transaction.ValueDate = entry.ValDt.Item;
|
||||
|
||||
|
|
@ -114,6 +108,8 @@ namespace Schaad.Finance.Formats.AccountStatements
|
|||
{
|
||||
transaction.Value *= -1;
|
||||
}
|
||||
|
||||
accountStatement.Transactions.Add(transaction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -123,13 +119,21 @@ namespace Schaad.Finance.Formats.AccountStatements
|
|||
return accountStatements;
|
||||
}
|
||||
|
||||
private string ParseEntry(ReportEntry4 entry)
|
||||
private (string text, string debtor, string creditor) ParseEntry(ReportEntry4 entry)
|
||||
{
|
||||
var transactionTexts = new HashSet<string>();
|
||||
var transactionReferences = new HashSet<string>();
|
||||
var debtors = new HashSet<string>();
|
||||
var creditors = new HashSet<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(entry.AddtlNtryInf))
|
||||
{
|
||||
var set = new HashSet<string>();
|
||||
foreach (var addtlNtryInf in SplitForNewline(entry.AddtlNtryInf).Where(a => !string.IsNullOrEmpty(a)))
|
||||
{
|
||||
set.Add(addtlNtryInf);
|
||||
transactionTexts.Add(addtlNtryInf);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var ntryDtl in entry.NtryDtls)
|
||||
{
|
||||
foreach (var txDtl in ntryDtl.TxDtls)
|
||||
|
|
@ -140,14 +144,57 @@ namespace Schaad.Finance.Formats.AccountStatements
|
|||
{
|
||||
foreach (var info in SplitForNewline(ustrd).Where(a => !string.IsNullOrEmpty(a)))
|
||||
{
|
||||
set.Add(info.Trim());
|
||||
}
|
||||
}
|
||||
transactionTexts.Add(info.Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return string.Join(" / ", set);
|
||||
if (txDtl.RltdPties?.Cdtr != null)
|
||||
{
|
||||
creditors.Add(ParseRelatedParty(txDtl.RltdPties.Cdtr));
|
||||
}
|
||||
|
||||
if (txDtl.RltdPties?.Dbtr != null)
|
||||
{
|
||||
debtors.Add(ParseRelatedParty(txDtl.RltdPties.Dbtr));
|
||||
}
|
||||
|
||||
if (txDtl.Refs != null)
|
||||
{
|
||||
transactionReferences.Add(ParseTransactionReference(txDtl.Refs));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var debtorsText = GetText(debtors);
|
||||
var creditorsText = GetText(creditors);
|
||||
var transactionTexts1 = transactionTexts.Count > 0 ? GetText(transactionTexts) : GetText(transactionReferences);
|
||||
|
||||
return (transactionTexts1, debtorsText, creditorsText);
|
||||
}
|
||||
|
||||
private string ParseRelatedParty(PartyIdentification43 entry)
|
||||
{
|
||||
var texts = new HashSet<string>();
|
||||
AddIfNotEmpty(texts, entry.Nm);
|
||||
if (entry.PstlAdr != null)
|
||||
{
|
||||
AddIfNotEmpty(texts, entry.PstlAdr.StrtNm);
|
||||
AddIfNotEmpty(texts, entry.PstlAdr.PstCd);
|
||||
AddIfNotEmpty(texts, entry.PstlAdr.TwnNm);
|
||||
|
||||
foreach (var line in entry.PstlAdr.AdrLine)
|
||||
{
|
||||
AddIfNotEmpty(texts, line);
|
||||
}
|
||||
}
|
||||
|
||||
return GetAddress(texts);
|
||||
}
|
||||
|
||||
private string ParseTransactionReference(TransactionReferences3 entry)
|
||||
{
|
||||
return entry.InstrId;
|
||||
}
|
||||
|
||||
private IReadOnlyList<string> SplitForNewline(string text)
|
||||
|
|
@ -155,6 +202,24 @@ namespace Schaad.Finance.Formats.AccountStatements
|
|||
return text.Split('\n');
|
||||
}
|
||||
|
||||
private string GetText(HashSet<string> lines)
|
||||
{
|
||||
return string.Join(" / ", lines);
|
||||
}
|
||||
|
||||
private string GetAddress(HashSet<string> lines)
|
||||
{
|
||||
return string.Join(" ", lines);
|
||||
}
|
||||
|
||||
private void AddIfNotEmpty(HashSet<string> lines, string value)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
lines.Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public string ValidateAccountStatement(AccountStatement accountStatement)
|
||||
{
|
||||
if (accountStatement.StartBalance.Currency != accountStatement.EndBalance.Currency)
|
||||
|
|
|
|||
Loading…
Reference in a new issue