summaryrefslogtreecommitdiff
path: root/app/helpers/version_reports_helper.rb
blob: 3045cede7041173db62343ee2dae330b4433d5ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module VersionReportsHelper

  def display_issue(issue, *options)
    text = issue.subject
    text = text.sub(/\!\!\w*/,'')
    if options.include? :tracker
      text = "#{issue.tracker} ##{issue.id} " + text
    end
    link_to text, issue_path(issue)
  end

  def issue_time(issue)
    if @show_time && issue.total_spent_hours > 0
      l_hours(issue.total_spent_hours)
    end
  end

  def issue_money(issue, *options)
    if @show_money
      amount = custom_price(issue) || hours_to_money(issue.total_spent_hours)
      amount = amount.round
      if options.include?(:format)
        number_to_currency amount, :precision => 0
      else
        amount
      end
    end
  end

  def hourly_rate
    @rate ||= to_float( custom_field_value(@version, :rate) || DEFAULT_REPORT_RATE )
  end

  def hours_to_money(hours)
    (hours||0) * hourly_rate
  end

  #
  # name is a key for REPORT_CUSTOM_FIELDS hash
  #
  def custom_field(object, name)
    object.custom_field_values.detect {|value|
      value.custom_field.name == REPORT_CUSTOM_FIELDS[name]
    }
  end

  def custom_field_value(object, name)
    v = custom_field(object, name)
    if v
      v.value
    end
  end

  def hour_budget_error(issue)
    return unless @show_budget && (@show_time || @show_money)
    time_budget = (custom_field_value(issue, :hours) || 0).to_i
    money_budget = to_float(custom_field_value(issue, :money)||0).round
    if time_budget != 0
      if issue.total_spent_hours.round != time_budget
        if issue.total_spent_hours < time_budget
          content_tag :div, :class => 'flash error' do
            "%s under budget!" % l_hours(time_budget - issue.total_spent_hours)
          end
        elsif issue.total_spent_hours > time_budget
          content_tag :div, :class => 'flash warning' do
            "%s over budget!" % l_hours(issue.total_spent_hours - time_budget)
          end
        end
      end
    elsif money_budget != 0
      actual_money = issue_money(issue).round
      if actual_money != money_budget
        if actual_money < money_budget
          content_tag :div, :class => 'flash error' do
            "%s under budget!" % l_hours((money_budget - actual_money) / hourly_rate)
          end
        elsif actual_money > money_budget
          content_tag :div, :class => 'flash warning' do
            "%s over budget!" % l_hours((actual_money - money_budget) / hourly_rate)
          end
        end
      end
    end
  end

  def custom_price(issue)
    value = custom_field_value(issue, :price)
    if value.present? && value != "0"
      to_float(value)
    end
  end

  def report_header(version)
    url = custom_field_value(version, :header)
    if !url.present?
      url = DEFAULT_REPORT_HEADER
    end
    %(<img src="#{url}" style="max-width:100%"/>).html_safe
  end

  # show_value requires CustomFieldsHelper
  def report_summary(version)
    show_value(custom_field(version, :summary))
  end

  def total_time(version, issues)
    l_hours(issues.sum(&:total_spent_hours)) if @show_time
  end

  def total_money(version, issues)
    if @show_money
      total = issues.sum {|issue| issue_money(issue)}
      number_to_currency total, :precision => 0
    end
  end

  # drops currency in a string.
  def to_float(string)
    if string.is_a? String
      string.gsub(/[^0-9\.]/, '').to_f
    else
      string.to_f
    end
  end

end