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

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



272
273
274
275
276
277
278
279
# File 'app/controllers/works_controller.rb', line 272

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



263
264
265
266
267
268
269
270
# File 'app/controllers/works_controller.rb', line 263

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_task_idObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'app/controllers/works_controller.rb', line 281

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



254
255
256
257
258
259
260
261
# File 'app/controllers/works_controller.rb', line 254

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



301
302
303
304
305
# File 'app/controllers/works_controller.rb', line 301

def calculate_hours
  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
end

#createObject



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 50

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
    @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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/controllers/works_controller.rb', line 197

def daily_work_sheet
  @date = (params[:id] && Date.parse(params[:id])) || Date.today
  @year = @date.year
  @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



190
191
192
193
194
195
# File 'app/controllers/works_controller.rb', line 190

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



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

def index
  list
end

#listObject



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

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



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

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



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

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

#timelisteObject



245
246
247
248
249
250
251
252
# File 'app/controllers/works_controller.rb', line 245

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



307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'app/controllers/works_controller.rb', line 307

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
# 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)
  @new_work = flash[:work] || Work.new(@works.last && @works.last.completed_at ? {:started_at => @works.last.completed_at} : nil)
end

#update_timeObject



156
157
158
159
160
# File 'app/controllers/works_controller.rb', line 156

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

#update_workObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/works_controller.rb', line 162

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



232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/controllers/works_controller.rb', line 232

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



222
223
224
225
226
227
228
229
230
# File 'app/controllers/works_controller.rb', line 222

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