MorningPeak/app/models/client.rb
Arthur Poulet 7838150f59 remove contact.last_contact, fix contact.view_at
all this modifications re done in contact
- rename the column last_contact to view_at
- fix view
- add a link in /contact/index to update the view date
2015-07-18 16:12:19 +02:00

50 lines
961 B
Ruby

class Client < ActiveRecord::Base
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