diff --git a/app/controllers/budgets/results_controller.rb b/app/controllers/budgets/results_controller.rb
new file mode 100644
index 000000000..aa94eb68b
--- /dev/null
+++ b/app/controllers/budgets/results_controller.rb
@@ -0,0 +1,21 @@
+module Budgets
+ class ResultsController < ApplicationController
+
+ load_and_authorize_resource :budget
+
+ def show
+ @result = load_result
+ end
+
+ private
+
+ def load_result
+ Budget::Result.new(@budget, heading)
+ end
+
+ def heading
+ @budget.headings.find(params[:heading_id])
+ end
+
+ end
+end
\ No newline at end of file
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 0c23c0d22..6a040ec69 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -55,4 +55,8 @@ module ApplicationHelper
def content_block(name, locale)
SiteCustomization::ContentBlock.block_for(name, locale)
end
+
+ def format_price(number)
+ number_to_currency(number, precision: 0, locale: I18n.default_locale)
+ end
end
diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb
index 7d0aafb8a..56a50deec 100644
--- a/app/models/budget/investment.rb
+++ b/app/models/budget/investment.rb
@@ -33,6 +33,7 @@ class Budget
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
scope :sort_by_confidence_score, -> { reorder(confidence_score: :desc, id: :desc) }
+ scope :sort_by_ballots, -> { reorder(ballot_lines_count: :desc, id: :desc) }
scope :sort_by_price, -> { reorder(price: :desc, confidence_score: :desc, id: :desc) }
scope :sort_by_random, -> { reorder("RANDOM()") }
diff --git a/app/models/budget/result.rb b/app/models/budget/result.rb
new file mode 100644
index 000000000..f29bc72cc
--- /dev/null
+++ b/app/models/budget/result.rb
@@ -0,0 +1,55 @@
+class Budget
+ class Result
+
+ attr_accessor :budget, :heading, :money_spent, :current_investment
+
+ def initialize(budget, heading)
+ @budget = budget
+ @heading = heading
+ end
+
+ def calculate_winners
+ reset_winners
+ investments.each do |investment|
+ @current_investment = investment
+ if inside_budget?
+ set_winner
+ end
+ end
+ end
+
+ def investments
+ heading.investments.selected.sort_by_ballots
+ end
+
+ def inside_budget?
+ available_budget >= @current_investment.price
+ end
+
+ def available_budget
+ total_budget - money_spent
+ end
+
+ def total_budget
+ heading.price
+ end
+
+ def money_spent
+ @money_spent ||= 0
+ end
+
+ def reset_winners
+ investments.update_all(winner: false)
+ end
+
+ def set_winner
+ @money_spent += @current_investment.price
+ @current_investment.update(winner: true)
+ end
+
+ def winners
+ investments.where(winner: true)
+ end
+
+ end
+end
\ No newline at end of file
diff --git a/app/views/budgets/results/_results_table.html.erb b/app/views/budgets/results/_results_table.html.erb
new file mode 100644
index 000000000..cb3407f9c
--- /dev/null
+++ b/app/views/budgets/results/_results_table.html.erb
@@ -0,0 +1,76 @@
+
+
+ <%= heading.name %>
+
+
+
+ <%= link_to t("budgets.results.show_all_link"), "#",
+ class: "js-toggle-link button hollow margin-bottom",
+ data: {'toggle-selector' => '.js-discarded',
+ 'toggle-text' => t("budgets.results.hide_discarded_link")} %>
+
+
+
+
+
+ |
+ <%= t("budgets.results.spending_proposal") %>
+ |
+
+ <%= t("budgets.results.ballot_lines_count") %>
+ |
+
+ <%= t("budgets.results.price") %>
+ |
+ "
+ style="width:120px">
+ <%= format_price(heading.price) %>
+ |
+
+
+
+
+ <% amount_available = heading.price %>
+ <% @result.investments.each do |investment| %>
+ <% if investment.winner? %>
+
+ <% else %>
+
+ <% end %>
+ |
+ <% if investment.winner? %>
+
+
+ <%= t("budgets.results.accepted") %>
+
+
+ <% else %>
+
+
+ <%= t("budgets.results.discarded") %>
+
+
+ <% end %>
+ <%= link_to investment.title,
+ budget_investment_path(@budget, investment) %>
+ |
+
+ <%= investment.ballot_lines_count %>
+ |
+
+ <%= format_price investment.price %>
+ |
+
+ <%= format_price amount_available - investment.price %>
+ <% amount_available -= investment.price if investment.winner? %>
+ |
+
+ <% end %>
+
+
+
+
\ No newline at end of file
diff --git a/app/views/budgets/results/show.html.erb b/app/views/budgets/results/show.html.erb
new file mode 100644
index 000000000..dd83aebfb
--- /dev/null
+++ b/app/views/budgets/results/show.html.erb
@@ -0,0 +1,50 @@
+<% provide :title, t("budgets.results.page_title") %>
+
+
+
+
+ <%= link_to budgets_path, class: "left back" do %>
+
+ <%= t("shared.back") %>
+ <% end %>
+
+
+ <%= t("budgets.results.heading") %>
+
+
+
+
+
+
+
+
+ -
+
+ <%= t("shared.you_are_in") %>
+
+ <%= link_to t("budgets.results.results_link"), "#", class: "is-active" %>
+
+
+
+
+
+
+
+
+
+
+ <%= render 'results_table', heading: @result.heading %>
+
diff --git a/config/locales/budgets.en.yml b/config/locales/budgets.en.yml
index e7ee15b5a..eb786c66e 100644
--- a/config/locales/budgets.en.yml
+++ b/config/locales/budgets.en.yml
@@ -114,4 +114,19 @@ en:
unfeasible_title: Unfeasible investments
unfeasible: See unfeasible investments
unselected_title: Investments not selected for balloting phase
- unselected: See investments not selected for balloting phase
\ No newline at end of file
+ unselected: See investments not selected for balloting phase
+ results:
+ page_title: "Participatory budget | Results"
+ heading: "PARTICIPATORY BUDGET"
+ results_link: Results
+ stats_link: "Participation stats"
+ geozone_selection_title: "By district"
+ spending_proposal: Proposal title
+ ballot_lines_count: Times selected
+ hide_discarded_link: Hide discarded
+ show_all_link: Show all
+ price: Cost
+ amount_available: Available budget
+ incompatible: Incompatibles
+ accepted: "Accepted spending proposal: "
+ discarded: "Discarded spending proposal: "
\ No newline at end of file
diff --git a/config/locales/budgets.es.yml b/config/locales/budgets.es.yml
index 7db4f73e6..8c824e2e2 100644
--- a/config/locales/budgets.es.yml
+++ b/config/locales/budgets.es.yml
@@ -113,5 +113,25 @@ es:
phase: Fase actual
unfeasible_title: Propuestas inviables
unfeasible: Ver las propuestas inviables
+<<<<<<< f0d7acd087346edce149efed36e057eb4b792a4c
unselected_title: Propuestas no seleccionadas para la votación final
- unselected: Ver las propuestas no seleccionadas para la votación final
\ No newline at end of file
+ unselected: Ver las propuestas no seleccionadas para la votación final
+=======
+ unselected_title: Propuestas que no pasan a la votación final
+ unselected: Ver las propuestas que no pasan a la votación final
+ results:
+ page_title: "Presupuestos participativos | Resultados"
+ heading: "PRESUPUESTOS PARTICIPATIVOS"
+ results_link: Resultados
+ stats_link: "Estadísticas de participación"
+ geozone_selection_title: "Ámbito de actuación"
+ spending_proposal: Título
+ ballot_lines_count: Votos
+ hide_discarded_link: Ocultar descartadas
+ show_all_link: Mostrar todas
+ price: Coste
+ amount_available: Presupuesto disponible
+ incompatible: Incompatibles
+ accepted: "Propuesta de inversión aceptada: "
+ discarded: "Propuesta de inversión descartada: "
+>>>>>>> adds budget results
diff --git a/config/routes.rb b/config/routes.rb
index 5fd94eaaf..4007595db 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -77,6 +77,7 @@ Rails.application.routes.draw do
resource :ballot, only: :show, controller: "budgets/ballots" do
resources :lines, controller: "budgets/ballot/lines", only: [:create, :destroy]
end
+ resource :results, only: :show, controller: "budgets/results"
end
scope '/participatory_budget' do
diff --git a/db/migrate/20170519084239_add_winner_to_budget_investments.rb b/db/migrate/20170519084239_add_winner_to_budget_investments.rb
new file mode 100644
index 000000000..af6085468
--- /dev/null
+++ b/db/migrate/20170519084239_add_winner_to_budget_investments.rb
@@ -0,0 +1,5 @@
+class AddWinnerToBudgetInvestments < ActiveRecord::Migration
+ def change
+ add_column :budget_investments, :winner, :boolean, default: false
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index d3953f638..8a34df55d 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170517123042) do
+ActiveRecord::Schema.define(version: 20170519084239) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -147,6 +147,7 @@ ActiveRecord::Schema.define(version: 20170517123042) do
t.datetime "unfeasible_email_sent_at"
t.integer "ballot_lines_count", default: 0
t.integer "previous_heading_id"
+ t.boolean "winner", default: false
end
add_index "budget_investments", ["administrator_id"], name: "index_budget_investments_on_administrator_id", using: :btree