2014-07-04 09:50:18 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2014-08-01 22:25:36 +02:00
|
|
|
require 'csv'
|
|
|
|
|
2014-07-14 16:18:41 +02:00
|
|
|
# Ruby wrapper module for calling ledger
|
2014-07-04 09:50:18 +02:00
|
|
|
module Ledger
|
|
|
|
module_function
|
|
|
|
|
|
|
|
@binary = 'ledger'
|
2014-07-31 17:14:08 +02:00
|
|
|
@file = ENV[ 'LEDGER_FILE' ]
|
2014-07-04 09:50:18 +02:00
|
|
|
|
|
|
|
def run( options, command = '', command_parameters = '' )
|
2014-07-14 16:18:22 +02:00
|
|
|
`#{@binary} -f #{@file} #{options} #{command} #{command_parameters}`
|
2014-07-04 09:50:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
run '--version'
|
|
|
|
end
|
|
|
|
|
|
|
|
def accounts( depth = 9999 )
|
|
|
|
run( '', 'accounts' )
|
|
|
|
.split( "\n" )
|
2014-07-14 16:18:41 +02:00
|
|
|
.map do |a|
|
|
|
|
a.split( ':' )
|
|
|
|
.each_slice( depth )
|
|
|
|
.to_a.first
|
|
|
|
end.uniq
|
2014-07-04 09:50:18 +02:00
|
|
|
end
|
|
|
|
|
2014-09-30 12:15:37 +02:00
|
|
|
def dates_salaries( category = 'salaire' )
|
|
|
|
CSV.parse( run( '', 'csv', category ) )
|
|
|
|
.map do
|
|
|
|
|row|
|
|
|
|
Date.parse row[ 0 ]
|
|
|
|
end
|
|
|
|
.uniq
|
|
|
|
end
|
|
|
|
|
2014-08-01 22:25:36 +02:00
|
|
|
def register( period = nil, categories = '' )
|
|
|
|
period = period.nil? ? '' : "-p '#{period}'"
|
2014-07-04 09:50:18 +02:00
|
|
|
|
2014-08-01 22:25:36 +02:00
|
|
|
CSV.parse( run( "--exchange '#{CURRENCY}' #{period}", 'csv', categories ) )
|
|
|
|
.map do
|
|
|
|
|row|
|
|
|
|
{ date: row[ 0 ],
|
|
|
|
payee: row[ 2 ],
|
|
|
|
account: row[ 3 ],
|
|
|
|
amount: row[ 5 ],
|
2014-08-01 22:32:40 +02:00
|
|
|
currency: row[ 4 ] }
|
2014-07-27 22:04:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-20 12:36:41 +02:00
|
|
|
def cleared
|
2014-10-20 13:06:03 +02:00
|
|
|
run( "--flat --no-total --exchange '#{CURRENCY}'", 'cleared', 'Assets Equity' )
|
|
|
|
.split( "\n" )
|
|
|
|
.map do |row|
|
|
|
|
fields = row.match( /\s*(\S+ €)\s*(\S+ €)\s*(\S+)\s*(\S+)/ )
|
|
|
|
{ account: fields[ 4 ],
|
|
|
|
amount: { cleared: fields[ 2 ],
|
|
|
|
all: fields[ 1 ] } } unless fields.nil?
|
2014-10-20 12:36:41 +02:00
|
|
|
end
|
|
|
|
end
|
2014-07-04 09:50:18 +02:00
|
|
|
end
|