Class: Work

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_work_for_day(date) ⇒ Object



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

def self.find_work_for_day date
  Work.find(:all, :conditions => "started_at < '#{date+1}' AND completed_at >= '#{date}' AND user_id = #{current_user.id}",
  :order => 'completed_at')
end

.work_totals_for_week(year, week_no, user = current_user) ⇒ 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>]



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/work.rb', line 52

def self.work_totals_for_week(year, week_no, user = current_user)
  first = Date.commercial(year, week_no, 1)
  last = first + 7
  works = find(:all, :conditions => "completed_at IS NOT NULL AND started_at BETWEEN '#{first.to_time.iso8601}' AND '#{last.to_time.iso8601}'", :order => 'started_at')
   = {}
  WorkAccount.find(:all).each do ||
    [.id] = [[], []]
     (0..6).each do |day|
      works_for_day = works.select {|work| (work. == ) && (work.started_at.to_date == (first + day)) && (work.user_id.nil? || (user && 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}
      [.id][0] << invoice_day_total
      [.id][1] << internal_day_total
    end
  end
  .reject! do |, day_totals|
    !day_totals[0].find{|day_total| day_total > 0} && !day_totals[1].find{|day_total| day_total > 0}
  end
  
end

.works_for_week(week_no, user = current_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],

]



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/work.rb', line 33

def self.works_for_week(week_no, user = current_user)
  first = Date.commercial(Date.today.year, week_no, 1)
  last = first + 7
  works = find(:all, :conditions => "completed_at IS NOT NULL AND started_at BETWEEN '#{first.to_time.iso8601}' AND '#{last.to_time.iso8601}'", :order => 'started_at')
  length = 0
  works_per_day = (0..6).map do |day|
    works_for_day = works.select {|work| work.started_at.to_date == (first + day) && (work.user_id.nil? || (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



96
97
98
# File 'app/models/work.rb', line 96

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

#completed_at_time=(new_value) ⇒ Object



100
101
102
103
104
105
# File 'app/models/work.rb', line 100

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

#old_work_accountObject



17
# File 'app/models/work.rb', line 17

alias_method :old_work_account, :work_account

#started?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/work.rb', line 81

def started?
  completed_at.nil?
end

#started_at_timeObject



85
86
87
# File 'app/models/work.rb', line 85

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

#started_at_time=(new_value) ⇒ Object



89
90
91
92
93
94
# File 'app/models/work.rb', line 89

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)


22
23
24
# File 'app/models/work.rb', line 22

def track_times?
  .track_times?
end

#validateObject



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

def validate
  errors.add(:work, "Work account is missing") unless 
end

#work_accountObject



18
19
20
# File 'app/models/work.rb', line 18

def 
  self. || (task && task.)
end