11# frozen_string_literal: true
22
3- require "cmxl"
4- require "camt_parser"
3+ require "sepa_file_parser"
54
65require_relative "../models/account"
76require_relative "../models/bank_statement"
87require_relative "../models/event"
98require_relative "../../lib/checksum_generator"
9+ require_relative "../../lib/data_mapping/statement_factory"
1010
1111module Box
1212 module BusinessProcesses
1313 class ImportStatements
14- PARSERS = { "mt940" => Cmxl , "camt53" => CamtParser :: Format053 ::Statement } . freeze
14+ PARSERS = { "mt940" => Cmxl , "camt53" => SepaFileParser :: Camt053 ::Statement } . freeze
1515
1616 def self . from_bank_statement ( bank_statement , upcoming = false )
1717 bank_transactions = parse_bank_statement ( bank_statement )
@@ -28,7 +28,9 @@ def self.from_bank_statement(bank_statement, upcoming = false)
2828 def self . parse_bank_statement ( bank_statement )
2929 parser = PARSERS . fetch ( bank_statement . account . statements_format , Cmxl )
3030 result = parser . parse ( bank_statement . content )
31- result . is_a? ( Array ) ? result . first . transactions : result . transactions
31+ statement_data = result . is_a? ( Array ) ? result . first : result
32+ statement = DataMapping ::StatementFactory . new ( statement_data , bank_statement . account ) . call
33+ statement . transactions
3234 end
3335
3436 def self . create_statement ( bank_statement , bank_transaction , upcoming = false )
@@ -71,51 +73,47 @@ def self.checksum(transaction, bank_statement)
7173 end
7274
7375 def self . checksum_attributes ( transaction , remote_account )
74- return [ remote_account , transaction . transaction_id ] if transaction . try ( : transaction_id) . present?
76+ return [ remote_account , transaction . transaction_id ] if transaction . transaction_id . present?
7577
7678 payload_from_transaction_attributes ( transaction , remote_account )
7779 end
7880
7981 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-
8482 [
8583 remote_account ,
8684 transaction . date ,
8785 transaction . amount_in_cents ,
8886 transaction . iban ,
8987 transaction . name ,
9088 transaction . sign ,
91- eref ,
92- mref ,
93- svwz ,
89+ transaction . eref ,
90+ transaction . mref ,
91+ transaction . svwz ,
9492 transaction . information . gsub ( /\s / , "" )
9593 ]
9694 end
9795
9896 def self . statement_attributes_from_bank_transaction ( transaction , bank_statement )
9997 {
100- sha : checksum ( transaction , bank_statement ) ,
101- date : transaction . date ,
102- entry_date : transaction . entry_date ,
10398 amount : transaction . amount_in_cents ,
104- sign : transaction . sign ,
105- debit : transaction . debit? ,
106- swift_code : transaction . swift_code ,
107- reference : transaction . reference ,
10899 bank_reference : transaction . bank_reference ,
109100 bic : transaction . bic ,
101+ creditor_identifier : transaction . creditor_identifier ,
102+ date : transaction . date ,
103+ debit : transaction . debit? ,
104+ description : transaction . description ,
105+ entry_date : transaction . entry_date ,
106+ eref : transaction . eref ,
110107 iban : transaction . iban ,
111- name : transaction . name ,
112108 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" ]
109+ mref : transaction . mref ,
110+ name : transaction . name ,
111+ reference : transaction . reference ,
112+ sha : checksum ( transaction , bank_statement ) ,
113+ sign : transaction . sign ,
114+ svwz : transaction . svwz ,
115+ swift_code : transaction . swift_code ,
116+ tx_id : transaction . transaction_id
119117 }
120118 end
121119 end
0 commit comments