Class: EmployeesController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, AttachmentsHelper
Defined in:
app/controllers/employees_controller.rb

Instance Method Summary collapse

Methods included from AttachmentsHelper

#base64_attachments_to_files, #base64_with_filename_to_files

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

#activate_manual_depositObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'app/controllers/employees_controller.rb', line 272

def activate_manual_deposit
  @employee.assign_attributes(employee_params.merge(accepts_direct_deposit: false))
  if @employee.pay_columns_changed? && !current_user.has_permission?(Permission::Codes::MODIFY_EMPLOYEE_PAY_VARIABLES)
    raise PermissionDeniedError.new(redirect_to: params[:from_url])
  end

  # raise ActiveRecord::RecordInvalid.new(@employee) unless @employee.valid?

  @employee.write_procedure
  render json: {
    message: I18n.t('messages.employee_updated'),
    redirect_to: params[:from_url]
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors,
    message: e.record.errors.full_messages.join(', '),
    redirect_to: params[:from_url]
  }, status: :bad_request
end

#createObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/employees_controller.rb', line 83

def create
  employee = Employees::CreateService.new(
    employee_params: employee_params,
    erp_user_params: erp_user_params,
    current_user: current_user,
    current_company: current_company
  ).call

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

#destroyObject



133
134
135
# File 'app/controllers/employees_controller.rb', line 133

def destroy
  render json: {}, status: :not_found
end

#employee_import_validationObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/controllers/employees_controller.rb', line 204

def employee_import_validation
  employees = request.params[:employees]
  results = []

  employees.each do |employee_data|
    employee = Employee.new
    employee.assign_attributes(
      {
        company_id: current_company_id,
        employee_name: employee_data['Employee_Name'],
        ssn: employee_data['Employee_SIN'],
        date_of_birth: employee_data['Date_Of_Birth'],
        start_date: employee_data['Hire_Date'],
        business_email: employee_data['Email'],
        vacation_percentage: employee_data['Vacation_percentage'],
        province_id: 1,
        address_line1: employee_data['Address_Line_1'],
        address_line2: employee_data['Address_Line_2'],
        city: employee_data['City'],
        postal_code: employee_data['Postal_Code'],
        federal_tax_category: employee_data['Federal_Tax_Code'],
        provincial_tax_category: employee_data['Provincial_Tax_Code'],
        health_insurance_amount: 0,
        dental_insurance_amount: 0,
        rrsp_amount: 0,
        long_term_disability_insurance_amount: 0,
        life_insurance_amount: 0,
        default_language: params.dig(:default_language, :value) || 'E'
      }
    )

     = {
      name: employee_data['Nom_de_la_banque'],
      branch_no: employee_data['No_de_succursale'],
      institution_no: employee_data['No_institution'],
      account_no: employee_data['No_de_compte']
    }
    employee. = 

    employee_role = {
      role_description: employee_data['Role_Description'],
      hourly_rate: employee_data['Hourly_Rate'],
      cnesst_percentage: employee_data['CNESST_Percentage']
    }
    employee.employee_role = employee_role

    employee.province_code = employee_data['Province_Code']

    results << if employee.valid?
                 employee_data.merge(is_valid: true, errors: [])
               else
                 employee_data.merge(is_valid: false, errors: employee.errors.full_messages)
               end
  end

  render json: { data: results }
end

#employee_reportObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/controllers/employees_controller.rb', line 171

def employee_report
  I18n.locale = params[:locale] if params[:locale]
  company_id = current_company_id

  employee_id = current_company
                .employees
                .find_by(employee_id: params[:employee_id])
                &.employee_id || 0

  filter_type = params[:filter_type]

  report_data = Employee.employees_report_data(
    company_id: company_id,
    employee_id: employee_id,
    filter_type: filter_type
  )

  render json: {
    data: report_data,
    report_translations: TranslationService.new.translations
  }
end

#employee_time_sheet_reportObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/employees_controller.rb', line 153

def employee_time_sheet_report
  I18n.locale = params[:locale] if params[:locale]
  company_id = current_company_id
  from_date = params[:from_date]
  to_date = params[:to_date]

  result = Employee.time_sheet_report_data(
    company_id: company_id,
    from_date: from_date,
    to_date: to_date
  )

  render json: {
    data: result,
    report_translations: TranslationService.new.translations
  }
end

#formObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/employees_controller.rb', line 137

