module Vfwcash

Constants

LibPath

Define constants to define where your are and location of config file

PWD
VERSION

Public Class Methods

config() click to toggle source
# File lib/vfwcash.rb, line 11
def self.config
  if ENV['VFWCASHCONFIG'].present?
    config = YAML.load_file(ENV['VFWCASHCONFIG'])
  else
    config = YAML.load_file("#{PWD}/config/vfwcash.yml")
    
  end
  ENV['VFWCASHDATABASE'] = config[:database]
  return config
end
install(options) click to toggle source
# File lib/vfwcash.rb, line 81
def self.install(options)
  wd = PWD
  ok = true
  if options['dir'].present?
    unless Dir.exist?(wd+options['dir'])
      Dir.mkdir(wd+'/'+options['dir'])
      puts "Created a new directory in #{wd}"
      ok = false
      Dir.chdir(wd+'/'+options['dir'])
      wd = Dir.pwd
    end
  end
  unless Dir.exist?(wd+'/config')
    Dir.mkdir(wd+'/config')
    puts "Created config directory in #{wd}"
    ok = false
  end
  unless Dir.exist?(wd+'/pdf')
    Dir.mkdir(wd+'/pdf')
    puts "Created pdf directory in #{wd}"
    ok = false
  end
  unless File.exist?(wd+"/config/"+"vfwcash.yml")
    FileUtils.cp((LibPath+"/templates/vfwcash.yml"),(wd+"/config/"+"vfwcash.yml"))
    puts "Created vfwcash.yml file in config directory, must be edited for your post."
    ok = false
  end
   if options['db']
    unless File.exist?(wd+"/config/"+"vfwcash.gnucash")
      FileUtils.cp((LibPath+"/templates/vfwcash.gnucash"),(wd+"/config/"+"vfwcash.gnucash"))
      puts "Created vfwcash.gnucash db in config directory."
      ok = false
    end
  end

  puts "No installation required" if ok
  puts "Installation complete. Edit your vfwcash.yml file to set your post information." if !ok
end
money(int) click to toggle source
# File lib/vfwcash.rb, line 66
def self.money(int)
  dollars = int / 100
  cents = (int % 100) / 100.0
  amt = dollars + cents
  set_zero = sprintf('%.2f',amt) # now have a string to 2 decimals
  set_zero.gsub(/(\d)(?=(\d{3})+(?!\d))/, "\\1,") # add commas
end
set_date(date=nil) click to toggle source
# File lib/vfwcash.rb, line 34
def self.set_date(date=nil)
  if date.nil? || date == ""
    date = Date.today
  elsif date.class == Date
  else
    # has to be string
    if date.length == 6
      date += '01'
      date = Date.parse(date)
    else
      date = Date.parse(date)
    end
  end
  date
end
str_date_range(from,to) click to toggle source
# File lib/vfwcash.rb, line 50
def self.str_date_range(from,to)
  # used for
  db = Tran.last.post_date.include?('-')
  from = Vfwcash.set_date(from)
  to = Vfwcash.set_date(to)
  from = from.to_datetime.beginning_of_day
  to = to.to_datetime.end_of_day
  
  if db
    return (from.to_s(:db))..(to.to_s(:db))
  else
    return (from.to_s(:number))..(to.to_s(:number))
  end
end
transaction_range() click to toggle source
# File lib/vfwcash.rb, line 27
def self.transaction_range
  t = Tran.order(:post_date)
  first = Date.parse(t.first.post_date).beginning_of_month
  last = Date.parse(t.last.post_date).end_of_month
  first..last
end
valid_root?() click to toggle source
# File lib/vfwcash.rb, line 74
def self.valid_root?
  unless Dir.exist?(PWD+'/config') && Dir.exist?(PWD+'/pdf') && File.exist?(PWD+"/config/"+"vfwcash.yml")
    puts "Error: vfwcash must be run from a diectory containing valid  configuration files"
    exit(0)
  end
end
yyyymm(date=nil) click to toggle source
# File lib/vfwcash.rb, line 22
def self.yyyymm(date=nil)
  date = Vfwcash.set_date(date)
  yyyymm = date.strftime('%Y%m') # "#{date.year}#{date.month.to_s.rjust(2,'0')}"
end