time_entry_query.rb 8.06 KB
Newer Older
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2016  Jean-Philippe Lang
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class TimeEntryQuery < Query

  self.queried_class = TimeEntry
jplang's avatar
jplang committed
21
  self.view_permission = :view_time_entries
22 23 24

  self.available_columns = [
    QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
25
    QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true),
26
    QueryColumn.new(:tweek, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :caption => l(:label_week)),
27 28 29
    QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
    QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true),
    QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"),
30 31
    QueryAssociationColumn.new(:issue, :tracker, :caption => :field_tracker, :sortable => "#{Tracker.table_name}.position"),
    QueryAssociationColumn.new(:issue, :status, :caption => :field_status, :sortable => "#{IssueStatus.table_name}.position"),
32
    QueryColumn.new(:comments),
33
    QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours", :totalable => true),
34 35 36 37 38 39 40 41
  ]

  def initialize(attributes=nil, *args)
    super attributes
    self.filters ||= {}
    add_filter('spent_on', '*') unless filters.present?
  end

42 43
  def initialize_available_filters
    add_available_filter "spent_on", :type => :date_past
44

45 46 47 48 49 50 51 52
    add_available_filter("project_id",
      :type => :list, :values => lambda { project_values }
    ) if project.nil?

    if project && !project.leaf?
      add_available_filter "subproject_id",
        :type => :list_subprojects,
        :values => lambda { subproject_values }
53
    end
54 55

    add_available_filter("issue_id", :type => :tree, :label => :label_issue)
56 57 58
    add_available_filter("issue.tracker_id",
      :type => :list,
      :name => l("label_attribute_of_issue", :name => l(:field_tracker)),
59
      :values => lambda { Tracker.sorted.map {|t| [t.name, t.id.to_s]} })
60 61 62
    add_available_filter("issue.status_id",
      :type => :list,
      :name => l("label_attribute_of_issue", :name => l(:field_status)),
63
      :values => lambda { IssueStatus.sorted.map {|s| [s.name, s.id.to_s]} })
64 65 66
    add_available_filter("issue.fixed_version_id",
      :type => :list,
      :name => l("label_attribute_of_issue", :name => l(:field_fixed_version)),
67
      :values => lambda { fixed_version_values }) if project
68

69
    add_available_filter("user_id",
70 71
      :type => :list_optional, :values => lambda { author_values }
    )
72

73
    activities = (project ? project.activities : TimeEntryActivity.shared)
74 75
    add_available_filter("activity_id",
      :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
76
    )
77 78 79

    add_available_filter "comments", :type => :text
    add_available_filter "hours", :type => :float
80

81
    add_custom_fields_filters(TimeEntryCustomField)
82 83 84
    add_associations_custom_fields_filters :project
    add_custom_fields_filters(issue_custom_fields, :issue)
    add_associations_custom_fields_filters :user
85 86
  end

87 88 89
  def available_columns
    return @available_columns if @available_columns
    @available_columns = self.class.available_columns.dup
90 91
    @available_columns += TimeEntryCustomField.visible.
                            map {|cf| QueryCustomFieldColumn.new(cf) }
92
    @available_columns += issue_custom_fields.visible.
93
                            map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf, :totalable => false) }
94 95
    @available_columns += ProjectCustomField.visible.
                            map {|cf| QueryAssociationCustomFieldColumn.new(:project, cf) }
96 97 98
    @available_columns
  end

99
  def default_columns_names
100 101 102 103 104
    @default_columns_names ||= begin
      default_columns = [:spent_on, :user, :activity, :issue, :comments, :hours]

      project.present? ? default_columns : [:project] | default_columns
    end
105 106
  end

107 108 109
  def default_totalable_names
    [:hours]
  end
110 111 112 113
 
  def default_sort_criteria
    [['spent_on', 'desc']]
  end
114 115

  def base_scope
