-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.rb
93 lines (79 loc) · 2.68 KB
/
account.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require "json"
require_relative "modules/helpers"
require_relative "modules/categories"
require_relative "modules/sessions"
require_relative "modules/transactions"
class Account
attr_reader :name, :date, :categories, :view_month
include Services
include Helpers
def initialize(token)
@name = "expense"
@date = Date.new(2021, 12) # Actual Date for instruccions
@token = token
@categories = Categories.index(@token)
@view_month = show_categories
end
def toggle
@name = @name == "expense" ? "income" : "expense"
@view_month = show_categories
end
def change_week(change_value)
@date = @date.next_month if change_value == "next"
@date = @date.prev_month if change_value == "prev"
@view_month = show_categories
end
def total_amount_per_month(transactions)
total = 0
transactions.each do |transaction|
transaction_date = Date.parse(transaction[:date])
next unless transaction_date.month == @date.month && transaction_date.year == @date.year
total += transaction[:amount]
end
total
end
def show_categories
rows = []
@categories.each do |category|
next unless category[:transaction_type] == @name
total = total_amount_per_month(category[:transactions])
rows.push([category[:id], category[:name], total]) if !total.zero? || category[:transactions].empty?
end
rows.sort_by(&:first)
end
# ---- FORMS METHODS TO CATEGORY ----
# -- Form Create Category --
def create_category
new_category = Categories.create(@token, form_create_category)
@categories = categories.push(new_category)
@view_month = show_categories
end
def find_category(id)
@categories.find { |category| category[:id] == id }
end
# -- Form Update Category --
def update_category(id)
update_category = Categories.update(@token, id, form_create_category)
find_category(id).update(update_category)
@view_month = show_categories
end
# -- Form Delete Category --
def delete_category(id)
Categories.destroy(@token, id)
@categories = @categories.reject { |category| category[:id] == id }
@view_month = show_categories
end
# ---- END FORMS TO CATEGORY ----
# ---- FORMS FOR TRANSACTIONS ----
# -- Form Add-to Transaction --
def add_to_transaction(id)
parameters = form_add_to_transaction
create_transaction = Transactions.create(@token, id, parameters)
(@categories.find { |category| category[:id] == id })[:transactions].push(create_transaction)
@view_month = show_categories
end
# ---- END FORMS TO TRANSACTIONS ----
end
# user = Sessions.login({email: "test1@mail.com", password: "123456"})
# account = Account.new(user[:token])
# account.show_categories