Module: Crm::Reports::LeadReportHelper

Defined in:
app/helpers/crm/reports/lead_report_helper.rb

Overview

View helpers for the CRM lead report: tab URLs, variance/trend indicators,
comparison bars, period legends, and Highcharts configuration hashes.

Instance Method Summary collapse

Instance Method Details

#lead_bar_chart_config(current_value:, previous_value:, height: 100) ⇒ Hash

Builds a Highcharts horizontal bar comparison chart config.

Parameters:

  • current_value (Numeric)

    the current period value

  • previous_value (Numeric)

    the previous period value

  • height (Integer) (defaults to: 100)

    chart height in pixels

Returns:

  • (Hash)

    the Highcharts configuration



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 302

def lead_bar_chart_config(current_value:, previous_value:, height: 100)
  {
    chart: {
      type: "bar",
      backgroundColor: "transparent",
      style: { fontFamily: "inherit" },
      height: height
    },
    title: { text: "" },
    subtitle: { text: "" },
    xAxis: { categories: [""], visible: false },
    yAxis: { visible: false },
    plotOptions: {
      bar: {
        dataLabels: { enabled: true, style: { fontWeight: "600" } },
        borderRadius: 4
      }
    },
    legend: {
      align: "center",
      verticalAlign: "bottom",
      floating: true,
      y: -5,
      itemStyle: { fontSize: "11px", fontWeight: "normal" }
    },
    exporting: { enabled: false },
    credits: { enabled: false },
    series: [
      { name: "Previous", color: "#fd7e14", data: [previous_value] },
      { name: "Current", color: "#0d6efd", data: [current_value] }
    ]
  }
end

#lead_comparison_bars(current_value, previous_value) ⇒ ActiveSupport::SafeBuffer

Renders a CSS progress-bar comparison of two values (replaces the Highcharts bar chart).
Shows current vs previous as horizontal bars with labels.

Parameters:

  • current_value (Numeric)

    the current period value

  • previous_value (Numeric)

    the previous period value

Returns:

  • (ActiveSupport::SafeBuffer)

    the comparison bars HTML



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
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 138

def lead_comparison_bars(current_value, previous_value)
  current  = current_value.to_f
  previous = previous_value.to_f
  max_val  = [current, previous, 1].max # avoid division by zero

  current_pct  = (current / max_val * 100).round(1)
  previous_pct = (previous / max_val * 100).round(1)

  (:div, class: "lead-comparison-bars") do
    current_bar = (:div, class: "lead-comparison-row mb-2") do
      (:div, class: "d-flex align-items-center gap-2") do
        (:span, "Current", class: "lead-comparison-label") +
          (:div, class: "flex-grow-1") do
            (:div, class: "progress", style: "height: 22px;") do
              (:div, values_format_without_symbol(current_value),
                class: "progress-bar",
                style: "width: #{current_pct}%; background-color: #0d6efd;",
                role: "progressbar")
            end
          end
      end
    end

    previous_bar = (:div, class: "lead-comparison-row") do
      (:div, class: "d-flex align-items-center gap-2") do
        (:span, "Previous", class: "lead-comparison-label") +
          (:div, class: "flex-grow-1") do
            (:div, class: "progress", style: "height: 22px;") do
              (:div, values_format_without_symbol(previous_value),
                class: "progress-bar",
                style: "width: #{previous_pct}%; background-color: #fd7e14;",
                role: "progressbar")
            end
          end
      end
    end

    safe_join([current_bar, previous_bar])
  end
end

#lead_dual_axis_chart_config(categories:, column_data:, column_name:, spline_data:, spline_name:, value_prefix: "$", height: 280) ⇒ Hash

Builds a dual-axis column+spline chart (e.g. won opps count + amount on same timeline).

Parameters:

  • categories (Array<String>)

    x-axis category labels

  • column_data (Array<Numeric>)

    data points for the column series

  • column_name (String)

    label for the column series

  • spline_data (Array<Numeric>)

    data points for the spline series

  • spline_name (String)

    label for the spline series

  • value_prefix (String) (defaults to: "$")

    tooltip prefix for spline values (e.g. "$")

  • height (Integer) (defaults to: 280)

    chart height in pixels

Returns:

  • (Hash)

    the Highcharts configuration



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 408

