34 lines
944 B
Ruby
34 lines
944 B
Ruby
class Homie < ApplicationRecord
|
|
has_many :money_moves
|
|
has_many :work
|
|
|
|
belongs_to :gang
|
|
|
|
def self.info(importance, gang)
|
|
# TODO: This can be improved
|
|
|
|
cash_totals = Homie.all.joins(:money_moves).where(money_moves: { deleted_at: nil}).group(:id).order(:id).sum(:amount)
|
|
work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount)
|
|
|
|
Homie.where(['importance > ? and gang_id = ?', importance, gang]).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
|