Class: Appydave::Tools::BankReconciliation::Models::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/bank_reconciliation/models/transaction.rb

Overview

Unified transaction model for raw and reconciled data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bsb_number: nil, account_number: nil, transaction_date: nil, narration: nil, cheque_number: nil, debit: nil, credit: nil, balance: nil, transaction_type: nil, platform: nil, coa_code: nil, coa_match_type: nil, account_name: nil) ⇒ Transaction

Returns a new instance of Transaction.



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
52
53
54
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 25

def initialize(bsb_number: nil,
               account_number: nil,
               transaction_date: nil,
               narration: nil,
               cheque_number: nil,
               debit: nil,
               credit: nil,
               balance: nil,
               transaction_type: nil,
               platform: nil,
               coa_code: nil,
               coa_match_type: nil,
               account_name: nil)
   = &.strip
  @bsb_number = bsb_number&.strip
  @account_number = 
  @transaction_date = parse_date(transaction_date)
  @narration = narration&.gsub(/\s{2,}/, ' ')&.strip
  @cheque_number = cheque_number&.strip
  @debit = debit # clean_amount(debit, account_number, coa_code)
  @credit = credit # clean_amount(credit, account_number, coa_code)
  @balance = balance&.strip
  @transaction_type = transaction_type&.strip
  @platform = platform
  @coa_code = coa_code
  @coa_match_type = coa_match_type
  @account_name = 
  @source_files = []
  @fin_year = determine_fin_year(@transaction_date)
end

Instance Attribute Details

#account_nameObject

Returns the value of attribute account_name.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def 
  @account_name
end

#account_numberObject

Returns the value of attribute account_number.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def 
  @account_number
end

#balanceObject

Returns the value of attribute balance.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def balance
  @balance
end

#bsb_numberObject

Returns the value of attribute bsb_number.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def bsb_number
  @bsb_number
end

#cheque_numberObject

Returns the value of attribute cheque_number.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def cheque_number
  @cheque_number
end

#coa_codeObject

Returns the value of attribute coa_code.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def coa_code
  @coa_code
end

#coa_match_typeObject

Returns the value of attribute coa_match_type.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def coa_match_type
  @coa_match_type
end

#creditObject

Returns the value of attribute credit.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def credit
  @credit
end

#debitObject

Returns the value of attribute debit.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def debit
  @debit
end

#fin_yearObject

Returns the value of attribute fin_year.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def fin_year
  @fin_year
end

#narrationObject

Returns the value of attribute narration.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def narration
  @narration
end

#platformObject

Returns the value of attribute platform.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def platform
  @platform
end

#source_filesObject

Returns the value of attribute source_files.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def source_files
  @source_files
end

#transaction_dateObject

Returns the value of attribute transaction_date.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def transaction_date
  @transaction_date
end

#transaction_typeObject

Returns the value of attribute transaction_type.



9
10
11
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 9

def transaction_type
  @transaction_type
end

Class Method Details

.csv_headersObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 60

def self.csv_headers
  %i[
    platform
    account_name
    bsb_number
    account_number
    transaction_date
    narration
    debit
    credit
    balance
    transaction_type
    coa_code
    coa_match_type
    source_files
    fin_year
  ]
end

Instance Method Details

#add_source_file(source_file) ⇒ Object



56
57
58
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 56

def add_source_file(source_file)
  @source_files << source_file.strip unless @source_files.include?(source_file.strip)
end

#clean_amount(amount) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 98

def clean_amount(amount)
  return nil if amount.nil? || amount.strip.empty?

  cleaned_amount = amount.to_f.round(2)

  if swap_plus_minus_for_transactions
    if cleaned_amount.negative?
      cleaned_amount = cleaned_amount.abs
      # else
      #   cleaned_amount *= -1
    end
    puts "Fixed negative amount for account: #{} and COA: #{coa_code}, original amount: #{amount}, fixed amount: #{cleaned_amount}" if coa_code == 'DANCE'
  end
  cleaned_amount
end

#swap_plus_minus_for_transactionsObject

Returns true if this transaction’s amount should have its sign flipped based on the per-account, per-FY, per-COA rules stored in config.

Rules live in ~/.config/appydave/bank-reconciliation.json under ‘sign_flip_rules` — see BankReconciliationConfig::SignFlipRule. Personal account numbers MUST NOT be hardcoded here; the config file is local-only and never committed to the repo.



121
122
123
124
125
126
127
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 121

def swap_plus_minus_for_transactions
  sign_flip_rules.any? do |rule|
    (rule.fin_year.nil? || rule.fin_year == fin_year) &&
      rule. ==  &&
      rule.coa_codes.include?(coa_code)
  end
end

#to_csv_rowObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/appydave/tools/bank_reconciliation/models/transaction.rb', line 79

def to_csv_row
  [
    @platform,
    @account_name,
    @bsb_number,
    @account_number,
    @transaction_date,
    @narration,
    @debit ? format('%.2f', @debit) : '',
    @credit ? format('%.2f', @credit) : '',
    @balance,
    @transaction_type,
    @coa_code,
    @coa_match_type,
    @source_files.join('; '),
    @fin_year
  ]
end