def lead_dual_axis_chart_config(categories:, column_data:, column_name:, spline_data:, spline_name:, value_prefix: "$", height: 280)
  {
    chart: {
      zoomType: "xy",
      backgroundColor: "transparent",
      style: { fontFamily: "inherit" },
      height: height
    },
    title: { text: "" },
    subtitle: { text: "" },
    xAxis: [{
      categories: categories,
      crosshair: true,
      labels: {
        rotation: -45,
        style: { fontSize: "10px", color: "#6c757d" }
      },
      tickInterval: categories.length > 20 ? 2 : 1
    }],
    yAxis: [
      {
        title: { text: "Amount (#{value_prefix})", style: { fontSize: "11px", color: "#6c757d" } },
        gridLineColor: "#e9ecef",
        labels: { style: { color: "#6c757d" } }
      },
      {
        title: { text: column_name, style: { fontSize: "11px", color: "#6c757d" } },
        opposite: true,
        gridLineColor: "#e9ecef",
        labels: { style: { color: "#6c757d" } }
      }
    ],
    tooltip: {
      shared: true,
      backgroundColor: "rgba(255,255,255,0.96)",
      borderColor: "#dee2e6",
      borderRadius: 8,
      shadow: true,
      style: { fontSize: "12px" }
    },
    legend: {
      layout: "horizontal",
      align: "left",
      verticalAlign: "top",
      itemStyle: { fontWeight: "normal", color: "#495057" }
    },
    credits: { enabled: false },
    plotOptions: {
      column: { borderRadius: 4 },
      series: { animation: { duration: 600 } }
    },
    series: [
      {
        name: column_name,
        type: "column",
        color: "#0d6efd",
        yAxis: 1,
        data: column_data
      },
      {
        name: spline_name,
        type: "spline",
        dashStyle: "ShortDash",
        color: "#212529",
        lineWidth: 2,
        data: spline_data,
        tooltip: { valuePrefix: value_prefix }
      }
    ]
  }
end

#lead_multi_series_chart_config(categories:, series:, title: "", y_axis_title: "", height: 240) ⇒ Hash

Builds a multi-series areaspline stacked chart (e.g. website traffic sources).

Parameters:

  • categories (Array<String>)

    x-axis category labels

  • series (Array<Hash>)

    Highcharts series definitions

  • title (String) (defaults to: "")

    chart title

  • y_axis_title (String) (defaults to: "")

    y-axis title

  • height (Integer) (defaults to: 240)

    chart height in pixels

Returns:

  • (Hash)

    the Highcharts configuration



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 344

def lead_multi_series_chart_config(categories:, series:, title: "", y_axis_title: "", height: 240)
  {
    chart: {
      type: "areaspline",
      backgroundColor: "transparent",
      style: { fontFamily: "inherit" },
      height: height
    },
    title: {
      text: title,
      style: { fontSize: "14px", fontWeight: "600", color: "#212529" }
    },
    xAxis: {
      categories: categories,
      crosshair: true,
      labels: {
        rotation: -45,
        style: { fontSize: "10px", color: "#6c757d" }
      },
      tickInterval: categories.length > 20 ? 2 : 1
    },
    yAxis: {
      title: { text: y_axis_title, style: { fontSize: "11px", color: "#6c757d" } },
      gridLineColor: "#e9ecef",
      labels: { style: { fontSize: "11px", color: "#6c757d" } }
    },
    legend: {
      align: "center",
      verticalAlign: "bottom",
      itemStyle: { fontSize: "11px", fontWeight: "normal" }
    },
    tooltip: {
      shared: true,
      backgroundColor: "rgba(255,255,255,0.96)",
      borderColor: "#dee2e6",
      borderRadius: 8,
      shadow: true,
      style: { fontSize: "12px" }
    },
    plotOptions: {
      areaspline: { fillOpacity: 0.15, lineWidth: 2, marker: { radius: 2.5 } },
      series: { animation: { duration: 600 } }
    },
    series: series,
    credits: { enabled: false },
    responsive: {
      rules: [{
        condition: { maxWidth: 500 },
        chartOptions: { legend: { layout: "horizontal", align: "center", verticalAlign: "bottom" } }
      }]
    }
  }
end

#lead_period_legend(start_date, end_date, compare_start_date, compare_end_date) ⇒ ActiveSupport::SafeBuffer

Renders the period legend bar used at the top of each tab.

Parameters:

  • start_date (Date, Time)

    start of the current period

  • end_date (Date, Time)

    end of the current period

  • compare_start_date (Date, Time)

    start of the comparison period

  • compare_end_date (Date, Time)

    end of the comparison period

Returns:

  • (ActiveSupport::SafeBuffer)

    the period legend HTML



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 186

