diff --git a/app/models/sales_goal.rb b/app/models/sales_goal.rb index 1a7b573..fb77c05 100644 --- a/app/models/sales_goal.rb +++ b/app/models/sales_goal.rb @@ -37,7 +37,7 @@ class SalesGoal < ApplicationRecord case metric when "customers_acquired" - opps.won.where(won_at: start_date.beginning_of_day..end_date.end_of_day).count + opps.won.where(won_at: start_date.beginning_of_day..end_date.end_of_day).distinct.count(:organization_id) when "won_value" opps.won.where(won_at: start_date.beginning_of_day..end_date.end_of_day).sum(:estimated_value).to_f when "trials" diff --git a/app/services/dashboard/metrics.rb b/app/services/dashboard/metrics.rb index b25a0c1..abacf4e 100644 --- a/app/services/dashboard/metrics.rb +++ b/app/services/dashboard/metrics.rb @@ -15,6 +15,10 @@ class Dashboard::Metrics organizations_scope.prospects.count end + def customers_count + organizations_scope.customers.count + end + def open_pipeline_value @scope.open_stage.sum(:estimated_value).to_f end diff --git a/app/views/dashboard/show.html.erb b/app/views/dashboard/show.html.erb index 58ca436..9f3639a 100644 --- a/app/views/dashboard/show.html.erb +++ b/app/views/dashboard/show.html.erb @@ -37,7 +37,7 @@
-

Conteggio per stage attuale: quante opportunità sono in quello stato ora.

+

Prospect e clienti sono società. Gli altri numeri sono opportunità nello stage attuale.

<% [ ["Prospect totali", @metrics.prospect_count], @@ -48,7 +48,7 @@ ["Demo/Trial", @stage_counts["demo_trial"]], ["Primo utilizzo", @stage_counts["first_use"]], ["Proposte", @stage_counts["proposal"]], - ["Clienti acquisiti", @stage_counts["won"]], + ["Clienti acquisiti", @metrics.customers_count], ["Persi", @stage_counts["lost"]], ["Valore WON", format_money(@metrics.won_value)], ["Pipeline aperta", format_money(@metrics.open_pipeline_value)], diff --git a/test/controllers/dashboard_controller_test.rb b/test/controllers/dashboard_controller_test.rb index 3eb95c0..073d5db 100644 --- a/test/controllers/dashboard_controller_test.rb +++ b/test/controllers/dashboard_controller_test.rb @@ -7,7 +7,7 @@ class DashboardControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_match(/Da contattare/, response.body) - assert_match(/Conteggio per stage attuale/, response.body) + assert_match(/Prospect e clienti sono società/, response.body) assert_match(/da qui in poi/, response.body) assert_match(/In trattativa senza next action/, response.body) assert_match(/Senza attività in timeline/, response.body) diff --git a/test/models/dashboard_metrics_test.rb b/test/models/dashboard_metrics_test.rb index d3b1580..4aaf844 100644 --- a/test/models/dashboard_metrics_test.rb +++ b/test/models/dashboard_metrics_test.rb @@ -82,12 +82,37 @@ class DashboardMetricsTest < ActiveSupport::TestCase assert_equal 0, metrics.demo_to_won_conversion end + test "customers acquired counts unique organizations not duplicate won deals" do + org = Organization.create!( + name: "Cliente doppio #{SecureRandom.hex(3)}", + status: "prospect", + organization_type: "societa_sportiva" + ) + 2.times { |i| add_opp("won", organization: org, name: "Deal #{i}") } + add_opp("won") + + assert_equal 3, metrics.stage_counts["won"] + assert_equal 2, metrics.customers_count + end + test "goal progress" do goal = sales_goals(:september) assert_equal 0, goal.current_value assert_equal 0, goal.progress_percentage end + test "customers_acquired goal counts distinct organizations in the period" do + org = Organization.create!( + name: "Cliente goal #{SecureRandom.hex(3)}", + status: "prospect", + organization_type: "societa_sportiva" + ) + 2.times { |i| add_opp("won", organization: org, name: "Goal deal #{i}") } + add_opp("won") + + assert_equal 2, sales_goals(:september).current_value + end + private def metrics @@ -95,7 +120,7 @@ class DashboardMetricsTest < ActiveSupport::TestCase end def add_opp(stage, **attrs) - org = Organization.create!( + org = attrs.delete(:organization) || Organization.create!( name: "Soc #{stage} #{SecureRandom.hex(3)}", status: "prospect", organization_type: "societa_sportiva"