Class: PayrollTransactionsController

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

#employee_transactionsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/payroll_transactions_controller.rb', line 25

def employee_transactions
  transaction_header_id = current_company.payroll_transaction_headers.find(
    params[:payroll_transaction_header_id]
  ).payroll_transaction_header_id
  employee_id = params[:employee_id] ? current_company.employees.find(params[:employee_id]).employee_id : 0

  data,  = PayrollTransactionHeader.employee_transactions(
    payroll_transaction_header_id: transaction_header_id,
    limit: params[:limit]&.to_i,
    offset: params[:offset]&.to_i,
    applies_to: params[:applies_to],
    employee_id: employee_id,
    tag_type: params[:tag_type],
    allow_modification: params[:allow_modification],
    query: "#{params[:query]}",
    grouping: "#{params[:order_by]} #{params[:order_direction]}",
    view: params[:view]
  ).values_at(:data, :metadata)
  render json: { data: data, metadata:  }
end

#get_payroll_transaction_header_reportObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/payroll_transactions_controller.rb', line 143

def get_payroll_transaction_header_report
  company_id = current_company.company_id
  payroll_year = params[:payroll_year] || Time.now.year

  data = ActiveRecord::Base.execute_procedure(
    'pr_get_payroll_transaction_header_report',
    company_id.presence,
    payroll_year.presence
  )

  render json: {
    data: data
  }
end

