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