+ foreach my $transaction (@{ $self->transactions }) {
+ next if $transaction->{error} || $transaction->{duplicate};
+
+ SL::DB::BankTransaction->new(
+ map { ($_ => $transaction->{$_}) } qw(amount currency_id local_bank_account_id purpose remote_account_number remote_bank_code remote_name transaction_code transdate valutadate)
+ )->save;
+
+ $imported++;
+ }
+
+ 1;
+ });
+
+ $self->statistics->{imported} = $imported;
+}
+
+sub check_auth {
+ $::auth->assert('bank_transaction');
+}
+
+sub make_bank_account_idx {
+ return join '/', map { my $q = $_; $q =~ s{ +}{}g; $q } @_;
+}
+
+sub normalize_text {
+ my ($text) = @_;
+
+ $text = lc($text // '');
+ $text =~ s{ }{}g;
+
+ return $text;
+}
+
+sub make_transaction_idx {
+ my ($transaction) = @_;
+
+ if (ref($transaction) eq 'SL::DB::BankTransaction') {
+ $transaction = { map { ($_ => $transaction->$_) } qw(local_bank_account_id transdate valutadate amount purpose) };
+ }
+
+ return normalize_text(join '/',
+ map { $_ // '' }
+ ($transaction->{local_bank_account_id},
+ $transaction->{transdate}->ymd,
+ $transaction->{valutadate}->ymd,
+ (apply { s{0+$}{} } $transaction->{amount} * 1),
+ $transaction->{purpose}));
+}
+
+sub init_bank_accounts {
+ my ($self) = @_;
+
+ my %bank_accounts;
+
+ foreach my $bank_account (@{ SL::DB::Manager::BankAccount->get_all }) {
+ if ($bank_account->bank_code && $bank_account->account_number) {
+ $bank_accounts{make_bank_account_idx($bank_account->bank_code, $bank_account->account_number)} = $bank_account;
+ }
+ if ($bank_account->iban) {
+ $bank_accounts{make_bank_account_idx('IBAN', $bank_account->iban)} = $bank_account;
+ }
+ }
+
+ return \%bank_accounts;
+}
+
+sub setup_upload_mt940_action_bar {
+ my ($self) = @_;
+
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ action => [
+ $::locale->text('Preview'),
+ submit => [ '#form', { action => 'BankImport/import_mt940_preview' } ],
+ accesskey => 'enter',
+ ],
+ );
+ }
+}
+
+sub setup_upload_mt940_preview_action_bar {
+ my ($self) = @_;
+
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ action => [
+ $::locale->text('Import'),
+ submit => [ '#form', { action => 'BankImport/import_mt940' } ],
+ accesskey => 'enter',
+ disabled => $self->statistics->{to_import} ? undef : $::locale->text('No entries can be imported.'),
+ ],
+ );
+ }
+}
+
+1;