2020-09-26 11:30:15 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
2020-09-26 11:59:06 +00:00
|
|
|
|
using System.CommandLine.DragonFruit;
|
2020-09-26 11:30:15 +00:00
|
|
|
|
using CsvHelper;
|
2020-09-26 11:59:06 +00:00
|
|
|
|
using CsvHelper.Configuration.Attributes;
|
2020-09-26 11:30:15 +00:00
|
|
|
|
|
|
|
|
|
namespace BunyMuny {
|
|
|
|
|
class Program {
|
2020-09-26 11:59:06 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// BunyMuny parses the CSV output of various bank statement listings and converts it to something more human readable with nice visualisations.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file">The CSV file to read</param>
|
|
|
|
|
/// <param name="rules">The JSON file to use for rules when parsing statement descriptions</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
static int Main(string file = "test.csv", string rules = "rules.json") {
|
2020-09-26 11:30:15 +00:00
|
|
|
|
Bank bank = Bank.ME;
|
|
|
|
|
var statements = new List<Statement>();
|
|
|
|
|
|
2020-09-26 11:59:06 +00:00
|
|
|
|
using (var sr = new StreamReader(file)) {
|
2020-09-26 11:30:15 +00:00
|
|
|
|
using (var csv = new CsvReader(sr, CultureInfo.InvariantCulture)) {
|
|
|
|
|
csv.Read();
|
|
|
|
|
csv.ReadHeader();
|
|
|
|
|
// get the first line of the CSV file (the header) as a string
|
|
|
|
|
string header = csv.Parser.Context.RawRecord;
|
|
|
|
|
if (header == null) {
|
|
|
|
|
Console.WriteLine("File is empty 0uo");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else if (header == "Date,Description,Debits and credits,Balance") {
|
|
|
|
|
bank = Bank.ME;
|
|
|
|
|
}
|
|
|
|
|
else if (header == "Whatever NAB uses I guess") {
|
|
|
|
|
bank = Bank.NAB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (csv.Read()) {
|
|
|
|
|
switch (bank) {
|
|
|
|
|
case Bank.ME:
|
|
|
|
|
double value = double.Parse(csv.GetField("Debits and credits").TrimStart().Replace("$", ""));
|
|
|
|
|
statements.Add(new Statement() {
|
|
|
|
|
Date = DateTime.ParseExact(csv.GetField("Date"), "dd/MM/yyyy", CultureInfo.InvariantCulture),
|
|
|
|
|
Description = csv.GetField("Description"),
|
|
|
|
|
Category = "Unknown",
|
|
|
|
|
Value = value
|
|
|
|
|
});
|
|
|
|
|
break;
|
2020-09-26 11:59:06 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Console.WriteLine(":(");
|
|
|
|
|
return 1;
|
2020-09-26 11:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var statement in statements) {
|
|
|
|
|
Console.WriteLine(statement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|