Class: Cmxl::Fields::Transaction

Inherits:
Cmxl::Field show all
Defined in:
lib/cmxl/fields/transaction.rb

Constant Summary

Constants inherited from Cmxl::Field

Cmxl::Field::DATE

Instance Attribute Summary collapse

Attributes inherited from Cmxl::Field

#data, #match, #modifier, #source, #tag

Instance Method Summary collapse

Methods inherited from Cmxl::Field

#initialize, #method_missing, parse, parsers, #to_amount, #to_amount_in_cents, #to_date

Constructor Details

This class inherits a constructor from Cmxl::Field

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cmxl::Field

Instance Attribute Details

#detailsObject

Returns the value of attribute details.



7
8
9
# File 'lib/cmxl/fields/transaction.rb', line 7

def details
  @details
end

Instance Method Details

#add_meta_data(content) ⇒ Object



9
10
11
# File 'lib/cmxl/fields/transaction.rb', line 9

def (content)
  self.details = Cmxl::Fields::StatementDetails.parse(content) unless content.nil?
end

#amountObject



81
82
83
# File 'lib/cmxl/fields/transaction.rb', line 81

def amount
  to_amount(data['amount'])
end

#amount_in_centsObject



85
86
87
# File 'lib/cmxl/fields/transaction.rb', line 85

def amount_in_cents
  to_amount_in_cents(data['amount'])
end

#bicObject



141
142
143
# File 'lib/cmxl/fields/transaction.rb', line 141

def bic
  details.bic if details
end

#charges_currencyObject



127
128
129
# File 'lib/cmxl/fields/transaction.rb', line 127

def charges_currency
  supplementary.charges_currency
end

#charges_in_centsObject



123
124
125
# File 'lib/cmxl/fields/transaction.rb', line 123

def charges_in_cents
  supplementary.charges_in_cents
end

#credit?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/cmxl/fields/transaction.rb', line 17

def credit?
  credit_debit_indicator.include?('C')
end

#credit_debit_indicatorObject



64
65
66
# File 'lib/cmxl/fields/transaction.rb', line 64

def credit_debit_indicator
  data['credit_debit_indicator'].to_s
end

#dateObject



89
90
91
# File 'lib/cmxl/fields/transaction.rb', line 89

def date
  to_date(data['date'])
end

#debit?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/cmxl/fields/transaction.rb', line 21

def debit?
  credit_debit_indicator.include?('D')
end

#descriptionObject

Fields from details



133
134
135
# File 'lib/cmxl/fields/transaction.rb', line 133

def description
  details.description if details
end

#entry_dateObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cmxl/fields/transaction.rb', line 93

def 
  return if !date || !data['entry_date']

  e_date = to_date(data['entry_date'], date.year)
  # we assume that valuta (date) and entry_date have a close connection. so valuta and entry_date should not be
  # further apart than one month. this leads to some edge cases

  # valuta is in january while entry_date is in december => entry_date was done the year before
  e_date = to_date(data['entry_date'], date.year - 1) if date.month == 1 && e_date.month == 12

  # valuta is in december but entry_date is in january => entry_date is actually in the year after valuta
  e_date = to_date(data['entry_date'], date.year + 1) if date.month == 12 && e_date.month == 1

  e_date
end

#expected?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/cmxl/fields/transaction.rb', line 60

def expected?
  credit_debit_indicator.include?('E')
end

#expected_credit?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cmxl/fields/transaction.rb', line 52

def expected_credit?
  credit? && expected?
end

#expected_debit?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cmxl/fields/transaction.rb', line 56

def expected_debit?
  debit? && expected?
end

#funds_codeObject



68
69
70
71
# File 'lib/cmxl/fields/transaction.rb', line 68

def funds_code
  warn "[DEPRECATION] `funds_code` is deprecated.  Please use `credit_debit_indicator` instead. It will be removed in version 3.0."
  data['credit_debit_indicator'].to_s
end

#ibanObject



149
150
151
# File 'lib/cmxl/fields/transaction.rb', line 149

