module AsciiMixins

Public Instance Methods

caterers_string(project, join = ", ") click to toggle source
# File lib/ascii_invoicer/mixins.rb, line 181
def caterers_string project, join = ", "
  data = project.data
  data[:hours][:caterers].map{
    |name, hours|
    if (hours.class == Fixnum || hours.class == Float ) and hours > 0
    "#{name} (#{hours})"
    end
  }.keep_if{|e|e}
  .join join if data[:hours][:caterers]
end
check_project(path) click to toggle source
# File lib/ascii_invoicer/mixins.rb, line 297
def check_project(path)
  project = InvoiceProject.new $SETTINGS
  project.open path
  unless project.validate(:offer)
    puts "\nWARNING: the file you just edited contains errors! (#{project.errors})"
    unless no? "would you like to edit it again? [y|N]"
      edit_files path
    end
  end
end
color_from_date(date) click to toggle source

TODO turn color_from_date(date) into a loopuk into $SETTINGS

# File lib/ascii_invoicer/mixins.rb, line 60
def color_from_date(date)
  diff = date - Date.today
  return (rand * 256**3).to_i.to_s(16) if Date.today.day == 1 and Date.today.month == 4 #april fools
  return :magenta                      if diff < -28
  return :cyan                         if diff < 0
  return [:yellow,:bright]             if diff == 0
  return :red                          if diff < 7
  return :yellow                       if diff < 14
  return [:green]
end
create_cal_file(projects) click to toggle source
# File lib/ascii_invoicer/mixins.rb, line 155
def create_cal_file(projects)
  cal = Icalendar::Calendar.new
  projects.each_index do |i|
    project = projects[i]
    events = project.data[:event][:calendaritems]
    if events
      events.each { |event| cal.add_event event}
    else
      $logger.warn "Calendar can't be parsed. (#{project.data[:name]})", :file
    end
  end

  cal_file_path = File.join(FileUtils.pwd, $SETTINGS.calendar_file)
  cal_file = File.open(cal_file_path, ?w)
  cal_file.write cal.to_ical
  puts "created #{cal_file_path}"
end
display_all(project, choice, show_errors = true) click to toggle source
# File lib/ascii_invoicer/mixins.rb, line 250
def display_all project, choice, show_errors = true
  raise "choice must be either :invoice or :offer" unless choice == :invoice or choice == :offer
  data = project.data

  table = Textboxes.new
  table.style[:border] = true
  table.title = "#{choice}:" + "\"#{data[:event][:name]}\"".rjust(25)
  table.add_row [nil, "name", "amount","price", "cost"]
  table.set_alignments :r, :l, :r, :r, :r

  i = 0
  data[:products].each {|product|
    amount = product.amount choice
    price = product.price
    cost  = product.cost choice
    table.add_row [i+=1,product.name, amount, price, cost]
  }
  table.add_row [i+1,"service", "#{data[:hours][:time]}h",  data[:hours][:salary], data[:hours][:total]] if project.data.get_path('hours/time').to_i> 0

  separator = table.column_widths.map{|w| ?=*w}
  separator[0] = nil
  table.add_row separator

  table.add_row  [nil,"Kosten",nil,nil,"#{data[choice][:costs]}"]
  data[:productsbytax].each {|tax,products|
    tpv = 0.to_euro # tax per value
    tax = (tax.rationalize * 100).to_f
    products.each{|p|
      tpv += p.hash[:tax_offer]   if choice == :offer
      tpv += p.hash[:tax_invoice] if choice == :invoice
    }
    table.add_row  [nil, "MWST #{tax}%",nil,nil,"#{tpv}"]
  }
  table.add_row   [nil,  "Final", nil, nil, "#{data[choice][:final]}"]

  if show_errors
    table.footer = "Errors: #{project.errors.length} (#{ project.errors.join ',' })" if project.errors.length >0
  end

  return table
end
display_products(project, choice = :offer, standalone = true) click to toggle source
# File lib/ascii_invoicer/mixins.rb, line 233
def display_products project, choice = :offer, standalone = true
  table.style[:border] = standalone
  table.title = "#{choice}:" + "\"#{project.data[:event][:name]}\"".rjust(25) if standalone
  table.add_row ["#", "name", "price", "cost"]
  table.set_alignments :r, :l, :r, :r
  project.data[:products].each {|product|
    amount = product.amount choice
    price = product.price
    cost  = product.cost choice
    table.add_row [amount, product.name, price, cost]
  }
  table.add_row ["#{project.data[:hours][:time]}h", "service" , project.data[:hours][:salary], project.data[:hours][:total]] if project.data.get_path('hours/time').to_i> 0
  table.add_row [nil, caterers_string(project)]

  return table
end
display_products_csv(project) click to toggle source
# File lib/ascii_invoicer/mixins.rb, line 292
def display_products_csv project
  puts [['name', 'price', 'amount', 'sold', 'tax_value'].to_csv(col_sep:?;)]+
  project.data[:products].map{|p| p.to_csv(col_sep:?;)}
end
edit_files(paths, editor = $SETTINGS.editor) click to toggle source

hand path to editor

# File lib/ascii_invoicer/mixins.rb, line 321
def edit_files(paths, editor = $SETTINGS.editor)
  paths = [paths] if paths.class == String
  paths.select! {|path| path}
  if paths.empty?
    $logger.error "no paths to open"
    return false
  end
  paths.map!{|path| "\"#{path}\"" }
  paths = paths.join ' '
  editor = $SETTINGS.editor unless editor
  $logger.info "Opening #{paths} in #{editor}"
  pid = spawn "#{editor} #{paths}"
  Process.wait pid
end
open_file(path) click to toggle source

hand path to default programm

# File lib/ascii_invoicer/mixins.rb, line 309
def open_file path
  unless path.class == String and File.exists? path
    $logger.error "Cannot open #{path}", :both
    return false
  end
  opener = $SETTINGS.opener
  $logger.info "Opening #{path} in #{opener}"
  pid = spawn "#{opener} \"#{path}\""
  Process.wait pid
end
print_project_list(projects, hash = {}) click to toggle source
print_project_list_csv(projects) click to toggle source

takes an array of invoices (@plumber.working_projects)

print_project_list_paths(projects) click to toggle source
print_project_list_yaml(projects) click to toggle source

takes an array of invoices (@plumber.working_projects)

print_row_simple(project,hash) click to toggle source
print_row_verbose(project, hash) click to toggle source
render_project(project, choice) click to toggle source

Use Option parser or leave it if only one argument is given

# File lib/ascii_invoicer/mixins.rb, line 50
def render_project project, choice
  project.validate choice
  if project.valid_for[choice]
    project.create_tex choice, options[:check]
  else
    $logger.error "#{project.name} is not ready for creating an #{choice.to_s}! #{project.data[:valid]} #{project.errors if project.errors.length > 0}"
  end
end
wages_string(project, join = ", ") click to toggle source
# File lib/ascii_invoicer/mixins.rb, line 192
def wages_string project, join = ", "
    data = project.data
    salary = data[:hours][:salary]
    data[:hours][:caterers].map{|name, hours| "#{name} (#{salary* hours})" if hours > 0 }.join join if data[:hours][:caterers]
end