Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
create ledger entries for repeating transactions
#!/bin/env ruby
# (C) 2015 Thomas R. Koll, <info@ananasblau.com>
# licensed under WTFPL
#
# This script generates entries for the ledger format.
# Think of it as an envelope generator
require 'highline'
require 'date'
require 'active_support/core_ext/date/calculations'
require 'active_support/core_ext/numeric/time'
def add_repeat(date, r)
case(r)
when 'daily'
return date.next_day
when 'weekly'
return date + 7
when 'monthly'
return date.next_month
when 'yearly'
return date.next_year
end
interval = r.match(/^([.\d]+)/)
if interval.nil?
puts "Bad interval"
return false
end
interval = interval[1].to_f
if r.match /days/
return date + interval
elsif r.match /months/
interval.to_i.times {|i| date = date.next_month }
return date
else
puts "Bad interval"
return
end
end
cli = HighLine.new
date_format = "%Y/%m/%d"
last_date = Date.today
transaction = {}
transaction[:start_date] = cli.ask("Date [%s]: " % last_date.strftime(date_format), Date) {|q| q.default = last_date }
transaction[:description] = cli.ask("Description (use # for the counter): ")
transaction[:account1] = cli.ask("Account 1: ")
transaction[:amount1] = cli.ask("Amount 1: ")
transaction[:account2] = cli.ask("Account 2: ")
negative_amount = "-" + transaction[:amount1]
transaction[:amount2] = cli.ask("Amount 2 [%s]: " % negative_amount) {|q| q.default = negative_amount}
transaction[:repeat] = cli.ask("Repeat (yearly, monthly, weekly, daily, N days, N months): ", lambda {|s| s.strip } ) do |q|
q.validate = /([\d.]+ (days|months)|yearly|monthly|weekly|daily)/
end
transaction[:times] = cli.ask("How often: ", Integer)
date = transaction[:start_date]
transaction[:times].times.each do |i|
puts "%s %s" % [date.strftime(date_format), transaction[:description].gsub("#", (i + 1).to_s)]
puts "\t%s %s" % [transaction[:account1], transaction[:amount1]]
puts "\t%s %s" % [transaction[:account2], transaction[:amount2]]
puts ""
date = add_repeat(date, transaction[:repeat])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.