def iban
  details.iban if details
end

#informationObject



137
138
139
# File 'lib/cmxl/fields/transaction.rb', line 137

def information
  details.information if details
end

#initial_amount_in_centsObject

Fields from supplementary



115
116
117
# File 'lib/cmxl/fields/transaction.rb', line 115

def initial_amount_in_cents
  supplementary.initial_amount_in_cents
end

#initial_currencyObject



119
120
121
# File 'lib/cmxl/fields/transaction.rb', line 119

def initial_currency
  supplementary.initial_currency
end

#nameObject



145
146
147
# File 'lib/cmxl/fields/transaction.rb', line 145

def name
  details.name if details
end

#primanotaObject



161
162
163
# File 'lib/cmxl/fields/transaction.rb', line 161

def primanota
  details.primanota if details
end

#reversal?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/cmxl/fields/transaction.rb', line 48

def reversal?
  credit_debit_indicator.include?('R')
end

#reversal_credit?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cmxl/fields/transaction.rb', line 30

def reversal_credit?
  credit? && storno?
end

#reversal_debit?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/cmxl/fields/transaction.rb', line 39

def reversal_debit?
  debit? && storno?
end

#sepaObject



153
154
155
# File 'lib/cmxl/fields/transaction.rb', line 153

def sepa
  details.sepa if details
end

#shaObject



13
14
15
# File 'lib/cmxl/fields/transaction.rb', line 13

def sha
  Digest::SHA2.new.update(source).to_s
end

#signObject



77
78
79
# File 'lib/cmxl/fields/transaction.rb', line 77

def sign
  credit? ? 1 : -1
end

#storno?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/cmxl/fields/transaction.rb', line 43

def storno?
  warn "[DEPRECATION] `storno?` is deprecated.  Please use `reversal?` instead. It will be removed in version 3.0."
  reversal?
end

#storno_credit?Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/cmxl/fields/transaction.rb', line 25

def storno_credit?
  warn "[DEPRECATION] `storno_credit?` is deprecated.  Please use `reversal_credit?` instead. It will be removed in version 3.0."
  reversal_credit?
end

#storno_debit?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/cmxl/fields/transaction.rb', line 34

def storno_debit?
  warn "[DEPRECATION] `storno_debit?` is deprecated.  Please use `reversal_debit?` instead. It will be removed in version 3.0."
  reversal_debit?
end

#storno_flagObject



73
74
75
# File 'lib/cmxl/fields/transaction.rb', line 73

def storno_flag
  reversal? ? 'R' : ''
end

#sub_fieldsObject



157
158
159
# File 'lib/cmxl/fields/transaction.rb', line 157

def sub_fields
  details.sub_fields if details
end

#supplementaryObject



109
110
111
# File 'lib/cmxl/fields/transaction.rb', line 109

def supplementary
  @supplementary ||= Cmxl::Fields::TransactionSupplementary.parse(data['supplementary'])
end

#to_hObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cmxl/fields/transaction.rb', line 165

def to_h
  {
    'sha' => sha,
    'date' => date,
    'entry_date' => ,
    'amount' => amount,
    'amount_in_cents' => amount_in_cents,
    'sign' => sign,
    'debit' => debit?,
    'credit' => credit?,
    'storno' => reversal?,
    'reversal' => reversal?,
    'expected' => expected?,
    'funds_code' => credit_debit_indicator,
    'credit_debit_indicator' => credit_debit_indicator,
    'swift_code' => swift_code,
    'reference' => reference,
    'bank_reference' => bank_reference,
    'currency_letter' => currency_letter
  }.tap do |h|
    h.merge!(details.to_h) if details
    h.merge!(supplementary.to_h) if supplementary.source
  end
end

#to_hashObject



190
191
192
# File 'lib/cmxl/fields/transaction.rb', line 190

def to_hash
  to_h
end

#to_json(*args) ⇒ Object



194
195
196
# File 'lib/cmxl/fields/transaction.rb', line 194

def to_json(*args)
  to_h.to_json(*args)
end