Class: TimeEntryReportsController

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

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

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.

See doc/COPYRIGHT.rdoc for more details. ++

Constant Summary

Constant Summary

Constants included from ApplicationHelper

ApplicationHelper::HEADING_RE, ApplicationHelper::RELATIVE_LINK_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_for_timelogs

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, #expand_current_menu, #favicon, #format_activity_day, #format_activity_description, #format_activity_title, #format_version_name, #gravatar, #has_content?, #heads_for_theme, #help_menu_item, #html_hours, #html_title, #image_to_function, #include_in_api_response?, #javascript_heads, #jquery_datepicker_settings, #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, #list_users, #main_top_menu_items, #more_top_menu_items, #other_formats_links, #page_header_title, #pagination_links_full, #parse_headings, #parse_inline_attachments, #parse_non_pre_blocks, #parse_redmine_links, #parse_relative_urls, #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, #projects_check_box_tags, #prompt_to_remote, #render_flash_messages, #render_page_hierarchy, #render_project_jump_box, #render_tabs, #reorder_links, #replace_toc, #robot_exclusion_tag, #simple_format_without_paragraph, #split_top_menu_into_main_or_more_menus, #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 SortHelper

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

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

- (Object) report



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

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
    while date_from <= @to.to_time
      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