MorningPeak/app/models/client.rb
Arthur Poulet a769142d2f update tickets
following #13

- tickets have no more master
- tickets are commentables
2016-01-31 02:22:11 +01:00

52 lines
984 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