Class: Activity::Labor

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActivityTypeModelHelper, ExtensibleObjectHelper
Defined in:
app/models/activity/labor.rb,
app/models/activity/labor/slimtimer.rb

Instance Method Summary collapse

Methods included from ActivityTypeModelHelper

append_features

Methods included from ExtensibleObjectHelper

append_features

Constructor Details

#initialize(*args) ⇒ Labor

Returns a new instance of Labor.



4
5
6
7
8
# File 'app/models/activity/labor.rb', line 4

def initialize(*args)
  # NOTE: This must be declared before the ActivityTypeModelHelper
  super(*args)
  self.minute_duration = 0 if minute_duration.nil?
end

Instance Method Details

#as_legacy_ledger_rowObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/activity/labor.rb', line 62

def as_legacy_ledger_row

  begin
    hourly_rate = '%.2f' % employee.labor_rate_for(activity.client).hourly_rate.to_f
  rescue
    hourly_rate = ' '
  end
  
  begin
    item_name = 'Labor-'+employee.short_name
  rescue
    item_name = 'Labor'
  end
  
  [
  '%.2fhr' % (minute_duration.to_f/60),
  hourly_rate,
  '%.2f' % activity.cost.to_f,
  item_name,
  occurred_on.strftime('%m/%d/%y'),
  comments.try(:tr, "\r\n", '')
  ]
end

#clock_durationObject Also known as: duration



56
57
58
# File 'app/models/activity/labor.rb', line 56

def clock_duration
  "%02d:%02d" % [ (minute_duration/60).floor, (minute_duration % 60).round ] unless minute_duration.nil?
end

#duration=(val) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/activity/labor.rb', line 27

def duration=(val)
  val = val.to_s unless val.class.name == 'String'

  self.minute_duration = @duration = case val
    # 40, 40m
    when /^[ ]*([\d]+)[ ]*[m]?[ ]*$/ then $1.to_i
    # 1h, 1hr
    when /^[ ]*([\d]+)[ ]*(h|hr)[ ]*$/ then $1.to_i*60
    # 1h 40m, 1 40, 1:15
    when /^[ ]*([\d]+)[ ]*(?:hr|[h\:]?)[ ]*([\d]{1,2})[ ]*[m]?[ ]*$/ then $1.to_i*60+$2.to_i
    # empty string?
    when /^[ ]*$/ then 0

    else raise StandardError
  end

  rescue 
    @duration = val
end

#friendly_durationObject



47
48
49
50
51
52
53
54
# File 'app/models/activity/labor.rb', line 47

def friendly_duration
  hour = (self.minute_duration / 60).floor
  min  = (self.minute_duration % 60).round
 
  (hour > 0) ? sprintf( "%dhr %dmin", hour, min ) : "#{min}min"
  rescue
    ""
end

#nameObject



23
24
25
# File 'app/models/activity/labor.rb', line 23

def name
  type_quick_namer '%s at %s on %s', employee, client
end

#validate_durationObject



19
20
21
# File 'app/models/activity/labor.rb', line 19

def validate_duration
  errors.add :duration, 'invalid' if !@duration.nil? and self.minute_duration != @duration
end