def lead_period_legend(start_date, end_date, compare_start_date, compare_end_date)
  (:div, class: "lead-report-period-legend d-flex flex-wrap gap-4 p-3 mb-4 bg-light rounded-3 border") do
    current_label = (:div, class: "d-flex align-items-center gap-2") do
      (:span, "", class: "lead-report-dot lead-report-dot--current") +
        (:span, class: "text-body-secondary") do
          safe_join([
                      "Current: ",
                      (:strong, "#{start_date.strftime('%b %d, %Y')}#{end_date.strftime('%b %d, %Y')}")
                    ])
        end
    end

    compare_label = (:div, class: "d-flex align-items-center gap-2") do
      (:span, "", class: "lead-report-dot lead-report-dot--previous") +
        (:span, class: "text-body-secondary") do
          safe_join([
                      "Previous: ",
                      (:strong, "#{compare_start_date.strftime('%b %d, %Y')}#{compare_end_date.strftime('%b %d, %Y')}")
                    ])
        end
    end

    safe_join([current_label, compare_label])
  end
end

#lead_report_tab_optionsHash{Symbol => Hash}

Builds the tab navigation options for the lead report, preserving the
current params[:command] filter in each tab's remote URL.

Returns:

  • (Hash{Symbol => Hash})

    tab id mapped to a :remote_href path



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 12

def lead_report_tab_options
  params[:command] ||= {}
  hsh = {}
  tabs = {
    customers_created: :reports_lead_report_tab_customers_created,
    opportunities: :reports_lead_report_tab_opportunities,
    orders: :reports_lead_report_tab_orders,
    services: :reports_lead_report_tab_services,
    rooms_created: :reports_lead_report_tab_rooms_created,
    calls_in_out: :reports_lead_report_tab_calls_in_out,
    communications: :reports_lead_report_tab_communications,
    sms: :reports_lead_report_tab_sms,
    website_visits: :reports_lead_report_tab_website_visits
  }
  tabs.each do |tab_id, route_name|
    hsh[tab_id] = { remote_href: send("#{route_name}_path", command: params[:command]) }
  end
  hsh
end

#lead_spline_chart_config(categories:, labels_current:, labels_previous:, current_data:, previous_data:, height: 325) ⇒ Hash

Builds a Highcharts spline chart configuration as a Ruby hash (for JSON serialization).

Parameters:

  • categories (Array<String>)

    x-axis category labels

  • labels_current (Array<String>)

    tooltip/legend labels for the current series (unused)

  • labels_previous (Array<String>)

    tooltip/legend labels for the previous series (unused)

  • current_data (Array<Numeric>)

    data points for the current period

  • previous_data (Array<Numeric>)

    data points for the previous period

  • height (Integer) (defaults to: 325)

    chart height in pixels

Returns:

  • (Hash)

    the Highcharts configuration



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 221

def lead_spline_chart_config(categories:, labels_current:, labels_previous:, current_data:, previous_data:, height: 325) # rubocop:disable Lint/UnusedMethodArgument
  {
    chart: {
      type: "spline",
      backgroundColor: "transparent",
      style: { fontFamily: "inherit" },
      height: height
    },
    title: { text: "" },
    subtitle: { text: "" },
    xAxis: {
      categories: categories,
      crosshair: true,
      labels: {
        rotation: -45,
        style: { fontSize: "10px", color: "#6c757d" }
      },
      tickInterval: categories.length > 20 ? 2 : 1
    },
    yAxis: {
      title: { text: "" },
      gridLineColor: "#e9ecef",
      labels: { style: { color: "#6c757d" } }
    },
    legend: {
      layout: "horizontal",
      align: "right",
      verticalAlign: "top",
      itemStyle: { fontWeight: "normal", color: "#495057" }
    },
    tooltip: {
      shared: true,
      backgroundColor: "rgba(255,255,255,0.96)",
      borderColor: "#dee2e6",
      borderRadius: 8,
      shadow: true,
      style: { fontSize: "12px" },
      useHTML: true
    },
    plotOptions: {
      series: {
        animation: { duration: 600 }
      }
    },
    series: [
      {
        name: "Current",
        color: "#0d6efd",
        type: "areaspline",
        fillOpacity: 0.12,
        lineWidth: 2,
        marker: { radius: 3, symbol: "circle" },
        data: current_data
      },
      {
        name: "Previous",
        color: "#fd7e14",
        lineWidth: 2,
        dashStyle: "ShortDash",
        marker: { radius: 3, symbol: "circle" },
        data: previous_data
      }
    ],
    credits: { enabled: false },
    responsive: {
      rules: [{
        condition: { maxWidth: 500 },
        chartOptions: {
          legend: { layout: "horizontal", align: "center", verticalAlign: "bottom" }
        }
      }]
    }
  }
end

