Skip to content

Commit

Permalink
# This is a combination of 2 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

Working on personal finance, implications table

# This is the commit message #2:

Unit in components table, ref #149
  • Loading branch information
michielbdejong committed Aug 29, 2022
1 parent 11cd80d commit 45a71ac
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 30 deletions.
14 changes: 11 additions & 3 deletions schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
15 changes: 12 additions & 3 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
32 changes: 18 additions & 14 deletions src/commands/import-bank-statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"]
]);
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/parsers/asnbank-CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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, [
Expand All @@ -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"]
]);
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/parsers/ingbank-CSV.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Example of the 2022 format of ING NL CSV download:
// "Datum";"Naam / Omschrijving";"Rekening";"Tegenrekening";"Code";"Af Bij";"Bedrag (EUR)";"Mutatiesoort";"Mededelingen";"Saldo na mutatie";"Tag"
// "Datum";"Naam / Omschrijving";"Rekening";"Tegenrekening";"Code";"Af Bij";"Bedrag (EUR)";"Mutatiesoort";"Mededelingen";"Saldo na mutatie";
// "20220713";"Kosten OranjePakket";"NL08INGB0006130373";"";"DV";"Af";"2,35";"Diversen";"1 jun t/m 30 jun 2022 ING BANK N.V. Valutadatum: 13-07-2022";"256,31";""

function checkHeaders($line, $COLUMN_NAMES)
Expand Down Expand Up @@ -38,10 +38,18 @@ function parseIngDate($str)
}

function parseIngDescription($obj)
{
return $obj["Mutatiesoort"]
{ // description:
// "Code" | "Mutatiesoort" | "Naam / Omschrijving" | "Mededelingen" | "Tag"

return $obj["Code"]
. ": "
. $obj["Mutatiesoort"]
. ": "
. $obj["Naam / Omschrijving"]
. ": "
. $obj["Mededelingen"]
. ": "
. $obj["Naam / Omschrijving"];
. $obj["Tag"];
}

function parseIngAccount2($obj)
Expand Down Expand Up @@ -102,7 +110,8 @@ function parseIngBankCSV($text, $owner)
"amount" => 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, [
Expand All @@ -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"]);
Expand Down

0 comments on commit 45a71ac

Please sign in to comment.