diff --git a/credger.cr b/credger.cr index 2895731..a560bc5 100644 --- a/credger.cr +++ b/credger.cr @@ -10,31 +10,22 @@ ledger = Ledger.new # Matches GET "http://host:port/" get "/" do |env| env.response.content_type = "text/html" - send_file env, "./public/index.html" + + send_file( env, "./public/index.html" ) +end + +get "/api/ledger/version" do |env| + env.response.content_type = "text" + + ledger.version end get "/api/ledger/accounts" do |env| env.response.content_type = "application/json" + ledger.accounts.to_json end -# get "/api/ledger/accounts/depth/:depth/?" do |env| -# env.response.content_type = "application/json" -# ledger.accounts( ).to_json -# end - -# get "/api/ledger/dates_salaries/?" do |env| -# env.response.content_type = "application/json" -# ledger.dates_salaries( "salaire" ).to_json -# end - -# get "/api/ledger/register/?" do |env| -# env.response.content_type = "application/json" -# { key: params[ :categories ], -# values: ledger.register( params[ :period ], params[ :categories ] ) } -# .to_json -# end - get "/api/ledger/balance" do |env| env.response.content_type = "application/json" @@ -47,27 +38,19 @@ get "/api/ledger/balance" do |env| .to_json end -# get "/api/ledger/cleared/?" do |env| -# env.response.content_type = "application/json" -# ledger.cleared().to_json -# end - -# get "/api/ledger/budget/?" do |env| -# env.response.content_type = "application/json" -# ledger.budget( params[ :period ], -# params[ :categories ] ).to_json -# end - get "/api/ledger/graph_values" do |env| env.response.content_type = "application/json" ledger.graph_values( env.params.query["period"], env.params.query["categories"].split(" ") ).to_json - #ledger.graph_values.to_json end -get "/api/ledger/version" do |env| - env.response.content_type = "text" - ledger.version +get "/api/ledger/register" do |env| + env.response.content_type = "application/json" + + { key: env.params.query[ "categories" ], + values: ledger.register( env.params.query[ "period" ], + env.params.query[ "categories" ].split(" ") ) } + .to_json end -Kemal.run 9292 +Kemal.run diff --git a/ledger.cr b/ledger.cr index 81f930f..3c17aa2 100644 --- a/ledger.cr +++ b/ledger.cr @@ -1,6 +1,7 @@ # encoding: utf-8 require "csv" +require "xml" # Crystal wrapper module for calling ledger class Ledger @@ -70,4 +71,18 @@ class Ledger result end + + def register( period : String = "", categories : Array(String) = ["Expenses"] ) : Array( NamedTuple( date: String, payee: String, account: String, amount: String, currency: String ) ) + period = period == "" ? "" : "-p '#{period}'" + + CSV + .parse( run( "--exchange '#{CURRENCY}' #{period}", "csv --no-revalued", categories.join(" ") ) ) + .map do |row| + { date: row[ 0 ], + payee: row[ 2 ], + account: row[ 3 ], + amount: row[ 5 ], + currency: row[ 4 ] } + end + end end diff --git a/public/index.html b/public/index.html index 46e84d0..5edb9bf 100644 --- a/public/index.html +++ b/public/index.html @@ -26,6 +26,7 @@ + diff --git a/public/ts/components/bucket.ts b/public/ts/components/bucket.ts index d6a3962..46249d9 100644 --- a/public/ts/components/bucket.ts +++ b/public/ts/components/bucket.ts @@ -30,17 +30,44 @@ app.component('bucket', duration: 500, labelThreshold: 0.01, labelSunbeamLayout: true, - labelsOutside: true + labelsOutside: true, + multibar: { + dispatch: { + elementClick: (event) => { + API.register(ctrl.period, event.data.account) + .then(function success(response) { + let format_transaction = (transaction) => { + return ` +
Date | Payee | Amount | +Total | ${event.data.amount} € | +