2014-07-04 09:50:18 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
|
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'
|
|
|
|
@file = '~/org/comptes.ledger'
|
|
|
|
|
|
|
|
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-07-27 22:04:00 +02:00
|
|
|
def register( category, options='' )
|
2014-07-28 16:47:33 +02:00
|
|
|
run( "#{options} --collapse --amount-data --exchange '#{CURRENCY}'", 'register', "#{category}" )
|
2014-07-04 09:50:18 +02:00
|
|
|
.split( "\n" )
|
2014-07-14 16:18:41 +02:00
|
|
|
.map do |line|
|
2014-07-04 09:50:18 +02:00
|
|
|
line_array = line.split
|
|
|
|
|
2014-07-30 20:48:52 +02:00
|
|
|
[ Time.new( line_array[ 0 ] ).to_i,
|
|
|
|
line_array[ 1 ].to_f ]
|
2014-07-27 22:04:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def monthly_register( category )
|
|
|
|
register category, "--monthly"
|
|
|
|
end
|
|
|
|
|
|
|
|
def yearly_register( category )
|
|
|
|
register category, "--yearly"
|
2014-07-04 09:50:18 +02:00
|
|
|
end
|
|
|
|
|
2014-07-30 20:48:52 +02:00
|
|
|
def balance( cleared=false, depth=nil, period=nil, categories='' )
|
|
|
|
period = period.nil? ? '' : "-p '#{period}'"
|
2014-07-27 22:04:26 +02:00
|
|
|
depth = depth.nil? ? '' : "--depth #{depth}"
|
|
|
|
operation = cleared ? 'cleared' : 'balance'
|
2014-07-30 20:48:52 +02:00
|
|
|
run( "--flat --no-total --exchange '#{CURRENCY}' #{period} #{depth}", operation, categories )
|
2014-07-28 16:47:33 +02:00
|
|
|
.split( "\n" )
|
|
|
|
.map do |line|
|
|
|
|
line_array = line.split( "#{CURRENCY}" )
|
|
|
|
|
|
|
|
{ account: line_array[ 1 ].strip,
|
|
|
|
amount: line_array[ 0 ].tr( SEPARATOR, '.' ).to_f }
|
|
|
|
end
|
2014-07-04 09:50:18 +02:00
|
|
|
end
|
|
|
|
end
|