#importObject



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'app/controllers/payroll_transactions_controller.rb', line 297

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

  payroll_period_id = current_company.payroll_periods.find_by!(
    payroll_period_id: params[:payroll_period_id]
  ).payroll_period_id

  service = EmployeePayrollTransactions::ImportService.new(
    current_user: current_user,
    current_company: current_company,
    payroll_period_id: payroll_period_id,
    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



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

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

  payroll_period_id = current_company.payroll_periods.find_by!(
    payroll_period_id: params[:payroll_period_id]
  ).payroll_period_id

  service = EmployeePayrollTransactions::ImportService.new(
    current_user: current_user,
    current_company: current_company,
    payroll_period_id: payroll_period_id,
    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



13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/payroll_transactions_controller.rb', line 13

def index
  data,  = PayrollTransactionHeader.all_procedure(
    company_id: current_company.company_id,
    payroll_year: params[:payroll_year],
    limit: params[:limit]&.to_i || 10,
    offset: params[:offset]&.to_i || 0,
    query: "#{params[:query]}",
    column_name: "#{params[:order_by] || 'payroll_period_id'} #{params[:order_direction] || 'desc'}"
  ).values_at(:data, :metadata)
  render json: { data: data, metadata:  }
end

#insert_update_payroll_transaction_headerObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/controllers/payroll_transactions_controller.rb', line 238

def insert_update_payroll_transaction_header
  payroll_period_id = current_company
                      .payroll_periods
                      .find_by(payroll_period_id: params[:payroll_period_id])
                      &.payroll_period_id || 0

  user = params[:user]

  payroll_transaction_header_id = insert_payroll_transaction_header(
    company_id: current_company_id,
    payroll_period_id: payroll_period_id,
    user: user
  )

  render json: {
    data: { payroll_transaction_header_id: payroll_transaction_header_id },
    redirect_to: "/app/employee/import_ytd/manually/#{payroll_transaction_header_id}/remunerations"
  }
end

#invoice_report_dataObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/payroll_transactions_controller.rb', line 115

def invoice_report_data
  I18n.locale = params[:locale] if params[:locale]
  payroll_transaction_header_id = current_company.payroll_transaction_headers.find(
    params[:payroll_transaction_header_id]
  ).payroll_transaction_header_id

  header = ActiveRecord::Base.execute_procedure(
    'pr_get_payroll_invoice_header',
    payroll_transaction_header_id,
    '0'
  )[0]

  payroll_invoice_header_id = header['payroll_invoice_header_id']

  details = ActiveRecord::Base.execute_procedure(
    'pr_get_payroll_invoice_details',
    payroll_invoice_header_id
  ) || []

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

#payslip_report_dataObject



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

def payslip_report_data
  I18n.locale = params[:locale] if params[:locale]
  transaction_header_id = if params[:payroll_transaction_header_id]
                            current_company.payroll_transaction_headers.find(
                              params[:payroll_transaction_header_id]
                            ).payroll_transaction_header_id
                          elsif params[:payroll_period_id]
                            current_company.payroll_transaction_headers.find_by(
                              payroll_period_id: params[:payroll_period_id]
                            )&.payroll_transaction_header_id
                          else
                            0
                          end
  unless transaction_header_id
    return render json: {
      data: []
    }
  end
  employee_ids = params[:employee_ids]
  return render json: {}, status: :bad_request unless employee_ids.is_a?(Array) && employee_ids.any?

  data = employee_ids.map do |employee_id|
    query_payslip_report_data(transaction_header_id: transaction_header_id, employee_id: employee_id)
  end.compact

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

#reopenObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/controllers/payroll_transactions_controller.rb', line 207

def reopen
  payroll_transaction_header_id = params[:payroll_transaction_header_id]
  payroll_transaction_header = current_company.payroll_transaction_headers.find(
    payroll_transaction_header_id
  )
  return render json: {}, status: :bad_request unless payroll_transaction_header.rejected?

  user_name = current_user.truncated_email
  stage = PayrollTransactionHeader::STAGES[:IN_PROGRESS]
  message = "Reopened by #{user_name}"

  ActiveRecord::Base.execute_procedure(
    'pr_insert_payroll_transaction_header_log',
    payroll_transaction_header_id,
    stage,
    message,
    user_name
  )
end

#reviewObject



171
172
173
174
175
176
177
178
179
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
# File 'app/controllers/payroll_transactions_controller.rb', line 171

def review
  payroll_transaction_header = current_company.payroll_transaction_headers.find(
    params[:payroll_transaction_header_id]
  )
  return render json: {}, status: :bad_request unless payroll_transaction_header.sent_for_approval?

  payroll_transaction_header_id = payroll_transaction_header.payroll_transaction_header_id

  user_name = current_user.truncated_email
  decision = params[:decision]

  message = if decision == 'approved'
              "Approved by #{user_name}"
            else
              "Rejected by #{user_name} with reason: #{params[:rejection_reason]}"
            end

  stage = if decision == 'approved'
            PayrollTransactionHeader::STAGES[:APPROVED_FOR_DEPOSIT]
          else
            PayrollTransactionHeader::STAGES[:REJECTED]
          end

  ActiveRecord::Base.execute_procedure(
    'pr_insert_payroll_transaction_header_log',
    payroll_transaction_header_id,
    stage,
    message,
    user_name
  )

  render json: {
    redirect_to: '/app/payroll/transactions'
  }
end

#transaction_reportObject



258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/controllers/payroll_transactions_controller.rb', line 258

def transaction_report
  payroll_year = params[:payroll_year] || Time.now.year

  data = PayrollTransactionHeader.transaction_report_procedure(
    company_id: current_company_id,
    payroll_year: payroll_year
  )

  render json: {
    data: data
  }
end

#unlock_transactionObject



158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/payroll_transactions_controller.rb', line 158

def unlock_transaction
  raise PermissionDeniedError unless current_user.has_permission?(Permission::Codes::UNLOCK_PAYROLL_CELLS)

  transaction = current_company.employee_payroll_transactions.find(params[:id])
  return render json: {}, status: :bad_request if transaction.locked?

  transaction.unlock(user_name: current_user.truncated_email)

  render json: {
    message: I18n.t('messages.payroll_transaction_unlocked')
  }
end

#update_from_table_editsObject



46
47
48
49
50
51
52
53
54
55
56
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
# File 'app/controllers/payroll_transactions_controller.rb', line 46

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

    transaction_header = current_company.payroll_transaction_headers.find(row_hash[:payroll_transaction_header_id])
    if transaction_header.locked? && !current_user.has_permission?(Permission::Codes::RUN_SPECIAL_PAYROLL)
      return render json: {},
                    status: :bad_request
    end

    transaction = current_company.employee_payroll_transactions.find(row_hash[:employee_payroll_transaction_id])
    raise ActionController::BadRequest.new('Not modifiable') unless transaction.modifyable?

    transaction.assign_attributes(amount: edits[:amount])
    transaction.match_amount_to_tag_type
    raise ActiveRecord::RecordInvalid.new(transaction) unless transaction.valid?

    transaction.update_procedure

    next if transaction.payroll_transaction_detail.manually_unlocked

    PayrollTransactionHeader.generate_payroll(
      payroll_transaction_header_id: transaction.payroll_transaction_header_id,
      payroll_transaction_detail_id: transaction.payroll_transaction_detail_id,
      user_name: current_user.truncated_email
    )
  end

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

#yearsObject



227
228
229
230
231
232
233
234
235
236
# File 'app/controllers/payroll_transactions_controller.rb', line 227

def years
  years = PayrollTransactionHeader.years_for_company(company_id: current_company.company_id)
  current_year = Time.now.year
  default_year = years.include?(current_year) ? current_year : years.last

  render json: {
    years: years,
    default_year: default_year
  }
end