Class: TimeEntryReportsController

Inherits:
ApplicationController show all
Includes:
CustomFieldsHelper, SortHelper, TimelogHelper
Defined in:
app/controllers/time_entry_reports_controller.rb

Constant Summary

Constants included from ApplicationHelper

ApplicationHelper::HEADING_RE, ApplicationHelper::TOC_RE

Instance Method Summary collapse

Methods included from CustomFieldsHelper

#custom_field_formats_for_select, #custom_field_label_tag, #custom_field_tag, #custom_field_tag_for_bulk_edit, #custom_field_tag_with_label, #custom_fields_tabs, #format_value, #render_api_custom_values, #show_value

Methods included from TimelogHelper

#activity_collection_for_select_options, #entries_to_csv, #format_criteria_value, #options_for_period_select, #render_timelog_breadcrumb, #report_criteria_to_csv, #report_to_csv, #select_hours, #sum_hours, #to_utf8

Methods included from ApplicationHelper

#accesskey, #api_meta, #authoring, #authorize_for, #avatar, #back_url_hidden_field_tag, #body_css_classes, #breadcrumb, #calendar_for, #check_all_links, #checked_image, #content_for, #context_menu, #context_menu_link, #current_theme, #due_date_distance_in_words, #favicon, #format_activity_day, #format_activity_description, #format_activity_title, #format_version_name, #has_content?, #heads_for_theme, #html_hours, #html_title, #image_to_function, #include_calendar_headers_tags, #include_in_api_response?, #javascript_heads, #label_tag_for, #labelled_tabular_form_for, #lang_options_for_select, #link_to_attachment, #link_to_if_authorized, #link_to_issue, #link_to_message, #link_to_project, #link_to_remote_if_authorized, #link_to_revision, #link_to_user, #other_formats_links, #page_header_title, #pagination_links_full, #parse_headings, #parse_inline_attachments, #parse_non_pre_blocks, #parse_redmine_links, #parse_wiki_links, #path_to_stylesheet, #per_page_links, #principals_check_box_tags, #progress_bar, #project_nested_ul, #project_tree, #project_tree_options_for_select, #prompt_to_remote, #render_flash_messages, #render_page_hierarchy, #render_project_jump_box, #render_tabs, #reorder_links, #replace_toc, #simple_format_without_paragraph, #stylesheet_path, #syntax_highlight, #textilizable, #time_tag, #to_path_param, #toggle_link, #truncate_lines, #truncate_single_line

Methods included from Redmine::I18n

#current_language, #day_name, #find_language, #format_date, #format_time, included, #l, #l_hours, #l_or_humanize, #ll, #month_name, #set_language_if_valid, #valid_languages

Methods included from Redmine::WikiFormatting::Macros::Definitions

#exec_macro, #extract_macro_options

Methods included from SortHelper

#sort_clause, #sort_clear, #sort_header_tag, #sort_init, #sort_link, #sort_name, #sort_update

Methods inherited from ApplicationController

accept_key_auth, #accept_key_auth_actions, #api_key_from_request, #api_offset_and_limit, #api_request?, #authorize, #authorize_global, #back_url, #check_if_login_required, #check_project_privacy, #check_project_uniqueness, #default_template, #delete_broken_cookies, #deny_access, #filename_for_content_disposition, #find_current_user, #find_issues, #find_model_object, #find_project, #find_project_by_project_id, #find_project_from_association, #invalid_authenticity_token, #logged_user=, model_object, #object_errors_to_json, #parse_qvalues, #per_page_option, #pick_layout, #query_statement_invalid, #redirect_back_or_default, #render_403, #render_404, #render_attachment_warning_if_needed, #render_error, #render_feed, #render_validation_errors, #require_admin, #require_login, #set_flash_from_bulk_issue_save, #set_localization, #use_layout, #user_setup

Methods included from Redmine::MenuManager::MenuController

#current_menu_item, included, #menu_items, #redirect_to_project_menu_item

Methods included from Redmine::Search::Controller

#default_search_scope, #default_search_scopes, included

Instance Method Details

#reportObject



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
# File 'app/controllers/time_entry_reports_controller.rb', line 14

def report
  @criterias = params[:criterias] || []
  @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
  @criterias.uniq!
  @criterias = @criterias[0,3]
  
  @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
  
  retrieve_date_range
  
  unless @criterias.empty?
    sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
    sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
    sql_condition = ''
    
    if @project.nil?
      sql_condition = Project.allowed_to_condition(User.current, :view_time_entries)
    elsif @issue.nil?
      sql_condition = @project.project_condition(Setting.display_subprojects_issues?)
    else
      sql_condition = "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
    end

    sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
    sql << " FROM #{TimeEntry.table_name}"
    sql << time_report_joins
    sql << " WHERE"
    sql << " (%s) AND" % sql_condition
    sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)]
    sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
    
    @hours = ActiveRecord::Base.connection.select_all(sql)
    
    @hours.each do |row|
      case @columns
      when 'year'
        row['year'] = row['tyear']
      when 'month'
        row['month'] = "#{row['tyear']}-#{row['tmonth']}"
      when 'week'
        row['week'] = "#{row['tyear']}-#{row['tweek']}"
      when 'day'
        row['day'] = "#{row['spent_on']}"
      end
    end
    
    @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
    
    @periods = []
    # Date#at_beginning_of_ not supported in Rails 1.2.x
    date_from = @from.to_time
    # 100 columns max
    while date_from <= @to.to_time && @periods.length < 100
      case @columns
      when 'year'
        @periods << "#{date_from.year}"
        date_from = (date_from + 1.year).at_beginning_of_year
      when 'month'
        @periods << "#{date_from.year}-#{date_from.month}"
        date_from = (date_from + 1.month).at_beginning_of_month
      when 'week'
        @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
        date_from = (date_from + 7.day).at_beginning_of_week
      when 'day'
        @periods << "#{date_from.to_date}"
        date_from = date_from + 1.day
      end
    end
  end
  
  respond_to do |format|
    format.html { render :layout => !request.xhr? }
    format.csv  { send_data(report_to_csv(@criterias, @periods, @hours), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
  end
end