$::cgi entfernt.
[kivitendo-erp.git] / bin / mozilla / sepa.pl
1 use strict;
2
3 use List::MoreUtils qw(any none uniq);
4 use List::Util qw(first);
5 use POSIX qw(strftime);
6
7 use SL::BankAccount;
8 use SL::Chart;
9 use SL::CT;
10 use SL::Form;
11 use SL::GenericTranslations;
12 use SL::ReportGenerator;
13 use SL::SEPA;
14 use SL::SEPA::XML;
15
16 require "bin/mozilla/common.pl";
17 require "bin/mozilla/reportgenerator.pl";
18
19 sub bank_transfer_add {
20   $main::lxdebug->enter_sub();
21
22   my $form          = $main::form;
23   my $locale        = $main::locale;
24   my $vc            = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
25
26   $form->{title}    = $vc eq 'customer' ? $::locale->text('Prepare bank collection via SEPA XML') : $locale->text('Prepare bank transfer via SEPA XML');
27
28   my $bank_accounts = SL::BankAccount->list();
29
30   if (!scalar @{ $bank_accounts }) {
31     $form->error($locale->text('You have not added bank accounts yet.'));
32   }
33
34   my $invoices = SL::SEPA->retrieve_open_invoices(vc => $vc);
35
36   if (!scalar @{ $invoices }) {
37     $form->show_generic_information($locale->text('Either there are no open invoices, or you have already initiated bank transfers ' .
38                                                   'with the open amounts for those that are still open.'));
39     $main::lxdebug->leave_sub();
40     return;
41   }
42
43   my $bank_account_label_sub = sub { $locale->text('Account number #1, bank code #2, #3', $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank}) };
44
45   my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
46   my %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
47
48   foreach my $invoice (@{ $invoices }) {
49     my $prefix                    = $translations{ $invoice->{language_id} } || $translations{default} || $::locale->text('Invoice');
50     $prefix                      .= ' ' unless $prefix =~ m/ $/;
51     $invoice->{reference_prefix}  = $prefix;
52   }
53
54   $form->header();
55   print $form->parse_html_template('sepa/bank_transfer_add',
56                                    { 'INVOICES'           => $invoices,
57                                      'BANK_ACCOUNTS'      => $bank_accounts,
58                                      'bank_account_label' => $bank_account_label_sub,
59                                      'vc'                 => $vc,
60                                    });
61
62   $main::lxdebug->leave_sub();
63 }
64
65 sub bank_transfer_create {
66   $main::lxdebug->enter_sub();
67
68   my $form          = $main::form;
69   my $locale        = $main::locale;
70   my $myconfig      = \%main::myconfig;
71   my $vc            = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
72
73   $form->{title}    = $vc eq 'customer' ? $::locale->text('Create bank collection via SEPA XML') : $locale->text('Create bank transfer via SEPA XML');
74
75   my $bank_accounts = SL::BankAccount->list();
76
77   if (!scalar @{ $bank_accounts }) {
78     $form->error($locale->text('You have not added bank accounts yet.'));
79   }
80
81   my $bank_account = first { $form->{bank_account}->{id} == $_->{id} } @{ $bank_accounts };
82
83   if (!$bank_account) {
84     $form->error($locale->text('The selected bank account does not exist anymore.'));
85   }
86
87   my $arap_id        = $vc eq 'customer' ? 'ar_id' : 'ap_id';
88   my $invoices       = SL::SEPA->retrieve_open_invoices(vc => $vc);
89
90   my %invoices_map   = map { $_->{id} => $_ } @{ $invoices };
91   my @bank_transfers =
92     map  +{ %{ $invoices_map{ $_->{$arap_id} } }, %{ $_ } },
93     grep  { $_->{selected} && (0 < $_->{amount}) && $invoices_map{ $_->{$arap_id} } }
94     map   { $_->{amount} = $form->parse_amount($myconfig, $_->{amount}); $_ }
95           @{ $form->{bank_transfers} || [] };
96
97   if (!scalar @bank_transfers) {
98     $form->error($locale->text('You have selected none of the invoices.'));
99   }
100
101   my ($vc_bank_info);
102   my $error_message;
103
104   if ($form->{confirmation}) {
105     $vc_bank_info = { map { $_->{id} => $_ } @{ $form->{vc_bank_info} || [] } };
106
107     foreach my $info (values %{ $vc_bank_info }) {
108       if (any { !$info->{$_} } qw(iban bic)) {
109         $error_message = $locale->text('The bank information must not be empty.');
110         last;
111       }
112     }
113   }
114
115   if ($error_message || !$form->{confirmation}) {
116     my @vc_ids                 = uniq map { $_->{vc_id} } @bank_transfers;
117     $vc_bank_info            ||= CT->get_bank_info('vc' => $vc,
118                                                    'id' => \@vc_ids);
119     my @vc_bank_info           = sort { lc $a->{name} cmp lc $b->{name} } values %{ $vc_bank_info };
120
121     my $bank_account_label_sub = sub { $locale->text('Account number #1, bank code #2, #3', $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank}) };
122
123     $form->{jsscript}          = 1;
124
125     $form->header();
126     print $form->parse_html_template('sepa/bank_transfer_create',
127                                      { 'BANK_TRANSFERS'     => \@bank_transfers,
128                                        'BANK_ACCOUNTS'      => $bank_accounts,
129                                        'VC_BANK_INFO'       => \@vc_bank_info,
130                                        'bank_account'       => $bank_account,
131                                        'bank_account_label' => $bank_account_label_sub,
132                                        'error_message'      => $error_message,
133                                        'vc'                 => $vc,
134                                      });
135
136   } else {
137     foreach my $bank_transfer (@bank_transfers) {
138       foreach (qw(iban bic)) {
139         $bank_transfer->{"vc_${_}"}  = $vc_bank_info->{ $bank_transfer->{vc_id} }->{$_};
140         $bank_transfer->{"our_${_}"} = $bank_account->{$_};
141       }
142
143       $bank_transfer->{chart_id} = $bank_account->{chart_id};
144     }
145
146     my $id = SL::SEPA->create_export('employee'       => $form->{login},
147                                      'bank_transfers' => \@bank_transfers,
148                                      'vc'             => $vc);
149
150     $form->header();
151     print $form->parse_html_template('sepa/bank_transfer_created', { 'id' => $id, 'vc' => $vc });
152   }
153
154   $main::lxdebug->leave_sub();
155 }
156
157 sub bank_transfer_search {
158   $main::lxdebug->enter_sub();
159
160   my $form   = $main::form;
161   my $locale = $main::locale;
162   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
163
164   $form->{title}    = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
165   $form->{jsscript} = 1;
166
167   $form->header();
168   print $form->parse_html_template('sepa/bank_transfer_search', { vc => $vc });
169
170   $main::lxdebug->leave_sub();
171 }
172
173
174 sub bank_transfer_list {
175   $main::lxdebug->enter_sub();
176
177   my $form   = $main::form;
178   my $locale = $main::locale;
179   my $cgi    = $::request->{cgi};
180   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
181
182   $form->{title}     = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
183
184   $form->{sort}    ||= 'id';
185   $form->{sortdir}   = '1' if (!defined $form->{sortdir});
186
187   $form->{callback}  = build_std_url('action=bank_transfer_list', 'sort', 'sortdir', 'vc');
188
189   my %filter         = map  +( $_ => $form->{"f_${_}"} ),
190                        grep  { $form->{"f_${_}"} }
191                              (qw(vc invnumber),
192                               map { ("${_}_date_from", "${_}_date_to") }
193                                   qw(export requested_execution execution));
194   $filter{executed}  = $form->{l_executed} ? 1 : 0 if ($form->{l_executed} != $form->{l_not_executed});
195   $filter{closed}    = $form->{l_closed}   ? 1 : 0 if ($form->{l_open}     != $form->{l_closed});
196
197   my $exports        = SL::SEPA->list_exports('filter'    => \%filter,
198                                               'sortorder' => $form->{sort},
199                                               'sortdir'   => $form->{sortdir},
200                                               'vc'        => $vc);
201
202   my $open_available = any { !$_->{closed} } @{ $exports };
203
204   my $report         = SL::ReportGenerator->new(\%main::myconfig, $form);
205
206   my @hidden_vars    = ('vc', grep { m/^[fl]_/ && $form->{$_} } keys %{ $form });
207
208   my $href           = build_std_url('action=bank_transfer_list', @hidden_vars);
209
210   my %column_defs = (
211     'selected'    => { 'text' => $cgi->checkbox(-name => 'select_all', -id => 'select_all', -label => ''), },
212     'id'          => { 'text' => $locale->text('Number'), },
213     'export_date' => { 'text' => $locale->text('Export date'), },
214     'employee'    => { 'text' => $locale->text('Employee'), },
215     'executed'    => { 'text' => $locale->text('Executed'), },
216     'closed'      => { 'text' => $locale->text('Closed'), },
217   );
218
219   my @columns = qw(selected id export_date employee executed closed);
220
221   foreach my $name (qw(id export_date employee executed closed)) {
222     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
223     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
224   }
225
226   $column_defs{selected}->{visible} = $open_available                                ? 'HTML' : 0;
227   $column_defs{executed}->{visible} = $form->{l_executed} && $form->{l_not_executed} ? 1 : 0;
228   $column_defs{closed}->{visible}   = $form->{l_closed}   && $form->{l_open}         ? 1 : 0;
229
230   my @options = ();
231   push @options, ($vc eq 'customer' ? $::locale->text('Customer') : $locale->text('Vendor')) . ' : ' . $form->{f_vc} if ($form->{f_vc});
232   push @options, $locale->text('Invoice number')                . ' : ' . $form->{f_invnumber}                     if ($form->{f_invnumber});
233   push @options, $locale->text('Export date from')              . ' : ' . $form->{f_export_date_from}              if ($form->{f_export_date_from});
234   push @options, $locale->text('Export date to')                . ' : ' . $form->{f_export_date_to}                if ($form->{f_export_date_to});
235   push @options, $locale->text('Requested execution date from') . ' : ' . $form->{f_requested_execution_date_from} if ($form->{f_requested_execution_date_from});
236   push @options, $locale->text('Requested execution date to')   . ' : ' . $form->{f_requested_execution_date_to}   if ($form->{f_requested_execution_date_to});
237   push @options, $locale->text('Execution date from')           . ' : ' . $form->{f_execution_date_from}           if ($form->{f_execution_date_from});
238   push @options, $locale->text('Execution date to')             . ' : ' . $form->{f_execution_date_to}             if ($form->{f_execution_date_to});
239   push @options, $form->{l_executed} ? $locale->text('executed') : $locale->text('not yet executed')               if ($form->{l_executed} != $form->{l_not_executed});
240   push @options, $form->{l_closed}   ? $locale->text('closed')   : $locale->text('open')                           if ($form->{l_open}     != $form->{l_closed});
241
242   $report->set_options('top_info_text'         => join("\n", @options),
243                        'raw_top_info_text'     => $form->parse_html_template('sepa/bank_transfer_list_top'),
244                        'raw_bottom_info_text'  => $form->parse_html_template('sepa/bank_transfer_list_bottom', { 'show_buttons' => $open_available, vc => $vc }),
245                        'std_column_visibility' => 1,
246                        'output_format'         => 'HTML',
247                        'title'                 => $form->{title},
248                        'attachment_basename'   => $locale->text('banktransfers') . strftime('_%Y%m%d', localtime time),
249     );
250   $report->set_options_from_form();
251   $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
252
253   $report->set_columns(%column_defs);
254   $report->set_column_order(@columns);
255   $report->set_export_options('bank_transfer_list', @hidden_vars);
256   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
257
258   my $edit_url = build_std_url('action=bank_transfer_edit', 'callback');
259
260   foreach my $export (@{ $exports }) {
261     my $row = { map { $_ => { 'data' => $export->{$_} } } keys %{ $export } };
262
263     map { $row->{$_}->{data} = $export->{$_} ? $locale->text('yes') : $locale->text('no') } qw(executed closed);
264
265     $row->{id}->{link} = $edit_url . '&id=' . E($export->{id}) . '&vc=' . E($vc);
266
267     if (!$export->{closed}) {
268       $row->{selected}->{raw_data} =
269           $cgi->hidden(-name => "exports[+].id", -value => $export->{id})
270         . $cgi->checkbox(-name => "exports[].selected", -value => 1, -label => '');
271     }
272
273     $report->add_data($row);
274   }
275
276   $report->generate_with_headers();
277
278   $main::lxdebug->leave_sub();
279 }
280
281 sub bank_transfer_edit {
282   $main::lxdebug->enter_sub();
283
284   my $form   = $main::form;
285   my $locale = $main::locale;
286   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
287
288   my @ids    = ();
289   if (!$form->{mode} || ($form->{mode} eq 'single')) {
290     push @ids, $form->{id};
291   } else {
292     @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
293
294     if (!@ids) {
295       $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
296     }
297   }
298
299   my $export;
300
301   foreach my $id (@ids) {
302     my $curr_export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, 'vc' => $vc);
303
304     foreach my $item (@{ $curr_export->{items} }) {
305       map { $item->{"export_${_}"} = $curr_export->{$_} } grep { !ref $curr_export->{$_} } keys %{ $curr_export };
306     }
307
308     if (!$export) {
309       $export = $curr_export;
310     } else {
311       push @{ $export->{items} }, @{ $curr_export->{items} };
312     }
313   }
314
315   if ($form->{mode} && ($form->{mode} eq 'multi')) {
316     $export->{items} = [ grep { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} } ];
317
318     if (!@{ $export->{items} }) {
319       $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'), 'back_button' => 1);
320     }
321
322   } elsif (!$export) {
323     $form->error($locale->text('That export does not exist.'));
324   }
325
326   $form->{jsscript} = 1;
327   $form->{title}    = $locale->text('View SEPA export');
328   $form->header();
329   print $form->parse_html_template('sepa/bank_transfer_edit',
330                                    { 'ids'                       => \@ids,
331                                      'export'                    => $export,
332                                      'current_date'              => $form->current_date(\%main::myconfig),
333                                      'show_post_payments_button' => any { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} },
334                                    });
335
336   $main::lxdebug->leave_sub();
337 }
338
339 sub bank_transfer_post_payments {
340   $main::lxdebug->enter_sub();
341
342   my $form   = $main::form;
343   my $locale = $main::locale;
344   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
345
346   my @items  = grep { $_->{selected} } @{ $form->{items} || [] };
347
348   if (!@items) {
349     $form->show_generic_error($locale->text('You have not selected any item.'), 'back_button' => 1);
350   }
351   my @export_ids    = uniq map { $_->{sepa_export_id} } @items;
352   my %exports       = map { $_ => SL::SEPA->retrieve_export('id' => $_, 'details' => 1, vc => $vc) } @export_ids;
353   my @items_to_post = ();
354
355   foreach my $item (@items) {
356     my $export = $exports{ $item->{sepa_export_id} };
357     next if (!$export || $export->{closed} || $export->{executed});
358
359     push @items_to_post, $item if (none { ($_->{id} == $item->{id}) && $_->{executed} } @{ $export->{items} });
360   }
361
362   if (!@items_to_post) {
363     $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'), 'back_button' => 1);
364   }
365
366   if (any { !$_->{execution_date} } @items_to_post) {
367     $form->show_generic_error($locale->text('You have to specify an execution date for each antry.'), 'back_button' => 1);
368   }
369
370   SL::SEPA->post_payment('items' => \@items_to_post, vc => $vc);
371
372   $form->show_generic_information($locale->text('The payments have been posted.'));
373
374   $main::lxdebug->leave_sub();
375 }
376
377 sub bank_transfer_payment_list_as_pdf {
378   $main::lxdebug->enter_sub();
379
380   my $form       = $main::form;
381   my %myconfig   = %main::myconfig;
382   my $locale     = $main::locale;
383   my $vc         = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
384
385   my @ids        = @{ $form->{items} || [] };
386   my @export_ids = uniq map { $_->{export_id} } @ids;
387
388   $form->show_generic_error($locale->text('Multi mode not supported.'), 'back_button' => 1) if 1 != scalar @export_ids;
389
390   my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1, vc => $vc);
391   my @items  = ();
392
393   foreach my $id (@ids) {
394     my $item = first { $_->{id} == $id->{id} } @{ $export->{items} };
395     push @items, $item if $item;
396   }
397
398   $form->show_generic_error($locale->text('No transfers were executed in this export.'), 'back_button' => 1) if 1 > scalar @items;
399
400   my $report         =  SL::ReportGenerator->new(\%main::myconfig, $form);
401
402   my %column_defs    =  (
403     'invnumber'      => { 'text' => $locale->text('Invoice'),                                                                  },
404     'vc_name'        => { 'text' => $vc eq 'customer' ? $locale->text('Customer')         : $locale->text('Vendor'),           },
405     'our_iban'       => { 'text' => $vc eq 'customer' ? $locale->text('Destination IBAN') : $locale->text('Source IBAN'),      },
406     'our_bic'        => { 'text' => $vc eq 'customer' ? $locale->text('Destination BIC')  : $locale->text('Source BIC'),       },
407     'vc_iban'        => { 'text' => $vc eq 'customer' ? $locale->text('Source IBAN')      : $locale->text('Destination IBAN'), },
408     'vc_bic'         => { 'text' => $vc eq 'customer' ? $locale->text('Source BIC')       : $locale->text('Destination BIC'),  },
409     'amount'         => { 'text' => $locale->text('Amount'),                                                                   },
410     'reference'      => { 'text' => $locale->text('Reference'),                                                                },
411     'execution_date' => { 'text' => $locale->text('Execution date'),                                                           },
412   );
413
414   map { $column_defs{$_}->{align} = 'right' } qw(amount execution_date);
415
416   my @columns        =  qw(invnumber vc_name our_iban our_bic vc_iban vc_bic amount reference execution_date);
417
418   $report->set_options('std_column_visibility' => 1,
419                        'output_format'         => 'PDF',
420                        'title'                 =>  $vc eq 'customer' ? $locale->text('Bank collection payment list for export #1', $export->{id}) : $locale->text('Bank transfer payment list for export #1', $export->{id}),
421                        'attachment_basename'   => ($vc eq 'customer' ? $locale->text('bank_collection_payment_list_#1', $export->{id}) : $locale->text('bank_transfer_payment_list_#1', $export->{id})) . strftime('_%Y%m%d', localtime time),
422     );
423
424   $report->set_columns(%column_defs);
425   $report->set_column_order(@columns);
426
427   foreach my $item (@items) {
428     my $row                = { map { $_ => { 'data' => $item->{$_} } } @columns };
429     $row->{amount}->{data} = $form->format_amount(\%myconfig, $item->{amount}, 2);
430
431     $report->add_data($row);
432   }
433
434   $report->generate_with_headers();
435
436   $main::lxdebug->leave_sub();
437 }
438
439 # TODO
440 sub bank_transfer_download_sepa_xml {
441   $main::lxdebug->enter_sub();
442
443   my $form     =  $main::form;
444   my $myconfig = \%main::myconfig;
445   my $locale   =  $main::locale;
446   my $cgi      =  $::request->{cgi};
447   my $vc       = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
448
449   if (!$myconfig->{company}) {
450     $form->show_generic_error($locale->text('You have to enter a company name in your user preferences (see the "Program" menu, "Preferences").'), 'back_button' => 1);
451   }
452
453   if (($vc eq 'customer') && !$myconfig->{sepa_creditor_id}) {
454     $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID in your user preferences (see the "Program" menu, "Preferences").'), 'back_button' => 1);
455   }
456
457   my @ids;
458   if ($form->{mode} && ($form->{mode} eq 'multi')) {
459      @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
460
461   } else {
462     @ids = ($form->{id});
463   }
464
465   if (!@ids) {
466     $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
467   }
468
469   my @items = ();
470
471   foreach my $id (@ids) {
472     my $export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, vc => $vc);
473     push @items, grep { !$_->{executed} } @{ $export->{items} } if ($export && !$export->{closed});
474   }
475
476   if (!@items) {
477     $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'), 'back_button' => 1);
478   }
479
480   my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$);
481
482   my $sepa_xml   = SL::SEPA::XML->new('company'     => $myconfig->{company},
483                                       'creditor_id' => $myconfig->{sepa_creditor_id},
484                                       'src_charset' => $::lx_office_conf{system}->{dbcharset} || 'ISO-8859-15',
485                                       'message_id'  => $message_id,
486                                       'grouped'     => 1,
487                                       'collection'  => $vc eq 'customer',
488     );
489
490   foreach my $item (@items) {
491     my $requested_execution_date;
492     if ($item->{requested_execution_date}) {
493       my ($yy, $mm, $dd)        = $locale->parse_date($myconfig, $item->{requested_execution_date});
494       $requested_execution_date = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
495     }
496
497     if ($vc eq 'customer') {
498       my ($yy, $mm, $dd)      = $locale->parse_date($myconfig, $item->{reference_date});
499       $item->{reference_date} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
500     }
501
502     $sepa_xml->add_transaction({ 'src_iban'       => $item->{our_iban},
503                                  'src_bic'        => $item->{our_bic},
504                                  'dst_iban'       => $item->{vc_iban},
505                                  'dst_bic'        => $item->{vc_bic},
506                                  'company'        => $item->{vc_name},
507                                  'amount'         => $item->{amount},
508                                  'reference'      => $item->{reference},
509                                  'reference_date' => $item->{reference_date},
510                                  'execution_date' => $requested_execution_date,
511                                  'end_to_end_id'  => $item->{end_to_end_id} });
512   }
513
514   my $xml = $sepa_xml->to_xml();
515
516   print $cgi->header('-type'                => 'application/octet-stream',
517                      '-content-disposition' => 'attachment; filename="SEPA_' . $message_id . ($vc eq 'customer' ? '.cdd' : '.cct') . '"',
518                      '-content-length'      => length $xml);
519   print $xml;
520
521   $main::lxdebug->leave_sub();
522 }
523
524 sub bank_transfer_mark_as_closed_step1 {
525   $main::lxdebug->enter_sub();
526
527   my $form       = $main::form;
528   my $locale     = $main::locale;
529   my $vc         = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
530
531   my @export_ids = map { $_->{id} } grep { $_->{selected} } @{ $form->{exports} || [] };
532
533   if (!@export_ids) {
534     $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
535   }
536
537   my @open_export_ids = ();
538   foreach my $id (@export_ids) {
539     my $export = SL::SEPA->retrieve_export('id' => $id, vc => $vc);
540     push @open_export_ids, $id if (!$export->{closed});
541   }
542
543   if (!@open_export_ids) {
544     $form->show_generic_error($locale->text('All of the exports you have selected were already closed.'), 'back_button' => 1);
545   }
546
547   $form->{title} = $locale->text('Close SEPA exports');
548   $form->header();
549   print $form->parse_html_template('sepa/bank_transfer_mark_as_closed_step1', { 'OPEN_EXPORT_IDS' => \@open_export_ids, vc => $vc });
550
551   $main::lxdebug->leave_sub();
552 }
553
554 sub bank_transfer_mark_as_closed_step2 {
555   $main::lxdebug->enter_sub();
556
557   my $form       = $main::form;
558   my $locale     = $main::locale;
559
560   map { SL::SEPA->close_export('id' => $_); } @{ $form->{open_export_ids} || [] };
561
562   $form->{title} = $locale->text('Close SEPA exports');
563   $form->header();
564   $form->show_generic_information($locale->text('The selected exports have been closed.'));
565
566   $main::lxdebug->leave_sub();
567 }
568
569 sub dispatcher {
570   my $form = $main::form;
571
572   foreach my $action (qw(bank_transfer_create bank_transfer_edit bank_transfer_list
573                          bank_transfer_post_payments bank_transfer_download_sepa_xml
574                          bank_transfer_mark_as_closed_step1 bank_transfer_mark_as_closed_step2
575                          bank_transfer_payment_list_as_pdf)) {
576     if ($form->{"action_${action}"}) {
577       call_sub($action);
578       return;
579     }
580   }
581
582   $form->error($main::locale->text('No action defined.'));
583 }
584
585 1;