time_entry_query.rb 5.51 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 30 31 32 33 34 35 36 37 38 39
    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"),
    QueryColumn.new(:comments),
    QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"),
  ]

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

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

    principals = []
    if project
45
      principals += project.principals.visible.sort
46
      unless project.leaf?
jplang's avatar
jplang committed
47
        subprojects = project.descendants.visible.to_a
48
        if subprojects.any?
49 50
          add_available_filter "subproject_id",
            :type => :list_subprojects,
51
            :values => subprojects.collect{|s| [s.name, s.id.to_s] }
52
          principals += Principal.member_of(subprojects).visible
53 54 55 56 57
        end
      end
    else
      if all_projects.any?
        # members of visible projects
58
        principals += Principal.member_of(all_projects).visible
59 60 61 62 63 64
        # project filter
        project_values = []
        if User.current.logged? && User.current.memberships.any?
          project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
        end
        project_values += all_projects_values
65 66 67
        add_available_filter("project_id",
          :type => :list, :values => project_values
        ) unless project_values.empty?
68 69 70 71 72 73 74 75 76
      end
    end
    principals.uniq!
    principals.sort!
    users = principals.select {|p| p.is_a?(User)}

    users_values = []
    users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
    users_values += users.collect{|s| [s.name, s.id.to_s] }
77 78 79
    add_available_filter("user_id",
      :type => :list_optional, :values => users_values
    ) unless users_values.empty?
80

81
    activities = (project ? project.activities : TimeEntryActivity.shared)
82 83 84 85 86 87
    add_available_filter("activity_id",
      :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
    ) unless activities.empty?

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

89
    add_custom_fields_filters(TimeEntryCustomField)
90
    add_associations_custom_fields_filters :project, :issue, :user
91 92
  end

93 94 95
  def available_columns
    return @available_columns if @available_columns
    @available_columns = self.class.available_columns.dup
96 97 98 99
    @available_columns += TimeEntryCustomField.visible.
                            map {|cf| QueryCustomFieldColumn.new(cf) }
    @available_columns += IssueCustomField.visible.
                            map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) }
100 101 102
    @available_columns
  end

103 104 105 106
  def default_columns_names
    @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
  end

107 108 109 110 111 112
  def results_scope(options={})
    order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)

    TimeEntry.visible.
      where(statement).
      order(order_option).
113
      joins(joins_for_order_statement(order_option.join(','))).
jplang's avatar
jplang committed
114 115
      includes(:activity).
      references(:activity)
116 117 118 119 120 121 122 123 124 125 126 127
  end

  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
128 129
  end

130 131 132 133 134 135 136 137 138 139 140 141 142
  # 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
end