From 8a7a0cc7203574240e03d621190afb58c4512829 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sun, 20 Nov 2022 16:21:28 -0500 Subject: [PATCH] Calculate burden by member --- app/models/expense.rb | 4 ++++ app/models/income.rb | 4 ++++ app/models/member.rb | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/app/models/expense.rb b/app/models/expense.rb index 1925fe6..2a1104e 100644 --- a/app/models/expense.rb +++ b/app/models/expense.rb @@ -10,4 +10,8 @@ class Expense < ApplicationRecord def monthly payment * PERIOD_OCCURENCES[period.to_sym] / 12 end + + def self.total + Expense.sum(&:payment) + end end diff --git a/app/models/income.rb b/app/models/income.rb index 2053c6c..f4cf3c4 100644 --- a/app/models/income.rb +++ b/app/models/income.rb @@ -1,3 +1,7 @@ class Income < ApplicationRecord belongs_to :member + + def self.total + Income.where(included: true).sum(&:amount) + end end diff --git a/app/models/member.rb b/app/models/member.rb index 8b44e8f..cfa4796 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -1,2 +1,23 @@ class Member < ApplicationRecord + has_many :incomes + + def income + incomes.sum(&:amount) + end + + def included_income + incomes.where(included: true).sum(&:amount) + end + + def others_included_income + Income.where(included: true) - included_income + end + + def burden_percent + (Income.total - others_included_income) / Income.total + end + + def burden_amount + burden_percent * Expense.total + end end