Class: BankAccountInformationImporter

Inherits:
Object
  • Object
show all
Defined in:
app/lib/bank_account_information_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(bank_account) ⇒ BankAccountInformationImporter

Returns a new instance of BankAccountInformationImporter.



2
3
4
# File 'app/lib/bank_account_information_importer.rb', line 2

def initialize()
  @bank_account = 
end

Instance Method Details

#import!(content) ⇒ Object



6
7
8
9
10
# File 'app/lib/bank_account_information_importer.rb', line 6

def import!(content)
  return nil if content.empty?

  import_data! JSON.parse(content, symbolize_names: true)
end

#import_data!(data) ⇒ Object



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
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/lib/bank_account_information_importer.rb', line 12

def import_data!(data)
  return 0 if data.empty?

  booked = data.fetch(:transactions, {}).fetch(:booked, [])

  ret = 0
  booked.each do |t|
    amount =  t[:transactionAmount]
    entity_name = amount < 0 ? t[:creditorName] : t[:debtorName]
     = amount < 0 ? t[:creditorAccount] : t[:debtorAccount]
    reference = [t[:endToEndId], t[:remittanceInformationUnstructured]].join("\n").strip

    @bank_account.bank_transactions.where(external_id: t[:transactionId]).first_or_create.update({
                                                                                                   date: t[:bookingDate],
                                                                                                   amount: amount,
                                                                                                   iban:  && [:iban],
                                                                                                   reference: reference,
                                                                                                   text: entity_name,
                                                                                                   receipt: t[:additionalInformation]
                                                                                                 })
    ret += 1
  end

  balances = (data[:balances] ? data[:balances].map { |b| [b[:balanceType], b[:balanceAmount]] } : []).to_h
  balance = balances.values.first
  %w[closingBooked expected authorised openingBooked interimAvailable forwardAvailable nonInvoiced].each do |type|
    value = balances[type]
    if value
      balance = value
      break
    end
  end

  @bank_account.balance = (balance) || @bank_account.bank_transactions.sum(:amount)
  @bank_account.import_continuation_point = booked.first&.fetch(:entryReference, nil) unless booked.empty?
  @bank_account.last_import = Time.now
  @bank_account.save!

  ret
end