Class: Work

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/work.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.work_totals_for_week(week_no, user_id = nil) ⇒ Object

Return a hash with an array of work totals per day:

backlog1.id => [[m, t, w, t, f, s, s], [m, t, w, t, f, s, s]],
backlog2.id => [<work for monday>, 0, <work for wednesday>, <work for thursday>, <work for friday>, 0, <work for sunday>]



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/work.rb', line 44

def self.work_totals_for_week(week_no, user_id = nil)
  first = Date.commercial(Date.today.year, week_no, 1)
  last = first + 7
  works = find(:all, :conditions => "completed_at BETWEEN '#{first.to_time.iso8601}' AND '#{last.to_time.iso8601}'", :order => 'completed_at')
  totals_per_backlog = {}
  Backlog.find(:all).each do |backlog|
    totals_per_backlog[backlog.id] = [[], []]
    (0..6).each do |day|
      works_for_day = works.select {|work| work.task.backlog == backlog && work.completed_at.to_date == (first + day) && (!work.task.backlog.enable_users || (user_id && work.user_id == user_id)) }
      invoice_works_for_day = works_for_day.select {|work| work.invoice? }
      internal_works_for_day = works_for_day.select {|work| !work.invoice? }

      invoice_day_total = invoice_works_for_day.reduce(BigDecimal('0')){|total, work| total += work.hours}
      internal_day_total = internal_works_for_day.reduce(BigDecimal('0')){|total, work| total += work.hours}
      totals_per_backlog[backlog.id][0] << invoice_day_total
      totals_per_backlog[backlog.id][1] << internal_day_total
    end
  end
  totals_per_backlog.reject! do |backlog_id, day_totals|
    !day_totals[0].find{|day_total| day_total > 0} && !day_totals[1].find{|day_total| day_total > 0}
  end
  totals_per_backlog
end

.works_for_week(week_no, user) ⇒ Object

Return an array with an array of works per day: [

[m0, t0, w0, t0, f0, nil, nil],
[m1, t1, w1, t1, f1, nil, nil],
[m2, t2, nil,t2, nil,nil, nil],
[nil,t2, nil,nil,nil,nil, nil],

]



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/work.rb', line 25

def self.works_for_week(week_no, user)
  first = Date.commercial(Date.today.year, week_no, 1)
  last = first + 7
  works = find(:all, :conditions => "completed_at BETWEEN '#{first.to_time.iso8601}' AND '#{last.to_time.iso8601}'", :order => 'completed_at')
  length = 0
  works_per_day = (0..6).map do |day|
    works_for_day = works.select {|work| work.completed_at.to_date == (first + day) && (!work.task.enable_users? || (user && work.user_id == user.id)) }
    length = [length, works_for_day.length].max
    works_for_day
  end
  works_per_day.each {|works_for_day| works_for_day[length-1] ||= nil} if length > 0
  works_by_row = works_per_day.transpose
end

Instance Method Details

#completed_at_timeObject



83
84
85
# File 'app/models/work.rb', line 83

def completed_at_time
  completed_at.strftime('%H:%M')
end

#completed_at_time=(new_value) ⇒ Object



87
88
89
90
91
92
# File 'app/models/work.rb', line 87

def completed_at_time=(new_value)
  raise "invalid time format: #{new_value}" unless new_value =~ /(\d{2}):(\d{2})/
  new_hour, new_minutes = $1, $2
  t = completed_at || Time.now
  self.completed_at = Time.local(t.year, t.month, t.day, new_hour, new_minutes)
end

#started?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/work.rb', line 68

def started?
  completed_at.nil?
end

#started_at_timeObject



72
73
74
# File 'app/models/work.rb', line 72

def started_at_time
  started_at && started_at.strftime('%H:%M')
end

#started_at_time=(new_value) ⇒ Object



76
77
78
79
80
81
# File 'app/models/work.rb', line 76

def started_at_time=(new_value)
  raise "invalid time format: #{new_value}" unless new_value =~ /(\d{2}):(\d{2})/
  new_hour, new_minutes = $1, $2
  t = started_at || Time.now
  self.started_at = Time.local(t.year, t.month, t.day, new_hour, new_minutes)
end

#track_times?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/models/work.rb', line 14

def track_times?
  task.backlog.track_times?
end

#validate_user?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'app/models/work.rb', line 10

def validate_user?
  task.enable_users?
end