-
Notifications
You must be signed in to change notification settings - Fork 154
/
entity_address.rb
206 lines (186 loc) · 7.61 KB
/
entity_address.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# = Informations
#
# == License
#
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon
# Copyright (C) 2010-2012 Brice Texier
# Copyright (C) 2012-2017 Brice Texier, David Joulin
#
# 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
# 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.
#
# == Table: entity_addresses
#
# by_default :boolean default(FALSE), not null
# canal :string not null
# coordinate :string not null
# created_at :datetime not null
# creator_id :integer
# deleted_at :datetime
# entity_id :integer not null
# id :integer not null, primary key
# latitude :float
# lock_version :integer default(0), not null
# longitude :float
# mail_auto_update :boolean default(FALSE), not null
# mail_country :string
# mail_geolocation :geometry({:srid=>4326, :type=>"point"})
# mail_line_1 :string
# mail_line_2 :string
# mail_line_3 :string
# mail_line_4 :string
# mail_line_5 :string
# mail_line_6 :string
# mail_postal_zone_id :integer
# name :string
# thread :string
# updated_at :datetime not null
# updater_id :integer
#
class EntityAddress < Ekylibre::Record::Base
attr_readonly :entity_id
refers_to :mail_country, class_name: 'Country'
belongs_to :mail_postal_zone, class_name: 'PostalZone'
belongs_to :entity, inverse_of: :addresses
has_many :buildings, foreign_key: :address_id
has_many :parcels, foreign_key: :address_id, dependent: :restrict_with_exception
has_many :purchases, foreign_key: :delivery_address_id
has_many :sales, foreign_key: :address_id
has_many :subscriptions, foreign_key: :address_id
enumerize :canal, in: [:mail, :email, :phone, :mobile, :fax, :website], default: :email, predicates: true
has_geometry :mail_geolocation, type: :point
# [VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`.
validates :by_default, :mail_auto_update, inclusion: { in: [true, false] }
validates :canal, :entity, presence: true
validates :coordinate, presence: true, length: { maximum: 500 }
validates :deleted_at, timeliness: { on_or_after: -> { Time.new(1, 1, 1).in_time_zone }, on_or_before: -> { Time.zone.now + 50.years } }, allow_blank: true
validates :latitude, :longitude, numericality: true, allow_blank: true
validates :mail_line_1, :mail_line_2, :mail_line_3, :mail_line_4, :mail_line_5, :mail_line_6, :name, :thread, length: { maximum: 500 }, allow_blank: true
# ]VALIDATORS]
validates :mail_country, length: { allow_nil: true, maximum: 2 }
validates :canal, length: { allow_nil: true, maximum: 20 }
validates :coordinate, length: { allow_nil: true, maximum: 500 }
validates :coordinate, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, if: :email? }
validates :canal, inclusion: { in: canal.values }
validates :mail_country, presence: { if: :mail? }
selects_among_all scope: [:entity_id, :canal], subset: :actives
# Use unscoped to get all historic
default_scope -> { actives }
scope :actives, -> { where(deleted_at: nil).order(:coordinate) }
geocoded_by :full_street_address
# Defines test and scope methods for.all canals
canal.values.each do |canal|
scope canal.to_s.pluralize, -> { where(canal: canal.to_s) }
scope "own_#{canal.to_s.pluralize}", -> { where(canal: canal.to_s, entity_id: Entity.of_company.id) }
end
before_validation do
if coordinate.is_a?(String)
coordinate.strip!
coordinate.downcase!
end
if mail?
self.mail_country = Preference[:country] if mail_country.blank?
if mail_line_6
self.mail_line_6 = mail_line_6.to_s.gsub(/\s+/, ' ').strip
if mail_line_6.blank?
self.mail_postal_zone_id = nil
else
unless self.mail_postal_zone = PostalZone.find_by('LOWER(TRIM(name)) LIKE ?', mail_line_6.lower)
self.mail_postal_zone = PostalZone.create!(name: mail_line_6, country: mail_country)
end
end
end
if entity
self.mail_line_1 = entity.full_name if mail_line_1.blank?
self.mail_auto_update = (entity.full_name == mail_line_1 ? true : false)
end
self.coordinate = mail_lines
elsif website?
self.coordinate = 'http://' + coordinate unless coordinate =~ /^.+p.*\/\//
end
end
# Each address have a distinct thread
before_validation(on: :create) do
if thread.blank?
self.thread = 'AAAA'
while self.class.where('entity_id = ? AND canal = ? AND thread = ?', entity_id, canal, thread).count > 0
thread.succ!
end
end
end
after_validation :geocode_address
after_update :geocode_address
def geocode_address
geocode
c = ::Charta.new_point(latitude, longitude) if latitude && longitude
self.mail_geolocation = c if c
end
def update # _without_.allbacks
# raise StandardError.new "UPDAAAAAAAAAAAATE"
current_time = Time.zone.now
stamper = begin
self.class.stamper_class.stamper
rescue
nil
end
# raise stamper.inspect unless stamper.nil?
stamper_id = stamper.id if stamper.is_a? Entity
nc = self.class.new
for attr, val in attributes.merge(created_at: current_time, updated_at: current_time, creator_id: stamper_id, updater_id: stamper_id).delete_if { |k, _v| k.to_s == 'id' }
nc.send("#{attr}=", val)
end
nc.save!
self.class.where(id: id).update_all(deleted_at: current_time)
nc
end
def destroy # _without_.allbacks
self.class.where(id: id).update_all(deleted_at: Time.zone.now) unless new_record?
end
def full_street_address
[mail_line_4, mail_line_6, mail_country].compact.join(', ')
end
def self.exportable_columns
content_columns.delete_if { |c| [:deleted_at, :closed_at, :lock_version, :thread, :created_at, :updated_at].include?(c.name.to_sym) }
end
def label
entity.number + '. ' + coordinate
end
def mail_line_6_code
mail_postal_zone.postal_code if mail_postal_zone
end
alias mail_postal_code mail_line_6_code
def mail_line_6_code=(value)
self.mail_line_6 = (value.to_s + ' ' + mail_line_6.to_s).strip
end
def mail_mail_line_6_city
mail_postal_zone.city if mail_postal_zone
end
def mail_line_6_city=(value)
self.mail_line_6 = (mail_line_6.to_s + ' ' + value.to_s).strip
end
def mail_lines(options = {})
options = { separator: ', ', with_city: true, with_country: true }.merge(options)
lines = []
lines << mail_line_1 unless options[:without] == :line_1
lines += [mail_line_2, mail_line_3, mail_line_4, mail_line_5]
lines << mail_line_6.to_s if options[:with_city]
lines << (Nomen::Country[mail_country] ? Nomen::Country[mail_country].human_name : '') if options[:with_country]
lines = lines.compact.collect { |x| x.gsub(options[:separator], ' ').gsub(/\ +/, ' ') }
lines.delete ''
lines.join(options[:separator])
end
def mail_coordinate
mail_lines(separator: "\r\n")
end
end