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