Module: ReportsHelper

Defined in:
app/helpers/reports_helper.rb

Instance Method Summary collapse

Instance Method Details

#format_seconds(seconds) ⇒ Object



2
3
4
5
6
# File 'app/helpers/reports_helper.rb', line 2

def format_seconds(seconds)
  Time.at(seconds).utc.strftime('%R:%S')
rescue StandardError
  '-'
end

#format_seconds_to_days(seconds) ⇒ Object



8
9
10
11
12
# File 'app/helpers/reports_helper.rb', line 8

def format_seconds_to_days(seconds)
  (seconds.to_f / 86_400.00).round(2).to_s
rescue StandardError
  '-'
end

#format_seconds_to_hours(seconds) ⇒ Object



14
15
16
17
18
# File 'app/helpers/reports_helper.rb', line 14

def format_seconds_to_hours(seconds)
  (seconds.to_f / 3600.00).round(2).to_s
rescue StandardError
  '-'
end

#nz(value) ⇒ Object

Simple helper, if value is present and non zero, display it



21
22
23
# File 'app/helpers/reports_helper.rb', line 21

def nz(value)
  value if value && value > 0
end

#up_down(value1, value2, text: nil, up_style: nil, down_style: nil, neutral_style: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/reports_helper.rb', line 25

def up_down(value1, value2, text: nil, up_style: nil, down_style: nil, neutral_style: nil)
  return unless value1 && value2

  up_style ||= 'color: darkgreen'
  down_style ||= 'color: darkred'
  neutral_style ||= 'color: gray'

   :span, class: 'arrow-indicator' do
    if value2.to_f > value1.to_f
      tag.span(fa_icon('arrow-up', text:), style: up_style)
    elsif value2.to_f < value1.to_f
      tag.span(fa_icon('arrow-down', text:), style: down_style)
    else
      tag.span(fa_icon('minus', text:), style: neutral_style)
    end
  end
end

#up_down_with_percentage(last_period_value, current_period_value, up_style: nil, down_style: nil, neutral_style: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/reports_helper.rb', line 43

def up_down_with_percentage(last_period_value, current_period_value, up_style: nil, down_style: nil, neutral_style: nil)
  return up_down(last_period_value, current_period_value) if last_period_value.blank? || current_period_value.blank?

  last_value = last_period_value.to_f
  current_value = current_period_value.to_f

  # Handle division by zero case
  if last_value.zero?
    percentage_text = if current_value.zero?
                        '0%'
                      else
                        # When starting from zero, show the current value as the percentage
                        "#{current_value.abs.round(0)}%"
                      end
  else
    # Standard percentage change: ((new - old) / old) * 100
    percentage_change = ((current_value - last_value) / last_value) * 100
    percentage_text = "#{percentage_change.abs.round(1)}%"
  end

  up_down(last_period_value, current_period_value, text: percentage_text, up_style: up_style, down_style: down_style, neutral_style: neutral_style)
end