Class: BankPayments::SwedbankExport::Sequence

Inherits:
Object
  • Object
show all
Defined in:
lib/bank_payments/swedbank_export/sequence.rb

Overview

An export file may contain multiple sequences. A sequence starts with an OpeningRecord and ends with an ReconciliationRecord. Each sequence will consist of at least the following:

In particular you need to supply the following per beneficiary/payee:

which describes where the payment should be made. Then you will supply a money record (payment or credit memo) together with it reason to to describe a payment. All of these records will have a unique serial number for the beneficiary.

According to the documentation you can group money records together with the beneficiary given that the have the correct serial number.

Author:

  • Michael Litton

Instance Method Summary collapse

Constructor Details

#initialize(account, name, address, payment_date = nil) ⇒ Sequence

Intializes the required records for a sequence given the sends account, name and adress



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bank_payments/swedbank_export/sequence.rb', line 38

def initialize(, name, address, payment_date = nil)
  @initial_records = []

  @initial_records << OpeningRecord.new do |o_record|
    o_record. = 
    o_record.name    = name
    o_record.address = address
  end

  @initial_records << ReconciliationRecord.new do |r_record|
    r_record.              = 
    r_record.sum_amount_sek       = 0
    r_record.sum_amount_foreign   = 0
    r_record.total_beneficiaries  = 0
    r_record.total_records        = 2
  end

  @payment_date = payment_date

  # TODO Make this thread safe using a mutex?
  @beneficiaries = []
end

Instance Method Details

#add_transaction(beneficiary, transaction) ⇒ Object

Adds a transaction to an existing beneficiary or creates a new one.



89
90
91
92
93
94
# File 'lib/bank_payments/swedbank_export/sequence.rb', line 89

def add_transaction(beneficiary, transaction)
  find_or_create_beneficiary(beneficiary) do |entry|
    entry[:transactions] << transaction
    entry[:transactions].sort_by!(&:amount_sek)
  end
end

#recordsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bank_payments/swedbank_export/sequence.rb', line 61

def records
  all_records = []
  all_records << @initial_records.first

  @beneficiaries.each_with_index do |entry,index|
    destination_records = entry[:beneficiary].to_spisu_records
    moneytary_records   = entry[:transactions].map(&:to_spisu_records)

    [destination_records + moneytary_records].flatten.each do|record|
      record.serial_number = index + 1
    end

    all_records << destination_records << moneytary_records
  end

  reconciliation                      = @initial_records.last
  reconciliation.sum_amount_sek       = sum_sek_transactions
  reconciliation.sum_amount_foreign   = sum_foreign_transactions
  reconciliation.total_beneficiaries  = @beneficiaries.size
  reconciliation.total_records        = all_records.flatten.size + 1

  all_records << reconciliation

  all_records.flatten
end

#to_file_dataObject



102
103
104
# File 'lib/bank_payments/swedbank_export/sequence.rb', line 102

def to_file_data
  records.join("\n")
end

#valid?Boolean

Goes through the sequence and validates that in corresponds to the rules in the specification

Returns:

  • (Boolean)


98
99
100
# File 'lib/bank_payments/swedbank_export/sequence.rb', line 98

def valid?
  all_requried_types_present?
end