1
|
# redMine - project management software
|
2
|
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
3
|
#
|
4
|
# This program is free software; you can redistribute it and/or
|
5
|
# modify it under the terms of the GNU General Public License
|
6
|
# as published by the Free Software Foundation; either version 2
|
7
|
# of the License, or (at your option) any later version.
|
8
|
#
|
9
|
# This program is distributed in the hope that it will be useful,
|
10
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
# GNU General Public License for more details.
|
13
|
#
|
14
|
# You should have received a copy of the GNU General Public License
|
15
|
# along with this program; if not, write to the Free Software
|
16
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
17
|
|
18
|
class TimelogController < ApplicationController
|
19
|
layout 'base'
|
20
|
before_filter :find_project, :authorize
|
21
|
|
22
|
helper :sort
|
23
|
include SortHelper
|
24
|
helper :issues
|
25
|
|
26
|
def report
|
27
|
@available_criterias = { 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
|
28
|
:values => @project.versions,
|
29
|
:label => :label_version},
|
30
|
'category' => {:sql => "#{Issue.table_name}.category_id",
|
31
|
:values => @project.issue_categories,
|
32
|
:label => :field_category},
|
33
|
'member' => {:sql => "#{TimeEntry.table_name}.user_id",
|
34
|
:values => @project.users,
|
35
|
:label => :label_member},
|
36
|
'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
|
37
|
:values => Tracker.find(:all),
|
38
|
:label => :label_tracker},
|
39
|
'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
|
40
|
:values => Enumeration::get_values('ACTI'),
|
41
|
:label => :label_activity}
|
42
|
}
|
43
|
|
44
|
@criterias = params[:criterias] || []
|
45
|
@criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
|
46
|
@criterias.uniq!
|
47
|
|
48
|
@columns = (params[:period] && %w(year month week).include?(params[:period])) ? params[:period] : 'month'
|
49
|
|
50
|
if params[:date_from]
|
51
|
begin; @date_from = params[:date_from].to_date; rescue; end
|
52
|
end
|
53
|
if params[:date_to]
|
54
|
begin; @date_to = params[:date_to].to_date; rescue; end
|
55
|
end
|
56
|
@date_from ||= Date.civil(Date.today.year, 1, 1)
|
57
|
|
58
|
|
59
|
if Date.today.month == 12
|
60
|
@date_to ||= Date.civil(Date.today.year+1, 1, 1) - 1
|
61
|
else
|
62
|
@date_to ||= Date.civil(Date.today.year, Date.today.month+1, 1) - 1
|
63
|
end
|
64
|
|
65
|
|
66
|
|
67
|
unless @criterias.empty?
|
68
|
sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
|
69
|
sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
|
70
|
|
71
|
sql = "SELECT #{sql_select}, tyear, tmonth, tweek, SUM(hours) AS hours"
|
72
|
sql << " FROM #{TimeEntry.table_name} LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
|
73
|
sql << " WHERE #{TimeEntry.table_name}.project_id = %s" % @project.id
|
74
|
sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@date_from.to_time), ActiveRecord::Base.connection.quoted_date(@date_to.to_time)]
|
75
|
sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek"
|
76
|
|
77
|
@hours = ActiveRecord::Base.connection.select_all(sql)
|
78
|
|
79
|
@hours.each do |row|
|
80
|
case @columns
|
81
|
when 'year'
|
82
|
row['year'] = row['tyear']
|
83
|
when 'month'
|
84
|
row['month'] = "#{row['tyear']}-#{row['tmonth']}"
|
85
|
when 'week'
|
86
|
row['week'] = "#{row['tyear']}-#{row['tweek']}"
|
87
|
end
|
88
|
end
|
89
|
end
|
90
|
|
91
|
@periods = []
|
92
|
date_from = @date_from
|
93
|
# 100 columns max
|
94
|
while date_from < @date_to && @periods.length < 100
|
95
|
case @columns
|
96
|
when 'year'
|
97
|
@periods << "#{date_from.year}"
|
98
|
date_from = date_from >> 12
|
99
|
when 'month'
|
100
|
@periods << "#{date_from.year}-#{date_from.month}"
|
101
|
date_from = date_from >> 1
|
102
|
when 'week'
|
103
|
@periods << "#{date_from.year}-#{date_from.cweek}"
|
104
|
date_from = date_from + 7
|
105
|
end
|
106
|
end
|
107
|
|
108
|
render :layout => false if request.xhr?
|
109
|
end
|
110
|
|
111
|
def details
|
112
|
sort_init 'spent_on', 'desc'
|
113
|
sort_update
|
114
|
|
115
|
@entries = (@issue ? @issue : @project).time_entries.find(:all, :include => [:activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], :order => sort_clause)
|
116
|
|
117
|
@total_hours = @entries.inject(0) { |sum,entry| sum + entry.hours }
|
118
|
@owner_id = logged_in_user ? logged_in_user.id : 0
|
119
|
|
120
|
send_csv and return if 'csv' == params[:export]
|
121
|
render :action => 'details', :layout => false if request.xhr?
|
122
|
end
|
123
|
|
124
|
def edit
|
125
|
render_404 and return if @time_entry && @time_entry.user != logged_in_user
|
126
|
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => logged_in_user, :spent_on => Date.today)
|
127
|
@time_entry.attributes = params[:time_entry]
|
128
|
if request.post? and @time_entry.save
|
129
|
flash[:notice] = l(:notice_successful_update)
|
130
|
redirect_to :action => 'details', :project_id => @time_entry.project, :issue_id => @time_entry.issue
|
131
|
return
|
132
|
end
|
133
|
@activities = Enumeration::get_values('ACTI')
|
134
|
end
|
135
|
|
136
|
private
|
137
|
def find_project
|
138
|
if params[:id]
|
139
|
@time_entry = TimeEntry.find(params[:id])
|
140
|
@project = @time_entry.project
|
141
|
elsif params[:issue_id]
|
142
|
@issue = Issue.find(params[:issue_id])
|
143
|
@project = @issue.project
|
144
|
elsif params[:project_id]
|
145
|
@project = Project.find(params[:project_id])
|
146
|
else
|
147
|
render_404
|
148
|
return false
|
149
|
end
|
150
|
end
|
151
|
|
152
|
def send_csv
|
153
|
ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
|
154
|
export = StringIO.new
|
155
|
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
|
156
|
# csv header fields
|
157
|
headers = [l(:field_spent_on),
|
158
|
l(:field_user),
|
159
|
l(:field_activity),
|
160
|
l(:field_issue),
|
161
|
l(:field_hours),
|
162
|
l(:field_comments)
|
163
|
]
|
164
|
csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
|
165
|
# csv lines
|
166
|
@entries.each do |entry|
|
167
|
fields = [l_date(entry.spent_on),
|
168
|
entry.user.name,
|
169
|
entry.activity.name,
|
170
|
(entry.issue ? entry.issue.id : nil),
|
171
|
entry.hours,
|
172
|
entry.comments
|
173
|
]
|
174
|
csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
|
175
|
end
|
176
|
end
|
177
|
export.rewind
|
178
|
send_data(export.read, :type => 'text/csv; header=present', :filename => 'export.csv')
|
179
|
end
|
180
|
end
|