Class: Work
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Work
- Extended by:
- UserSystem
- Includes:
- UserSystem
- Defined in:
- app/models/work.rb
Class Method Summary collapse
- .find_work_for_day(date) ⇒ Object
-
.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>] }.
-
.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], ].
-
.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], ].
Instance Method Summary collapse
- #completed_at_time ⇒ Object
- #completed_at_time=(new_value) ⇒ Object
- #old_work_account ⇒ Object
- #started? ⇒ Boolean
- #started_at_time ⇒ Object
- #started_at_time=(new_value) ⇒ Object
- #track_times? ⇒ Boolean
- #validate ⇒ Object
- #work_account ⇒ Object
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') totals_per_work_account = {} works.map{|w| w.work_account}.uniq.each do |work_account| totals_per_work_account[work_account.id] = [[], []] (0..6).each do |day| works_for_day = works.select {|work| (work.work_account == work_account) && (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} totals_per_work_account[work_account.id][0] << invoice_day_total totals_per_work_account[work_account.id][1] << internal_day_total end end totals_per_work_account.reject! do |work_account_id, day_totals| !day_totals[0].find{|day_total| day_total > 0} && !day_totals[1].find{|day_total| day_total > 0} end totals_per_work_account 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.works_for_week_by_work_account(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.work_account] ||= [] result[work.work_account][day_of_week] ||= BigDecimal('0') result[work.work_account][day_of_week] += work.hours end result.values.each {|work_account_totals| work_account_totals[6] ||= nil} result.values.each {|work_account_totals| (0..6).each {|i| work_account_totals[i] = nil if work_account_totals[i] == 0}} result end |
Instance Method Details
#completed_at_time ⇒ Object
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_account ⇒ Object
17 |
# File 'app/models/work.rb', line 17 alias_method :old_work_account, :work_account |
#started? ⇒ Boolean
103 104 105 |
# File 'app/models/work.rb', line 103 def started? completed_at.nil? end |
#started_at_time ⇒ Object
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
22 23 24 |
# File 'app/models/work.rb', line 22 def track_times? work_account.track_times? end |
#validate ⇒ Object
13 14 15 |
# File 'app/models/work.rb', line 13 def validate errors.add(:work, "Work account is missing") unless work_account end |
#work_account ⇒ Object
18 19 20 |
# File 'app/models/work.rb', line 18 def work_account self.old_work_account || (task && task.work_account) end |