Class: EmployeePayrollTransactions::ImportService

Inherits:
Object
  • Object
show all
Includes:
SheetHelper
Defined in:
app/services/employee_payroll_transactions/import_service.rb

Constant Summary collapse

ACCEPTED_COLUMNS =
%i[
  employee_ssn employee_full_name tag_sequence description type applies_to amount
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SheetHelper

#each_row, #hashes_array_from_file, #process_import_results

Constructor Details

#initialize(current_user:, current_company:, importation_file:, payroll_period_id:) ⇒ ImportService

Returns a new instance of ImportService.



16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/employee_payroll_transactions/import_service.rb', line 16

def initialize(
  current_user:,
  current_company:,
  importation_file:,
  payroll_period_id:
)
  @current_user = current_user
  @current_company = current_company
  @importation_file = importation_file
  @payroll_period_id = payroll_period_id
end

Instance Attribute Details

#current_companyObject (readonly)

Returns the value of attribute current_company.



11
12
13
# File 'app/services/employee_payroll_transactions/import_service.rb', line 11

def current_company
  @current_company
end

#current_userObject (readonly)

Returns the value of attribute current_user.



11
12
13
# File 'app/services/employee_payroll_transactions/import_service.rb', line 11

def current_user
  @current_user
end

#importation_fileObject (readonly)

Returns the value of attribute importation_file.



11
12
13
# File 'app/services/employee_payroll_transactions/import_service.rb', line 11

def importation_file
  @importation_file
end

#payroll_period_idObject (readonly)

Returns the value of attribute payroll_period_id.



11
12
13
# File 'app/services/employee_payroll_transactions/import_service.rb', line 11

def payroll_period_id
  @payroll_period_id
end

Instance Method Details

#importObject



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
# File 'app/services/employee_payroll_transactions/import_service.rb', line 28

def import
  rows = []
  file_rows = hashes_array_from_file(file: importation_file)
  processed_rows_count = 0

  ActiveRecord::Base.transaction do
    file_rows.each do |row|
      gross_amount = file_rows.filter { |r| r[:employee_ssn] == row[:employee_ssn] }.sum { |r| r[:amount].to_f }
      record = create_payroll_transaction(row, gross_amount)
      raise ActiveRecord::Rollback unless record

      rows << create_creation_row(row, record)
    rescue StandardError => e
      rows << create_failed_creation_row(row, e)
      raise ActiveRecord::Rollback
    ensure
      processed_rows_count += 1
    end
  end

  process_import_results(
    rows,
    file_rows,
    processed_rows_count,
    'Importation rollbacked',
    [:employee_payroll_transaction_id]
  )
end

#validateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/employee_payroll_transactions/import_service.rb', line 57

def validate
  rows = []

  each_row(importation_file) do |row|
    invalid_columns = row.keys - ACCEPTED_COLUMNS
    if invalid_columns.any?
      rows << create_invalid_column_row(row, invalid_columns)
      next
    end

    rows << create_validated_row(row)
  end

  rows
end