Class: Abo

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

Defined Under Namespace

Classes: AboPaymentOrder, AboStatement, AboTransaction

Constant Summary collapse

LINE_TYPE_STATEMENT =
"statement"
LINE_TYPE_TRANSACTION =
"transaction"
LINE_TYPE_TRANSACTION_NOTE =
"transaction_note"
POSTING_CODE_DEBIT =
1
POSTING_CODE_CREDIT =
2
POSTING_CODE_DEBIT_REVERSAL =
4
POSTING_CODE_CREDIT_REVERSAL =
5

Instance Method Summary collapse

Instance Method Details

#export_to_gpc(path, statement) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/abo.rb', line 71

def export_to_gpc(path, statement)
  raise "Statement must be a hash" unless statement.is_a?(Hash)

  gpc = ""
  gpc += "074"
  gpc += statement[:client_account_prefix_number].to_s.rjust(6, "0")
  gpc += statement[:client_account_number].to_s.rjust(10, "0")
  gpc += statement[:abbreviated_client_name].to_s.ljust(20, " ")
  gpc += statement[:old_balance_date].strftime("%d%m%y")
  gpc += (statement[:old_balance] * 100).to_i.to_s.rjust(14, "0")
  gpc += statement[:old_balance] >= 0 ? "+" : "-" # sign of old balance
  gpc += (statement[:new_balance] * 100).to_i.to_s.rjust(14, "0")
  gpc += statement[:new_balance] >= 0 ? "+" : "-" # sign of new balance
  gpc += (statement[:transactions_debit] * 100).to_i.to_s.rjust(14, "0")
  gpc += "0" # sign of new balance
  gpc += (statement[:transactions_credit] * 100).to_i.to_s.rjust(14, "0")
  gpc += "0" # sign of new balance
  gpc += statement[:statement_sequence_number].to_s.rjust(3, "0")
  gpc += statement[:posting_date].strftime("%d%m%y")
  gpc += " " * 14
  gpc += " " * 2

  File.open(path, "w:cp1250") do |file|
    file.puts gpc
  end

  statement[:transactions].each do |transaction|
    case transaction[:posting_code]
    when "Debit"
      transaction[:posting_code] = 1
    when "Credit"
      transaction[:posting_code] = 2
    when "Debit Reversal"
      transaction[:posting_code] = 4
    when "Credit Reversal"
      transaction[:posting_code] = 5
    end

    gpc = "075"
    gpc += transaction[:client_account_prefix_number].to_s.rjust(6, "0")
    gpc += transaction[:client_account_number].to_s.rjust(10, "0")
    gpc += transaction[:counter_account_prefix_number].to_s.rjust(6, "0")
    gpc += transaction[:counter_account_number].to_s.rjust(10, "0")
    gpc += transaction[:document_number].to_s.ljust(13, " ")
    gpc += (transaction[:amount] * 100).abs.to_i.to_s.rjust(12, "0")
    gpc += transaction[:posting_code].to_s
    gpc += transaction[:variable_symbol].to_s.rjust(10, "0")
    gpc += (transaction[:counter_account_bank_code].to_s.rjust(4, "0") +
      transaction[:constant_symbol].to_s.rjust(4, "0")).rjust(
        10, "0"
      )
    gpc += transaction[:specific_symbol].to_s.rjust(10, "0")
    gpc += transaction[:value].strftime("%d%m%y")
    gpc += transaction[:additional_information].to_s.ljust(20, " ")
    gpc += transaction[:change_of_item_code].to_s.rjust(1, "0")
    gpc += transaction[:type_of_data].to_s.rjust(4, "0")
    gpc += transaction[:due_date].strftime("%d%m%y")
    gpc += " " * 2

    File.open(path, "a:cp1250") do |file|
      file.puts gpc
    end

    next if transaction[:note].nil?

    gpc = "078#{transaction[:note].ljust(70, " ")}"

    File.open(path, "a:cp1250") do |file|
      file.puts gpc
    end
  end
end

#export_to_p11(path, payment_orders) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/abo.rb', line 31

def export_to_p11(path, payment_orders)
  raise "Payment orders must be an array of hashes" unless payment_orders.is_a?(Array)

  p11s = []
  payment_orders.each do |payment_order|
    p11 = ""
    p11 += payment_order[:order_number_of_statement].to_s.rjust(6, "0")
    p11 += "11"
    p11 += payment_order[:file_creation_date].strftime("%y%m%d")
    p11 += payment_order[:own_bank_code].to_s.rjust(4, "0")
    p11 += " " * 3
    p11 += payment_order[:counter_account_bank_code].to_s.rjust(4, "0")
    p11 += " " * 3
    p11 += (payment_order[:amount] * 100).to_i.to_s.rjust(15, "0")
    p11 += payment_order[:due_date].strftime("%y%m%d")
    p11 += payment_order[:constant_symbol].to_s.rjust(10, "0")
    p11 += payment_order[:variable_symbol_credit].to_s.rjust(10, "0")
    p11 += payment_order[:specific_symbol_credit].to_s.rjust(10, "0")
    p11 += payment_order[:own_account_prefix].to_s.rjust(6, "0")
    p11 += payment_order[:own_account_number].to_s.rjust(10, "0")
    p11 += payment_order[:counter_account_prefix].to_s.rjust(6, "0")
    p11 += payment_order[:counter_account_number].to_s.rjust(10, "0")
    p11 += payment_order[:credit_information].to_s.ljust(140, " ")
    p11 += payment_order[:own_account_name].to_s.ljust(20, " ")
    p11 += payment_order[:counter_account_name].to_s.ljust(20, " ")
    p11 += payment_order[:variable_symbol_debit].to_s.rjust(10, "0")
    p11 += payment_order[:specific_symbol_debit].to_s.rjust(10, "0")
    p11 += payment_order[:debit_information].to_s.ljust(140, " ")
    p11 += payment_order[:bank_information].to_s.ljust(140, " ")

    p11s.push(p11)
  end

  File.open(path, "w") do |file|
    p11s.each do |p11|
      file.puts p11
    end
  end
end

#parse_file(file_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/abo.rb', line 16

def parse_file(file_path)
  file_object = File.new(file_path)
  file_extention = file_path.to_s.split(".")[1].downcase
  case file_extention
  when "gpc"
    parse_bank_statement_file(file_object)
  when "p11"
    parse_payment_order_file(file_object)
  else
    raise "File type not supported"
  end
rescue StandardError => e
  raise "Error parsing file: #{e.message}"
end