-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (99 loc) · 3.85 KB
/
Copy pathProgram.cs
File metadata and controls
109 lines (99 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Linq;
using System.Xml.Linq;
public class Program
{
static void Main()
{
XNamespace ns = "urn:iso:std:iso:20022:tech:xsd:camt.054.001.04";
XDocument doc = XDocument.Load("camt054.xml"); // FILE NAME.
var notifications = doc
.Descendants(ns + "Ntfctn")
.Select(n => new
{
Account = n.Element(ns + "Acct")?
.Element(ns + "Id")?
.Element(ns + "IBAN")?.Value,
Transactions = n.Elements(ns + "Ntry")
.Select(entry => new
{
Amount = entry.Element(ns + "Amt")?.Value,
Currency = entry.Element(ns + "Amt")?.Attribute("Ccy")?.Value,
CreditOrDebit = entry.Element(ns + "CdtDbtInd")?.Value,
Date = entry.Element(ns + "BookgDt")?.Element(ns + "Dt")?.Value,
Message = entry.Descendants(ns + "Ustrd").FirstOrDefault()?.Value, // Här kan vi läsa ut fakturanummret om det skickas med.
CurrencyRate = entry.Descendants(ns + "NtryDtls").Descendants(ns + "TxDtls").Descendants(ns + "AmtDtls").Descendants(ns + "TxAmt").Descendants(ns + "CcyXchg").Elements(ns + "XchgRate").FirstOrDefault()?.Value,
}).ToList()
//CustomerDetails = n.Element(ns + "")
});
foreach (var n in notifications)
{
Console.WriteLine($"Acccount: {n.Account}");
Console.WriteLine("Transactions:");
foreach (var t in n.Transactions)
{
Console.WriteLine($" Date: {t.Date}");
Console.WriteLine($" Payment type: {t.CreditOrDebit}");
Console.WriteLine($" Amount: {t.Amount} {t.Currency}");
Console.WriteLine($" Currency rate: {t.CurrencyRate}");
Console.WriteLine($" Message: {t.Message}");
Console.WriteLine();
if (t.Message != null)
{
var possibleInvoice = SearchForInvoiceInMessage(t.Message);
if(possibleInvoice != null)
{
Console.WriteLine($" Possible invoice: {possibleInvoice}");
}
}
}
}
}
private static object SearchForInvoiceInMessage(string message)
{
int letterCount = 0;
var index = 0;
var startindex = 0;
foreach (char c in message)
{
// letterCount++;
if (!char.IsWhiteSpace(c))
{
letterCount++;
}
else
{
letterCount = 0;
startindex = index + 1;
}
if (letterCount == 13)
{
var candidate = message.Substring(startindex, 13);
var isInvoiceNumber = CheckInvoiceNumber(candidate);
if (isInvoiceNumber)
{
return candidate;
}
}
index++;
}
return null;
}
private static bool CheckInvoiceNumber(string candidate)
{
// EXAMPLE INVOICE NUMBER: D25BCF99-0005
if (!candidate.Contains('-')) return false;
if(candidate.ElementAt(8) != '-') //
for (int i = 0; i < 8; i++)
{
if (!char.IsLetterOrDigit(candidate[i]))
return false;
}
for (int i = 9; i < 13; i++)
{
if (!char.IsDigit(candidate[i]))
return false;
}
return true;
}
}