diff --git a/app.rb b/app.rb
index 1286244..66279de 100644
--- a/app.rb
+++ b/app.rb
@@ -4,6 +4,12 @@ require 'sinatra-helpers/haml/partials'
require 'haml'
require 'lib/models'
+helpers do
+ def commify(amount)
+ amount.to_s.gsub(/(\d)(?=(\d{3})+$)/,'\1,')
+ end
+end
+
get '/' do
@directorates = Directorate.all( :order => ['name'] )
@results = repository(:default).adapter.query("SELECT COUNT(*) FROM payments")
@@ -67,6 +73,12 @@ get '/services/:slug.csv' do
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)
diff --git a/lib/models.rb b/lib/models.rb
index b3671f9..6a5f2d9 100644
--- a/lib/models.rb
+++ b/lib/models.rb
@@ -3,6 +3,7 @@ require 'dm-core'
require 'dm-validations'
require 'dm-timestamps'
require 'dm-aggregates'
+require 'dm-serializer'
SITE_URL = 'http://armchairauditor.co.uk/'
diff --git a/views/payment.haml b/views/payment.haml
index 5222522..9bfeb6e 100644
--- a/views/payment.haml
+++ b/views/payment.haml
@@ -22,7 +22,7 @@
= @payment.supplier.name
%tr
%td Amount £
- %td= sprintf("%0.2f", @payment.amount)
+ %td= commify(sprintf("%0.2f", @payment.amount))
.clear
= haml_partial "comment_header"
diff --git a/views/service.haml b/views/service.haml
index 513adcf..0b2d8c7 100644
--- a/views/service.haml
+++ b/views/service.haml
@@ -34,11 +34,11 @@
%th.right Average
%tr
- %td.right= @count
- %td.right= sprintf("£%0d", @total)
- %td.right= sprintf("£%0d", @max)
- %td.right= sprintf("£%0d", @min)
- %td.right= sprintf("£%0d", @avg)
+ %td.right= commify(@count)
+ %td.right= commify(sprintf("£%0d", @total))
+ %td.right= commify(sprintf("£%0d", @max))
+ %td.right= commify(sprintf("£%0d", @min))
+ %td.right= commify(sprintf("£%0d", @avg))
%p
@@ -54,7 +54,7 @@
%td
%a{ :href => "/suppliers/#{row['supplier_slug']}" }
= row['supplier_name']
- %td.right= sprintf("%0d", row['total'])
+ %td.right= commify(sprintf("%0d", row['total']))
%td.right= sprintf("%0.1f", row['total'] / @total * 100)
%p
@@ -77,14 +77,14 @@
%td
%a{ :href => '/suppliers/' + payment.supplier.slug }
= payment.supplier.name
- %td.right= sprintf("%0d", payment.amount)
+ %td.right= commify(sprintf("%0d", payment.amount))
%tr
%td
%td
%td
%strong TOTAL
- %td.right= sprintf("%0d", @total)
+ %td.right= commify(sprintf("%0d", @total))
.clear
diff --git a/views/supplier.haml b/views/supplier.haml
index 24361e5..ecf052e 100644
--- a/views/supplier.haml
+++ b/views/supplier.haml
@@ -29,11 +29,11 @@
%th.right Average £
%tr
- %td.right= @count
- %td.right= sprintf("%0d", @total)
- %td.right= sprintf("%0d", @max)
- %td.right= sprintf("%0d", @min)
- %td.right= sprintf("%0d", @avg)
+ %td.right= commify(@count)
+ %td.right= commify(sprintf("%0d", @total))
+ %td.right= commify(sprintf("%0d", @max))
+ %td.right= commify(sprintf("%0d", @min))
+ %td.right= commify(sprintf("%0d", @avg))
%h3 Payments from the Council
@@ -57,7 +57,7 @@
%td
%a{ :href => '/services/' + payment.service.slug }
= payment.service.name
- %td.right= sprintf("%0d", payment.amount)
+ %td.right= commify(sprintf("%0d", payment.amount))
%tr
%td
@@ -65,7 +65,7 @@
%td
%td
%strong TOTAL
- %td.right= sprintf("%0d", @total)
+ %td.right= commify(sprintf("%0d", @total))
.clear