Class: WorksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/works_controller.rb

Constant Summary

Constants included from Localization

Localization::LOCALIZED_STRINGS

Instance Method Summary collapse

Methods inherited from ApplicationController

in_place_edit_for, #initialize, #render_to_string

Methods included from ApplicationHelper

#back_or_link_to, #detour?, #detour_to, #display_notice, #h, #image_button_to, #image_detour_to, #image_link_to, #image_link_to_remote, #insert, #record, #resolution_image, #t, #update_task, #with_detour

Methods included from Localization

#l, load_localized_strings, #valid_language?

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#auto_complete_for_work_backlog_nameObject



278
279
280
281
282
283
284
285
# File 'app/controllers/works_controller.rb', line 278

def auto_complete_for_work_backlog_name
  @backlogs = Backlog.find(:all, 
                           :conditions => [ 'LOWER(name) LIKE ?',
  '%' + params[:work][:backlog_name].downcase + '%' ], 
  :order => 'name ASC',
  :limit => 16)
  render :partial => '/backlogs/name_list'
end

#auto_complete_for_work_customer_nameObject



269
270
271
272
273
274
275
276
# File 'app/controllers/works_controller.rb', line 269

def auto_complete_for_work_customer_name
  @customers = Customer.find(:all, 
                             :conditions => [ 'LOWER(name) LIKE ?',
  '%' + params[:work][:customer_name].downcase + '%' ], 
  :order => 'name ASC',
  :limit => 16)
  render :partial => '/customers/name_list'
end

#auto_complete_for_work_descriptionObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/controllers/works_controller.rb', line 307

