-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
contact.rb
190 lines (172 loc) · 8.29 KB
/
contact.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Fat Free CRM
# Copyright (C) 2008-2011 by Michael Dvorkin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------
# == Schema Information
# Schema version: 27
#
# Table name: contacts
#
# id :integer(4) not null, primary key
# user_id :integer(4)
# lead_id :integer(4)
# assigned_to :integer(4)
# reports_to :integer(4)
# first_name :string(64) default(""), not null
# last_name :string(64) default(""), not null
# access :string(8) default("Private")
# title :string(64)
# department :string(64)
# source :string(32)
# email :string(64)
# alt_email :string(64)
# phone :string(32)
# mobile :string(32)
# fax :string(32)
# blog :string(128)
# linkedin :string(128)
# facebook :string(128)
# twitter :string(128)
# skype :string(128)
# born_on :date
# do_not_call :boolean(1) not null
# deleted_at :datetime
# created_at :datetime
# updated_at :datetime
# background_info :string(255)
#
class Contact < ActiveRecord::Base
belongs_to :user
belongs_to :lead
belongs_to :assignee, :class_name => "User", :foreign_key => :assigned_to
has_one :account_contact, :dependent => :destroy
has_one :account, :through => :account_contact
has_many :contact_opportunities, :dependent => :destroy
has_many :opportunities, :through => :contact_opportunities, :uniq => true, :order => "opportunities.id DESC"
has_many :tasks, :as => :asset, :dependent => :destroy, :order => 'created_at DESC'
has_many :activities, :as => :subject, :order => 'created_at DESC'
has_one :business_address, :dependent => :destroy, :as => :addressable, :class_name => "Address", :conditions => "address_type = 'Business'"
has_many :emails, :as => :mediator
accepts_nested_attributes_for :business_address, :allow_destroy => true
scope :created_by, lambda { |user| { :conditions => [ "user_id = ?", user.id ] } }
scope :assigned_to, lambda { |user| { :conditions => ["assigned_to = ?", user.id ] } }
scope :search, lambda { |query|
query = query.gsub(/[^@\w\s\-\.'\p{L}]/u, '').strip
# We can't be sure that names are always entered in the right order, so we take the query and
# split it into all possible first/last name combinations.
# => "Zhong Fai Gao" matches last name "Zhong Fai" and "Fai Gao"
a = query.split(" ")
parts = [[a[0], a[1..-1].join(" ")],[a.reverse[0], a.reverse[1..-1].reverse.join(" ")]]
name_query = if a.size > 1
parts.map{ |first, last|
"(upper(first_name) LIKE upper('%#{first}%') AND upper(last_name) LIKE upper('%#{last}%'))"
}.join(" OR ")
else
"upper(first_name) LIKE upper('%#{query}%') OR upper(last_name) LIKE upper('%#{query}%')"
end
where("#{name_query} OR upper(email) LIKE upper(:m) OR upper(alt_email) LIKE upper(:m) OR phone LIKE :m OR mobile LIKE :m", :m => "%#{query}%")
}
uses_user_permissions
acts_as_commentable
is_paranoid
exportable
sortable :by => [ "first_name ASC", "last_name ASC", "created_at DESC", "updated_at DESC" ], :default => "created_at DESC"
validates_presence_of :first_name, :message => :missing_first_name
validates_presence_of :last_name, :message => :missing_last_name
validate :users_for_shared_access
# Default values provided through class methods.
#----------------------------------------------------------------------------
def self.per_page ; 20 ; end
def self.outline ; "long" ; end
def self.first_name_position ; "before" ; end
#----------------------------------------------------------------------------
def full_name(format = nil)
if format.nil? || format == "before"
"#{self.first_name} #{self.last_name}"
else
"#{self.last_name}, #{self.first_name}"
end
end
alias :name :full_name
# Backend handler for [Create New Contact] form (see contact/create).
#----------------------------------------------------------------------------
def save_with_account_and_permissions(params)
account = Account.create_or_select_for(self, params[:account], params[:users])
self.account_contact = AccountContact.new(:account => account, :contact => self) unless account.id.blank?
self.opportunities << Opportunity.find(params[:opportunity]) unless params[:opportunity].blank?
self.save_with_permissions(params[:users])
end
# Backend handler for [Update Contact] form (see contact/update).
#----------------------------------------------------------------------------
def update_with_account_and_permissions(params)
if params[:account][:id] == "" || params[:account][:name] == ""
self.account = nil # Contact is not associated with the account anymore.
self.reload
else
account = Account.create_or_select_for(self, params[:account], params[:users])
self.account_contact = AccountContact.new(:account => account, :contact => self) unless account.id.blank?
end
self.update_with_permissions(params[:contact], params[:users])
end
# Attach given attachment to the contact if it hasn't been attached already.
#----------------------------------------------------------------------------
def attach!(attachment)
unless self.send("#{attachment.class.name.downcase}_ids").include?(attachment.id)
self.send(attachment.class.name.tableize) << attachment
end
end
# Discard given attachment from the contact.
#----------------------------------------------------------------------------
def discard!(attachment)
if attachment.is_a?(Task)
attachment.update_attribute(:asset, nil)
else # Opportunities
self.send(attachment.class.name.tableize).delete(attachment)
end
end
# Class methods.
#----------------------------------------------------------------------------
def self.create_for(model, account, opportunity, params)
attributes = {
:lead_id => model.id,
:user_id => params[:account][:user_id],
:assigned_to => params[:account][:assigned_to],
:access => params[:access]
}
%w(first_name last_name title source email alt_email phone mobile blog linkedin facebook twitter skype do_not_call background_info).each do |name|
attributes[name] = model.send(name.intern)
end
contact = Contact.new(attributes)
contact.business_address = Address.new(:street1 => model.business_address.street1, :street2 => model.business_address.street2, :city => model.business_address.city, :state => model.business_address.state, :zipcode => model.business_address.zipcode, :country => model.business_address.country, :full_address => model.business_address.full_address, :address_type => "Business") unless model.business_address.nil?
# Save the contact only if the account and the opportunity have no errors.
if account.errors.empty? && opportunity.errors.empty?
# Note: contact.account = account doesn't seem to work here.
contact.account_contact = AccountContact.new(:account => account, :contact => contact) unless account.id.blank?
contact.opportunities << opportunity unless opportunity.id.blank?
if contact.access != "Lead" || model.nil?
contact.save_with_permissions(params[:users])
else
contact.save_with_model_permissions(model)
end
end
contact
end
private
# Make sure at least one user has been selected if the contact is being shared.
#----------------------------------------------------------------------------
def users_for_shared_access
errors.add(:access, :share_contact) if self[:access] == "Shared" && !self.permissions.any?
end
end