Class: TimeEntry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/time_entry.rb

Overview

create_table :time_entries do |t|

t.datetime :from_datetime
t.datetime :thru_datetime
t.integer :regular_hours_in_seconds
t.integer :overtime_hours_in_seconds
t.text :comment
t.references :timesheet
t.references :work_effort

t.timestamps

end

add_index :time_entries, :timesheet_id add_index :time_entries, :work_effort_id

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open_entriesObject



25
26
27
28
29
# File 'app/models/time_entry.rb', line 25

def open_entries
  where(TimeEntry.arel_table[:from_datetime].not_eq(nil))
      .where(thru_datetime: nil)
      .where(TimeEntry.arel_table[:manual_entry].eq(nil).or(TimeEntry.arel_table[:manual_entry].eq(false)))
end

.scope_by_party(party) ⇒ Object

entries scoped by party

Parameters:

  • party (Party)

    party to get time_entries by



44
45
46
# File 'app/models/time_entry.rb', line 44

def scope_by_party(party)
  joins(:timesheet => :timesheet_party_roles).where('timesheet_party_roles.party_id' => party)
end

.scope_by_work_effort(work_effort) ⇒ ActiveRecord::Relation

scope by work_effort

or an array of WorkEffort ids

Parameters:

  • work_effort (Integer | WorkEffort | Array)

    either a id of WorkEffort record, a WorkEffort record, an array of WorkEffort records

Returns:

  • (ActiveRecord::Relation)


37
38
39
# File 'app/models/time_entry.rb', line 37

def scope_by_work_effort(work_effort)
  where(work_effort_id: work_effort)
end

.total_formatted(opts) ⇒ String

total seconds by work_effort formatted as HH:MM:SS

Parameters:

  • opts (Hash)

    opts to calculate the total with

Options Hash (opts):

  • :work_effort (WorkEffort)

    WorkEffort to get total hours for

  • :party (Party)

    Party to get total hours for

  • :start (Date)

    start date range

  • :end (Date)

    end date range

Returns:

  • (String)

    HH:MM:SS



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/time_entry.rb', line 118

def total_formatted(opts)
  _total_seconds = total_seconds(opts)

  if _total_seconds.nil? or _total_seconds == 0
    '00:00:00'
  else
    seconds =_total_seconds % 60
    minutes = (_total_seconds / 60) % 60
    hours = _total_seconds / (60 * 60)

    format("%02d:%02d:%02d", hours, minutes, seconds)
  end
end

.total_hours(opts) ⇒ BigDecimal

total hours as decimal round to the nearest 15th

Parameters:

  • opts (Hash)

    opts to calculate the total with

Options Hash (opts):

  • :work_effort (WorkEffort)

    WorkEffort to get total hours for

  • :party (Party)

    Party to get total hours for

  • :start (Date)

    start date range

  • :end (Date)

    end date range

Returns:

  • (BigDecimal)

    hours



98
99
100
101
102
103
104
105
106
107
# File 'app/models/time_entry.rb', line 98

def total_hours(opts)
  _total_seconds = total_seconds(opts)

  if _total_seconds.nil? or _total_seconds == 0
    0
  else
    # get hours by dividing seconds by 3600 then get fractional minutes
    ((_total_seconds / 3600).floor + (((_total_seconds % 3600) / 60)) / 100.0)
  end
end

.total_seconds(opts) ⇒ Integer

total seconds by work_effort

Parameters:

  • opts (Hash)

    opts to calculate the total with

Options Hash (opts):

  • :work_effort (WorkEffort)

    WorkEffort to get total hours for

  • :party (Party)

    Party to get total hours for

  • :start (Date)

    start date range

  • :end (Date)

    end date range

Returns:

  • (Integer)

    total seconds



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/time_entry.rb', line 57

def total_seconds(opts)
  seconds = 0

  statement = self

  time_entry_arel_tbl = self.arel_table

  if opts[:work_effort]
    statement = statement.scope_by_work_effort(opts[:work_effort])
  end

  if opts[:party]
    statement = statement.scope_by_party(opts[:party])
  end

  if opts[:start]
    statement = statement.where(time_entry_arel_tbl[:from_datetime].gteq(opts[:start].utc).
                                    or(time_entry_arel_tbl[:manual_entry_start_date].gteq(opts[:start].utc)))
  end

  if opts[:end]
    statement = statement.where(time_entry_arel_tbl[:from_datetime].lteq(opts[:end].utc).
                                    or(time_entry_arel_tbl[:manual_entry_start_date].lteq(opts[:end].utc)))
  end

  statement.each do |time_entry|
    seconds += ((time_entry.regular_hours_in_seconds || 0) + (time_entry.overtime_hours_in_seconds || 0))
  end

  seconds
end

Instance Method Details

#calculate_regular_hours_in_seconds!Object



