class Work < ActiveRecord::Base extend UserSystem include UserSystem belongs_to :task belongs_to :user belongs_to :work_account validates_associated :task validates_presence_of :work_account validates_presence_of :started_at, :if => :track_times? def validate errors.add(:work, "Work account is missing") unless work_account end alias_method :old_work_account, :work_account def work_account self.old_work_account || (task && task.work_account) end def track_times? work_account && work_account.track_times? end # 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], # ] 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 # 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], # ] 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 # 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 => [, 0, , , , 0, ] # } 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 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 def started? completed_at.nil? end def started_at_time started_at && started_at.strftime('%H:%M') end 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 def completed_at_time completed_at && completed_at.strftime('%H:%M') end 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 end