Class: Api::V1::TimeEntriesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/time_entries_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/api/v1/time_entries_controller.rb', line 5

def create
  begin
    ActiveRecord::Base.connection.transaction do
      party = current_user.party

      time_entry = TimeEntry.new(
        manual_entry: true
      )

      if params[:from_datetime]
        time_entry.from_datetime = params[:from_datetime].to_time
      end

      if params[:thru_datetime]
        time_entry.from_datetime = params[:thru_datetime].to_time
      end

      if params[:comment]
        time_entry.comment = params[:comment].strip
      end

      if params[:regular_hours_in_seconds]
        time_entry.regular_hours_in_seconds = params[:regular_hours_in_seconds].to_i
      end

      if params[:overtime_hours_in_seconds]
        time_entry.overtime_hours_in_seconds = params[:regular_hours_in_seconds].to_i
      end

      if params[:work_effort_id]
        time_entry.work_effort = params[:work_effort_id]
      end

      # if a timesheet id is passed assoicate to that timesheet if not associate to
      # the current user's timesheet
      if params[:timesheet_id]
        time_entry.timesheet_id = params[:timesheet_id]
      else
        time_sheet = party.timesheets.current!(current_user.party, RoleType.iid('work_resource'))
        time_entry.timesheet_id = time_sheet.id
      end

      render json: {
        success: true,
        time_entry: time_entry.to_data_hash,
      }

    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors

    render :json => {:success => false, :message => invalid.record.errors}
  rescue StandardError => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render json: {success: false, message: 'Error creating Time Entry'}
  end
end

#openObject

if a work effort id is passed get the last open time_entry for that work_effort for the current user if there are no open time entries then return only the totals



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'app/controllers/api/v1/time_entries_controller.rb', line 231

def open
  if params[:work_effort_id]
    work_effort = WorkEffort.find(params[:work_effort_id])
    party = current_user.party

    open_time_entry = work_effort.time_entries.scope_by_party(current_user.party).open_entries.first
    time_helper = ErpBaseErpSvcs::Helpers::Time::Client.new(params[:client_utc_offset])

    render :json => {success: true,
                     time_entry: open_time_entry.nil? ? nil : open_time_entry.to_data_hash,
                     day_total_formatted: TimeEntry.total_formatted(work_effort: work_effort,
                                                                    party: party,
                                                                    start: time_helper.beginning_of_day,
                                                                    end: time_helper.end_of_day),
                     week_total_formatted: TimeEntry.total_formatted(work_effort: work_effort,
                                                                     party: party,
                                                                     start: time_helper.beginning_of_week,
                                                                     end: time_helper.end_of_week)
                     }
  else
    render :json => {success: true, time_entries: TimeEntry.open_entries.collect { |time_entry| time_entry.to_data_hash }}
  end
end

#showObject



116
117
118
119
120
# File 'app/controllers/api/v1/time_entries_controller.rb', line 116

def show
  time_entry = TimeEntry.find(params[:id])

  render :json => {success: true, time_entry: time_entry.to_data_hash}
end

#startObject

start TimeEntry by setting the from_datetime but not the thru_datetime if there is already an open time_entry do not let another one be started It is assumed that a TimeEntry is always logged against a work effort so a WorkEffort id should be passed.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/api/v1/time_entries_controller.rb', line 127

def start
  begin
    ActiveRecord::Base.connection.transaction do

      party = current_user.party
      work_effort = WorkEffort.find(params[:work_effort_id])

      # check for an open TimeEntry
      open_time_entry = party.open_time_entry

      # if there is an open TimeEntry stop it and start a new one
      if open_time_entry
        open_time_entry.thru_datetime = Time.now

        open_time_entry.calculate_regular_hours_in_seconds!
      end

      time_entry = TimeEntry.create(
        from_datetime: params[:start_at].to_time
      )

      time_entry.work_effort = work_effort

      # associate to a timesheet
      time_sheet = party.timesheets.current!(RoleType.iid('work_resource'))
      time_sheet.time_entries << time_entry

      # update task statuses
      time_entry.update_task_status('task_status_in_progress')
      time_entry.update_task_assignment_status('task_resource_status_in_progress')

      render json: {
        success: true,
        time_entry: time_entry.to_data_hash,
      }
    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors

    render :json => {:success => false, :message => invalid.record.errors}
  rescue StandardError => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render json: {success: false, message: 'Error starting Time Entry'}
  end
end

#stopObject

