CSV Import Defaults in die Worker ausgelagert
[kivitendo-erp.git] / SL / Controller / BankTransaction.pm
1 package SL::Controller::BankTransaction;
2
3 # idee- möglichkeit bankdaten zu übernehmen in stammdaten
4 # erst Kontenabgleich, um alle gl-Einträge wegzuhaben
5 use strict;
6
7 use parent qw(SL::Controller::Base);
8
9 use SL::Controller::Helper::GetModels;
10 use SL::Controller::Helper::ReportGenerator;
11 use SL::ReportGenerator;
12
13 use SL::DB::BankTransaction;
14 use SL::Helper::Flash;
15 use SL::Locale::String;
16 use SL::SEPA;
17 use SL::DB::Invoice;
18 use SL::DB::PurchaseInvoice;
19 use SL::DB::RecordLink;
20 use SL::JSON;
21 use SL::DB::Chart;
22 use SL::DB::AccTransaction;
23 use SL::DB::Tax;
24 use SL::DB::Draft;
25 use SL::DB::BankAccount;
26 use SL::Presenter;
27 use List::Util qw(max);
28
29 use Rose::Object::MakeMethods::Generic
30 (
31  'scalar --get_set_init' => [ qw(models) ],
32 );
33
34 __PACKAGE__->run_before('check_auth');
35
36
37 #
38 # actions
39 #
40
41 sub action_search {
42   my ($self) = @_;
43
44   my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
45
46   $self->render('bank_transactions/search',
47                  BANK_ACCOUNTS => $bank_accounts);
48 }
49
50 sub action_list_all {
51   my ($self) = @_;
52
53   $self->make_filter_summary;
54   $self->prepare_report;
55
56   $self->report_generator_list_objects(report => $self->{report}, objects => $self->models->get);
57 }
58
59 sub action_list {
60   my ($self) = @_;
61
62   if (!$::form->{filter}{bank_account}) {
63     flash('error', t8('No bank account chosen!'));
64     $self->action_search;
65     return;
66   }
67
68   my $sort_by = $::form->{sort_by} || 'transdate';
69   $sort_by = 'transdate' if $sort_by eq 'proposal';
70   $sort_by .= $::form->{sort_dir} ? ' DESC' : ' ASC';
71
72   my $fromdate = $::locale->parse_date_to_object($::form->{filter}->{fromdate});
73   my $todate   = $::locale->parse_date_to_object($::form->{filter}->{todate});
74   $todate->add( days => 1 ) if $todate;
75
76   my @where = ();
77   push @where, (transdate => { ge => $fromdate }) if ($fromdate);
78   push @where, (transdate => { lt => $todate })   if ($todate);
79   my $bank_account = SL::DB::Manager::BankAccount->find_by( id => $::form->{filter}{bank_account} );
80   # bank_transactions no younger than starting date,
81   # but OPEN invoices to be matched may be from before
82   if ( $bank_account->reconciliation_starting_date ) {
83     push @where, (transdate => { gt => $bank_account->reconciliation_starting_date });
84   };
85
86   my $bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => [ amount => {ne => \'invoice_amount'},
87                                                                                local_bank_account_id => $::form->{filter}{bank_account},
88                                                                                @where ],
89                                                                     with_objects => [ 'local_bank_account', 'currency' ],
90                                                                     sort_by => $sort_by, limit => 10000);
91
92   my $all_open_ar_invoices = SL::DB::Manager::Invoice->get_all(where => [amount => { gt => \'paid' }], with_objects => 'customer');
93   my $all_open_ap_invoices = SL::DB::Manager::PurchaseInvoice->get_all(where => [amount => { gt => \'paid' }], with_objects => 'vendor');
94
95   my @all_open_invoices;
96   # filter out invoices with less than 1 cent outstanding
97   push @all_open_invoices, grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ar_invoices };
98   push @all_open_invoices, grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ap_invoices };
99
100   # try to match each bank_transaction with each of the possible open invoices
101   # by awarding points
102
103   foreach my $bt (@{ $bank_transactions }) {
104     next unless $bt->{remote_name};  # bank has no name, usually fees, use create invoice to assign
105
106     $bt->{remote_name} .= $bt->{remote_name_1} if $bt->{remote_name_1};
107
108     # try to match the current $bt to each of the open_invoices, saving the
109     # results of get_agreement_with_invoice in $open_invoice->{agreement} and
110     # $open_invoice->{rule_matches}.
111
112     # The values are overwritten each time a new bt is checked, so at the end
113     # of each bt the likely results are filtered and those values are stored in
114     # the arrays $bt->{proposals} and $bt->{rule_matches}, and the agreement
115     # score is stored in $bt->{agreement}
116
117     foreach my $open_invoice (@all_open_invoices){
118       ($open_invoice->{agreement}, $open_invoice->{rule_matches}) = $bt->get_agreement_with_invoice($open_invoice);
119     };
120
121     $bt->{proposals} = [];
122
123     my $agreement = 15;
124     my $min_agreement = 3; # suggestions must have at least this score
125
126     my $max_agreement = max map { $_->{agreement} } @all_open_invoices;
127
128     # add open_invoices with highest agreement into array $bt->{proposals}
129     if ( $max_agreement >= $min_agreement ) {
130       $bt->{proposals} = [ grep { $_->{agreement} == $max_agreement } @all_open_invoices ];
131       $bt->{agreement} = $max_agreement; #scalar @{ $bt->{proposals} } ? $agreement + 1 : '';
132
133       # store the rule_matches in a separate array, so they can be displayed in template
134       foreach ( @{ $bt->{proposals} } ) {
135         push(@{$bt->{rule_matches}}, $_->{rule_matches});
136       };
137     };
138   }  # finished one bt
139   # finished all bt
140
141   # separate filter for proposals (second tab, agreement >= 5 and exactly one match)
142   # to qualify as a proposal there has to be
143   # * agreement >= 5  TODO: make threshold configurable in configuration
144   # * there must be only one exact match
145   # * depending on whether sales or purchase the amount has to have the correct sign (so Gutschriften don't work?)
146   my $proposal_threshold = 5;
147   my @proposals = grep { $_->{agreement} >= $proposal_threshold
148                          and 1 == scalar @{ $_->{proposals} }
149                          and (@{ $_->{proposals} }[0]->is_sales ? abs(@{ $_->{proposals} }[0]->amount - $_->amount) < 0.01  : abs(@{ $_->{proposals} }[0]->amount + $_->amount) < 0.01) } @{ $bank_transactions };
150
151   # sort bank transaction proposals by quality (score) of proposal
152   $bank_transactions = [ sort { $a->{agreement} <=> $b->{agreement} } @{ $bank_transactions } ] if $::form->{sort_by} eq 'proposal' and $::form->{sort_dir} == 1;
153   $bank_transactions = [ sort { $b->{agreement} <=> $a->{agreement} } @{ $bank_transactions } ] if $::form->{sort_by} eq 'proposal' and $::form->{sort_dir} == 0;
154
155
156   $self->render('bank_transactions/list',
157                 title             => t8('Bank transactions MT940'),
158                 BANK_TRANSACTIONS => $bank_transactions,
159                 PROPOSALS         => \@proposals,
160                 bank_account      => $bank_account );
161 }
162
163 sub action_assign_invoice {
164   my ($self) = @_;
165
166   $self->{transaction} = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
167
168   $self->render('bank_transactions/assign_invoice', { layout  => 0 },
169                 title      => t8('Assign invoice'),);
170 }
171
172 sub action_create_invoice {
173   my ($self) = @_;
174   my %myconfig = %main::myconfig;
175
176   $self->{transaction} = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
177   my $vendor_of_transaction = SL::DB::Manager::Vendor->find_by(account_number => $self->{transaction}->{remote_account_number});
178
179   my $drafts = SL::DB::Manager::Draft->get_all(where => [ module => 'ap'] , with_objects => 'employee');
180
181   my @filtered_drafts;
182
183   foreach my $draft ( @{ $drafts } ) {
184     my $draft_as_object = YAML::Load($draft->form);
185     my $vendor = SL::DB::Manager::Vendor->find_by(id => $draft_as_object->{vendor_id});
186     $draft->{vendor} = $vendor->name;
187     $draft->{vendor_id} = $vendor->id;
188     push @filtered_drafts, $draft;
189   }
190
191   #Filter drafts
192   @filtered_drafts = grep { $_->{vendor_id} == $vendor_of_transaction->id } @filtered_drafts if $vendor_of_transaction;
193
194   my $all_vendors = SL::DB::Manager::Vendor->get_all();
195
196   $self->render('bank_transactions/create_invoice', { layout  => 0 },
197       title      => t8('Create invoice'),
198       DRAFTS     => \@filtered_drafts,
199       vendor_id  => $vendor_of_transaction ? $vendor_of_transaction->id : undef,
200       vendor_name => $vendor_of_transaction ? $vendor_of_transaction->name : undef,
201       ALL_VENDORS => $all_vendors,
202       limit      => $myconfig{vclimit},
203       callback   => $self->url_for(action                => 'list',
204                                    'filter.bank_account' => $::form->{filter}->{bank_account},
205                                    'filter.todate'       => $::form->{filter}->{todate},
206                                    'filter.fromdate'     => $::form->{filter}->{fromdate}),
207       );
208 }
209
210 sub action_ajax_payment_suggestion {
211   my ($self) = @_;
212
213   # based on a BankTransaction ID and a Invoice or PurchaseInvoice ID passed via $::form,
214   # create an HTML blob to be used by the js function add_invoices in templates/webpages/bank_transactions/list.html
215   # and return encoded as JSON
216
217   my $bt = SL::DB::Manager::BankTransaction->find_by( id => $::form->{bt_id} );
218   my $invoice = SL::DB::Manager::Invoice->find_by( id => $::form->{prop_id} );
219   $invoice = SL::DB::Manager::PurchaseInvoice->find_by( id => $::form->{prop_id} ) unless $invoice;
220
221   die unless $bt and $invoice;
222
223   my @select_options = $invoice->get_payment_select_options_for_bank_transaction($::form->{bt_id});
224
225   my $html;
226   $html .= SL::Presenter->input_tag('invoice_ids.' . $::form->{bt_id} . '[]', $::form->{prop_id} , type => 'hidden');
227   $html .= SL::Presenter->escape( $invoice->invnumber );
228   $html .= SL::Presenter->select_tag('invoice_skontos.' . $::form->{bt_id} . '[]', \@select_options,
229                                               value_key => 'payment_type',
230                                               title_key => 'display' ) if @select_options;
231   $html .= '<a href=# onclick="delete_invoice(' . $::form->{bt_id} . ',' . $::form->{prop_id} . ');">x</a>';
232   $html = SL::Presenter->html_tag('div', $html, id => $::form->{bt_id} . '.' . $::form->{prop_id});
233
234   $self->render(\ SL::JSON::to_json( { 'html' => $html } ), { layout => 0, type => 'json', process => 0 });
235 };
236
237 sub action_filter_drafts {
238   my ($self) = @_;
239
240   $self->{transaction} = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
241   my $vendor_of_transaction = SL::DB::Manager::Vendor->find_by(account_number => $self->{transaction}->{remote_account_number});
242
243   my $drafts = SL::DB::Manager::Draft->get_all(with_objects => 'employee');
244
245   my @filtered_drafts;
246
247   foreach my $draft ( @{ $drafts } ) {
248     my $draft_as_object = YAML::Load($draft->form);
249     my $vendor = SL::DB::Manager::Vendor->find_by(id => $draft_as_object->{vendor_id});
250     $draft->{vendor} = $vendor->name;
251     $draft->{vendor_id} = $vendor->id;
252     push @filtered_drafts, $draft;
253   }
254
255   my $vendor_name = $::form->{vendor};
256   my $vendor_id = $::form->{vendor_id};
257
258   #Filter drafts
259   @filtered_drafts = grep { $_->{vendor_id} == $vendor_id } @filtered_drafts if $vendor_id;
260   @filtered_drafts = grep { $_->{vendor} =~ /$vendor_name/i } @filtered_drafts if $vendor_name;
261
262   my $output  = $self->render(
263       'bank_transactions/filter_drafts',
264       { output      => 0 },
265       DRAFTS => \@filtered_drafts,
266       );
267
268   my %result = ( count => 0, html => $output );
269
270   $self->render(\to_json(\%result), { type => 'json', process => 0 });
271 }
272
273 sub action_ajax_add_list {
274   my ($self) = @_;
275
276   my @where_sale     = (amount => { ne => \'paid' });
277   my @where_purchase = (amount => { ne => \'paid' });
278
279   if ($::form->{invnumber}) {
280     push @where_sale,     (invnumber => { ilike => '%' . $::form->{invnumber} . '%'});
281     push @where_purchase, (invnumber => { ilike => '%' . $::form->{invnumber} . '%'});
282   }
283
284   if ($::form->{amount}) {
285     push @where_sale,     (amount => $::form->parse_amount(\%::myconfig, $::form->{amount}));
286     push @where_purchase, (amount => $::form->parse_amount(\%::myconfig, $::form->{amount}));
287   }
288
289   if ($::form->{vcnumber}) {
290     push @where_sale,     ('customer.customernumber' => { ilike => '%' . $::form->{vcnumber} . '%'});
291     push @where_purchase, ('vendor.vendornumber'     => { ilike => '%' . $::form->{vcnumber} . '%'});
292   }
293
294   if ($::form->{vcname}) {
295     push @where_sale,     ('customer.name' => { ilike => '%' . $::form->{vcname} . '%'});
296     push @where_purchase, ('vendor.name'   => { ilike => '%' . $::form->{vcname} . '%'});
297   }
298
299   if ($::form->{transdatefrom}) {
300     my $fromdate = $::locale->parse_date_to_object($::form->{transdatefrom});
301     if ( ref($fromdate) eq 'DateTime' ) {
302       push @where_sale,     ('transdate' => { ge => $fromdate});
303       push @where_purchase, ('transdate' => { ge => $fromdate});
304     };
305   }
306
307   if ($::form->{transdateto}) {
308     my $todate = $::locale->parse_date_to_object($::form->{transdateto});
309     if ( ref($todate) eq 'DateTime' ) {
310       $todate->add(days => 1);
311       push @where_sale,     ('transdate' => { lt => $todate});
312       push @where_purchase, ('transdate' => { lt => $todate});
313     };
314   }
315
316   my $all_open_ar_invoices = SL::DB::Manager::Invoice->get_all(where => \@where_sale, with_objects => 'customer');
317   my $all_open_ap_invoices = SL::DB::Manager::PurchaseInvoice->get_all(where => \@where_purchase, with_objects => 'vendor');
318
319   my @all_open_invoices;
320   # filter out subcent differences from ap invoices
321   push @all_open_invoices, grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ap_invoices };
322
323   @all_open_invoices = sort { $a->id <=> $b->id } @all_open_invoices;
324
325   my $output  = $self->render(
326       'bank_transactions/add_list',
327       { output      => 0 },
328       INVOICES => \@all_open_invoices,
329       );
330
331   my %result = ( count => 0, html => $output );
332
333   $self->render(\to_json(\%result), { type => 'json', process => 0 });
334 }
335
336 sub action_ajax_accept_invoices {
337   my ($self) = @_;
338
339   my @selected_invoices;
340   foreach my $invoice_id (@{ $::form->{invoice_id} || [] }) {
341     my $invoice_object = SL::DB::Manager::Invoice->find_by(id => $invoice_id);
342     $invoice_object ||= SL::DB::Manager::PurchaseInvoice->find_by(id => $invoice_id);
343
344     push @selected_invoices, $invoice_object;
345   }
346
347   $self->render('bank_transactions/invoices', { layout => 0 },
348                 INVOICES => \@selected_invoices,
349                 bt_id    => $::form->{bt_id} );
350 }
351
352 sub action_save_invoices {
353   my ($self) = @_;
354
355   my $invoice_hash = delete $::form->{invoice_ids}; # each key (the bt line with a bt_id) contains an array of invoice_ids
356   my $skonto_hash  = delete $::form->{invoice_skontos} || {}; # array containing the payment type, could be empty
357
358   while ( my ($bt_id, $invoice_ids) = each(%$invoice_hash) ) {
359     my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
360     my $sign = $bank_transaction->amount < 0 ? -1 : 1;
361     my $amount_of_transaction = $sign * $bank_transaction->amount;
362
363     my @invoices;
364     foreach my $invoice_id (@{ $invoice_ids }) {
365       push @invoices, (SL::DB::Manager::Invoice->find_by(id => $invoice_id) || SL::DB::Manager::PurchaseInvoice->find_by(id => $invoice_id));
366     }
367     @invoices = sort { return 1 if ($a->is_sales and $a->amount > 0);
368                           return 1 if (!$a->is_sales and $a->amount < 0);
369                           return -1; } @invoices                if $bank_transaction->amount > 0;
370     @invoices = sort { return -1 if ($a->is_sales and $a->amount > 0);
371                        return -1 if (!$a->is_sales and $a->amount < 0);
372                        return 1; } @invoices                    if $bank_transaction->amount < 0;
373
374     foreach my $invoice (@invoices) {
375       my $payment_type;
376       if ( @$skonto_hash{"$bt_id"} ) {
377         $payment_type = shift( @$skonto_hash{"$bt_id"} );
378       } else {
379         $payment_type = 'without_skonto';
380       };
381       if ($amount_of_transaction == 0) {
382         flash('warning',  $::locale->text('There are invoices which could not be paid by bank transaction #1 (Account number: #2, bank code: #3)!',
383                                             $bank_transaction->purpose,
384                                             $bank_transaction->remote_account_number,
385                                             $bank_transaction->remote_bank_code));
386         last;
387       }
388       #pay invoice or go to the next bank transaction if the amount is not sufficiently high
389       if ($invoice->amount <= $amount_of_transaction) {
390         $invoice->pay_invoice(chart_id     => $bank_transaction->local_bank_account->chart_id,
391                               trans_id     => $invoice->id,
392                               amount       => $invoice->amount,
393                               payment_type => $payment_type,
394                               transdate    => $bank_transaction->transdate->to_kivitendo);
395         if ($invoice->is_sales) {
396           $amount_of_transaction -= $sign * $invoice->amount;
397           $bank_transaction->invoice_amount($bank_transaction->invoice_amount + $invoice->amount);
398         } else {
399           $amount_of_transaction += $sign * $invoice->amount if (!$invoice->is_sales);
400           $bank_transaction->invoice_amount($bank_transaction->invoice_amount - $invoice->amount);
401         }
402       } else {
403         $invoice->pay_invoice(chart_id     => $bank_transaction->local_bank_account->chart_id,
404                               trans_id     => $invoice->id,
405                               amount       => $amount_of_transaction,
406                               payment_type => $payment_type,
407                               transdate    => $bank_transaction->transdate->to_kivitendo);
408         $bank_transaction->invoice_amount($bank_transaction->amount) if $invoice->is_sales;
409         $bank_transaction->invoice_amount($bank_transaction->amount) if !$invoice->is_sales;
410         $amount_of_transaction = 0;
411       }
412
413       #Record a link from the bank transaction to the invoice
414       my @props = (
415           from_table => 'bank_transactions',
416           from_id    => $bt_id,
417           to_table   => $invoice->is_sales ? 'ar' : 'ap',
418           to_id      => $invoice->id,
419           );
420
421       my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
422
423       SL::DB::RecordLink->new(@props)->save if !$existing;
424     }
425     $bank_transaction->save;
426   }
427
428   $self->action_list();
429 }
430
431 sub action_save_proposals {
432   my ($self) = @_;
433
434   foreach my $bt_id (@{ $::form->{proposal_ids} }) {
435     #mark bt as booked
436     my $bt = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
437     $bt->invoice_amount($bt->amount);
438     $bt->save;
439
440     #pay invoice
441     my $arap = SL::DB::Manager::Invoice->find_by(id => $::form->{"proposed_invoice_$bt_id"});
442     $arap    = SL::DB::Manager::PurchaseInvoice->find_by(id => $::form->{"proposed_invoice_$bt_id"}) if not defined $arap;
443     $arap->pay_invoice(chart_id  => $bt->local_bank_account->chart_id,
444                        trans_id  => $arap->id,
445                        amount    => $arap->amount,
446                        transdate => $bt->transdate->to_kivitendo);
447     $arap->save;
448
449     #create record link
450     my @props = (
451         from_table => 'bank_transactions',
452         from_id    => $bt_id,
453         to_table   => $arap->is_sales ? 'ar' : 'ap',
454         to_id      => $arap->id,
455         );
456
457     my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
458
459     SL::DB::RecordLink->new(@props)->save if !$existing;
460   }
461
462   flash('ok', t8('#1 proposal(s) saved.', scalar @{ $::form->{proposal_ids} }));
463
464   $self->action_list();
465 }
466
467 #
468 # filters
469 #
470
471 sub check_auth {
472   $::auth->assert('bank_transaction');
473 }
474
475 #
476 # helpers
477 #
478
479 sub make_filter_summary {
480   my ($self) = @_;
481
482   my $filter = $::form->{filter} || {};
483   my @filter_strings;
484
485   my @filters = (
486     [ $filter->{"transdate:date::ge"},  $::locale->text('Transdate')  . " " . $::locale->text('From Date') ],
487     [ $filter->{"transdate:date::le"},  $::locale->text('Transdate')  . " " . $::locale->text('To Date')   ],
488     [ $filter->{"valutadate:date::ge"}, $::locale->text('Valutadate') . " " . $::locale->text('From Date') ],
489     [ $filter->{"valutadate:date::le"}, $::locale->text('Valutadate') . " " . $::locale->text('To Date')   ],
490     [ $filter->{"amount:number"},       $::locale->text('Amount')                                          ],
491     [ $filter->{"bank_account_id:integer"}, $::locale->text('Local bank account')                          ],
492   );
493
494   for (@filters) {
495     push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
496   }
497
498   $self->{filter_summary} = join ', ', @filter_strings;
499 }
500
501 sub prepare_report {
502   my ($self)      = @_;
503
504   my $callback    = $self->models->get_callback;
505
506   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
507   $self->{report} = $report;
508
509   my @columns     = qw(local_bank_name transdate valudate remote_name remote_account_number remote_bank_code amount invoice_amount invoices currency purpose local_account_number local_bank_code id);
510   my @sortable    = qw(local_bank_name transdate valudate remote_name remote_account_number remote_bank_code amount                                  purpose local_account_number local_bank_code);
511
512   my %column_defs = (
513     transdate             => { sub => sub { $_[0]->transdate_as_date } },
514     valutadate            => { sub => sub { $_[0]->valutadate_as_date } },
515     remote_name           => { },
516     remote_account_number => { },
517     remote_bank_code      => { },
518     amount                => { sub => sub { $_[0]->amount_as_number },
519                                align => 'right' },
520     invoice_amount        => { sub => sub { $_[0]->invoice_amount_as_number },
521                                align => 'right' },
522     invoices              => { sub => sub { $_[0]->linked_invoices } },
523     currency              => { sub => sub { $_[0]->currency->name } },
524     purpose               => { },
525     local_account_number  => { sub => sub { $_[0]->local_bank_account->account_number } },
526     local_bank_code       => { sub => sub { $_[0]->local_bank_account->bank_code } },
527     local_bank_name       => { sub => sub { $_[0]->local_bank_account->name } },
528     id                    => {},
529   );
530
531   map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
532
533   $report->set_options(
534     std_column_visibility => 1,
535     controller_class      => 'BankTransaction',
536     output_format         => 'HTML',
537     top_info_text         => $::locale->text('Bank transactions'),
538     title                 => $::locale->text('Bank transactions'),
539     allow_pdf_export      => 1,
540     allow_csv_export      => 1,
541   );
542   $report->set_columns(%column_defs);
543   $report->set_column_order(@columns);
544   $report->set_export_options(qw(list_all filter));
545   $report->set_options_from_form;
546   $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
547   $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
548
549   my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted();
550
551   $report->set_options(
552     raw_top_info_text     => $self->render('bank_transactions/report_top',    { output => 0 }, BANK_ACCOUNTS => $bank_accounts),
553     raw_bottom_info_text  => $self->render('bank_transactions/report_bottom', { output => 0 }),
554   );
555 }
556
557 sub init_models {
558   my ($self) = @_;
559
560   SL::Controller::Helper::GetModels->new(
561     controller => $self,
562     sorted => {
563       _default => {
564         by    => 'transdate',
565         dir   => 0,   # 1 = ASC, 0 = DESC : default sort is newest at top
566       },
567       transdate             => t8('Transdate'),
568       remote_name           => t8('Remote name'),
569       amount                => t8('Amount'),
570       invoice_amount        => t8('Assigned'),
571       invoices              => t8('Linked invoices'),
572       valutadate            => t8('Valutadate'),
573       remote_account_number => t8('Remote account number'),
574       remote_bank_code      => t8('Remote bank code'),
575       currency              => t8('Currency'),
576       purpose               => t8('Purpose'),
577       local_account_number  => t8('Local account number'),
578       local_bank_code       => t8('Local bank code'),
579       local_bank_name       => t8('Bank account'),
580     },
581     with_objects => [ 'local_bank_account', 'currency' ],
582   );
583 }
584
585 1;