MorningPeak/app/models/client.rb
Arthur Poulet 2a7f23d2c5 add acts_as_commentable gem
- add acts_as_commentable to gemfile
- update gemfile.lock
- generate migration comment
- update schema
- add acts_as_commentable to client, contact, bill
2015-07-25 21:33:44 +02:00

51 lines
983 B
Ruby

class Client < ActiveRecord::Base
acts_as_commentable
has_many :contacts
has_many :bills
has_many :tickets, through: :user
belongs_to :contact
belongs_to :user
delegate :name, to: :contact, allow_nil: true, prefix: true
def last_contact
contacts.pluck(:view_at).max || "never"
end
def bills_retard
bills.where("due_date <= '#{Date.today}'").where(paid: false)
end
def bills_advanced
bills.where("due_date > '#{Date.today}'").where(paid: true)
end
def bills_next
bills.where("due_date > '#{Date.today}'").where(paid: false)
end
def bills_old
bills.where("due_date <= '#{Date.today}'").where(paid: true)
end
def financial_level
weight = weight()
retard = retard()
th = weight.to_f / (weight + retard)
th.nan? ? nil : (th * 100).round
end
def weight
bills_old.pluck(:amount).sum
end
def retard
bills_retard.pluck(:amount).sum
end
def next
bills_next.pluck(:amount).sum
end
end