Class: TimesheetsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/timesheets_controller.rb

Instance Method Summary collapse

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#createObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/timesheets_controller.rb', line 41

def create
  timesheet = EmployeeTimesheet.new(**timesheet_params)

  EmployeeTimesheetPolicy.new(current_user, timesheet).authorize_create!

  timesheet.insert_or_update_procedure!(erp_user_name: current_user.truncated_email)

  response = {
    message: I18n.t('messages.timesheet_created')
  }

  if params[:save_and_create_another]
    response[:redirect_to] = '/app/payroll/timesheets/new?creatingAnother=true'
  end

  render json: response
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors,
    message: e.record.errors.full_messages.join(', '),
    redirect_to: '/app/payroll/timesheets/new'
  }, status: :bad_request
end

#indexObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/timesheets_controller.rb', line 13

def index
  data,  = EmployeeTimesheet.all_procedure(
    company_id: current_company.company_id,
    query: params[:query],
    offset: params[:offset].to_i,
    grouping: "#{params[:order_by] || 'transaction_date'} #{params[:order_direction] || 'DESC'}",
    limit: params[:limit].to_i,
    employee_id: params[:employee_id]&.to_i || 0,
    payroll_period_id: params[:payroll_period_id]&.to_i || 0,
    view: params[:view],
    revision_no: params[:revision_no].to_i,
    start_date: params[:start_date],
    end_date: params[:end_date]
  ).values_at(:data, :metadata)
  render json: { data: data, metadata:  }
end

#showObject



30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/timesheets_controller.rb', line 30

def show
  data = EmployeeTimesheet.find_procedure(
    company_id: current_company.company_id,
    employee_time_sheet_id: params[:id]
  )

  render json: {
    data: data
  }
end

#updateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/timesheets_controller.rb', line 65

def update
  @employee_time_sheet.assign_attributes(**timesheet_params)

  EmployeeTimesheetPolicy.new(current_user, @employee_time_sheet).authorize_update!

  @employee_time_sheet.insert_or_update_procedure!(erp_user_name: current_user.truncated_email)

  render json: {
    message: I18n.t('messages.timesheet_updated'),
    redirect_to: if params[:save_and_create_another]
                   '/app/payroll/timesheets/new?creatingAnother=true'
                 else
                   "/app/payroll/timesheets/#{params[:id]}/"
                 end
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors,
    message: e.record.errors.full_messages.join(', '),
    redirect_to: "/app/payroll/timesheets/#{params[:id]}/"
  }, status: :bad_request
end

#update_from_table_editsObject



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
# File 'app/controllers/timesheets_controller.rb', line 88

def update_from_table_edits
  rows_edits = params[:rows_edits]
  rows_edits.each do |row_edits|
    row_hash = row_edits[:rowHash]
    edits = row_edits[:edits]

    set_records(
      employee_id: row_hash[:employee_id],
      employee_role_id: row_hash[:employee_role_id],
      employee_time_sheet_id: row_hash[:employee_time_sheet_id]
    )
    @employee_time_sheet.assign_attributes(hours_worked: edits[:hours_worked])
    EmployeeTimesheetPolicy.new(current_user, @employee_time_sheet).authorize_update!

    @employee_time_sheet.insert_or_update_procedure!(erp_user_name: current_user.truncated_email)
  end

  render json: {
    message: rows_edits.length > 1 ? I18n.t('messages.timesheets_updated') : I18n.t('messages.timesheet_updated')
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    message: e.record.errors.full_messages.join(', ')
  }, status: :bad_request
end