116 117 118 119
    TimeEntry.visible.
      joins(:project, :user).
      joins("LEFT OUTER JOIN issues ON issues.id = time_entries.issue_id").
      where(statement)
120 121
  end

122
  def results_scope(options={})
123
    order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)
124

125
    base_scope.
126
      order(order_option).
127
      joins(joins_for_order_statement(order_option.join(','))).
jplang's avatar
jplang committed
128 129
      includes(:activity).
      references(:activity)
130
  end
131 132 133 134 135

  # Returns sum of all the spent hours
  def total_for_hours(scope)
    map_total(scope.sum(:hours)) {|t| t.to_f.round(2)}
  end
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  def sql_for_issue_id_field(field, operator, value)
    case operator
    when "="
      "#{TimeEntry.table_name}.issue_id = #{value.first.to_i}"
    when "~"
      issue = Issue.where(:id => value.first.to_i).first
      if issue && (issue_ids = issue.self_and_descendants.pluck(:id)).any?
        "#{TimeEntry.table_name}.issue_id IN (#{issue_ids.join(',')})"
      else
        "1=0"
      end
    when "!*"
      "#{TimeEntry.table_name}.issue_id IS NULL"
    when "*"
      "#{TimeEntry.table_name}.issue_id IS NOT NULL"
    end
  end
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  def sql_for_issue_fixed_version_id_field(field, operator, value)
    issue_ids = Issue.where(:fixed_version_id => value.first.to_i).pluck(:id)
    case operator
    when "="
      if issue_ids.any?
        "#{TimeEntry.table_name}.issue_id IN (#{issue_ids.join(',')})"
      else
        "1=0"
      end
    when "!"
      if issue_ids.any?
        "#{TimeEntry.table_name}.issue_id NOT IN (#{issue_ids.join(',')})"
      else
        "1=1"
      end
    end
  end

173 174 175 176 177 178 179 180 181 182
  def sql_for_activity_id_field(field, operator, value)
    condition_on_id = sql_for_field(field, operator, value, Enumeration.table_name, 'id')
    condition_on_parent_id = sql_for_field(field, operator, value, Enumeration.table_name, 'parent_id')
    ids = value.map(&:to_i).join(',')
    table_name = Enumeration.table_name
    if operator == '='
      "(#{table_name}.id IN (#{ids}) OR #{table_name}.parent_id IN (#{ids}))"
    else
      "(#{table_name}.id NOT IN (#{ids}) AND (#{table_name}.parent_id IS NULL OR #{table_name}.parent_id NOT IN (#{ids})))"
    end
183 184
  end

185 186 187 188 189 190 191 192
  def sql_for_issue_tracker_id_field(field, operator, value)
    sql_for_field("tracker_id", operator, value, Issue.table_name, "tracker_id")
  end

  def sql_for_issue_status_id_field(field, operator, value)
    sql_for_field("status_id", operator, value, Issue.table_name, "status_id")
  end

193 194 195 196 197 198 199 200 201 202 203 204
  # Accepts :from/:to params as shortcut filters
  def build_from_params(params)
    super
    if params[:from].present? && params[:to].present?
      add_filter('spent_on', '><', [params[:from], params[:to]])
    elsif params[:from].present?
      add_filter('spent_on', '>=', [params[:from]])
    elsif params[:to].present?
      add_filter('spent_on', '<=', [params[:to]])
    end
    self
  end
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

  def joins_for_order_statement(order_options)
    joins = [super]

    if order_options
      if order_options.include?('issue_statuses')
        joins << "LEFT OUTER JOIN #{IssueStatus.table_name} ON #{IssueStatus.table_name}.id = #{Issue.table_name}.status_id"
      end
      if order_options.include?('trackers')
        joins << "LEFT OUTER JOIN #{Tracker.table_name} ON #{Tracker.table_name}.id = #{Issue.table_name}.tracker_id"
      end
    end

    joins.compact!
    joins.any? ? joins.join(' ') : nil
  end
221
end