Module: Crm::Reports::LeadReportHelper

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

Instance Method Summary collapse

Instance Method Details

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

Builds a Highcharts horizontal bar comparison chart config



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

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) ⇒ Object

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



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

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) ⇒ Object

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



343
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 343

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) ⇒ Object

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



288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
335
336
337
338
339
340
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 288

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) ⇒ Object

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



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

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_optionsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 3

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) ⇒ Object

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



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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 177

def lead_spline_chart_config(categories:, labels_current:, labels_previous:, current_data:, previous_data:, height: 325)
  {
    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) ⇒ Object

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 51

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"
    (:i, "", class: "fa-solid fa-arrow-trend-up #{css} opacity-50 me-1")
  elsif rounded.zero?
    (:i, "", class: "fa-solid fa-minus text-warning opacity-50 me-1")
  else
    css = inverse ? "text-success" : "text-danger"
    (:i, "", class: "fa-solid fa-arrow-trend-down #{css} opacity-50 me-1")
  end
end

#lead_trend_icon_lg(value, inverse: false) ⇒ Object

Large variant of lead_trend_icon for standalone display above KPI labels.



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

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"
    (:i, "", class: "fa-solid fa-arrow-trend-up #{css} opacity-50 fa-2x")
  elsif rounded.zero?
    (:i, "", class: "fa-solid fa-minus text-warning opacity-50 fa-2x")
  else
    css = inverse ? "text-success" : "text-danger"
    (:i, "", class: "fa-solid fa-arrow-trend-down #{css} opacity-50 fa-2x")
  end
end

#lead_variance_badge(value, inverse: false) ⇒ Object

Renders a styled variance badge with percentage and icon



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

def lead_variance_badge(value, inverse: false)
  return (:span, "--", class: "text-muted") 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-muted"
        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") ⇒ Object

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).



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/crm/reports/lead_report_helper.rb', line 26

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
      (:i, "", class: "fa-solid fa-arrow-up text-danger #{icon_size}")
    else
      (:i, "", class: "fa-solid fa-arrow-up text-success #{icon_size}")
    end
  elsif rounded.zero?
    (:i, "", class: "fa-solid fa-minus text-warning #{icon_size}")
  else
    if inverse
      (:i, "", class: "fa-solid fa-arrow-down text-success #{icon_size}")
    else
      (:i, "", class: "fa-solid fa-arrow-down text-danger #{icon_size}")
    end
  end
end