class UOB::Payroll::HashCalculator

Attributes

header[R]
rows[R]

Public Class Methods

calculate(header:, rows:) click to toggle source
# File lib/uob/payroll/hash_calculator.rb, line 11
def calculate(header:, rows:)
  new(header: header, rows: rows).calculate
end
new(header:, rows:) click to toggle source
# File lib/uob/payroll/hash_calculator.rb, line 16
def initialize(header:, rows:)
  @header = header
  @rows = rows
end

Public Instance Methods

calculate() click to toggle source
# File lib/uob/payroll/hash_calculator.rb, line 21
def calculate
  sum_header +
    sum_rows
end
sum_header() click to toggle source
# File lib/uob/payroll/hash_calculator.rb, line 28
def sum_header
  calculate_string(header.originating_bic_code) +
    calculate_padded_string(string: header.originating_account_number, size: 34) +
    calculate_padded_string(string: sanitize(header.originating_account_name), size: 140)
end
sum_rows() click to toggle source
# File lib/uob/payroll/hash_calculator.rb, line 34
def sum_rows
  hash_code = 0
  sum = 0

  rows.each.map do |row|
    hash_code = 0 if hash_code == 9
    hash_code += 1

    sum += calculate_string(row.receiving_bic_code) +
      hash_code * calculate_padded_string(string: row.receiving_account_number, size: 34) +
      hash_code * calculate_padded_string(string: sanitize(row.receiving_account_name), size: 140) +
      calculate_payment_type(hash_code) +
      calculate_string('SGD') +
      calculate_padded_string(string: row.formatted_amount, size: 18, pad: '0', just: :right) +
      calculate_string('SALA')
  end

  sum
end

Private Instance Methods

calculate_padded_string(string:, size:, pad: ' ', just: :left) click to toggle source
# File lib/uob/payroll/hash_calculator.rb, line 66
def calculate_padded_string(string:, size:, pad: ' ', just: :left)
  if just == :left
    calculate_string string[0...size].ljust(size, pad)
  else
    calculate_string string[0...size].rjust(size, pad)
  end
end
calculate_payment_type(index) click to toggle source

Payment Type is always 'R', so Payment Code = 22

# File lib/uob/payroll/hash_calculator.rb, line 62
def calculate_payment_type(index)
  22 * (index)
end
calculate_string(string) click to toggle source
# File lib/uob/payroll/hash_calculator.rb, line 74
def calculate_string(string)
  StringCalculator.calculate(string)
end
sanitize(string) click to toggle source

Remove “-” from names.

# File lib/uob/payroll/hash_calculator.rb, line 57
def sanitize(string)
  string.gsub(/-|'/,'')
end