MorningPeak/app/models/client.rb
2016-02-01 02:37:12 +01:00

52 lines
1013 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.where('view_at IS NOT NULL').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