Project

General

Profile

Feature #2848 » 2848.patch

Yuichi HARADA, 2021-09-15 10:18

View differences:

lib/redmine/helpers/time_report.rb
47 47
          time_columns = %w(tyear tmonth tweek spent_on)
48 48
          @hours = []
49 49
          @scope.includes(:activity).
50
              reorder(nil).
50
              reorder(@criteria.collect{|criteria| @available_criteria[criteria][:order]}.compact).
51 51
              group(@criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).
52 52
              joins(@criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact).
53 53
              sum(:hours).each do |hash, hours|
......
105 105
      def load_available_criteria
106 106
        @available_criteria = {
107 107
          'project' => {:sql => "#{TimeEntry.table_name}.project_id",
108
                        :order => {Arel.sql("LOWER(#{Project.table_name}.name)") => :asc, "#{TimeEntry.table_name}.project_id" => :asc},
108 109
                        :klass => Project,
109 110
                        :label => :label_project},
110 111
          'status' => {:sql => "#{Issue.table_name}.status_id",
112
                       :joins => "LEFT OUTER JOIN #{IssueStatus.table_name} ON #{IssueStatus.table_name}.id = #{Issue.table_name}.status_id",
113
                       :order => {"#{IssueStatus.table_name}.position" => :asc, "#{Issue.table_name}.status_id" => :asc},
111 114
                       :klass => IssueStatus,
112 115
                       :label => :field_status},
113 116
          'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
117
                        :joins => "LEFT OUTER JOIN #{Version.table_name} ON #{Version.table_name}.id = #{Issue.table_name}.fixed_version_id",
118
                        :order => {"#{Version.table_name}.name" => :asc, "#{Issue.table_name}.fixed_version_id" => :asc},
114 119
                        :klass => ::Version,
115 120
                        :label => :label_version},
116 121
          'category' => {:sql => "#{Issue.table_name}.category_id",
122
                         :joins => "LEFT OUTER JOIN #{IssueCategory.table_name} ON #{IssueCategory.table_name}.id = #{Issue.table_name}.category_id",
123
                         :order => {"#{IssueCategory.table_name}.name" => :asc, "#{Issue.table_name}.category_id" => :asc},
117 124
                         :klass => IssueCategory,
118 125
                         :label => :field_category},
119 126
          'user' => {:sql => "#{TimeEntry.table_name}.user_id",
127
                     :order => User.fields_for_order_statement.index_with{|_| :asc},
120 128
                     :klass => User,
121 129
                     :label => :label_user},
122 130
          'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
131
                        :joins => "LEFT OUTER JOIN #{Tracker.table_name} ON #{Tracker.table_name}.id = #{Issue.table_name}.tracker_id",
132
                        :order => {"#{Tracker.table_name}.position" => :asc, "#{Issue.table_name}.tracker_id" => :asc},
123 133
                        :klass => Tracker,
124 134
                        :label => :label_tracker},
125 135
          'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
136
                         :order => {"#{TimeEntryActivity.table_name}.position" => :asc, "#{TimeEntry.table_name}.activity_id" => :asc},
126 137
                         :klass => TimeEntryActivity,
127 138
                         :label => :field_activity},
128 139
          'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
140
                      :order => {"#{TimeEntry.table_name}.issue_id" => :asc},
129 141
                      :klass => Issue,
130 142
                      :label => :label_issue}
131 143
        }
......
141 153

  
142 154
        # Add list and boolean custom fields as available criteria
143 155
        custom_fields.select {|cf| %w(list bool).include?(cf.field_format) && !cf.multiple?}.each do |cf|
156
          subquery_name = "#{cf.format.__send__(:join_alias, cf)}_options"
157
          subquery =
158
            cf.format.possible_values_options(cf).collect.with_index(1) do |option, idx|
159
              value = option.is_a?(Array) ? option.last : option
160
              cf.class.sanitize_sql_array(["SELECT %d AS id, '%s' AS value", idx, value])
161
            end.join(' UNION ')
162
          joins = cf.join_for_order_statement
163
          joins << " LEFT OUTER JOIN (#{subquery}) AS #{subquery_name} ON #{subquery_name}.value = #{cf.group_statement}"
144 164
          @available_criteria["cf_#{cf.id}"] = {:sql => cf.group_statement,
145
                                                 :joins => cf.join_for_order_statement,
165
                                                 :joins => joins,
166
                                                 :order => {"#{subquery_name}.id" => :asc},
146 167
                                                 :format => cf.field_format,
147 168
                                                 :custom_field => cf,
148 169
                                                 :label => cf.name}
(3-3/5)