From 45a71ac425e620d5176d5c30010e992c2fee8613 Mon Sep 17 00:00:00 2001 From: Michiel de Jong Date: Mon, 29 Aug 2022 11:30:00 +0200 Subject: [PATCH] # This is a combination of 2 commits. # This is the 1st commit message: Working on personal finance, implications table # This is the commit message #2: Unit in components table, ref #149 --- schema.php | 14 ++++++++--- schema.sql | 15 +++++++++--- src/commands/import-bank-statement.php | 32 +++++++++++++++----------- src/parsers/asnbank-CSV.php | 16 +++++++++---- src/parsers/ingbank-CSV.php | 22 +++++++++++++----- 5 files changed, 69 insertions(+), 30 deletions(-) diff --git a/schema.php b/schema.php index 8303fee0..e9eedff2 100644 --- a/schema.php +++ b/schema.php @@ -24,17 +24,25 @@ function getTables() "create table movements ( id SERIAL PRIMARY KEY, userId integer, - type_ varchar(54), /* 'invoice', 'payment', 'worked' */ fromComponent integer, toComponent integer, timestamp_ timestamp, - amount decimal + amount decimal, + unit varchar );", + "drop table if exists implications;", + + "create table implications ( + id SERIAL PRIMARY KEY, + userId integer, + type_ varchar(54), /* 'invoice', 'payment', 'worked' */ + movementId integer, + statementId integer +);", "drop table if exists statements;", "create table statements ( id SERIAL PRIMARY KEY, - movementId integer, userId integer, sourceDocumentFormat varchar, /* could be an invoice, bank statement csv file, API call etc */ sourceDocumentFilename varchar, /* TODO: work out how to store files when on Heroku */ diff --git a/schema.sql b/schema.sql index 386d8fbb..6014d850 100644 --- a/schema.sql +++ b/schema.sql @@ -23,18 +23,27 @@ drop table if exists movements; create table movements ( id SERIAL PRIMARY KEY, userId integer, - type_ varchar(54), /* 'invoice', 'payment', 'worked' */ fromComponent integer, toComponent integer, timestamp_ timestamp, - amount decimal + amount decimal, + unit varchar +); + +drop table if exists implications; + +create table implications ( + id SERIAL PRIMARY KEY, + userId integer, + type_ varchar(54), /* 'invoice', 'payment', 'worked' */ + movementId integer, + statementId integer ); drop table if exists statements; create table statements ( id SERIAL PRIMARY KEY, - movementId integer, userId integer, sourceDocumentFormat varchar, /* could be an invoice, bank statement csv file, API call etc */ sourceDocumentFilename varchar, /* TODO: work out how to store files when on Heroku */ diff --git a/src/commands/import-bank-statement.php b/src/commands/import-bank-statement.php index 548f95f5..b56d5596 100644 --- a/src/commands/import-bank-statement.php +++ b/src/commands/import-bank-statement.php @@ -21,30 +21,34 @@ function importBankStatement($context, $command) $format = $command[1]; $fileName = $command[2]; $importTime = strtotime($command[3]); - $type_ = "payment"; $entries = $parserFunctions[$format](file_get_contents($fileName), $context["user"]["username"]); for ($i = 0; $i < count($entries); $i++) { // var_dump($entries[$i]); $movementIdsOutside = ensureMovementsLookalikeGroup($context, [ - "type_" => $type_, + "type_" => "outer", "fromComponent" => strval(getComponentId($entries[$i]["from"])), "toComponent" => strval(getComponentId($entries[$i]["to"])), "timestamp_" => $entries[$i]["date"], "amount" => $entries[$i]["amount"] ], 1); - for ($j = 0; $j < count($movementIdsOutside); $j++) { - ensureStatement($context, [ - "create-statement", - intval($movementIdsOutside[$j]), - $importTime, - "outside movement from bank statement: " .$entries[$i]["comment"], - $format, - "$fileName#$i" - ]); - } + // for ($j = 0; $j < count($movementIdsOutside); $j++) { + // ensureStatement($context, [ + // "create-statement", + // intval($movementIdsOutside[$j]), + // $importTime, + // "outside movement from bank statement: " .$entries[$i]["comment"], + // $format, + // // FIXME: statement is about a message + // // remoteID is about the subject of that message + // // so maybe we need an extra table for tracking + // // data object at neighbouring systems? + // // + // "$fileName#" . $entries[$i]["lineNum"] . " " . $entries[$i]["remoteID"] + // ]); + // } $movementIdsInside = ensureMovementsLookalikeGroup($context, [ - "type_" => $type_, + "type_" => "inner", "fromComponent" => strval(getComponentId($entries[$i]["insideFrom"])), "toComponent" => strval(getComponentId($entries[$i]["insideTo"])), "timestamp_" => $entries[$i]["date"], @@ -57,7 +61,7 @@ function importBankStatement($context, $command) $importTime, "inside movement from bank statement: " .$entries[$i]["comment"], $format, - "$fileName#$i" + "$fileName#" . $entries[$i]["lineNum"] . " " . $entries[$i]["remoteID"] ]); } } diff --git a/src/parsers/asnbank-CSV.php b/src/parsers/asnbank-CSV.php index cec1d2db..55896c16 100644 --- a/src/parsers/asnbank-CSV.php +++ b/src/parsers/asnbank-CSV.php @@ -17,7 +17,7 @@ function normalizeAccountName($str) function parseAccount2($obj) { if (strlen($obj["tegenrekeningnummer"]) > 0) { - return $obj["tegenrekeningnummer"]; + return $obj["tegenrekeningnummer"] . " " . $obj["naamTegenrekening"]; } if ($obj["globaleTransactiecode"] == "BEA") { return normalizeAccountName(substr($obj["omschrijving"], 1, 22)); @@ -53,7 +53,11 @@ function parseAccount2($obj) function parseDescription($obj) { - return str_replace("*", " ", $obj["naamTegenrekening"] . " " .$obj["globaleTransactiecode"] . " " . $obj["omschrijving"]); + return str_replace("*", " ", + $obj["interneTransactiecode"] . " " . + $obj["globaleTransactiecode"] . " " . + $obj["betalingskenmerk"] . " " . + $obj["omschrijving"]); } function parseAsnBankCSV($text, $owner) @@ -108,7 +112,9 @@ function parseAsnBankCSV($text, $owner) "amount" => floatval($obj["transactiebedrag"]), "balanceAfter" => floatval($obj["saldoRekeningVoorMutatie"]) + floatval($obj["transactiebedrag"]), "insideFrom" => $obj["opdrachtgeversrekening"], - "insideTo" => $owner + "insideTo" => $owner, + "lineNum" => $i + 1, + "remoteId" => $obj["journaaldatum"] . " " . $obj["volgnummerTransactie"] ]); } else { array_push($ret, [ @@ -119,7 +125,9 @@ function parseAsnBankCSV($text, $owner) "amount" => -floatval($obj["transactiebedrag"]), "balanceAfter" => floatval($obj["saldoRekeningVoorMutatie"]) + floatval($obj["transactiebedrag"]), "insideFrom" => $owner, - "insideTo" => $obj["opdrachtgeversrekening"] + "insideTo" => $obj["opdrachtgeversrekening"], + "lineNum" => $i + 1, + "remoteId" => $obj["journaaldatum"] . " " . $obj["volgnummerTransactie"] ]); } } diff --git a/src/parsers/ingbank-CSV.php b/src/parsers/ingbank-CSV.php index b444526d..ed7c86d7 100644 --- a/src/parsers/ingbank-CSV.php +++ b/src/parsers/ingbank-CSV.php @@ -1,7 +1,7 @@ parseIngAmount($obj["Bedrag (EUR)"]), "balanceAfter" => parseIngAmount($obj["Saldo na mutatie"]), "insideFrom" => $owner, - "insideTo" => $obj["Rekening"] + "insideTo" => $obj["Rekening"], + "lineNum" => $i + 1 ]); } elseif ($obj["Af Bij"] == 'Bij') { array_push($ret, [ @@ -113,7 +122,8 @@ function parseIngBankCSV($text, $owner) "amount" => parseIngAmount($obj["Bedrag (EUR)"]), "balanceAfter" => parseIngAmount($obj["Saldo na mutatie"]), "insideFrom" => $obj["Rekening"], - "insideTo" => $owner + "insideTo" => $owner, + "lineNum" => $i + 1 ]); } else { throw new Error("Af Bij not parseable! " . $obj["Af Bij"]);