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