stop TimeEntry by setting the thru_datetime and calculating the hours in seconds it returns the TimeEntry record as well as formatted totals for the day and week



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/controllers/api/v1/time_entries_controller.rb', line 180

def stop
  begin
    ActiveRecord::Base.connection.transaction do
      party = current_user.party
      work_effort = WorkEffort.find(params[:work_effort_id])
      time_entry = TimeEntry.find(params[:id])

      time_entry.thru_datetime = params[:end_at].to_time
      time_entry.comment = params[:comment].present? ? params[:comment].strip : nil

      time_entry.calculate_regular_hours_in_seconds!

      time_entry.update_task_assignment_status('task_resource_status_hold')

      result = {
        success: true,
        time_entry: time_entry.to_data_hash,
      }

      time_helper = ErpBaseErpSvcs::Helpers::Time::Client.new(params[:client_utc_offset])

      result[:day_total_formatted] = TimeEntry.total_formatted(work_effort: work_effort,
                                                               party: party,
                                                               start: time_helper.beginning_of_day,
                                                               end: time_helper.end_of_day
                                                               )
      result[:week_total_formatted] = TimeEntry.total_formatted(work_effort: work_effort,
                                                                party: party,
                                                                start: time_helper.beginning_of_week,
                                                                end: time_helper.end_of_week
                                                                )

      render json: result
    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors

    render :json => {:success => false, :message => invalid.record.errors}
  rescue StandardError => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render json: {success: false, message: 'Error stopping Time Entry'}
  end
end

#totalsObject

returns totals for time entries. If a work effort id is passed it will get totals for the passed work effort. If no work effort id is passed it will get totals for the current user passed on their timesheet



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/controllers/api/v1/time_entries_controller.rb', line 259

def totals
  result = {
    success: true,
    day_total_seconds: 0,
    week_total_seconds: 0,
    day_total_formatted: '00:00:00',
    week_total_formatted: '00:00:00',
    total_formatted: '00:00:00'
  }

  work_effort = nil
  party = nil
  time_helper = ErpBaseErpSvcs::Helpers::Time::Client.new(params[:client_utc_offset])

  if params[:work_effort_id]
    work_effort = WorkEffort.find(params[:work_effort_id])
  end

  if params[:party_id]
    party = Party.find(params[:party_id])
  end

  result[:day_total_seconds] = TimeEntry.total_seconds(work_effort: work_effort,
                                                       party: party,
                                                       start: time_helper.beginning_of_day,
                                                       end: time_helper.end_of_day
                                                       )
  result[:week_total_seconds] = TimeEntry.total_seconds(work_effort: work_effort,
                                                        party: party,
                                                        start: time_helper.beginning_of_week,
                                                        end: time_helper.end_of_week
                                                        )
  result[:total_seconds] = TimeEntry.total_seconds(work_effort: work_effort,
                                                   party: party
                                                   )
  result[:day_total_formatted] = TimeEntry.total_formatted(work_effort: work_effort,
                                                           party: party,
                                                           start: time_helper.beginning_of_day,
                                                           end: time_helper.end_of_day
                                                           )
  result[:week_total_formatted] = TimeEntry.total_formatted(work_effort: work_effort,
                                                            party: party,
                                                            start: time_helper.beginning_of_week,
                                                            end: time_helper.end_of_week
                                                            )
  result[:total_formatted] = TimeEntry.total_formatted(work_effort: work_effort,
                                                       party: party
                                                       )

  render json: result
end

#updateObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/api/v1/time_entries_controller.rb', line 67

def update
  begin
    ActiveRecord::Base.connection.transaction do

      time_entry = TimeEntry.find(params[:id])

      if params[:from_datetime]
        time_entry.from_datetime = params[:from_datetime].to_time
      end

      if params[:thru_datetime]
        time_entry.from_datetime = params[:thru_datetime].to_time
      end

      if params[:comment]
        time_entry.comment = params[:comment].strip
      end

      if params[:regular_hours_in_seconds]
        time_entry.regular_hours_in_seconds = params[:regular_hours_in_seconds].to_i
      end

      if params[:overtime_hours_in_seconds]
        time_entry.overtime_hours_in_seconds = params[:regular_hours_in_seconds].to_i
      end

      # update to manual entry
      time_entry.manual_entry = true

      render json: {
        success: time_entry.save!,
        time_entry: time_entry.to_data_hash,
      }

    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors

    render :json => {:success => false, :message => invalid.record.errors}
  rescue StandardError => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render json: {success: false, message: 'Error updating Time Entry'}
  end
end