def form
  render json: {
    data: {
      federal_deduction_codes: TypesMaster.federal_tax_codes.as_json(
        methods: %i[type_description]
      ),
      provincial_deduction_codes: TypesMaster.provincial_tax_codes.as_json(
        methods: %i[type_description]
      ),
      termination_reasons: TypesMaster.termination_reasons.as_json(
        methods: %i[type_description]
      )
    }
  }
end

#headerObject



194
195
196
197
198
199
200
201
202
# File 'app/controllers/employees_controller.rb', line 194

def header
  render json: {
    data: {
      roles_count: @employee.employee_roles.count,
      bank_accounts_count: @employee.bank_accounts.count,
      timesheets_count: @employee.employee_timesheets.count
    }
  }
end

#importObject



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'app/controllers/employees_controller.rb', line 314

def import
  files = base64_attachments_to_files(params[:files_base64])
  file = files[0]

  service = Employees::ImportService.new(
    current_user: current_user,
    current_company: current_company,
    importation_file: file
  )

  validated_data = service.validate
  file_validity = validated_data.all? do |row|
    row[:is_valid]
  end

  unless file_validity
    return render json: {
      data: validated_data
    }, status: :bad_request
  end

  importation_result = service.import

  render json: {
    data: importation_result
  }
end

#import_verificationObject



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'app/controllers/employees_controller.rb', line 293

def import_verification
  files = base64_attachments_to_files(params[:files_base64])
  file = files[0]

  service = Employees::ImportService.new(
    current_user: current_user,
    current_company: current_company,
    importation_file: file
  )

  validated_data = service.validate
  file_validity = validated_data.all? do |row|
    row[:is_valid]
  end

  render json: {
    data: validated_data,
    file_validity: file_validity
  }
end

#indexObject



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

def index
  query = params[:query] || ''
  limit = params[:limit].to_i || 10
  offset = params[:offset].to_i || 0
  order_by = params[:order_by] || 'employee_id'
  order_direction = params[:order_direction] || 'DESC'
  data,  = Employee.where_procedure(
    company_id: current_company.company_id,
    fields: %w[
      employee_id
      employee_name
      date_of_birth
      start_date
      provincial_tax_category
      federal_tax_category
      vacation_percentage
      color
      buisness_email
      personal_email
      phone_no
      count_of_roles
      count_of_timesheets
      list_of_roles
    ],
    order_by: order_by,
    order: order_direction,
    query: query,
    limit: limit,
    offset: offset
  ).values_at(:data, :metadata)

  render json: {
    data: data,
    metadata: 
  }
end

#optionsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/employees_controller.rb', line 52

def options
  payroll_period_id = if params[:payroll_period_id].to_i > 0
                        current_company.payroll_periods.find(params[:payroll_period_id]).payroll_period_id
                      elsif params[:payroll_transaction_header_id].to_i > 0
                        current_company.payroll_transaction_headers.find(params[:payroll_transaction_header_id]).payroll_period_id
                      else
                        0
                      end

  options = Employee.options_procedure(
    payroll_period_id: payroll_period_id,
    company_id: current_company.company_id,
    must_have_timesheets: (params[:must_have_timesheets].to_i == 1) || false
  )

  render json: {
    data: options
  }
end

#rl1Object



267
268
269
270
# File 'app/controllers/employees_controller.rb', line 267

def rl1
  file = Rl1PdfForm.dummy.to_file(read_only: false)
  send_file file.path, type: 'application/pdf', disposition: 'inline'
end

#showObject



72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/employees_controller.rb', line 72

def show
  employee = Employee.find_employee_procedure(
    company_id: current_company.company_id,
    employee_id: @employee.employee_id
  )

  render json: {
    data: employee
  }
end

#t4Object



262
263
264
265
# File 'app/controllers/employees_controller.rb', line 262

def t4
  file = current_company.t4_pdf(year: params[:year], summary: params[:summary])
  send_file file.path, type: 'application/pdf', disposition: 'inline'
end

#updateObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/employees_controller.rb', line 107

def update
  @employee.assign_attributes(employee_params)
  if @employee.pay_columns_changed? && !current_user.has_permission?(Permission::Codes::MODIFY_EMPLOYEE_PAY_VARIABLES)
    raise PermissionDeniedError.new(redirect_to: params[:from_url])
  end
  raise ActiveRecord::RecordInvalid, @employee unless @employee.valid?

  Employees::UpdateService.new(
    employee_params: employee_params,
    erp_user_params: erp_user_params,
    current_user: current_user,
    current_company: current_company
  ).call

  render json: {
    message: I18n.t('messages.employee_updated'),
    redirect_to: params[:from_url]
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors,
    message: e.record.errors.full_messages.join(', '),
    redirect_to: params[:from_url]
  }, status: :bad_request
end