#lead_trend_icon(value, inverse: false) ⇒ String

Renders a trend icon (up/down/flat) for KPI header labels.
Uses fa-arrow-trend-up / fa-arrow-trend-down for a visual-friendly look.

Parameters:

  • value (Numeric, nil)

    the trend value to visualize

  • inverse (Boolean) (defaults to: false)

    whether a decrease is the positive outcome

Returns:

  • (String)

    HTML for the icon, or an empty string when value is nil



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 67

def lead_trend_icon(value, inverse: false)
  return "" unless value

  rounded = value.respond_to?(:round) ? value.round(2) : value.to_f.round(2)

  if rounded.positive?
    css = inverse ? "text-danger" : "text-success"
    fa_icon("arrow-trend-up", family: :solid, class: "#{css} opacity-50 me-1")
  elsif rounded.zero?
    fa_icon("minus", family: :solid, class: "text-warning opacity-50 me-1")
  else
    css = inverse ? "text-success" : "text-danger"
    fa_icon("arrow-trend-down", family: :solid, class: "#{css} opacity-50 me-1")
  end
end

#lead_trend_icon_lg(value, inverse: false) ⇒ String

Large variant of lead_trend_icon for standalone display above KPI labels.

Parameters:

  • value (Numeric, nil)

    the trend value to visualize

  • inverse (Boolean) (defaults to: false)

    whether a decrease is the positive outcome

Returns:

  • (String)

    HTML for the icon, or an empty string when value is nil

See Also:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 89

def lead_trend_icon_lg(value, inverse: false)
  return "" unless value

  rounded = value.respond_to?(:round) ? value.round(2) : value.to_f.round(2)

  if rounded.positive?
    css = inverse ? "text-danger" : "text-success"
    fa_icon("arrow-trend-up", family: :solid, class: "#{css} opacity-50 fa-2x")
  elsif rounded.zero?
    fa_icon("minus", family: :solid, class: "text-warning opacity-50 fa-2x")
  else
    css = inverse ? "text-success" : "text-danger"
    fa_icon("arrow-trend-down", family: :solid, class: "#{css} opacity-50 fa-2x")
  end
end

#lead_variance_badge(value, inverse: false) ⇒ ActiveSupport::SafeBuffer

Renders a styled variance badge with percentage and icon.

Parameters:

  • value (Numeric, nil)

    the percentage variance to display

  • inverse (Boolean) (defaults to: false)

    whether a decrease is the positive outcome

Returns:

  • (ActiveSupport::SafeBuffer)

    the badge HTML, or a -- placeholder when value is nil

See Also:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 111

def lead_variance_badge(value, inverse: false)
  return (:span, "--", class: "text-body-secondary") unless value

  rounded = value.respond_to?(:round) ? value.round(1) : value.to_f.round(1)
  css = if rounded.positive?
          inverse ? "text-danger" : "text-success"
        elsif rounded.negative?
          inverse ? "text-success" : "text-danger"
        else
          "text-body-secondary"
        end

  (:span, class: "fw-semibold #{css}") do
    safe_join([
                (:span, "#{rounded}%"),
                " ",
                lead_variance_icon(rounded, inverse: inverse)
              ])
  end
end

#lead_variance_icon(value, inverse: false, size: "sm") ⇒ String

Renders a Font Awesome variance indicator icon based on a numeric value.
Positive = green arrow up, Zero = yellow dash, Negative = red arrow down.
Pass inverse: true for metrics where decrease is good (e.g. time to convert, lost opps).

Parameters:

  • value (Numeric, nil)

    the variance value to visualize

  • inverse (Boolean) (defaults to: false)

    whether a decrease is the positive outcome

  • size (String) (defaults to: "sm")

    icon size, "sm" (default) or "lg"

Returns:

  • (String)

    HTML for the icon, or an empty string when value is nil



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 40

def lead_variance_icon(value, inverse: false, size: "sm")
  return "" unless value

  rounded = value.respond_to?(:round) ? value.round(2) : value.to_f.round(2)
  icon_size = size == "lg" ? "fa-lg" : ""

  if rounded.positive?
    if inverse
      fa_icon("arrow-up", family: :solid, class: "text-danger #{icon_size}")
    else
      fa_icon("arrow-up", family: :solid, class: "text-success #{icon_size}")
    end
  elsif rounded.zero?
    fa_icon("minus", family: :solid, class: "text-warning #{icon_size}")
  elsif inverse
    fa_icon("arrow-down", family: :solid, class: "text-success #{icon_size}")
  else
    fa_icon("arrow-down", family: :solid, class: "text-danger #{icon_size}")
  end
end