-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
162 lines (128 loc) · 4.37 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
require 'sinatra'
require 'tilt/haml'
require 'data_mapper'
require './models'
SETTING = Setting.first # Could also do this with Sinatra filters before/do
PAYMENTS_FILTER_MIN = 1000
helpers do
def commify(amount)
amount.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse
end
def yesno(boolean)
boolean == true ? 'Yes' : 'No'
end
def nicedate(d)
d.strftime("%d %b %Y")
end
end
get '/' do
@directorates = Directorate.all( :order => ['name'] )
@payments_count = Payment.count
@suppliers_count = Supplier.count
@services_count = Service.count
haml :index
end
get '/directorates/:slug' do
@directorate = Directorate.first(:slug => params[:slug])
haml :directorate
end
get '/suppliers/:slug.csv' do
@supplier = Supplier.first(:slug => params[:slug])
headers "Content-Disposition" => "attachment;filename=supplier-#{@supplier.slug}.csv",
"Content-Type" => "application/octet-stream"
result = "Date,Ref.,URL,Directorate,Service,Amount ex. VAT\n"
for payment in @supplier.payments
result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.service.name},#{sprintf("%0.2f", payment.amount)}\n"
end
result
end
get '/suppliers/:slug' do
@supplier = Supplier.first(:slug => params[:slug])
@total = @supplier.payments.sum(:amount)
@count = @supplier.payments.size # Payment.count(:supplier_id => @supplier.id) ?
@avg = @supplier.payments.avg(:amount)
@max = @supplier.payments.max(:amount)
@min = @supplier.payments.min(:amount)
@d_start = @supplier.payments.min(:d)
@d_end = @supplier.payments.max(:d)
haml :supplier
end
get '/suppliers/?' do
@suppliers = Supplier.all( :order => ['name'] )
haml :suppliers
end
get '/services/:slug/payments.csv' do
@service = Service.first(:slug => params[:slug])
headers "Content-Disposition" => "attachment;filename=service-#{@service.slug}.csv",
"Content-Type" => "application/octet-stream"
result = "Date,Ref.,URL,Directorate,Supplier,Amount ex. VAT\n"
for payment in @service.payments
result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.supplier.name},#{sprintf("%0.2f", payment.amount)}\n"
end
result
end
# get '/services/:slug.json' do
# @service = Service.first(:slug => params[:slug])
# headers "Content-Type" => "application/json"
# @service.to_json(:relationships => { :payments => { :include => :all }, :directorate => { :include => :all } })
# end
get '/services/:slug' do
@service = Service.first(:slug => params[:slug])
@total = @service.payments.sum(:amount)
@count = @service.payments.size
@avg = @service.payments.avg(:amount)
@max = @service.payments.max(:amount)
@min = @service.payments.min(:amount)
@d_start = @service.payments.min(:d)
@d_end = @service.payments.max(:d)
@results = repository(:default).adapter.select("
SELECT s.name AS supplier_name, s.slug AS supplier_slug, SUM(p.amount) AS total
FROM payments p, suppliers s
WHERE p.supplier_id = s.id
AND p.service_id = #{@service.id}
GROUP BY s.name, s.slug
ORDER BY total DESC")
haml :service
end
get '/services/:slug/payments' do
@FILTER_VALUES = %w[ 500 1000 2500 5000 10000 20000 ]
@service = Service.first(:slug => params[:slug])
# payments_filter_min cookie persists user selection of filter value
unless @min = request.cookies["payments_filter_min"]
@min = PAYMENTS_FILTER_MIN
response.set_cookie(
"payments_filter_min",
{ :value => @min, :expires => Time.now + (60 * 24 * 60 * 60) }
) # 60 days
end
@payments = Payment.all(:service_id => @service.id, :amount.gte => @min, :order => [ 'd' ])
@total = @payments.sum(:amount)
haml :servicepayments
end
get '/services/:slug/paymentsdetail' do
@service = Service.first(:slug => params[:slug])
min = PAYMENTS_FILTER_MIN
if params[:min].to_i > 0
min = params[:min].to_i
end
@payments = Payment.all(:service_id => @service.id, :amount.gte => min, :order => [ 'd' ])
@total = @payments.sum(:amount)
haml :servicepaymentsdetail, :layout => false
end
get '/services/?' do
@services = Service.all( :order => ['name'] )
haml :services
end
get '/payments/:id' do
@payment = Payment.get(params[:id])
haml :payment
end
get '/error' do
haml :error
end
# get '/about' do
# haml :about
# end
not_found do
haml :not_found
end