133
134
135
136
# File 'app/models/time_entry.rb', line 133

def calculate_regular_hours_in_seconds!
  self.regular_hours_in_seconds = ((thru_datetime - from_datetime)).to_i
  self.save!
end

#create_work_effort_relationship(work_effort) ⇒ TimeEntry

create relationship between a TimeEntry and WorkEffort

Parameters:

  • work_effort (WorkEffort)

    work_effort to relate this time_entry to

Returns:



147
148
149
150
151
# File 'app/models/time_entry.rb', line 147

def create_work_effort_relationship(work_effort)
  self.work_effort_id = work_effort.id

  self
end

#destroy_work_effort_relationshipTimeEntry

destroy relationship between a TimeEntry and WorkEffort

Returns:



156
157
158
159
160
# File 'app/models/time_entry.rb', line 156

def destroy_work_effort_relationship
  self.work_effort_id = nil

  self
end

#find_party_by_role(role_type) ⇒ Party

finds party with passed role to this TimeEntry

Parameters:

  • role_type (RoleType)

    role type to use in the association

Returns:

  • (Party)

    party



214
215
216
# File 'app/models/time_entry.rb', line 214

def find_party_by_role(role_type)
  self.timesheet.find_party_by_role(role_type)
end

#hoursBigDecimal

get fractional hours for this TimeEntry

Returns:

  • (BigDecimal)

    hours



199
200
201
202
203
204
205
206
207
208
# File 'app/models/time_entry.rb', line 199

def hours
  _total_seconds = (self.regular_hours_in_seconds || 0) + (self.overtime_hours_in_seconds || 0)

  if _total_seconds.nil? or _total_seconds == 0
    0
  else
    # get hours by dividing seconds by 3600 then get fractional minutes
    ((_total_seconds / 3600).floor + (((_total_seconds % 3600) / 60)) / 100.0)
  end
end

#overtime_hours_formattedString

overtime hours formatted as HH:MM:SS

Returns:

  • (String)

    HH:MM:SS



184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/time_entry.rb', line 184

def overtime_hours_formatted
  if self.overtime_hours_in_seconds.nil? or self.overtime_hours_in_seconds == 0
    '00:00:00'
  else
    seconds = self.overtime_hours_in_seconds % 60
    minutes = (self.overtime_hours_in_seconds / 60) % 60
    hours = self.overtime_hours_in_seconds / (60 * 60)

    format("%02d:%02d:%02d", hours, minutes, seconds)
  end
end

#regular_hours_formattedString

regular hours formatted as HH:MM:SS

Returns:

  • (String)

    HH:MM:SS



169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/time_entry.rb', line 169

def regular_hours_formatted
  if self.regular_hours_in_seconds.nil? or self.regular_hours_in_seconds == 0
    '00:00:00'
  else
    seconds = self.regular_hours_in_seconds % 60
    minutes = (self.regular_hours_in_seconds / 60) % 60
    hours = self.regular_hours_in_seconds / (60 * 60)

    format("%02d:%02d:%02d", hours, minutes, seconds)
  end
end

#to_data_hashObject



218
219
220
221
222
223
224
225
226
227
228
# File 'app/models/time_entry.rb', line 218

def to_data_hash
  to_hash(only: [:id,
                 :regular_hours_in_seconds,
                 :overtime_hours_in_seconds,
                 :comment],
          from_datetime: (from_datetime.nil? ? nil : from_datetime.utc.iso8601),
          thru_datetime: (thru_datetime.nil? ? nil : thru_datetime.utc.iso8601),
          updated_at: (updated_at.nil? ? nil : updated_at.utc.iso8601),
          created_at: (created_at.nil? ? nil : created_at.utc.iso8601)
  )
end

#update_task_assignment_status(status) ⇒ Object

Sets the current status of the WorkEffortAssignment to In Progress

Parameters:

  • status (String)

    Internal Identifier of TrackedStatusType to set



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/models/time_entry.rb', line 243

def update_task_assignment_status(status)
  # make sure this TimeEntry is related to a WorkEffort
  if self.work_effort
    work_resource_role_types = RoleType.find_child_role_types(['work_resource'])

    # find the party with work_resource related to this TimeEntry
    work_resource_party = self.find_party_by_role(work_resource_role_types)

    assignment = work_effort.work_effort_party_assignments.where(party_id: work_resource_party)
                     .where(role_type_id: work_resource_role_types).first

    if assignment
      assignment.current_status = status
    end
  end
end

#update_task_status(status) ⇒ Object

Sets the current status of the WorkEffort to In Progress

Parameters:

  • status (String)

    Internal Identifier of TrackedStatusType to set



233
234
235
236
237
238
# File 'app/models/time_entry.rb', line 233

def update_task_status(status)
  # make sure this TimeEntry is related to a WorkEffort
  if self.work_effort
    work_effort.current_status = status
  end
end