From 2283893f252d738cfd03ae4f1b661f7e93841a32 Mon Sep 17 00:00:00 2001 From: Hans Fast Date: Mon, 10 Nov 2025 12:58:12 +0100 Subject: [PATCH] initial commit --- .gitignore | 3 + Sakefile.yaml | 265 +++++++++++++++++++++++++++ addUsageAmounts.js | 1 + bankersRound.js | 15 ++ data.ori | 74 ++++++++ dependencies.svg | 115 ++++++++++++ fix_missing_names.sql | 7 + inputs/bank-temp.csv | 37 ++++ inputs/bank-transactions.csv | 51 ++++++ inputs/collective-usage-expenses.csv | 3 + inputs/kwh-meters.csv | 2 + inputs/kwh-usage.csv | 8 + inputs/months.csv | 8 + inputs/txns-other.csv | 2 + insert-percents.js | 3 + outputFormat.js | 8 + outputFormatBank.js | 7 + percent-divide.js | 105 +++++++++++ percent-round.js | 88 +++++++++ percentRound.js | 8 + roundMonths.js | 2 + roundMonthsArray.js | 12 ++ roundUsage.js | 3 + subtree.ori | 4 + withBankPercents.js | 11 ++ withMonthPercents.js | 1 + withMonths.js | 5 + withPercents.js | 9 + 28 files changed, 857 insertions(+) create mode 100644 .gitignore create mode 100644 Sakefile.yaml create mode 100644 addUsageAmounts.js create mode 100644 bankersRound.js create mode 100644 data.ori create mode 100644 dependencies.svg create mode 100644 fix_missing_names.sql create mode 100644 inputs/bank-temp.csv create mode 100644 inputs/bank-transactions.csv create mode 100644 inputs/collective-usage-expenses.csv create mode 100644 inputs/kwh-meters.csv create mode 100644 inputs/kwh-usage.csv create mode 100644 inputs/months.csv create mode 100644 inputs/txns-other.csv create mode 100644 insert-percents.js create mode 100644 outputFormat.js create mode 100644 outputFormatBank.js create mode 100644 percent-divide.js create mode 100644 percent-round.js create mode 100644 percentRound.js create mode 100644 roundMonths.js create mode 100644 roundMonthsArray.js create mode 100644 roundUsage.js create mode 100644 subtree.ori create mode 100644 withBankPercents.js create mode 100644 withMonthPercents.js create mode 100644 withMonths.js create mode 100644 withPercents.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e5a321 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +process/ +output/ +.shastore diff --git a/Sakefile.yaml b/Sakefile.yaml new file mode 100644 index 0000000..904738f --- /dev/null +++ b/Sakefile.yaml @@ -0,0 +1,265 @@ +#using sake instead of just because need file outputs for dependencies ... +#! P=process +#! I=inputs +#! O=output +#! C=csvsql --no-inference --snifflimit 0 --query +# + + +#skip for demo: demo dataset already has normalized names. +# txns-names: +# help: normalize account names and fix missing names +# (ignore) doc: |- +# a case statement with a when for each variant present in the input data, +# becauase the payee field varies (e.g. 'Bob Fischer', 'B. Fischer'). +# Then also add missing names (some bank transactions don't have a payee) +# (ignore) WARNING: |- +# might need to check the input data over time, as new variants occur... Is there some kind of warning I can build in to see if there are any UNKNOWNS left over? +# also, the second query, 'fix missing names', sets all remaining UNKNOWNs to Bank. Is it possible to do that so there's remaining UNKNOWNs to detect? +# dependencies: +# - "$I/bank-transactions.csv" +# - "fix_missing_names.sql" +# - "query_normalize_names.sql" #not present in this demo dataset +# formula: >- +# $C "normalize_names.sql" +# --query "fix_missing_names.sql" +# < "$I/bank-transactions.csv" +# > $P/txns-names.csv +# output: +# - $P/txns-names.csv + + +txns-fromto: + help: convert transactions to format 'from — amount — to' + dependencies: +# - $P/txns-names.csv + - $I/bank-transactions.csv + formula: >- + $C 'select id, date, name as "from", + amount, + "shared" as "to", description + from stdin where cast(amount as real) > 0 + union select id, date, "shared" as "from", + -amount, + name as "to", description + from stdin where cast(amount as real) < 0 + order by id, date;' + < $I/bank-transactions.csv + > $P/txns-fromto.csv + output: + - $P/txns-fromto.csv + + + +#skip for demo. In the real dataset, kWh-meters:users is many:one so the meter usages have to be grouped and summed. +#months added to output file manually. +# kwh-usage: +# help: everyone's usage per period, including kWh used and months active in period. +# (ignore) doc: | +# in the real dataset, some households user multiple kWh meters. We need one kWh usage figure per account, +# so we have to sum the amount, grouped by account. +# dependencies: +# - $I/kwh-meters.csv +# - $I/months.csv +# formula: >- +# $C 'with vbwsum as +# (select "end" - "start" as usage, period, account from meters), +# vb as(select period, account, cast(sum(usage) as int) as usage +# from vbwsum +# group by period, account) +# select vb.*, months.months from vb +# inner join months on vb.period = months.period +# and vb.account = months.account;' +# --tables meters,months +# $I/kwh-meters.csv +# $I/months.csv +# > $P/kwh-usage.csv +# output: +# - $P/kwh-usage.csv + +bank-costs: + help: extract totals for each supplier from initial transaction table. + (ignore) doc: | + This gets calculated so that it gets updated when bank transactions input file changes. Only Bank is actually used for now... + dependencies: + - $P/txns-fromto.csv + formula: >- + $C 'select "to" as account, cast(round(sum(amount), 2) as string) as amount from stdin + where "to" = "Bank" + group by "to";' + < $P/txns-fromto.csv + > $P/bank-costs.csv + output: + - $P/bank-costs.csv + + +total-val-txns: + help: reverse value transactions for electricity usage variable and fixed, and banking costs + (ignore) doc: | + Doing this calculation is the main premise of this whole pipeline. + To figure out how much all the users owe or are owed by the shared account, + we allocate the final costs of energy delivered (from the invoices) to each account + based on their proportion of kWh used (that gets done in the next step, indiv-val-txns). + And then in the step txns-with-generated, we add these as 'virtual' transactions in reverse, from the Energy company to the shared account, + and then from the shared account to the individual users. + If everyone's account is settled correctly, then in the final balance every account should be at zero. + dependencies: + - $I/collective-usage-expenses.csv + - $P/bank-costs.csv + formula: >- + $C ' + with bank as ( + select account || "total" as id, "2025-09-01" as date, account, amount as amount, "value of banking costs" as description + from sup where account = "Bank" + ) + select period || account || "valvar" as id, + enddate as date, + account as "from", + exp_var as amount, + "shared" as "to", + "value of variable electricity expenses" as description + from elec + union all select period || account || "valfixed" as id, + enddate as date, + account as "from", + exp_fixed as amount, + "shared" as "to", + "value of fixed electricity expenses" as description + from elec + + union all select id, date, account, amount, "shared", description from bank + + order by id, date, account;' + --tables elec,sup + $I/collective-usage-expenses.csv + $P/bank-costs.csv + > $P/total-val-txns.csv + output: + - $P/total-val-txns.csv + +#Javascript version: uses percent-divide.js +indiv-val-txns: + help: create txns for value consumed by each household + (ignore) doc: | + this step gets done in Javascript because sqlite can't do accurate floating point division. + We need to allocate a proportion of the total expenses to each user: + - collective energy usage * proportion of kWh per user + - banking costs * months the user was participating (Sarah joined in the last month of period 2). + based on + The math is still not 100% accurate, but it's good enough for the current purposes. + (the errors are to the order of 10e-10). + Possible improvement: use Big.js to do the arithmetic, though there might still be a final rounding error at the end. + dependencies: + - $I/collective-usage-expenses.csv + - $P/bank-costs.csv + - $I/kwh-usage.csv + - percent-divide.js + formula: >- + deno run -A percent-divide.js $I/collective-usage-expenses.csv $P/bank-costs.csv $I/kwh-usage.csv + > $P/indiv-val-txns.csv + output: + - $P/indiv-val-txns.csv + +#Web Origami version: uses data.ori +#indiv-val-txns: +# help: create txns for value consumed by each household +# (ignore) doc: | +# this step gets done in Javascript because sqlite can't do accurate floating point division. +# We need to allocate a proportion of the total expenses to each user: +# - collective energy usage * proportion of kWh per user +# - banking costs * months the user was participating (Sarah joined in the last month of period 2). +# based on +# The math is still not 100% accurate, but it's good enough for the current purposes. +# (the errors are to the order of 10e-10). +# Possible improvement: use Big.js to do the arithmetic, though there might still be a final rounding error at the end. +# dependencies: +# - $I/collective-usage-expenses.csv +# - $P/bank-costs.csv +# - $I/kwh-usage.csv +# - data.ori +##there are more dependencies -- all the JS scripts used in data.ori -- but it doesn't make sense to include those all here. +# formula: >- +# ori 'Origami.csv data.ori/txns' > $P/indiv-val-txns.csv +# output: +# - $P/indiv-val-txns.csv + + +txns-with-other: + help: manually add txns which were not in input bank transaction csv + (ignore) doc: | + we add this in the 'fromto' step. + dependencies: + - $P/txns-fromto.csv + - $I/txns-other.csv + formula: >- + $C 'select * from "txns-fromto" union all + select * from "txns-other" + order by date, id;' + $P/txns-fromto.csv + $I/txns-other.csv + > $P/txns-with-other.csv + output: + - $P/txns-with-other.csv + +txns-with-generated: + help: add the generated value transactions. + dependencies: + - $P/txns-with-other.csv + - $P/total-val-txns.csv + - $P/indiv-val-txns.csv + formula: >- + $C 'select * from "txns-with-other" + union all select * from "total-val-txns" + union all select * from "indiv-val-txns" + order by date, id;' + $P/txns-with-other.csv + $P/total-val-txns.csv + $P/indiv-val-txns.csv + > $P/txns-with-generated.csv + output: + - $P/txns-with-generated.csv + +txns: + help: expand transactions to two rows per transaction. + (ignore) doc: By expanding the transactions to two entries, one for the 'from' account and one for the 'to' account, we can do double-entry accounting (lite) + dependencies: + - $P/txns-with-generated.csv + formula: >- + $C 'select id, date, + "from" as account, + -amount as amount, + description + from stdin union all + select id, date, + "to" as account, + amount, + description + from stdin + order by date, id;' + < $P/txns-with-generated.csv + > $P/txns.csv + output: + - $P/txns.csv + + +balance: + help: get total balance + dependencies: + - $P/txns.csv + formula: >- + $C 'with total as ( + select "total" as account, sum(amount) as amount + from stdin + ), pre as ( + select account, + round(sum(amount),2) as amount + from stdin + where id is not null + group by account + ) + select * from pre union all select * from total;' + < $P/txns.csv + >$O/balance.csv + output: + - $O/balance.csv + diff --git a/addUsageAmounts.js b/addUsageAmounts.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/addUsageAmounts.js @@ -0,0 +1 @@ + diff --git a/bankersRound.js b/bankersRound.js new file mode 100644 index 0000000..d850488 --- /dev/null +++ b/bankersRound.js @@ -0,0 +1,15 @@ +//from https://stackoverflow.com/a/49080858 +function bankersRound(n, d=2) { + var x = n * Math.pow(10, d); + var r = Math.round(x); + var br = Math.abs(x) % 1 === 0.5 ? (r % 2 === 0 ? r : r-1) : r; + return br / Math.pow(10, d); +} + +//this function divides obj.valuekey by totalamt, putting the result rounded to two positions in obj.resultkeyname. +export default async function(obj, valuekeyname, resultkeyname, totalamt) { + obj[resultkeyname] = bankersRound(obj[valuekeyname] / 100 * parseFloat(totalamt)); + return obj; + +} + diff --git a/data.ori b/data.ori new file mode 100644 index 0000000..be8b457 --- /dev/null +++ b/data.ori @@ -0,0 +1,74 @@ +{ + //load input data: individual usage, banktotal and collective usage. + indivusage = ./inputs/kwh-usage.csv/ + banktotal = Tree.filter(./process/bank-costs.csv/, entry => entry.account === 'ASN')[0].amount + collective_usage = ./inputs/collective-usage-expenses.csv/ + + //group collective usage by period + collective_usage_by_period = Tree.map(collective_usage, {key: (value, key) => value.period}) + + //group individual usage per account under period + indiv_by_period = Tree.groupBy(indivusage, line => line.period) + + //group individual usage per period under account. + indiv_by_account = Tree.groupBy(indivusage, line => line.account) + + //sum all users' months for all periods (one total sum) + totalmonths = Tree.mapReduce(indivusage, null, (lines) => lines.reduce((a,b) => a + parseInt(b.months),0)) + + + //calculate percent for months and usage. + //roundUsage.js calculates each account's percentage of the total usage, + //roundMonths.js does the same for how many months each was active in the entire period + //since some people came and left partway through. + percent_months = Tree.map(indiv_by_period, roundMonths.js) + percent_usage = Tree.map(indiv_by_period, roundUsage.js) + + + //add percents to each entry and flatten (reverse the Tree.groupBy) + //the output of the roundX algorithm above is a bare array of percents. + //they need to be mapped back to the individual's entries using the array index, + //which is what withPercents.js does. + with_percents = Tree.map( + indiv_by_period, + (values, key) => withPercents.js(values, key, percent_usage, percent_months) + ) → + Tree.deepValues → //object of arrays → array of arrays + (values) => values.flat() //array of arrays → flat array + + //now calculate usage for fixed and variable electricity expenses using the percents + //For each record, add two new properties 'amount_fixed' and 'amount_var' + //bankersRound.js multiplies the percents by the total from the collective usage table, + //using the banker's rounding rule. + with_usage = with_percents/ → (withPercents) => + Tree.map(withPercents, (record) => bankersRound.js(record, 'percent_months', 'amount_fixed', collective_usage_by_period[record.period].exp_fixed)) → + (withFixed) => Tree.map(withFixed, (record) => bankersRound.js(record, 'percent_usage', 'amount_var', collective_usage_by_period[record.period].exp_var)) + + //need to calculate a single banking costs amount for the whole period. + //It's not quite accurate, because banking costs have gone up over time, but it will do. + //this pipeline is akin to the with_percents one above, but this is not subdivided per period. + //hence the use of a difference script, withBankPercents.js, with a slightly different structure. + user_months = Tree.map( + indiv_by_account, + withMonths.js + ) + + //the array is placed under a key 'bank' for compatibility: + //in the original dataset, there were multiple suppliers that had to be accounted for, + //and the scripts expected that structure. + user_months_flat = Tree.deepValues(user_months) → (vals) => {'bank': vals.flat()} + percent_bank = Tree.map(user_months_flat, roundMonths.js) + + with_bank_percents = Tree.map( + user_months_flat, + (values, key) => withBankPercents.js(values, key, percent_bank) + ) → Tree.deepValues → (values) => values.flat() + + with_bank_usage = Tree.map( + with_bank_percents, (record) => bankersRound.js(record, 'percent_bank', 'amount_bank', banktotal)) + + //the output records: convert with_usage and with_bank_usage to a csv representing transactions. + txns_elec = Tree.map(with_usage, outputFormat.js).flat() + txns_bank = Tree.map(with_bank_usage, outputFormatBank.js) + txns = Tree.deepMerge(txns_elec, txns_bank) → Tree.deepValues //→ (values) => values.flat() +} diff --git a/dependencies.svg b/dependencies.svg new file mode 100644 index 0000000..293e04e --- /dev/null +++ b/dependencies.svg @@ -0,0 +1,115 @@ + + + + + + +DependencyDiagram + + + +bank-costs + +bank-costs + + + +indiv-val-txns + +indiv-val-txns + + + +bank-costs->indiv-val-txns + + + + + +total-val-txns + +total-val-txns + + + +bank-costs->total-val-txns + + + + + +txns-with-generated + +txns-with-generated + + + +indiv-val-txns->txns-with-generated + + + + + +total-val-txns->txns-with-generated + + + + + +txns + +txns + + + +txns-with-generated->txns + + + + + +balance + +balance + + + +txns->balance + + + + + +txns-fromto + +txns-fromto + + + +txns-fromto->bank-costs + + + + + +txns-with-other + +txns-with-other + + + +txns-fromto->txns-with-other + + + + + +txns-with-other->txns-with-generated + + + + + diff --git a/fix_missing_names.sql b/fix_missing_names.sql new file mode 100644 index 0000000..85009a9 --- /dev/null +++ b/fix_missing_names.sql @@ -0,0 +1,7 @@ +--2nd query of 2, using temp defined in earlier step. +--(not used in demo) +UPDATE temp set account = 'Bob' where id in (221130, 222138,222146); +UPDATE temp set account = 'Alice' where id = 231248; +update temp set account = 'Bank' where account = 'UNKNOWN'; +select * from temp; + diff --git a/inputs/bank-temp.csv b/inputs/bank-temp.csv new file mode 100644 index 0000000..720fa87 --- /dev/null +++ b/inputs/bank-temp.csv @@ -0,0 +1,37 @@ +;transactions +25018,2025-04-25,James,105,electricity advance may +25019,2025-04-25,Alice,90,electricity advance may +25020,2025-04-26,Bob,95,electricity advance may +25021,2025-04-27,ElecCo,-280,electricity advance may + +25021a,2025-05-05,ElecCo,-120,extra payment for actual usage period 1 + +25021b,2025-05-25,Bank,-2,banking costs may +25022,2025-05-25,James,105,electricity advance june +25023,2025-05-25,Alice,90,electricity advance june +25024,2025-05-26,Bob,95,electricity advance june +25025,2025-05-27,ElecCo,-280,electricity advance june +25025b,2025-06-25,Bank,-2,banking costs june +25026,2025-06-25,James,105,electricity advance july +25027,2025-06-25,Alice,90,electricity advance july +25028,2025-06-26,Bob,95,electricity advance july +25029,2025-06-27,ElecCo,-280,electricity advance july +25029b,2025-07-25,Bank,-2,banking costs july +25030,2025-07-25,James,105,electricity advance august +25031,2025-07-25,Alice,90,electricity advance august +25032,2025-07-26,Bob,95,electricity advance august +25033,2025-07-26,Sarah,85,electricity advance august +25034,2025-07-27,ElecCo,-280,electricity advance august +25034b,2025-08-25,Bank,-2,banking costs august +25035,2025-09-05,ElecCo,200,reimbursement electricity advance period 2 + + + +;collective usage +2025b,2025-04-01,2025-08-31,552,368,920,ElecCo, + +;kwh-usage +2025b,James,320,4 +2025b,Alice,300,4 +2025b,Bob,250,4 +2025b,Sarah,5,1 diff --git a/inputs/bank-transactions.csv b/inputs/bank-transactions.csv new file mode 100644 index 0000000..39b81da --- /dev/null +++ b/inputs/bank-transactions.csv @@ -0,0 +1,51 @@ +id,date,name,amount,description +25000a,2024-12-25,Bank,-2,banking costs december +25001,2024-12-25,James,105,electricity advance january +25002,2024-12-25,Alice,90,electricity advance january +25003,2424-12-26,Bob,-185,reimburse advance to ElecCo minus your advance to shared for this month +25003a,2025-01-25,Bank,-2,banking costs january +25005,2025-01-25,James,105,electricity advance february +25006,2025-01-25,Alice,90,electricity advance february +25007,2025-01-26,Bob,95,electricity advance february +25008,2025-01-27,ElecCo,-280,electricity advance february +25008a,2025-02-25,Bank,-2,banking costs february +25009,2025-02-25,James,105,electricity advance march +25010,2025-02-25,Alice,90,electricity advance march +25011,2025-02-26,Bob,95,electricity advance march +25012,2025-02-27,ElecCo,-280,electricity advance march +25012a,2025-03-25,Bank,-2,banking costs march +25013,2025-03-25,James,105,electricity advance april +25014,2025-03-25,Alice,90,electricity advance april +25015,2025-03-26,Bob,95,electricity advance april +25016,2025-03-27,ElecCo,-280,electricity advance april +25016a,2025-04-25,Bank,-2,banking costs april +25018,2025-04-25,James,105,electricity advance may +25019,2025-04-25,Alice,90,electricity advance may +25020,2025-04-26,Bob,95,electricity advance may +25021,2025-04-27,ElecCo,-280,electricity advance may +25016b,2025-05-01,James,53.25,extra payment for actual usage period 1 +25016c,2025-05-01,Alice,39.33,extra payment for actual usage period 1 +25016d,2025-05-01,Bob,77.41,extra payment for actual usage period 1 +25021a,2025-05-05,ElecCo,-200,extra payment for actual usage period 1 +25021b,2025-05-25,Bank,-2,banking costs may +25022,2025-05-25,James,105,electricity advance june +25023,2025-05-25,Alice,90,electricity advance june +25024,2025-05-26,Bob,95,electricity advance june +25025,2025-05-27,ElecCo,-280,electricity advance june +25025b,2025-06-25,Bank,-2,banking costs june +25026,2025-06-25,James,105,electricity advance july +25027,2025-06-25,Alice,90,electricity advance july +25028,2025-06-26,Bob,95,electricity advance july +25029,2025-06-27,ElecCo,-280,electricity advance july +25029b,2025-07-25,Bank,-2,banking costs july +25030,2025-07-25,James,105,electricity advance august +25031,2025-07-25,Alice,90,electricity advance august +25032,2025-07-26,Bob,95,electricity advance august +25033,2025-07-26,Sarah,85,electricity advance august +25034,2025-07-27,ElecCo,-280,electricity advance august +25034b,2025-08-25,Bank,-2,banking costs august +25035,2025-09-05,ElecCo,200,reimbursement too much advanced for period 2 +25036,2025-09-06,James,-115.24,reimbursement advance period 2 +25037,2025-09-06,Alice,-62.6,reimbursement advance period 2 +25038,2025-09-06,Bob,-101,reimbursement advance period 2 +25039,2025-09-06,Sarah,-38.14,reimbursement advance period 2 diff --git a/inputs/collective-usage-expenses.csv b/inputs/collective-usage-expenses.csv new file mode 100644 index 0000000..ae25ba7 --- /dev/null +++ b/inputs/collective-usage-expenses.csv @@ -0,0 +1,3 @@ +period,startdate,enddate,exp_fixed,exp_var,exp_total,account,notes +2025a,2025-01-01,2024-04-30,792,528,1320,ElecCo, +2025b,2025-04-01,2025-08-31,552,368,920,ElecCo, diff --git a/inputs/kwh-meters.csv b/inputs/kwh-meters.csv new file mode 100644 index 0000000..2a862d0 --- /dev/null +++ b/inputs/kwh-meters.csv @@ -0,0 +1,2 @@ +period,meter,account,start,end +2025a,1,James,400, diff --git a/inputs/kwh-usage.csv b/inputs/kwh-usage.csv new file mode 100644 index 0000000..bdcb503 --- /dev/null +++ b/inputs/kwh-usage.csv @@ -0,0 +1,8 @@ +period,account,usage,months +2025a,James,500,4 +2025a,Alice,320,4 +2025a,Bob,470,4 +2025b,James,320,4 +2025b,Alice,300,4 +2025b,Bob,250,4 +2025b,Sarah,5,1 diff --git a/inputs/months.csv b/inputs/months.csv new file mode 100644 index 0000000..653ef26 --- /dev/null +++ b/inputs/months.csv @@ -0,0 +1,8 @@ +period,account,months +2025a,James,4 +2025a,Alice,4 +2025a,Bob,4 +2025b,James,4 +2025b,Alice,4 +2025b,Bob,4 +2025b,Sarah,1 diff --git a/inputs/txns-other.csv b/inputs/txns-other.csv new file mode 100644 index 0000000..5e94e3f --- /dev/null +++ b/inputs/txns-other.csv @@ -0,0 +1,2 @@ +id,date,from,amount,to,description +25000,2024-12-15,Bob,280,ElecCo,first advance for january before shared account was active diff --git a/insert-percents.js b/insert-percents.js new file mode 100644 index 0000000..6f0b33b --- /dev/null +++ b/insert-percents.js @@ -0,0 +1,3 @@ +export default function(value, key, tree) { + +} diff --git a/outputFormat.js b/outputFormat.js new file mode 100644 index 0000000..aea05c3 --- /dev/null +++ b/outputFormat.js @@ -0,0 +1,8 @@ +export default async function(record) { + const rows = []; + const {account, amount_fixed, amount_var, period} = record; + rows.push({id: `${account}_valfixed_${period}`, date: '2025-09-01', from: 'shared', amount: amount_fixed, to: account, description: 'value transaction for fixed energy expenses'}) + rows.push({id: `${account}_valvar_${period}`, date: '2025-09-01', from: 'shared', amount: amount_var, to: account, description: 'value transaction for var energy expenses'}) + return rows; + +} diff --git a/outputFormatBank.js b/outputFormatBank.js new file mode 100644 index 0000000..354225a --- /dev/null +++ b/outputFormatBank.js @@ -0,0 +1,7 @@ +export default async function(record) { + const rows = []; + const {account, amount_fixed, amount_bank, period} = record; + rows.push({id: `${account}_valbank`, date: '2025-09-01', from: 'shared', amount: amount_bank, to: account, description: 'value transaction for fixed energy expenses'}) + return rows; +} + diff --git a/percent-divide.js b/percent-divide.js new file mode 100644 index 0000000..7d11d71 --- /dev/null +++ b/percent-divide.js @@ -0,0 +1,105 @@ +import { parse as parseCSV, stringify as stringifyCSV } from "jsr:@std/csv"; +import percentRound from './percent-round.js'; + +const [totalexpensefile, suppliertotals, indivusagefile] = Deno.args; + +const totexpinput = await Deno.readTextFile(totalexpensefile); +const supptotinput = await Deno.readTextFile(suppliertotals); +const supptots = await parseCSV(supptotinput, {skipFirstRow: true}); +const asntot = supptots.find(e => e.account === 'Bank').amount; +const ivbinput = await Deno.readTextFile(indivusagefile); +const txp = await parseCSV(totexpinput, {skipFirstRow: true}); +const ivb = await parseCSV(ivbinput, {skipFirstRow: true}); + +const vbByPeriod = Object.groupBy(ivb, i => i.period); +const totalMonths = ivb.reduce((a,b) => a + parseInt(b.months),0) +const vbRowsByUser = Object.groupBy(ivb, i => i.account); + + + +const entriesWithPercents = []; +//values is an array of objects with props 'months' and 'usages'. +for (const [key, values] of Object.entries(vbByPeriod)) { + const months = values.map(p => p.months); + const percents_mo = percentRound(months, 4); + const usages = values.map(p => p.usage); + const percents_us = percentRound(usages, 0); + for (let [index, value] of values.entries()) { + value.percent_mo = percents_mo[index]; + value.percent_us = percents_us[index]; + entriesWithPercents.push(value); + } + +} + +const userMonthsTotal = []; +for (const [key, value] of Object.entries(vbRowsByUser)) { + const monthssum = value.reduce((a,b) => a + parseInt(b.months),0); + userMonthsTotal.push({account: key, months: monthssum}); +} + +//oh, for ASN I need the total percentage for the whole dataset. +//create a single record for each user at the end and add it to the array created for var expenses. +const global_mo_perc = percentRound(userMonthsTotal.map(t => t.months), 2); +for (const [index, value] of userMonthsTotal.entries()) { + value.percent_mo = global_mo_perc[index]; +} + + +const asnExpSrc = userMonthsTotal.map((entry) => { + entry.amount = bankersRound(entry.percent_mo / 100 * asntot); + return entry; +}) + + +const totExpByPeriod = {}; +for (const p of txp) { + totExpByPeriod[p.period] = p; +} +const withElecExp = entriesWithPercents.map((entry) => { + entry.fixed_exp = bankersRound(entry.percent_mo / 100 * parseFloat(totExpByPeriod[entry.period].exp_fixed)); + entry.date = totExpByPeriod[entry.period].enddate; + entry.var_exp = bankersRound(entry.percent_us / 100 * parseFloat(totExpByPeriod[entry.period].exp_var)); + return entry; + +}) + + +// console.log(withElecExp); +// +// I guess I could check whether these amounts add up ... + +const output = []; + +for (const {period, account, fixed_exp, date, var_exp} of withElecExp) { + const omschrijving_var = 'value of variable expenses'; + const omschrijving_fixed = 'value of fixed expenses'; + + //fixed + output.push({id: `${period}_${account}_fixed`, datum: date, from: 'shared', amount: fixed_exp, to: account , omschrijving: omschrijving_fixed}); + //var + output.push({id: `${period}_${account}_var`, datum: date, from: 'shared', amount: var_exp, to: account, omschrijving: omschrijving_var}); + +} +//emhans: var: 75537, fixed 33746 + +for (const {account, amount} of asnExpSrc) { + output.push({id: `${account}_asn`, datum: '2025-12-31', from: 'shared', amount, to: account , omschrijving: 'value of banking costs ASN'}); +} + +console.log(stringifyCSV(output, {columns: ['id', 'datum', 'from', 'amount', 'to', 'omschrijving']} ).replace(/\n$/, "")); + +//from https://stackoverflow.com/a/49080858 +function bankersRound(n, d=2) { + var x = n * Math.pow(10, d); + var r = Math.round(x); + var br = Math.abs(x) % 1 === 0.5 ? (r % 2 === 0 ? r : r-1) : r; + return br / Math.pow(10, d); +} + +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; +} + diff --git a/percent-round.js b/percent-round.js new file mode 100644 index 0000000..ebb03ac --- /dev/null +++ b/percent-round.js @@ -0,0 +1,88 @@ +//source: https://github.com/super-ienien/percent-round +/*Copyright 2020 Vivien Anglesio + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +export default function percentRound(ipt, precision) { + if (!precision) { + precision = 0; + } + if (!Array.isArray(ipt)) { + throw new Error('percentRound input should be an Array'); + } + const iptPercents = ipt.slice(); + const length = ipt.length; + const out = new Array(length); + + let total = 0; + for (let i = length - 1; i >= 0; i--) { + if (typeof iptPercents[i] === "string") { + iptPercents[i] = Number.parseFloat(iptPercents[i]); + } + total += iptPercents[i] * 1; + } + if (isNaN(total)) { + throw new Error('percentRound invalid input'); + } + + if (total === 0) { + out.fill(0); + } else { + const powPrecision = Math.pow(10, precision); + const pow100 = 100 * powPrecision; + let check100 = 0; + for (let i = length - 1; i >= 0; i--) { + iptPercents[i] = 100 * iptPercents[i] / total;//hpf: insert bankersRound here? no, I think it's not necessary in this case! + check100 += out[i] = Math.round(iptPercents[i] * powPrecision); //or here? Or does this subsume the need for bankers round? + } + + if (check100 !== pow100) { + const totalDiff = (check100 - pow100) ; + const roundGrain = 1; + let grainCount = Math.abs(totalDiff); + const diffs = new Array(length); + + for (let i = 0; i < length; i++) { + diffs[i] = Math.abs(out[i] - iptPercents[i] * powPrecision); + } + + while (grainCount > 0) { + let idx = 0; + let maxDiff = diffs[0]; + for (let i = 1; i < length; i++) { + if (maxDiff < diffs[i]) { + // avoid negative result + if (check100 > pow100 && out[i] - roundGrain < 0) { + continue; + } + idx = i; + maxDiff = diffs[i]; + } + } + if (check100 > pow100) { + out[idx] -= roundGrain; + } else { + out[idx] += roundGrain; + } + diffs[idx] -= roundGrain; + grainCount--; + } + } + + if (powPrecision > 1) { + for (let i = 0; i < length; i++) { + out[i] = out[i] / powPrecision; + } + } + } + + return out; +} + +// For es import compatibility +percentRound.default = percentRound; diff --git a/percentRound.js b/percentRound.js new file mode 100644 index 0000000..d06e952 --- /dev/null +++ b/percentRound.js @@ -0,0 +1,8 @@ +//a function to use percent-round in origami +import percentRound from './percent-round.js'; + +export default function (key) { + return function(value) { + return percentRound(value.map(v => v[key]), 3); + } +} diff --git a/roundMonths.js b/roundMonths.js new file mode 100644 index 0000000..5fa4e25 --- /dev/null +++ b/roundMonths.js @@ -0,0 +1,2 @@ +import percentRound from './percentRound.js'; +export default percentRound('months'); diff --git a/roundMonthsArray.js b/roundMonthsArray.js new file mode 100644 index 0000000..157f277 --- /dev/null +++ b/roundMonthsArray.js @@ -0,0 +1,12 @@ +import percentRound from './percent-round.js'; +//use percentRound on an AsyncTree array: first convert it to a normal array. +export default async function (asyncVals) { + const records = []; + for (const key of await asyncVals.keys()) { + const record = await asyncVals.get(key); + records.push(record.months); + } + console.log(records); + const foobar = percentRound(records, 3) + console.log(foobar); +} diff --git a/roundUsage.js b/roundUsage.js new file mode 100644 index 0000000..9e73f3b --- /dev/null +++ b/roundUsage.js @@ -0,0 +1,3 @@ +import percentRound from './percentRound.js'; +export default percentRound('usage'); + diff --git a/subtree.ori b/subtree.ori new file mode 100644 index 0000000..0b9cfee --- /dev/null +++ b/subtree.ori @@ -0,0 +1,4 @@ +{ + foo = 'bar' + bananas = data.ori/boo + 'biz' +} diff --git a/withBankPercents.js b/withBankPercents.js new file mode 100644 index 0000000..79d3397 --- /dev/null +++ b/withBankPercents.js @@ -0,0 +1,11 @@ +//this is actually a more general function: can create the key name dynamically. +export default async function(values, key, percentarray) { + const percents = await percentarray.get(key); + // return {...value, percent_bank: bankp} + return values.map(async (value, index) => {; + value['percent_'+key.substring(0,key.length -1)] = await percents[index]; + return value; + + }) +} + diff --git a/withMonthPercents.js b/withMonthPercents.js new file mode 100644 index 0000000..c642bb1 --- /dev/null +++ b/withMonthPercents.js @@ -0,0 +1 @@ +//calculat diff --git a/withMonths.js b/withMonths.js new file mode 100644 index 0000000..f86f133 --- /dev/null +++ b/withMonths.js @@ -0,0 +1,5 @@ +//calculate total months for each user (out of entire data period) +export default async function (records, account) { + const months = records.reduce((a,b) => a + parseInt(b.months),0); + return {account, months}; +} diff --git a/withPercents.js b/withPercents.js new file mode 100644 index 0000000..1734a28 --- /dev/null +++ b/withPercents.js @@ -0,0 +1,9 @@ +export default async function(values, period, percent_usage, percent_months) { + const usagepercents = await percent_usage.get(period); + const monthpercents = await percent_months.get(period); + return values.map((value, index) => { + value.percent_months = monthpercents[index]; + value.percent_usage = usagepercents[index]; + return value; + }) +}