MT940-Import: Implementation eines eigenen Parsers anstelle von AQBanking
[kivitendo-erp.git] / SL / Controller / BankImport.pm
1 package SL::Controller::BankImport;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use List::MoreUtils qw(apply);
8 use List::Util qw(max min);
9
10 use SL::DB::BankAccount;
11 use SL::DB::BankTransaction;
12 use SL::DB::Default;
13 use SL::Helper::Flash;
14 use SL::MT940;
15 use SL::SessionFile::Random;
16
17 use Rose::Object::MakeMethods::Generic
18 (
19   scalar                  => [ qw(file_name transactions statistics) ],
20   'scalar --get_set_init' => [ qw(bank_accounts) ],
21 );
22
23 __PACKAGE__->run_before('check_auth');
24
25 sub action_upload_mt940 {
26   my ($self, %params) = @_;
27
28   $self->setup_upload_mt940_action_bar;
29   $self->render('bank_import/upload_mt940', title => $::locale->text('MT940 import'));
30 }
31
32 sub action_import_mt940_preview {
33   my ($self, %params) = @_;
34
35   if (!$::form->{file}) {
36     flash_later('error', $::locale->text('You have to upload an MT940 file to import.'));
37     return $self->redirect_to(action => 'upload_mt940');
38   }
39
40   die "missing file for action import_mt940_preview" unless $::form->{file};
41
42   my $file = SL::SessionFile::Random->new(mode => '>');
43   $file->fh->print($::form->{file});
44   $file->fh->close;
45
46   $self->file_name($file->file_name);
47   $self->parse_and_analyze_transactions;
48
49   $self->setup_upload_mt940_preview_action_bar;
50   $self->render('bank_import/import_mt940', title => $::locale->text('MT940 import preview'), preview => 1);
51 }
52
53 sub action_import_mt940 {
54   my ($self, %params) = @_;
55
56   die "missing file for action import_mt940" unless $::form->{file_name};
57
58   $self->file_name($::form->{file_name});
59   $self->parse_and_analyze_transactions;
60   $self->import_transactions;
61
62   $self->render('bank_import/import_mt940', title => $::locale->text('MT940 import result'));
63 }
64
65 sub parse_and_analyze_transactions {
66   my ($self, %params) = @_;
67
68   my $errors     = 0;
69   my $duplicates = 0;
70   my ($min_date, $max_date);
71
72   my $currency_id = SL::DB::Default->get->currency_id;
73
74   $self->transactions([ sort { $a->{transdate} cmp $b->{transdate} } SL::MT940->parse($self->file_name) ]);
75
76   foreach my $transaction (@{ $self->transactions }) {
77     $transaction->{bank_account}   = $self->bank_accounts->{ make_bank_account_idx($transaction->{local_bank_code}, $transaction->{local_account_number}) };
78     $transaction->{bank_account} //= $self->bank_accounts->{ make_bank_account_idx('IBAN',                          $transaction->{local_account_number}) };
79
80     if (!$transaction->{bank_account}) {
81       $transaction->{error} = $::locale->text('No bank account configured for bank code/BIC #1, account number/IBAN #2.', $transaction->{local_bank_code}, $transaction->{local_account_number});
82       $errors++;
83       next;
84     }
85
86     $transaction->{local_bank_account_id} = $transaction->{bank_account}->id;
87     $transaction->{currency_id}           = $currency_id;
88
89     $min_date = min($min_date // $transaction->{transdate}, $transaction->{transdate});
90     $max_date = max($max_date // $transaction->{transdate}, $transaction->{transdate});
91   }
92
93   my %existing_bank_transactions;
94
95   if ((scalar(@{ $self->transactions }) - $errors) > 0) {
96     my @entries =
97       @{ SL::DB::Manager::BankTransaction->get_all(
98           where => [
99             transdate => { ge => $min_date },
100             transdate => { lt => $max_date->clone->add(days => 1) },
101           ],
102           inject_results => 1) };
103
104     %existing_bank_transactions = map { (make_transaction_idx($_) => 1) } @entries;
105   }
106
107   foreach my $transaction (@{ $self->transactions }) {
108     next if $transaction->{error};
109
110     if ($existing_bank_transactions{make_transaction_idx($transaction)}) {
111       $transaction->{duplicate} = 1;
112       $duplicates++;
113       next;
114     }
115   }
116
117   $self->statistics({
118     total      => scalar(@{ $self->transactions }),
119     errors     => $errors,
120     duplicates => $duplicates,
121     to_import  => scalar(@{ $self->transactions }) - $errors - $duplicates,
122   });
123 }
124
125 sub import_transactions {
126   my ($self, %params) = @_;
127
128   my $imported = 0;
129
130   SL::DB::client->with_transaction(sub {
131     # make Emacs happy
132     1;
133
134     foreach my $transaction (@{ $self->transactions }) {
135       next if $transaction->{error} || $transaction->{duplicate};
136
137       SL::DB::BankTransaction->new(
138         map { ($_ => $transaction->{$_}) } qw(amount currency_id local_bank_account_id purpose remote_account_number remote_bank_code remote_name transaction_code transdate valutadate)
139       )->save;
140
141       $imported++;
142     }
143
144     1;
145   });
146
147   $self->statistics->{imported} = $imported;
148 }
149
150 sub check_auth {
151   $::auth->assert('bank_transaction');
152 }
153
154 sub make_bank_account_idx {
155   return join '/', map { my $q = $_; $q =~ s{ +}{}g; $q } @_;
156 }
157
158 sub normalize_text {
159   my ($text) = @_;
160
161   $text = lc($text // '');
162   $text =~ s{ }{}g;
163
164   return $text;
165 }
166
167 sub make_transaction_idx {
168   my ($transaction) = @_;
169
170   if (ref($transaction) eq 'SL::DB::BankTransaction') {
171     $transaction = { map { ($_ => $transaction->$_) } qw(local_bank_account_id transdate valutadate amount purpose) };
172   }
173
174   return normalize_text(join '/',
175                         map { $_ // '' }
176                         ($transaction->{local_bank_account_id},
177                          $transaction->{transdate}->ymd,
178                          $transaction->{valutadate}->ymd,
179                          (apply { s{0+$}{} } $transaction->{amount} * 1),
180                          $transaction->{purpose}));
181 }
182
183 sub init_bank_accounts {
184   my ($self) = @_;
185
186   my %bank_accounts;
187
188   foreach my $bank_account (@{ SL::DB::Manager::BankAccount->get_all }) {
189     if ($bank_account->bank_code && $bank_account->account_number) {
190       $bank_accounts{make_bank_account_idx($bank_account->bank_code, $bank_account->account_number)} = $bank_account;
191     }
192     if ($bank_account->iban) {
193       $bank_accounts{make_bank_account_idx('IBAN', $bank_account->iban)} = $bank_account;
194     }
195   }
196
197   return \%bank_accounts;
198 }
199
200 sub setup_upload_mt940_action_bar {
201   my ($self) = @_;
202
203   for my $bar ($::request->layout->get('actionbar')) {
204     $bar->add(
205       action => [
206         $::locale->text('Preview'),
207         submit    => [ '#form', { action => 'BankImport/import_mt940_preview' } ],
208         accesskey => 'enter',
209       ],
210     );
211   }
212 }
213
214 sub setup_upload_mt940_preview_action_bar {
215   my ($self) = @_;
216
217   for my $bar ($::request->layout->get('actionbar')) {
218     $bar->add(
219       action => [
220         $::locale->text('Import'),
221         submit    => [ '#form', { action => 'BankImport/import_mt940' } ],
222         accesskey => 'enter',
223         disabled  => $self->statistics->{to_import} ? undef : $::locale->text('No entries can be imported.'),
224       ],
225     );
226   }
227 }
228
229 1;