Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Fix budget page 500 when expected income is nil (#4199) (#4240)

TheRodzzbf3fd1b179c8
diff --git a/app/models/assistant/function/update_budget.rb b/app/models/assistant/function/update_budget.rb
index 77d07ee525..b86fd4d9bc 100644
--- a/app/models/assistant/function/update_budget.rb
+++ b/app/models/assistant/function/update_budget.rb
@@ -109,6 +109,10 @@ class Assistant::Function::UpdateBudget < Assistant::Function
Budget.transaction do
budget = Budget.find_or_bootstrap(family, start_date: start_date, user: user)
+ if attrs.key?(:budgeted_spending) && !attrs.key?(:expected_income) && budget.expected_income.nil?
+ attrs[:expected_income] = 0
+ end
+
budget.update!(attrs) if attrs.any?
changes = category_changes.map do |change|
diff --git a/app/models/budget.rb b/app/models/budget.rb
index 3a477d99ea..609728c904 100644
--- a/app/models/budget.rb
+++ b/app/models/budget.rb
@@ -20,7 +20,7 @@ class Budget < ApplicationRecord
validates :start_date, :end_date, uniqueness: { scope: [ :family_id, :user_id ] }
monetize :available_cash, :earmarked_for_goals, :free_cash
- monetize :budgeted_spending, :expected_income, :allocated_spending,
+ monetize :budgeted_spending, :allocated_spending,
:actual_spending, :available_to_spend, :available_to_allocate,
:estimated_spending, :estimated_income, :actual_income, :remaining_expected_income,
:total_rolled_over
@@ -481,18 +481,25 @@ class Budget < ApplicationRecord
income_statement.income_totals(period: self.period).total
end
+ def expected_income_money
+ return nil if monetizable_currency.nil?
+
+ Money.new(expected_income || 0, monetizable_currency)
+ end
+
def actual_income_percent
- return 0 unless expected_income > 0
+ return 0 unless expected_income && expected_income > 0
(actual_income / expected_income.to_f) * 100
end
def remaining_expected_income
- expected_income - actual_income
+ (expected_income || 0) - actual_income
end
def surplus_percent
return 0 unless remaining_expected_income.negative?
+ return 100 unless expected_income && expected_income > 0
remaining_expected_income.abs / expected_income.to_f * 100
end
diff --git a/test/controllers/budgets_controller_test.rb b/test/controllers/budgets_controller_test.rb
index dc1894ce6b..8ea76fadde 100644
--- a/test/controllers/budgets_controller_test.rb
+++ b/test/controllers/budgets_controller_test.rb
@@ -19,6 +19,14 @@ class BudgetsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
+ test "show renders when the budget has spending but no expected income" do
+ budgets(:one).update!(budgeted_spending: 4000, expected_income: nil)
+
+ get budget_url(Budget.date_to_param(Date.current))
+
+ assert_response :success
+ end
+
test "show reconciles parent overage without marking shared children over budget" do
budget = budgets(:one)
parent_category = Category.create!(
diff --git a/test/models/assistant/function/update_budget_test.rb b/test/models/assistant/function/update_budget_test.rb
index 8eb985084c..aafd466419 100644
--- a/test/models/assistant/function/update_budget_test.rb
+++ b/test/models/assistant/function/update_budget_test.rb
@@ -88,6 +88,25 @@ class Assistant::Function::UpdateBudgetTest < ActiveSupport::TestCase
assert_equal true, result[:success]
created = @family.budgets.find_by(start_date: target)
assert_equal 1000, created.budgeted_spending
+ assert_equal 0, created.expected_income
+ end
+
+ test "setting spending alone fills a missing expected income with zero" do
+ @budget.update!(expected_income: nil)
+
+ result = @function.call("budgeted_spending" => 4000)
+
+ assert_equal true, result[:success]
+ @budget.reload
+ assert_equal 4000, @budget.budgeted_spending
+ assert_equal 0, @budget.expected_income
+ end
+
+ test "setting spending alone keeps an existing expected income" do
+ result = @function.call("budgeted_spending" => 4000)
+
+ assert_equal true, result[:success]
+ assert_equal 7000, @budget.reload.expected_income
end
test "rejects a category outside the family and rolls back totals from the same call" do
diff --git a/test/models/budget_test.rb b/test/models/budget_test.rb
index cd8fc98ece..11cad89ec8 100644
--- a/test/models/budget_test.rb
+++ b/test/models/budget_test.rb
@@ -567,4 +567,35 @@ class BudgetTest < ActiveSupport::TestCase
assert_equal 0, budget.days_remaining
end
end
+
+ test "income methods treat a nil expected_income as zero" do
+ budget = budgets(:one)
+ budget.update!(expected_income: nil)
+ budget.stubs(:actual_income).returns(BigDecimal("1200"))
+
+ assert budget.initialized?
+ assert_equal Money.new(0, budget.currency), budget.expected_income_money
+ assert_equal 0, budget.actual_income_percent
+ assert_equal(-1200, budget.remaining_expected_income)
+ assert_equal Money.new(-1200, budget.currency), budget.remaining_expected_income_money
+ assert_equal 100, budget.surplus_percent
+ end
+
+ test "surplus_percent is 100 when nothing was expected and income came in" do
+ budget = budgets(:one)
+ budget.update!(expected_income: 0)
+ budget.stubs(:actual_income).returns(BigDecimal("50"))
+
+ assert_equal 100, budget.surplus_percent
+ end
+
+ test "income methods with no expected_income and no income show nothing earned" do
+ budget = budgets(:one)
+ budget.update!(expected_income: nil)
+ budget.stubs(:actual_income).returns(BigDecimal("0"))
+
+ assert_equal 0, budget.actual_income_percent
+ assert_equal 0, budget.remaining_expected_income
+ assert_equal 0, budget.surplus_percent
+ end
end