Files
old-roraccounting/app/models/homie.rb

31 lines
842 B
Ruby
Raw Normal View History

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
belongs_to :chip
2019-06-22 06:17:48 +02:00
2020-10-06 00:17:21 +03:00
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)
2019-06-22 06:17:48 +02:00
Homie.where(["importance > ?", importance]).map do |homie|
2020-10-06 00:17:21 +03:00
cash_total = cash_totals.fetch(homie.id, 0)
work_total = work_totals.fetch(homie.id, 0)
{ homie: homie, amount: cash_total, work: work_total }
2019-06-22 06:17:48 +02:00
end
end
2020-09-17 15:00:55 +03:00
2020-10-05 14:25:42 +03:00
def settle(amount)
total = amount.nil? ? MoneyMove.where(homie_id: id).all.sum(:amount) : amount
2020-09-17 15:00:55 +03:00
all_money_moves = MoneyMove.where(homie_id: id).all
transaction do
all_money_moves.destroy_all
MoneyMove.create_settle_record(id, total)
end
true
2020-10-05 14:25:42 +03:00
rescue StandardError
2020-09-17 15:00:55 +03:00
false
end
2019-06-20 22:47:39 +02:00
end