31 lines
842 B
Ruby
31 lines
842 B
Ruby
class Homie < ApplicationRecord
|
|
has_many :money_moves
|
|
has_many :work
|
|
belongs_to :chip
|
|
|
|
def self.info(importance)
|
|
cash_totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount)
|
|
work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount)
|
|
|
|
Homie.where(["importance > ?", importance]).map do |homie|
|
|
cash_total = cash_totals.fetch(homie.id, 0)
|
|
work_total = work_totals.fetch(homie.id, 0)
|
|
{ homie: homie, amount: cash_total, work: work_total }
|
|
end
|
|
end
|
|
|
|
def settle(amount)
|
|
total = amount.nil? ? MoneyMove.where(homie_id: id).all.sum(:amount) : amount
|
|
all_money_moves = MoneyMove.where(homie_id: id).all
|
|
|
|
transaction do
|
|
all_money_moves.destroy_all
|
|
|
|
MoneyMove.create_settle_record(id, total)
|
|
end
|
|
true
|
|
rescue StandardError
|
|
false
|
|
end
|
|
end
|