Class: PayrollCalculationTemplatesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/payroll_calculation_templates_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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/payroll_calculation_templates_controller.rb', line 44

def create
  payroll_calculation_template = PayrollCalculationTemplate.new(**payroll_calculation_template_params)
  raise ActiveRecord::RecordInvalid.new(payroll_calculation_template) unless payroll_calculation_template.valid?

  payroll_calculation_template.create_or_update_procedure(user_name: current_user.truncated_email)

  render json: {
    message: I18n.t('messages.payroll_calculation_template_created'),
    redirect_to: '/app/payroll/calculation_templates'
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors,
    message: e.record.errors.full_messages.join(', '),
    redirect_to: '/app/payroll/calculation_templates'
  }, status: :bad_request
end

#indexObject



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

def index
  order_by = params[:order_by] || 'rate'
  order_direction = params[:order_direction] || 'asc'
  query = params[:query] || ''
  limit = params[:limit] || 10
  offset = params[:offset] || 0
  province_id = params.dig(:province, :province_id) || Province::QUEBEC_PROVINCE_ID

  data,  = fetch_records(
    records: PayrollCalculationTemplate.where(province_id: province_id),
    order_by: order_by,
    fields: %w[
      payroll_calculation_template_id
      tag_type
      applies_to
      tag_display
      active
      tag_sequence
      tag_name
      sql_exec_command
      allow_modification
      active
    ],
    search_fields: %w[tag_type applies_to tag_display active],
    order: order_direction,
    query: query,
    limit: limit,
    offset: offset
  ).values_at(:data, :metadata)

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

#provincesObject



103
104
105
106
107
# File 'app/controllers/payroll_calculation_templates_controller.rb', line 103

def provinces
  render json: {
    data: PayrollDeductionRate.provinces
  }
end

#showObject



40
41
42
# File 'app/controllers/payroll_calculation_templates_controller.rb', line 40

def show
  render json: { data: PayrollCalculationTemplate.find(params[:id]) }
end

#updateObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/payroll_calculation_templates_controller.rb', line 62

def update
  payroll_calculation_template = PayrollCalculationTemplate.find(params[:id])
  payroll_calculation_template.assign_attributes(payroll_calculation_template_params)
  raise ActiveRecord::RecordInvalid.new(payroll_calculation_template) unless payroll_calculation_template.valid?

  payroll_calculation_template.create_or_update_procedure(user_name: current_user.truncated_email)

  render json: {
    message: I18n.t('messages.payroll_calculation_template_updated'),
    redirect_to: "/app/payroll/calculation_templates/#{params[:id]}"
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors,
    message: e.record.errors.full_messages.join(', '),
    redirect_to: "/app/payroll/calculation_templates/#{params[:id]}"
  }, status: :bad_request
end

#update_from_table_editsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/payroll_calculation_templates_controller.rb', line 81

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

    payroll_calculation_template = PayrollCalculationTemplate.find(row_hash[:payroll_calculation_template_id])
    attributes = edits.slice(:tag_display, :active).reject { |_, v| v.nil? }
    payroll_calculation_template.assign_attributes(attributes)
    raise ActiveRecord::RecordInvalid.new(payroll_calculation_template) unless payroll_calculation_template.valid?

    payroll_calculation_template.create_or_update_procedure
  end

  render json: {
    message: I18n.t('messages.payroll_parameters_updated')
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    message: e.record.errors.full_messages.join(', ')
  }, status: :bad_request
end