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