diff --git a/Gemfile b/Gemfile index 1c8d42edca..8c3a8231be 100644 --- a/Gemfile +++ b/Gemfile @@ -166,6 +166,7 @@ gem 'mechanize' gem 'savon' gem 'rubyntlm', '>= 0.3.2' gem 'rest-client', require: false +gem 'geocoder' gem 'luhn' diff --git a/Gemfile.lock b/Gemfile.lock index 681b7c59ab..ee850af321 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -561,6 +562,7 @@ DEPENDENCIES figaro foreman formize (~> 2.1.0) + geocoder haml http_accept_language humanize diff --git a/app/models/entity_address.rb b/app/models/entity_address.rb index f288f81609..b019aed2fb 100644 --- a/app/models/entity_address.rb +++ b/app/models/entity_address.rb @@ -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"}) @@ -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 } @@ -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) } @@ -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 @@ -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 diff --git a/app/views/backend/sale_opportunities/_popup.html.haml b/app/views/backend/sale_opportunities/_popup.html.haml new file mode 100644 index 0000000000..f26add0426 --- /dev/null +++ b/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) diff --git a/app/views/backend/sale_opportunities/index.html.haml b/app/views/backend/sale_opportunities/index.html.haml index 3c61d0bfed..3f4cb8a4f5 100644 --- a/app/views/backend/sale_opportunities/index.html.haml +++ b/app/views/backend/sale_opportunities/index.html.haml @@ -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 + + + + + diff --git a/config/locales/eng/models.yml b/config/locales/eng/models.yml index 3ecf2b0282..9eef12b74f 100644 --- a/config/locales/eng/models.yml +++ b/config/locales/eng/models.yml @@ -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" @@ -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" diff --git a/db/migrate/20170115181701_add_geocoder_attributes.rb b/db/migrate/20170115181701_add_geocoder_attributes.rb new file mode 100644 index 0000000000..74ea842eda --- /dev/null +++ b/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 diff --git a/db/structure.sql b/db/structure.sql index 65a6246286..9830719260 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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 ); @@ -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'); + diff --git a/db/tables.yml b/db/tables.yml index a26e06ec4e..13c52d5370 100644 --- a/db/tables.yml +++ b/db/tables.yml @@ -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 diff --git a/test/fixtures/entity_addresses.yml b/test/fixtures/entity_addresses.yml index 50dd61cd6d..d090b60725 100644 --- a/test/fixtures/entity_addresses.yml +++ b/test/fixtures/entity_addresses.yml @@ -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"}) diff --git a/test/models/entity_address_test.rb b/test/models/entity_address_test.rb index d8819298eb..31279184eb 100644 --- a/test/models/entity_address_test.rb +++ b/test/models/entity_address_test.rb @@ -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"})