Class: CabooseRets::Agent

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose_rets/agent.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assign_to_new_user(user) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/caboose_rets/agent.rb', line 64

def self.assign_to_new_user(user)
  if user && user.site && user.site.use_rets
    rc = CabooseRets::RetsConfig.where(:site_id => user.site_id).first
    agents                 = rc ? CabooseRets::Agent.joins(:meta).where(:office_mls_id => rc.office_mls, rets_agents_meta: {hide: FALSE, accepts_listings: true}).order(:sort_order).all : []
    last_agent_mls_id      = Caboose::Setting.where(:name => "latest_user_agent", :site_id => user.site_id).exists? ? Caboose::Setting.where(:name => "latest_user_agent", :site_id => user.site_id).first : Caboose::Setting.create(:name => "latest_user_agent", :site_id => user.site_id, :value => agents.first.mls_id)
    last_agent             = CabooseRets::Agent.joins(:meta).where(:mls_id => last_agent_mls_id.value, rets_agents_meta: {hide: FALSE, accepts_listings: true}).first
    agent_index            = last_agent.present? ? agents.find_index(last_agent) : 0
    agent_index + 1 < agents.count ? agent_index += 1 : agent_index = 0
    agent = agents[agent_index]   
    if agent.mls_id == '118593505' # Steven Deal shouldn't be assigned
      agent_index + 1 < agents.count ? agent_index += 1 : agent_index = 0
      agent = agents[agent_index]
    end
    Caboose.log("Assigning agent #{agent.mls_id} to user #{user.id}") if Rails.env.development?
    user.rets_agent_mls_id = agent.mls_id
    last_agent_mls_id.value = agent.mls_id
    user.save
    Rails.env.production? ? agent.delay(:queue => 'rets').send_new_user_email(user) : agent.send_new_user_email(user)
    last_agent_mls_id.save
    role = Caboose::Role.where(:name => 'RETS Visitor', :site_id => user.site_id).exists? ? Caboose::Role.where(:name => 'RETS Visitor', :site_id => user.site_id).first : Caboose::Role.create(:name => 'RETS Visitor', :site_id => user.site_id)
    Caboose::RoleMembership.create(:user_id => user.id, :role_id => role.id)
  end
end

.refresh_from_mls(agent_number) ⇒ Object



38
39
40
41
# File 'app/models/caboose_rets/agent.rb', line 38

def self.refresh_from_mls(agent_number)
  CabooseRets::RetsImporter.import('Member', "(MemberMlsId=#{self.mls_id})")
  CabooseRets::RetsImporter.import_agent(self.mls_id)          
end

Instance Method Details

#fix_nameObject



23
24
25
26
27
28
29
30
31
# File 'app/models/caboose_rets/agent.rb', line 23

def fix_name
  return if self.first_name.nil?
  self.first_name = self.first_name.split(' ').collect{ |str| str.downcase.capitalize }.join(' ')
  return if self.last_name.nil?          
  self.last_name  = self.last_name.split(' ').collect{ |str| str.downcase.capitalize }.join(' ')    
  if self.last_name.starts_with?('Mc')
    self.last_name[2] = self.last_name[2].upcase
  end
end

#imageObject



9
10
11
12
# File 'app/models/caboose_rets/agent.rb', line 9

def image
  return nil if self.meta.nil?
  return self.meta.image
end

#image_url(style) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'app/models/caboose_rets/agent.rb', line 118

def image_url(style)                
  if CabooseRets::use_hosted_images == true
    return "#{CabooseRets::agents_base_url}/#{self.image_location}"
    self.verify_meta_exists      
    return self.meta.image_location
  end
  return "" if self.image.nil?
  return self.image.url(style)           
end

#officeObject

end



19
20
21
# File 'app/models/caboose_rets/agent.rb', line 19

def office
  CabooseRets::Office.where(:lo_mls_id => self.mls_id).first
end

#parse(data) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/caboose_rets/agent.rb', line 88

def parse(data)
#    self.agent_number                 = data['AgentNumber']
  self.cell_phone                   = data['MemberMobilePhone']
  self.direct_work_phone            = data['MemberDirectPhone']
  self.email                        = data['MemberEmail']
  self.fax_phone                    = data['MemberFax']
  self.first_name                   = data['MemberFirstName']
  self.full_name                    = data['MemberFullName']
 # self.generational_name            = data['MemberMiddleName']
  self.last_name                    = data['MemberLastName']
  self.matrix_unique_id             = data['MemberMlsId']
  self.matrix_modified_dt           = data['ModificationTimestamp']
  self.middle_name                  = data['MemberMiddleName']
  self.mls                          = 'West Alabama Multiple Listing Service'
  self.mls_id                       = data['MemberMlsId']
  self.office_mui                   = data['OfficeMlsId']
  self.office_mls_id                = data['OfficeMlsId']
  self.office_phone                 = data['MemberOfficePhone']
  self.other_phone                  = data['MemberPreferredPhone']
  self.phone_toll_free              = data['MemberTollFreePhone']
  self.phone_voice_mail             = data['MemberVoiceMail']        
#  self.photo_count                  = data['PhotoCount']
#  self.photo_modification_timestamp = data['PhotoModificationTimestamp']
  self.slug                         = "#{data['MemberFirstName']}-#{data['MemberLastName']}".parameterize
end

#refresh_from_mlsObject



33
34
35
36
# File 'app/models/caboose_rets/agent.rb', line 33

def refresh_from_mls
  CabooseRets::RetsImporter.import('Member',"(MemberMlsId=#{self.mls_id})")
  CabooseRets::RetsImporter.download_property_images(self)
end

#send_new_user_email(user) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/caboose_rets/agent.rb', line 43

def send_new_user_email(user)
  if !self.email.blank? && user && user.site
    settings = Caboose::SmtpConfig.where(:site_id => user.site_id).first
    if settings
      delivery_options = {
        user_name: settings.user_name, 
        password: settings.password,
        address: settings.address,
        port: settings.port,
        domain: settings.domain,
        authentication: settings.authentication,
        enable_starttls_auto: settings.enable_starttls_auto
      }
      from_address = "#{settings.site.description} <#{settings.from_address}>"
      msg = CabooseRets::RetsMailer.send("new_user", self, user, from_address)
      msg.delivery_method.settings.merge!(delivery_options)
      msg.deliver_now
    end
  end
end

#verify_meta_existsObject



114
115
116
# File 'app/models/caboose_rets/agent.rb', line 114

def verify_meta_exists
   self.meta = CabooseRets::AgentMeta.create(:la_code => self.mls_id) if self.meta.nil?
end