class Comcalc::Cli

Constants

DB_CON

Public Instance Methods

add_bill() click to toggle source
# File lib/comcalc/cli.rb, line 67
def add_bill
  print 'Value: '
  value = $stdin.gets.chomp.to_f
  print 'Date (format: DD.MM.YYYY or today): '
  date = $stdin.gets.chomp
  date=='today' || date == '' ? date = Time.now.to_s: date = Time.parse(date).to_s
  puts date
  DB_CON.get_market
  print 'Market: '
  market_id = $stdin.gets.chomp.to_i
  DB_CON.get_all_persons
  print 'Who payed: '
  person_id = $stdin.gets.chomp.to_i
  print 'Note: '
  note = $stdin.gets.chomp
  DB_CON.insert_into_bill(nil, value, date, market_id, person_id, note)
  insert_involved_ppl(DB_CON.get_last_bill_id, person_id)
  DB_CON.update_market_count(market_id)
  # Trigger when bill gets added increment market_count identified by market_id of bill
end
add_new_person() click to toggle source
# File lib/comcalc/cli.rb, line 118
def add_new_person
  print 'Frist Name: '
  first_name = $stdin.gets.chomp
  print 'Last Name: '
  last_name = $stdin.gets.chomp
  date = Time.now.to_s
  DB_CON.add_new_person(nil, first_name, last_name, 0, date)
  puts 'Success!'
end
calculate_new_balance() click to toggle source
# File lib/comcalc/cli.rb, line 105
def calculate_new_balance
  involved_ppl = DB_CON.get_involved_ppl(DB_CON.get_last_bill_id)
  payer = DB_CON.get_person_id_from_bill(DB_CON.get_last_bill_id).flatten.first
  involved_ppl_without_payer = involved_ppl.flatten - [payer]
  change_balance_for_payer =((DB_CON.get_amount_from_bill(DB_CON.get_last_bill_id).flatten.first - (DB_CON.get_amount_from_bill(DB_CON.get_last_bill_id).flatten.first / involved_ppl.size)))
  change_balance_for_involved = (DB_CON.get_amount_from_bill(DB_CON.get_last_bill_id).flatten.first / involved_ppl.size)
  DB_CON.update_balance_payer(payer, change_balance_for_payer)
  involved_ppl_without_payer.each do |x|
    DB_CON.update_balance_involved(x, change_balance_for_involved)
  end
  puts "Success."
end
create_statistics() click to toggle source
# File lib/comcalc/cli.rb, line 34
def create_statistics
  puts "Bills this month: #{DB_CON.monthly_data('value', 'bill').flatten.size}"
  puts "Each bill was an average of: #{ DB_CON.monthly_data('value', 'bill').flatten.inject(:+) / DB_CON.monthly_data('value','bill').size} "
  puts "Money spent monthly overall: #{DB_CON.monthly_data('value','bill').flatten.inject(:+)}"
  most_frequent = get_most_frequent(DB_CON.monthly_data('person_id','bill').flatten)
  person_name = DB_CON.refactored_get_single_value('first_name', 'person', 'id', most_frequent)

  most_frequent = get_most_frequent(DB_CON.monthly_data('market_id','bill').flatten)
  market_name = DB_CON.refactored_get_single_value('name', 'market', 'id', most_frequent)
  nums = DB_CON.monthly_data('person_id','bill').flatten
  num_hash = Hash[nums.uniq.map { |num| [num, nums.count(num)] }]
  puts "money spent for each person per month:"
  num_hash.keys.each do |ids|
    puts "#{DB_CON.get_name_by_id(ids).flatten.first}: #{DB_CON.monthly_data_person('value', 'bill', ids).flatten.inject(:+)}"
  end

  puts "most frequent market: #{market_name.flatten.first}"
  puts "most frequent buyer: #{person_name.flatten.first}"
end
get_most_frequent(nums) click to toggle source
# File lib/comcalc/cli.rb, line 61
def get_most_frequent(nums)
  num_hash = Hash[nums.uniq.map { |num| [num, nums.count(num)] }]
  nums.sort_by { |num| num_hash[num] }.last
end
insert_involved_ppl(bill_id, payer_id) click to toggle source
# File lib/comcalc/cli.rb, line 89
def insert_involved_ppl(bill_id, payer_id)
  person_involved = [payer_id]
  while @done != 'y'
    puts 'Who was involved?: '
    DB_CON.get_all_persons
    person_involved.push($stdin.gets.chomp.to_i)
    # do not allow to set a person more than once (foreign key?)
    print 'done?: (y/n)'
    @done = $stdin.gets.chomp
  end
  person_involved.each do |ids|
    DB_CON.involved_in_bill(nil, ids, bill_id)
  end
  calculate_new_balance
end
menu() click to toggle source
refactor_try(column, table, info, table2, search_criterium) click to toggle source

Dont know if it makes sense :>

# File lib/comcalc/cli.rb, line 55
def refactor_try(column, table, info, table2, search_criterium)
  most_frequent = get_most_frequent(nums = DB_CON.monthly_data("#{column}","#{table}").flatten)
  DB_CON.refactored_get_single_value(info, table2, search_criterium, most_frequent)

end