Class: Comcalc::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/comcalc/cli.rb

Constant Summary collapse

DB_CON =
Comcalc::Database.new

Instance Method Summary collapse

Instance Method Details

#add_billObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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_personObject



118
119
120
121
122
123
124
125
126
# 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_balanceObject



105
106
107
108
109
110
111
112
113
114
115
116
# 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_statisticsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



61
62
63
64
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/comcalc/cli.rb', line 9

def menu
  puts '1. Add bill'
  puts '2. Show balance'
  puts '3. Update bill'
  puts '4. Statistics'
  puts '5. Add new person'
  print "Your choice: "
  choice = $stdin.gets.chomp.to_i
  case choice
    when 1
      add_bill
    when 2
      DB_CON.query_spec('first_name', 'last_name', 'balance', 'person')
    when 3
      puts 'update'
      #TODO:
    when 4
      puts create_statistics
    when 5
      add_new_person
    else
      puts "You gave me #{choice} -- I have no idea what to do with that."
  end
end

#refactor_try(column, table, info, table2, search_criterium) ⇒ Object

Dont know if it makes sense :>



55
56
57
58
59
# 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