From ada376412a9c841d1d11ca359995c5a0e4ae71ba Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Mon, 3 Nov 2014 17:33:15 +0100 Subject: [PATCH] birth of the Assets page --- app.rb | 8 ++++++++ lib/ledger.rb | 13 +++++++++++++ public/app/index.html | 1 + public/app/js/controllers/AssetsCtrl.js | 12 ++++++++++++ public/app/js/controllers/NavbarCtrl.js | 2 +- public/app/js/services/API.js | 18 ++++++++++-------- public/app/js/state.js | 23 ++++++++++++++--------- public/app/js/templates/assets.tpl.html | 5 +++++ 8 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 public/app/js/controllers/AssetsCtrl.js create mode 100644 public/app/js/templates/assets.tpl.html diff --git a/app.rb b/app.rb index 0a86b26a..f6b6e55f 100644 --- a/app.rb +++ b/app.rb @@ -42,6 +42,14 @@ class LedgerRbApp < Sinatra::Base .to_json end + get '/api/ledger/monthly_register/?' do + param :categories, String, required: true + + { key: params[ :categories ], + values: Ledger.monthly_register( params[ :categories ] ) } + .to_json + end + get '/api/ledger/balance/?' do param :depth, Integer, default: false param :period, String, default: nil diff --git a/lib/ledger.rb b/lib/ledger.rb index b7a21e3a..72e267a9 100644 --- a/lib/ledger.rb +++ b/lib/ledger.rb @@ -36,6 +36,18 @@ module Ledger .uniq end + def monthly_register( categories = '' ) + CSV.parse( run( '--monthly', 'csv', categories ) ) + .map do + |row| + { date: row[ 0 ], + payee: row[ 2 ], + account: row[ 3 ], + amount: row[ 5 ], + currency: row[ 4 ] } + end + end + def register( period = nil, categories = '' ) period = period.nil? ? '' : "-p '#{period}'" @@ -54,6 +66,7 @@ module Ledger period = period.nil? ? '' : "-p '#{period}'" depth = depth.nil? ? '' : "--depth #{depth}" operation = cleared ? 'cleared' : 'balance' + run( "--flat --no-total --exchange '#{CURRENCY}' #{period} #{depth}", operation, categories ) .split( "\n" ) .map do |line| diff --git a/public/app/index.html b/public/app/index.html index 7a5769e9..3d49783f 100644 --- a/public/app/index.html +++ b/public/app/index.html @@ -59,6 +59,7 @@ + diff --git a/public/app/js/controllers/AssetsCtrl.js b/public/app/js/controllers/AssetsCtrl.js new file mode 100644 index 00000000..9abfc669 --- /dev/null +++ b/public/app/js/controllers/AssetsCtrl.js @@ -0,0 +1,12 @@ +app.controller( 'AssetsCtrl', + [ '$scope', 'API', + function ( $scope, API ) { + API.accounts().then( function( response ) { + $scope.assets = _(response.data).groupBy( 0 ).Assets.map( function( account ) { return account.join( ':' ); } ); + + API.monthly_register( { categories: 'Assets' } ) + .then( function ( response ) { + $scope.monthly_assets = response.data; + } ); + } ); + } ] ); diff --git a/public/app/js/controllers/NavbarCtrl.js b/public/app/js/controllers/NavbarCtrl.js index 2f101316..20ed7a7f 100644 --- a/public/app/js/controllers/NavbarCtrl.js +++ b/public/app/js/controllers/NavbarCtrl.js @@ -1,5 +1,5 @@ app.controller( 'NavbarCtrl', [ '$scope', function( $scope ) { - $scope.items = [ 'Balance' ]; + $scope.items = [ 'Assets', 'Balance' ]; } ] ); diff --git a/public/app/js/services/API.js b/public/app/js/services/API.js index 120dc254..ea6b5184 100644 --- a/public/app/js/services/API.js +++ b/public/app/js/services/API.js @@ -3,19 +3,21 @@ app.service( 'API', function( $http ) { this.balance = function( params ) { return $http.get( '/api/ledger/balance', { - params: { - period: params.period, - categories: params.categories - } + params: { period: params.period, + categories: params.categories } + } ); + }; + + this.monthly_register = function( params ) { + return $http.get( '/api/ledger/monthly_register', { + params: { categories: params.categories } } ); }; this.register = function( params ) { return $http.get( '/api/ledger/register', { - params: { - period: params.period, - categories: params.categories - } + params: { period: params.period, + categories: params.categories } } ); }; diff --git a/public/app/js/state.js b/public/app/js/state.js index 0c13dd62..fededa58 100644 --- a/public/app/js/state.js +++ b/public/app/js/state.js @@ -4,28 +4,33 @@ app.config( [ '$stateProvider', '$urlRouterProvider', $urlRouterProvider.when( '', '/balance' ); $stateProvider - .state( 'app.balance', { - url: '/balance', - templateUrl: 'js/templates/balance.tpl.html', - controller: 'BalanceCtrl' - } ) .state( 'app', { url: '', controller: 'AppCtrl', views: { 'navbar': { - templateUrl: 'js/templates/navbar.tpl.html', - controller: 'NavbarCtrl' + controller: 'NavbarCtrl', + templateUrl: 'js/templates/navbar.tpl.html' }, 'main': { templateUrl: 'js/templates/main.tpl.html' } } } ) + .state( 'app.balance', { + url: '/balance', + controller: 'BalanceCtrl', + templateUrl: 'js/templates/balance.tpl.html' + } ) + .state( 'app.assets', { + url: '/assets', + controller: 'AssetsCtrl', + templateUrl: 'js/templates/assets.tpl.html' + } ) .state( '404', { url: '/404', - templateUrl: 'js/templates/404.tpl.html', - controller: 'AppCtrl' + controller: 'AppCtrl', + templateUrl: 'js/templates/404.tpl.html' } ); } diff --git a/public/app/js/templates/assets.tpl.html b/public/app/js/templates/assets.tpl.html new file mode 100644 index 00000000..b2611916 --- /dev/null +++ b/public/app/js/templates/assets.tpl.html @@ -0,0 +1,5 @@ +
+ {{assets}} +
+ {{monthly_assets}} +