2019-06-20 22:47:39 +02:00
|
|
|
class Homie < ApplicationRecord
|
2020-05-18 11:35:34 +02:00
|
|
|
has_many :money_moves
|
2020-09-19 04:46:17 +03:00
|
|
|
has_many :work
|
2020-10-06 06:26:23 +00:00
|
|
|
belongs_to :chip
|
2019-06-22 06:17:48 +02:00
|
|
|
|
|
|
|
|
def self.cash(importance)
|
2020-09-12 01:26:56 +03:00
|
|
|
totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount)
|
2019-06-22 06:17:48 +02:00
|
|
|
|
|
|
|
|
Homie.where(["importance > ?", importance]).map do |homie|
|
2020-09-12 01:26:56 +03:00
|
|
|
total = totals.fetch(homie.id, 0)
|
|
|
|
|
{ homie: homie, amount: total }
|
2019-06-22 06:17:48 +02:00
|
|
|
end
|
|
|
|
|
end
|
2020-09-17 15:00:55 +03:00
|
|
|
|
|
|
|
|
def settle
|
|
|
|
|
total = MoneyMove.where(homie_id: id).all.sum(: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 => e
|
|
|
|
|
false
|
|
|
|
|
end
|
2019-06-20 22:47:39 +02:00
|
|
|
end
|