-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
321 lines (258 loc) · 7.95 KB
/
app.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
require 'haml'
require 'logger'
require 'sinatra'
require 'sequel'
require 'sinatra/js'
require 'json'
require 'sinatra/contrib'
set :haml, :format => :html5
db = Sequel.connect('postgres://localhost/budget')
db.extension(:pagination)
db.loggers << Logger.new($stdout)
class Item < Sequel::Model; end
class Receipt < Sequel::Model; end
class Sequel::Dataset
def to_json
naked.all.to_json
end
end
defaults = {:funding => 'General', :expense => 'Personal' }
receipt_columns = {
:date=> {:type => 'date', :display => 'show'},
:amount=> {:type => 'number', :display => 'show'},
:name=> {:type => 'text', :display => 'show'},
:description=> {:type => 'text', :display => 'hide'},
:item=> {:type => 'select', :display => 'show'},
:method=> {:type => 'text', :display => 'show'},
:funding=> {:type => 'text', :display => 'hide'},
:expense=> {:type => 'text', :display => 'hide'},
:envelope=> {:type => 'text', :display => 'hide'},
:roommate=> {:type => 'text', :display => 'hide'},
:notes=> {:type => 'text', :display => 'hide'},
:tag=> {:type => 'text', :display => 'hide'}
}
item_columns = {
:item => {:type => 'text', :display => 'show'},
:category => {:type => 'text', :display => 'show'},
:recurring => {:type => 'text', :display => 'show'},
:order => {:type => 'text', :display => 'show'},
}
items_set = Item.order(Sequel.asc(:item)).distinct(:item)
#items_sql = "select distinct item from receipts order by item asc"
receipts_set = Receipt.order(Sequel.desc(:date))
categories_set = Item.distinct(:category, :order).grep(:item, '1%').order(Sequel.desc(:order)).map(:category)
income_cats_set = Item.distinct(:category, :order).grep(:item, '2%').order(Sequel.desc(:order)).map(:category)
methods_set = Receipt.order(Sequel.asc(:method)).distinct(:method).select(:method).map(:method)
year_months_sql = "select distinct to_char(date, 'YYYY-MM') as year_month
from receipts
where date >= ?
order by year_month asc"
income_sql = "select round(sum(amount), 2) as amount,
to_char(date, 'YYYY-MM') as month
from receipts join items
on receipts.item = items.item
where items.category = 'Primary income'
and date >= ?
and funding='General'
group by items.category, month
order by month asc;"
costs_sql = "select round(sum(amount), 2) as amount,
to_char(date, 'YYYY-MM') as month
from receipts join items
on receipts.item = items.item
where items.category = ?
and date >= ?
and funding = 'General'
group by items.category, month, items.order
order by items.order desc limit 12"
subcategory_costs_sql = "select round(sum(amount), 2) as amount,
to_char(date, 'YYYY-MM') as month
where item = ?
and date >= ?
and funding = 'General'
group by item, month
order by item desc"
earliest_date = Receipt.select(:date).order(:date).first[:date]
get '/receipts' do
@id = 'new_receipt'
@action = '/receipts/new'
@columns = receipt_columns.dup
@receipts= receipts_set.limit(100)
@items = items_set.map(:item)
@methods = methods_set
haml :receipts
end
get '/receipts/new' do
@id = 'new_receipt'
@action = '/receipts/new'
@columns = receipt_columns.dup
@items = items_set.map(:item)
@methods = methods_set
haml :new_receipt
end
get '/items' do
@columns = item_columns.dup
@items = items_set.map(:item)
@action = '/items/new'
haml :items
end
get '/items/new' do
@columns = item_columns.dup
@items = items_set.map(:item)
@action = '/items/new'
haml :form
end
route :get, :post, '/' do
cost_series_array = []
drilldown_array = []
if params[:date]
puts params[:date]
@date = Date.strptime(params[:date], "%Y-%m")
puts @date
else
@date = get_one_year_ago()
puts @date
end
@all_year_months = db.fetch(year_months_sql, earliest_date).map(:year_month)
@year_months = db.fetch(year_months_sql, @date).map(:year_month)
@cats = categories_set
@cats.each do |cat|
cost_series = Hash.new
data = db.fetch(costs_sql, cat, @date)#.map{|x| x[:amount].to_f}
puts data.all
data_array = @year_months[0..11].dup
cost_series_data = set_series_data(data, data_array)
cost_series['name'] = cat
cost_series['data'] = cost_series_data
cost_series['stack'] = 'cost'
#cost_series['drilldown'] = cat
cost_series_array.push(cost_series)
end
@costs_series = cost_series_array.to_json
haml :index
end
# API routes
get '/api/receipts/:id' do
receipt = Receipt[:id]
return receipt.values.to_json
end
delete '/api/receipts/:id' do
puts :id
receipt = Receipt[params[:id]]
if receipt
receipt.delete
end
end
patch '/api/receipts/:id' do
puts :id
insert_params = {}
request_payload = JSON.parse request.body.read
puts request_payload
request_payload.delete('submit')
request_payload.delete('id')
record = Receipt[params[:id]]
record.update(request_payload)
record.save_changes
return record.values.to_json
end
get '/api/income/:date' do
if params[:date]
date = Date.strptime(params[:date], "%Y-%m")
puts date
else
date = get_one_year_ago()
end
@year_months = db.fetch(year_months_sql, date).map(:year_month)
puts @year_months
income_series = Hash.new
data = db.fetch(income_sql, date)#.map{|x| x[:amount].to_f}
puts data.all
data_array = @year_months[0..11].dup
puts data_array
income_series_data = set_series_data(data, data_array)
puts income_series_data
income_series['name'] = 'Primary income'
income_series['data'] = income_series_data
income_series['stack'] = 'income'
#cost_series['drilldown'] = cat
return income_series.to_json
end
get '/api/receipts' do
allowed_filters = ["item", "method", "name"]
page = params[:page].blank?? 1 : Integer(params[:page])
puts page
filters = params[:filters].blank?? Hash.new : params[:filters].select { |i| allowed_filters.include?(i) }
filters = Hash[filters.map{|(k,v)| [k.to_sym,v]}]
puts filters
results = receipts_set.paginate(page, 50)
filters.each do |key, value|
results = results.where("#{key} = ?", value)
end
return results.to_json
end
def get_page_range(page)
at_a_time = 50
return page * at_a_time
end
get '/api/receipts/new' do
@id = 'new_receipt'
@action = '/receipts/new'
@columns = columns.dup
@items = items_set.map(:item)
@methods = methods_set
haml :new_receipt
end
post '/api/receipts' do
insert_params = {}
request_payload = JSON.parse request.body.read
puts request_payload
request_payload.delete('submit')
request_payload.delete('new')
already_exists = check_for_duplicates(request_payload)
if not already_exists
new = Receipt.create(request_payload)
puts new
redirect '/receipts'
else
puts 'already exists in db'
redirect '/receipts'
end
end
get '/api/methods' do
@methods = methods_set
return @methods.to_json
end
get '/api/items' do
@items = Item.order(:item)
return @items.to_json
end
get '/api/items/:id' do
@item = Item[:id]
return @item.values.to_json
end
def set_series_data(data, data_array)
data.each do |x|
index = data_array.index(x[:month])
if index
data_array[index] = x[:amount].to_f
end
data_array.each do |x|
if @year_months.include? x
x = 0
end
end
end
puts data_array
return data_array
end
def get_one_year_ago
return Date.strptime((Date.today.year - 1).to_s << '-' << (Date.today.month + 1).to_s, '%Y-%m')
end
def check_for_duplicates(data)
#data.each do |x|
# puts x
#end
existing = Receipt.where(:date => data[:date], :amount => data[:amount], :item => data[:item])
puts existing.all.length
return (existing.all.length > 0)
end