def auto_complete_for_work_description
  max = 16
  works = Work.find(:all, :select => "description, count(*) as freq", 
  :conditions => [ 'LOWER(description) LIKE ? AND work_account_id = ?', '%' + params[:description].downcase + '%', params[:work_account_id] ],
  :group => "description",
  :order => 'freq DESC, description ASC',
  :limit => max)
  
  if works.size < max
    works += Work.find(:all, :select => "description, count(*) as freq", 
    :conditions => [ 'LOWER(description) LIKE ? AND work_account_id <> ?', '%' + params[:description].downcase + '%', params[:work_account_id] ],
    :group => "description",
    :order => 'freq DESC, description ASC',
    :limit => max - works.size)
  end    
  works = works.map{|w| w.description}
  works.uniq!
  works = works[0..max-1]
  
  items = works.map{|w| %Q[<li class="description"><div class="name">#{w.gsub(/(#{params[:description]})/i, '<b>\1</b>')}</div></li>]}
  description_list = %Q{<ul class="work"> #{items} </ul>}
  render :text => description_list
end

#auto_complete_for_work_task_idObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'app/controllers/works_controller.rb', line 287

def auto_complete_for_work_task_id
  @tasks = []
  if (search_id = params[:work][:task_id].to_i) > 0
    @tasks += Task.find(:all,
                        :conditions => [ "id = ? OR id BETWEEN ? AND ?",
    search_id, search_id * 10, ((search_id+1) * 10 -1) ], 
    :order => 'id',
    :limit => 10)
  end
  
  
  @tasks += Task.find(:all,
                      :conditions => [ "finished_at IS NULL AND LOWER(description) LIKE ?",
                      '%' + params[:work][:task_id].downcase + '%' ], 
  :order => 'description ASC',
  :limit => 10)
  
  render :partial => 'task_id_list'
end

#auto_complete_for_work_work_account_nameObject



260
261
262
263
264
265
266
267
# File 'app/controllers/works_controller.rb', line 260

def 
  @accounts = WorkAccount.find(:all, 
                               :conditions => [ 'LOWER(name) LIKE ?',
  '%' + params[:work][:work_account_name].downcase + '%' ], 
  :order => 'name ASC',
  :limit => 16)
  render :partial => '/work_accounts/name_list'
end

#calculate_hoursObject



331
332
333
334
335
336
337
338
339
# File 'app/controllers/works_controller.rb', line 331

def calculate_hours
  if params[:started_at] && params[:completed_at]
    calculated_duration = (Time.parse(params[:completed_at]) - Time.parse(params[:started_at]))
    new_value = '%2d:%02d' % [calculated_duration / 3600, (calculated_duration % 3600) / 60]
    render :update do |page| page['work_hours_time'].value = new_value end
  else
    render :update do |page| page['work_hours_time'].value = '' end
  end
end

#createObject



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

def create
  if params[:work]
    backlog_name = params[:work].delete(:backlog_name)
    task_description = params[:work].delete(:task_description)
    if backlog_name && backlog_name.size > 0 && task_description && task_description.size > 0
      backlog = Backlog.find_by_name(backlog_name)
      task = backlog.tasks_with_children.find{|t| t.description == task_description}
      params[:work][:task_id] = task.id if task
    end
    
    convert_customer_param
    unless convert_hours_param
      flash[:notice] = "Illegal time format"
      #@work.errors.add :hours_time, "Illegal time format."
      params[:work].delete(:hours_time)
    end
    params[:work][:task_id].strip! unless params[:work][:task_id].nil?
    @work = Work.new(params[:work])
    @work.started_on ||= Date.today
    #@work.completed_at = Time.now unless @work.start_time || @work.completed_at
    @work.calculate_hours! unless @work.hours && @work.hours.to_f > 0
    @work.user_id = current_user.id
  end
  if @work && @work.save
    flash[:notice] = 'Work was successfully created.'
    @work.task.grab if @work.task
  else
    if params[:detour]
      flash[:notice] = 'Error creating new work record.'
      flash[:work] = @work
      back
    else
      new
      render :action => 'new'
    end
    return
  end
  
  @work.task.estimates.create!(params[:estimate]) if @work.task && params[:estimate]
  
  if @work.task
    back_or_redirect_to :controller => 'periods', :action => 'show', :id => @work.task && @work.task.period, :task_id => @work.task && @work.task.id
  else
    back_or_redirect_to :controller => 'works', :action => 'daily_work_sheet', :id => @work.started_on.strftime('%Y-%m-%d')
  end
end

#daily_work_sheetObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/controllers/works_controller.rb', line 203

def daily_work_sheet
  @date = (params[:id] && Date.parse(params[:id])) || Date.today
  @year = @date.cwyear
  @week = @date.cweek
  if params[:user_id]
    @user = User.find_by_id(params[:user_id])
    if @user.nil?
      flash[:notice] = l(:cannot_find_user, params[:user_id])
      back_or_redirect_to '/'
      return
    end
  else
    @user = current_user
  end
  @periods = []
  @works = Work.find_work_for_day(@date, @user)
  @customers = Customer.find(:all, :order => :name)
  @started_works = Task.find_started
  @new_work = flash[:work] || Work.new(@works.last && @works.last.completed_at ? {:started_at => @works.last.completed_at} : nil)
  @work_accounts = WorkAccount.find(:all, :order => :name)
  @absence = Absence.find(:first, :conditions => {:on => @date, :user_id => @user.id})
  @public_holiday = PublicHoliday.find(:first, :conditions => {:on => @date})
  render :layout => 'wide'
end

#destroyObject



196
197
198
199
200
201
# File 'app/controllers/works_controller.rb', line 196

def destroy
  work = Work.find(params[:id])
  period = work.task && work.task.period
  work.destroy
  back_or_redirect_to :controller => 'periods', :action => :list_work, :id => period 
end

#editObject



118
119
120
121
122
123
124
125
126
# File 'app/controllers/works_controller.rb', line 118

def edit
  @work ||= Work.find(params[:id])
  @work.attributes = params[:work]
  @estimate = Estimate.new(params[:estimate])
  @work_accounts = WorkAccount.find(:all)
  @tasks = Task.find_open
  @tasks.unshift @work.task if @work.task
  @users = User.find(:all)
end

#indexObject



8
9
10
# File 'app/controllers/works_controller.rb', line 8

def index
  list
end

#listObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/works_controller.rb', line 16

def list
   = (params[:work_account_id] && WorkAccount.find(params[:work_account_id]).name) || l(:all_accounts)
  @report_filter = WorksReportFilter.new(params[:report_filter])
  @report_filter.title = "#{l :hours} for #{} #{@report_filter.start_on && @report_filter.start_on.strftime('%Y-%m-%d - ')}#{@report_filter.end_on && @report_filter.end_on.strftime('%Y-%m-%d')}"
  @period = params[:id] && Period.find(params[:id])
  @works = Work.paginate :conditions => ["started_on BETWEEN ? AND ? #{'AND work_account_id = ?' if @report_filter.} #{@report_filter.invoice.nil? ? '' : 'AND invoice = ?'} #{@report_filter.user_id.nil? ? '' : 'AND user_id = ?'}", @report_filter.start_on, @report_filter.end_on, @report_filter., @report_filter.invoice, @report_filter.user_id].compact,
  :page => params[:page], :per_page => @report_filter.page_size,
  :order => 'started_on, start_time, completed_at'
  @work_accounts = WorkAccount.find(:all, :order => :name)
  @users = User.find(:all, :order => 'first_name, last_name')
  if params[:export] == 'excel'
    @works = @works.sort_by {|w| [w.user_id || 0, w.task_id || 999999, w.started_on]}
    render :template => '/works/list_excel', :layout => false
  else
    render :template => '/works/list'
  end
end

#newObject



38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/works_controller.rb', line 38

def new
  unless @work
    @work = Work.new(params[:work])
    @work.user = user
  end
  @estimate ||= Estimate.new(params[:estimate])
  @work_accounts = WorkAccount.find(:all, :order => :name)
  @tasks = Task.find_open
  @users = User.find(:all)
end

#showObject



34
35
36
# File 'app/controllers/works_controller.rb', line 34

def show
  @work = Work.find(params[:id])
end

#timelisteObject



251
252
253
254
255
256
257
258
# File 'app/controllers/works_controller.rb', line 251

def timeliste
  @year = (params[:year] && params[:year].to_i) || Date.today.year
  @week = (params[:week] && params[:week].to_i) || Date.today.cweek
  @work_totals_per_work_account = Work.work_totals_for_week(@year, @week)
  headers["Content-Type"] = "application/vnd.ms-excel"
  headers["Content-Disposition"] = 'attachment; filename="export.xml"'
  render :layout => false
end

#updateObject



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/works_controller.rb', line 128

def update
  if update_work
    if @work.task
      back_or_redirect_to :controller => 'periods', :action => 'show', :id => @work.task.period, :task_id => @work.task.id
    else
      back_or_redirect_to :controller => 'welcome', :action => 'index'
    end
  else
    @task = @work.task
    edit
    render :action => 'edit'
  end
end

#update_new_rowObject



341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'app/controllers/works_controller.rb', line 341

def update_new_row
  @field = params[:field]
  @next_field = params[:next_field]
  
  convert_customer_param
  raise "Unknown time format: #{params[:hours_time]}" unless convert_hours_param
  @work = Work.new(params[:work])
  @updated_fields = [@field]
  if @work.hours == 0 && @work.started_on && @work.start_time && @work.completed_at
    @work.calculate_hours!
    @updated_fields << :hours_time
  end
end

#update_rowObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/works_controller.rb', line 142

def update_row
  if update_work
    #flash.discard
    @next_field = params[:next_field] || 'description'
    @works = Work.find_work_for_day((@work.started_on || @work.completed_at).to_date)
    @day_total = @works.inject(BigDecimal('0')){|total,work|total+=work.hours}
  else
    @next_field = params[:field] || 'description'
  end
  @work_accounts = WorkAccount.find(:all, :order => :name)
  @customers = Customer.find(:all, :order => :name)
  if flash[:work]
    @new_work = flash[:work]
  elsif @works
    @new_work = Work.new((@works.last && @works.last.completed_at) ? {:started_at => @works.last.completed_at} : nil)
  else
    @new_work = Work.new
  end
end

#update_timeObject



162
163
164
165
166
# File 'app/controllers/works_controller.rb', line 162

def update_time
  update_work
  flash.discard
  @field = params[:field] || 'start_time'
end

#update_workObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/works_controller.rb', line 168

def update_work
  @work = Work.find(params[:id])
  unless convert_hours_param
    flash[:notice] = "Illegal time format"
    @work.errors.add :hours_time, "Illegal time format."
    return false
  end
  if @work.update_attributes(params[:work])
    if params[:work] && (params[:work][:start_time] || params[:work][:completed_at_time])
      @work.calculate_hours!
      @work.save!
    end
    flash[:notice] = 'Work was successfully updated.'
    if @work.task
      if params[:estimate]
        if (@estimate = @work.task.estimates.create(params[:estimate])).errors.size == 0
          return true
        end
      else
        return true
      end
    else
      return true
    end
  end
  return false
end

#weekly_work_sheetObject



238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/controllers/works_controller.rb', line 238

def weekly_work_sheet
  @year = (params[:year] && params[:year].to_i) || Date.today.year
  @week = (params[:week] && params[:week].to_i) || Date.today.cweek
  @user = params[:user_id] ? User.find(params[:user_id]) : current_user
  @work_accounts = Work.(@year, @week, @user)
  @first_date = Date.commercial(@year, @week, 1)
  @last_date = @first_date + 6
  @lock = WorkLock.find_by_week(@year, @week)
  @absences = Absence.find_by_week(@year, @week, @user)
  @public_holidays = PublicHoliday.find_by_week(@year, @week)
  render :layout => 'wide'
end

#weekly_work_sheet_detailsObject



228
229
230
231
232
233
234
235
236
# File 'app/controllers/works_controller.rb', line 228

def weekly_work_sheet_details
  @year = (params[:year] && params[:year].to_i) || Date.today.year
  @week = (params[:week] && params[:week].to_i) || Date.today.cweek
  @rows = Work.works_for_week(@year, @week)
  @first_date = Date.commercial(@year, @week, 1)
  @last_date = @first_date + 6
  @lock = WorkLock.find(:first, :conditions => ['user_id = ? AND start_on <= ? AND end_on >= ?', current_user.id, @first_date, @last_date])
  render :layout => 'wide'
end