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



98
99
100
101
# File 'app/models/work.rb', line 98

def self.find_work_for_day date
  Work.find(:all, :conditions => "(completed_at IS NULL OR completed_at BETWEEN '#{date}' AND '#{date+1}') AND user_id = #{current_user.id}",
  :order => 'completed_at, started_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>]



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/work.rb', line 74

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 completed_at BETWEEN '#{first.to_time.iso8601}' AND '#{last.to_time.iso8601}'", :order => 'completed_at, started_at')
   = {}
  works.map{|w| w.}.uniq.each do ||
    [.id] = [[], []]
     (0..6).each do |day|
      works_for_day = works.select {|work| (work. == ) && (work.completed_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(year, 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(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')
  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

.works_for_week_by_work_account(year, week_no, user = current_user) ⇒ Object

Return a hash with work accounts as keys an array of work hours totals per day as values: {

<#WorkAccount#1> => [  8,   7,   9,  12,   4, nil, nil],
<#WorkAccount#2> => [nil,   1, nil, nil,   4, nil, nil],
<#WorkAccount#3> => [nil, nil, nil, nil, nil,   4,   3],

]



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/work.rb', line 53

def self.(year, week_no, user = current_user)
  first_date = Date.commercial(year, week_no, 1)
  last_date = first_date + 6
  works = find(:all, :conditions => "completed_at IS NOT NULL AND completed_at BETWEEN '#{first_date.to_time.iso8601}' AND '#{(last_date+1).to_time.iso8601}'", :order => 'completed_at, started_at')
  result = {}
  works.each do |work|
    day_of_week = work.completed_at.to_date.cwday - 1
    result[work.] ||= []
    result[work.][day_of_week] ||= BigDecimal('0')
    result[work.][day_of_week] += work.hours
  end
  result.values.each {|| [6] ||= nil}
  result.values.each {|| (0..6).each {|i| [i] = nil if [i] == 0}}
  result
end

Instance Method Details

#completed_at_timeObject



118
119
120
# File 'app/models/work.rb', line 118

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

#completed_at_time=(new_value) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'app/models/work.rb', line 122

def completed_at_time=(new_value)
  if new_value == ''
    self.completed_at = nil
    return
  end
  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)


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

def started?
  completed_at.nil?
end

#started_at_timeObject



107
108
109
# File 'app/models/work.rb', line 107

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

#started_at_time=(new_value) ⇒ Object



111
112
113
114
115
116
# File 'app/models/work.rb', line 111

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