An open source, stand-alone, customisable public spending data web app.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

139 行
3.7 KiB

  1. require 'rubygems'
  2. require 'sinatra'
  3. require 'sinatra-helpers/haml/partials'
  4. require 'haml'
  5. require 'lib/models'
  6. PAYMENTS_FILTER_MIN = 1000
  7. helpers do
  8. def commify(amount)
  9. amount.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse
  10. end
  11. end
  12. get '/' do
  13. @directorates = Directorate.all( :order => ['name'] )
  14. @payments_count = Payment.count
  15. @suppliers_count = Supplier.count
  16. @services_count = Service.count
  17. haml :home
  18. end
  19. get '/directorates/:slug' do
  20. @directorate = Directorate.first(:slug => params[:slug])
  21. haml :directorate
  22. end
  23. get '/suppliers/:slug.csv' do
  24. @supplier = Supplier.first(:slug => params[:slug])
  25. headers "Content-Disposition" => "attachment;filename=supplier-#{@supplier.slug}.csv",
  26. "Content-Type" => "application/octet-stream"
  27. result = "Date,Ref.,URL,Directorate,Service,Amount ex. VAT\n"
  28. for payment in @supplier.payments
  29. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.service.name},#{sprintf("%0.2f", payment.amount)}\n"
  30. end
  31. result
  32. end
  33. get '/suppliers/:slug' do
  34. @supplier = Supplier.first(:slug => params[:slug])
  35. @total = @supplier.payments.sum(:amount)
  36. @count = @supplier.payments.size # Payment.count(:supplier_id => @supplier.id) ?
  37. @avg = @supplier.payments.avg(:amount)
  38. @max = @supplier.payments.max(:amount)
  39. @min = @supplier.payments.min(:amount)
  40. haml :supplier
  41. end
  42. get '/suppliers/?' do
  43. @suppliers = Supplier.all( :order => ['name'] )
  44. haml :suppliers
  45. end
  46. get '/services/:slug/payments.csv' do
  47. @service = Service.first(:slug => params[:slug])
  48. headers "Content-Disposition" => "attachment;filename=service-#{@service.slug}.csv",
  49. "Content-Type" => "application/octet-stream"
  50. result = "Date,Ref.,URL,Directorate,Supplier,Amount ex. VAT\n"
  51. for payment in @service.payments
  52. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.supplier.name},#{sprintf("%0.2f", payment.amount)}\n"
  53. end
  54. result
  55. end
  56. get '/services/:slug.json' do
  57. @service = Service.first(:slug => params[:slug])
  58. headers "Content-Type" => "application/json"
  59. @service.to_json(:relationships => { :payments => { :include => :all }, :directorate => { :include => :all } })
  60. end
  61. get '/services/:slug' do
  62. @service = Service.first(:slug => params[:slug])
  63. @total = @service.payments.sum(:amount)
  64. @count = @service.payments.size
  65. @avg = @service.payments.avg(:amount)
  66. @max = @service.payments.max(:amount)
  67. @min = @service.payments.min(:amount)
  68. @results = repository(:default).adapter.query("
  69. SELECT s.name AS supplier_name, s.slug AS supplier_slug, SUM(p.amount) AS total
  70. FROM payments p, suppliers s
  71. WHERE p.supplier_id = s.id
  72. AND p.service_id = #{@service.id}
  73. GROUP BY s.name, s.slug
  74. ORDER BY total DESC")
  75. haml :service
  76. end
  77. get '/services/:slug/payments' do
  78. @service = Service.first(:slug => params[:slug])
  79. @payments = Payment.all(:service_id => @service.id, :amount.gte => PAYMENTS_FILTER_MIN, :order => [ 'd' ])
  80. @total = @payments.sum(:amount)
  81. haml :servicepayments
  82. end
  83. get '/services/:slug/paymentsdetail' do
  84. @service = Service.first(:slug => params[:slug])
  85. min = PAYMENTS_FILTER_MIN
  86. if params[:min].to_i > 0
  87. min = params[:min].to_i
  88. end
  89. @payments = Payment.all(:service_id => @service.id, :amount.gte => min, :order => [ 'd' ])
  90. @total = @payments.sum(:amount)
  91. haml :servicepaymentsdetail, :layout => false
  92. end
  93. get '/services/?' do
  94. @services = Service.all( :order => ['name'] )
  95. haml :services
  96. end
  97. get '/payments/:id' do
  98. @payment = Payment.get(params[:id])
  99. haml :payment
  100. end
  101. get '/error' do
  102. haml :error
  103. end
  104. get '/about' do
  105. haml :about
  106. end
  107. not_found do
  108. haml :not_found
  109. end