Skip to content

Commit

Permalink
Add geocoding capacity for entity address and map to sale opportunity…
Browse files Browse the repository at this point in the history
… visualisation.
  • Loading branch information
ionosphere committed Jan 15, 2017
1 parent e3802e5 commit 43b04c7
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -166,6 +166,7 @@ gem 'mechanize'
gem 'savon'
gem 'rubyntlm', '>= 0.3.2'
gem 'rest-client', require: false
gem 'geocoder'

gem 'luhn'

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -188,6 +188,7 @@ GEM
jquery-rails (>= 3)
jquery-ui-rails (>= 5)
rails (>= 3.2)
geocoder (1.4.1)
globalid (0.3.7)
activesupport (>= 4.1.0)
gyoku (1.3.1)
Expand Down Expand Up @@ -561,6 +562,7 @@ DEPENDENCIES
figaro
foreman
formize (~> 2.1.0)
geocoder
haml
http_accept_language
humanize
Expand Down
20 changes: 19 additions & 1 deletion app/models/entity_address.rb
Expand Up @@ -30,7 +30,9 @@
# 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"})
Expand Down Expand Up @@ -58,12 +60,13 @@ class EntityAddress < Ekylibre::Record::Base
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 }
Expand All @@ -79,6 +82,8 @@ class EntityAddress < Ekylibre::Record::Base
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) }
Expand Down Expand Up @@ -122,6 +127,15 @@ class EntityAddress < Ekylibre::Record::Base
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
Expand All @@ -145,6 +159,10 @@ 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
Expand Down
2 changes: 2 additions & 0 deletions app/views/backend/sale_opportunities/_popup.html.haml
@@ -0,0 +1,2 @@
= button_group do
= tool_to(:show.ta, {action: :show, controller: "/backend/sale_opportunities", id: opportunity.id, redirect: params[:redirect] }, tool: false)
33 changes: 33 additions & 0 deletions app/views/backend/sale_opportunities/index.html.haml
Expand Up @@ -45,3 +45,36 @@
= column_highcharts(series, y_axis: {title: {text: :pretax_amount.tl}, stack_labels: {enabled: true, format: "{total} #{unit.symbol}"}, labels: { format: "{value} #{unit.symbol}" }}, x_axis: { categories: categories.map(&:tl)}, legend: true, plot_options: { column: {stacking: 'normal'}})
- else
= no_data
- j.face :map do
:ruby
data = SaleOpportunity.all.collect do |s|
a = s.client.default_mail_address
next unless a && a.mail_geolocation

popup_content = []
popup_content << {label: SaleOpportunity.human_attribute_name(:pretax_amount), value: s.pretax_amount}
popup_content << {label: SaleOpportunity.human_attribute_name(:probability_percentage), value: s.probability_percentage}
popup_content << {label: SaleOpportunity.human_attribute_name(:state_label), value: s.state_label}
popup_content << {value: labels_info(s.labels)} if s.labels.any?
popup_content << {value: lights(s.status)}
popup_content << render('popup', opportunity: s)

{
name: s.client.full_name,
shape: a.mail_geolocation,
pretax_amount: s.pretax_amount.to_s.to_f,
popup: {header: true, content: popup_content}
}
end
= visualization(box: {height: "100%"}) do |v|
- v.serie :main, data
- v.bubbles :pretax_amount, :main
- v.control :zoom
- v.control :scale
- v.control :fullscreen
- v.control :layer_selector





2 changes: 2 additions & 0 deletions config/locales/eng/models.yml
Expand Up @@ -893,6 +893,7 @@ eng:
last_value: "Last value"
last_year: "Last year"
latest: "Latest"
# latitude: "Latitude"
leaves_parameters: "Leaves parameters"
lender: "Lender"
letter: "Letter"
Expand All @@ -915,6 +916,7 @@ eng:
lock_version: "Version"
locked: "Locked"
locked_at: "Locked at"
# longitude: "Longitude"
mail: "Mail"
mail_addresses: "Mail addresses"
mail_auto_update: "Mail auto update"
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20170115181701_add_geocoder_attributes.rb
@@ -0,0 +1,8 @@
class AddGeocoderAttributes < ActiveRecord::Migration
def change

add_column :entity_addresses, :latitude, :float
add_column :entity_addresses, :longitude, :float

end
end
6 changes: 5 additions & 1 deletion db/structure.sql
Expand Up @@ -2209,7 +2209,9 @@ CREATE TABLE entity_addresses (
updated_at timestamp without time zone NOT NULL,
creator_id integer,
updater_id integer,
lock_version integer DEFAULT 0 NOT NULL
lock_version integer DEFAULT 0 NOT NULL,
latitude double precision,
longitude double precision
);


Expand Down Expand Up @@ -16840,3 +16842,5 @@ INSERT INTO schema_migrations (version) VALUES ('20161231234533');

INSERT INTO schema_migrations (version) VALUES ('20170113183701');

INSERT INTO schema_migrations (version) VALUES ('20170115181701');

4 changes: 4 additions & 0 deletions db/tables.yml
Expand Up @@ -2248,9 +2248,13 @@ entity_addresses:
id:
type: integer
required: true
latitude:
type: float
lock_version:
type: integer
required: true
longitude:
type: float
mail_auto_update:
type: boolean
required: true
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/entity_addresses.yml
Expand Up @@ -30,7 +30,9 @@
# 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"})
Expand Down
2 changes: 2 additions & 0 deletions test/models/entity_address_test.rb
Expand Up @@ -30,7 +30,9 @@
# 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"})
Expand Down

0 comments on commit 43b04c7

Please sign in to comment.