diff --git a/decidim-budgets/app/commands/decidim/budgets/add_line_item.rb b/decidim-budgets/app/commands/decidim/budgets/add_line_item.rb index ab2f512ceb520..551734bf102f2 100644 --- a/decidim-budgets/app/commands/decidim/budgets/add_line_item.rb +++ b/decidim-budgets/app/commands/decidim/budgets/add_line_item.rb @@ -23,7 +23,7 @@ def initialize(current_order, project, current_user) # Returns nothing. def call transaction do - return broadcast(:invalid) if voting_not_enabled? || order.checked_out? + return broadcast(:invalid) if voting_not_enabled? || order.checked_out? || exceeds_budget? add_line_item broadcast(:ok, order) @@ -44,6 +44,10 @@ def add_line_item end end + def exceeds_budget? + order.allocation_for(project) + order.total > project.budget.total_budget + end + def voting_not_enabled? project.component.current_settings.votes != "enabled" end diff --git a/decidim-budgets/app/controllers/decidim/budgets/line_items_controller.rb b/decidim-budgets/app/controllers/decidim/budgets/line_items_controller.rb index 227612c0b8a4b..f81b01e2f3ed9 100644 --- a/decidim-budgets/app/controllers/decidim/budgets/line_items_controller.rb +++ b/decidim-budgets/app/controllers/decidim/budgets/line_items_controller.rb @@ -20,7 +20,7 @@ def create end on(:invalid) do - render nothing: true, status: :unprocessable_entity + format.js { render "update_budget", status: :unprocessable_entity } end end end @@ -35,7 +35,7 @@ def destroy end on(:invalid) do - render nothing: true, status: :unprocessable_entity + format.js { render "update_budget", status: :unprocessable_entity } end end end