11# frozen_string_literal: true
22
3- require "cmxl "
4- require "camt_parser"
3+ require "sepa_file_parser "
4+
55
66require_relative "../models/account"
77require_relative "../models/bank_statement"
88require_relative "../models/event"
99require_relative "../../lib/checksum_generator"
10+ require_relative "../../lib/data_mapping/statement_factory"
1011
1112module Box
1213 module BusinessProcesses
1314 class ImportStatements
14- PARSERS = { "mt940" => Cmxl , "camt53" => CamtParser :: Format053 ::Statement } . freeze
15+ PARSERS = { "mt940" => Cmxl , "camt53" => SepaFileParser :: Camt053 ::Statement } . freeze
1516
1617 def self . from_bank_statement ( bank_statement , upcoming = false )
1718 bank_transactions = parse_bank_statement ( bank_statement )
@@ -28,7 +29,9 @@ def self.from_bank_statement(bank_statement, upcoming = false)
2829 def self . parse_bank_statement ( bank_statement )
2930 parser = PARSERS . fetch ( bank_statement . account . statements_format , Cmxl )
3031 result = parser . parse ( bank_statement . content )
31- result . is_a? ( Array ) ? result . first . transactions : result . transactions
32+ statement_data = result . is_a? ( Array ) ? result . first : result
33+ statement = DataMapping ::StatementFactory . new ( statement_data , bank_statement . account ) . call
34+ statement . transactions
3235 end
3336
3437 def self . create_statement ( bank_statement , bank_transaction , upcoming = false )
@@ -71,51 +74,47 @@ def self.checksum(transaction, bank_statement)
7174 end
7275
7376 def self . checksum_attributes ( transaction , remote_account )
74- return [ remote_account , transaction . transaction_id ] if transaction . try ( : transaction_id) . present?
77+ return [ remote_account , transaction . transaction_id ] if transaction . transaction_id . present?
7578
7679 payload_from_transaction_attributes ( transaction , remote_account )
7780 end
7881
7982 def self . payload_from_transaction_attributes ( transaction , remote_account )
80- eref = transaction . respond_to? ( :eref ) ? transaction . eref : transaction . sepa [ "EREF" ]
81- mref = transaction . respond_to? ( :mref ) ? transaction . mref : transaction . sepa [ "MREF" ]
82- svwz = transaction . respond_to? ( :svwz ) ? transaction . svwz : transaction . sepa [ "SVWZ" ]
83-
8483 [
8584 remote_account ,
8685 transaction . date ,
8786 transaction . amount_in_cents ,
8887 transaction . iban ,
8988 transaction . name ,
9089 transaction . sign ,
91- eref ,
92- mref ,
93- svwz ,
90+ transaction . eref ,
91+ transaction . mref ,
92+ transaction . svwz ,
9493 transaction . information . gsub ( /\s / , "" )
9594 ]
9695 end
9796
9897 def self . statement_attributes_from_bank_transaction ( transaction , bank_statement )
9998 {
100- sha : checksum ( transaction , bank_statement ) ,
101- date : transaction . date ,
102- entry_date : transaction . entry_date ,
10399 amount : transaction . amount_in_cents ,
104- sign : transaction . sign ,
105- debit : transaction . debit? ,
106- swift_code : transaction . swift_code ,
107- reference : transaction . reference ,
108100 bank_reference : transaction . bank_reference ,
109101 bic : transaction . bic ,
102+ creditor_identifier : transaction . creditor_identifier ,
103+ date : transaction . date ,
104+ debit : transaction . debit? ,
105+ description : transaction . description ,
106+ entry_date : transaction . entry_date ,
107+ eref : transaction . eref ,
110108 iban : transaction . iban ,
111- name : transaction . name ,
112109 information : transaction . information ,
113- description : transaction . description ,
114- eref : transaction . respond_to? ( :eref ) ? transaction . eref : transaction . sepa [ "EREF" ] ,
115- mref : transaction . respond_to? ( :mref ) ? transaction . mref : transaction . sepa [ "MREF" ] ,
116- svwz : transaction . respond_to? ( :svwz ) ? transaction . svwz : transaction . sepa [ "SVWZ" ] ,
117- tx_id : transaction . try ( :primanota ) || transaction . try ( :transaction_id ) ,
118- creditor_identifier : transaction . respond_to? ( :creditor_identifier ) ? transaction . creditor_identifier : transaction . sepa [ "CRED" ]
110+ mref : transaction . mref ,
111+ name : transaction . name ,
112+ reference : transaction . reference ,
113+ sha : checksum ( transaction , bank_statement ) ,
114+ sign : transaction . sign ,
115+ svwz : transaction . svwz ,
116+ swift_code : transaction . swift_code ,
117+ tx_id : transaction . transaction_id ,
119118 }
120119 end
121120 end
0 commit comments