3 use List::MoreUtils qw(any none uniq);
 
   4 use List::Util qw(sum first);
 
   5 use POSIX qw(strftime);
 
   8 use SL::DB::BankAccount;
 
  12 use SL::GenericTranslations;
 
  13 use SL::ReportGenerator;
 
  17 require "bin/mozilla/common.pl";
 
  18 require "bin/mozilla/reportgenerator.pl";
 
  20 sub bank_transfer_add {
 
  21   $main::lxdebug->enter_sub();
 
  23   my $form          = $main::form;
 
  24   my $locale        = $main::locale;
 
  25   my $vc            = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
  27   $form->{title}    = $vc eq 'customer' ? $::locale->text('Prepare bank collection via SEPA XML') : $locale->text('Prepare bank transfer via SEPA XML');
 
  29   my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
 
  31   if (!scalar @{ $bank_accounts }) {
 
  32     $form->error($locale->text('You have not added bank accounts yet.'));
 
  35   my $invoices = SL::SEPA->retrieve_open_invoices(vc => $vc);
 
  37   if (!scalar @{ $invoices }) {
 
  38     $form->show_generic_information($locale->text('Either there are no open invoices, or you have already initiated bank transfers ' .
 
  39                                                   'with the open amounts for those that are still open.'));
 
  40     $main::lxdebug->leave_sub();
 
  44   # Only include those per default that require manual action from our
 
  45   # side. For sales invoices these are the ones for which direct debit
 
  46   # has been selected. For purchase invoices it's the other way
 
  47   # around: if direct debit is active then the vendor will collect
 
  48   # from us automatically and we don't have to send money manually.
 
  49   $_->{checked} = ($vc eq 'customer' ? $_->{direct_debit} : !$_->{direct_debit}) for @{ $invoices };
 
  51   my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
 
  52   my %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
 
  54   foreach my $invoice (@{ $invoices }) {
 
  55     my $prefix                    = $translations{ $invoice->{language_id} } || $translations{default} || $::locale->text('Invoice');
 
  56     $prefix                      .= ' ' unless $prefix =~ m/ $/;
 
  57     $invoice->{reference_prefix}  = $prefix;
 
  61   print $form->parse_html_template('sepa/bank_transfer_add',
 
  62                                    { 'INVOICES'           => $invoices,
 
  63                                      'BANK_ACCOUNTS'      => $bank_accounts,
 
  67   $main::lxdebug->leave_sub();
 
  70 sub bank_transfer_create {
 
  71   $main::lxdebug->enter_sub();
 
  73   my $form          = $main::form;
 
  74   my $locale        = $main::locale;
 
  75   my $myconfig      = \%main::myconfig;
 
  76   my $vc            = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
  78   $form->{title}    = $vc eq 'customer' ? $::locale->text('Create bank collection via SEPA XML') : $locale->text('Create bank transfer via SEPA XML');
 
  80   my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
 
  81   if (!scalar @{ $bank_accounts }) {
 
  82     $form->error($locale->text('You have not added bank accounts yet.'));
 
  85   my $bank_account = SL::DB::Manager::BankAccount->find_by( id => $form->{bank_account} );
 
  87   unless ( $bank_account ) {
 
  88     $form->error($locale->text('The selected bank account does not exist anymore.'));
 
  91   my $arap_id        = $vc eq 'customer' ? 'ar_id' : 'ap_id';
 
  92   my $invoices       = SL::SEPA->retrieve_open_invoices(vc => $vc);
 
  94   # load all open invoices (again), but grep out the ones that were selected with checkboxes beforehand ($_->selected). At this stage we again have all the invoice information, including dropdown with payment_type options
 
  95   # all the information from retrieve_open_invoices is then ADDED to what was passed via @{ $form->{bank_transfers} }
 
  96   # parse amount from the entry in the form, but take skonto_amount from PT again
 
  97   # the map inserts the values of invoice_map directly into the array of hashes
 
  98   my %invoices_map   = map { $_->{id} => $_ } @{ $invoices };
 
 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} || [] };
 
 105   # override default payment_type selection and set it to the one chosen by the user
 
 106   # in the previous step, so that we don't need the logic in the template
 
 107   foreach my $bt (@bank_transfers) {
 
 108     foreach my $type ( @{$bt->{payment_select_options}} ) {
 
 109       if ( $type->{payment_type} eq $bt->{payment_type} ) {
 
 110         $type->{selected} = 1;
 
 112         $type->{selected} = 0;
 
 117   if (!scalar @bank_transfers) {
 
 118     $form->error($locale->text('You have selected none of the invoices.'));
 
 121   my $total_trans = sum map { $_->{open_amount} } @bank_transfers;
 
 126   my @bank_columns    = qw(iban bic);
 
 127   push @bank_columns, qw(mandator_id mandate_date_of_signature) if $vc eq 'customer';
 
 129   if ($form->{confirmation}) {
 
 130     $vc_bank_info = { map { $_->{id} => $_ } @{ $form->{vc_bank_info} || [] } };
 
 132     foreach my $info (values %{ $vc_bank_info }) {
 
 133       if (any { !$info->{$_} } @bank_columns) {
 
 134         $error_message = $locale->text('The bank information must not be empty.');
 
 140   if ($error_message || !$form->{confirmation}) {
 
 141     my @vc_ids                 = uniq map { $_->{vc_id} } @bank_transfers;
 
 142     $vc_bank_info            ||= CT->get_bank_info('vc' => $vc,
 
 144     my @vc_bank_info           = sort { lc $a->{name} cmp lc $b->{name} } values %{ $vc_bank_info };
 
 147     print $form->parse_html_template('sepa/bank_transfer_create',
 
 148                                      { 'BANK_TRANSFERS'     => \@bank_transfers,
 
 149                                        'BANK_ACCOUNTS'      => $bank_accounts,
 
 150                                        'VC_BANK_INFO'       => \@vc_bank_info,
 
 151                                        'bank_account'       => $bank_account,
 
 152                                        'error_message'      => $error_message,
 
 154                                        'total_trans'        => $total_trans,
 
 158     foreach my $bank_transfer (@bank_transfers) {
 
 159       foreach (@bank_columns) {
 
 160         $bank_transfer->{"vc_${_}"}  = $vc_bank_info->{ $bank_transfer->{vc_id} }->{$_};
 
 161         $bank_transfer->{"our_${_}"} = $bank_account->{$_};
 
 164       $bank_transfer->{chart_id} = $bank_account->{chart_id};
 
 167     my $id = SL::SEPA->create_export('employee'       => $::myconfig{login},
 
 168                                      'bank_transfers' => \@bank_transfers,
 
 172     print $form->parse_html_template('sepa/bank_transfer_created', { 'id' => $id, 'vc' => $vc });
 
 175   $main::lxdebug->leave_sub();
 
 178 sub bank_transfer_search {
 
 179   $main::lxdebug->enter_sub();
 
 181   my $form   = $main::form;
 
 182   my $locale = $main::locale;
 
 183   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
 185   $form->{title}    = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
 
 188   print $form->parse_html_template('sepa/bank_transfer_search', { vc => $vc });
 
 190   $main::lxdebug->leave_sub();
 
 194 sub bank_transfer_list {
 
 195   $main::lxdebug->enter_sub();
 
 197   my $form   = $main::form;
 
 198   my $locale = $main::locale;
 
 199   my $cgi    = $::request->{cgi};
 
 200   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
 202   $form->{title}     = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
 
 204   $form->{sort}    ||= 'id';
 
 205   $form->{sortdir}   = '1' if (!defined $form->{sortdir});
 
 207   $form->{callback}  = build_std_url('action=bank_transfer_list', 'sort', 'sortdir', 'vc');
 
 209   my %filter         = map  +( $_ => $form->{"f_${_}"} ),
 
 210                        grep  { $form->{"f_${_}"} }
 
 212                               map { ("${_}_date_from", "${_}_date_to") }
 
 213                                   qw(export requested_execution execution));
 
 214   $filter{executed}  = $form->{l_executed} ? 1 : 0 if ($form->{l_executed} != $form->{l_not_executed});
 
 215   $filter{closed}    = $form->{l_closed}   ? 1 : 0 if ($form->{l_open}     != $form->{l_closed});
 
 217   my $exports        = SL::SEPA->list_exports('filter'    => \%filter,
 
 218                                               'sortorder' => $form->{sort},
 
 219                                               'sortdir'   => $form->{sortdir},
 
 222   my $open_available = any { !$_->{closed} } @{ $exports };
 
 224   my $report         = SL::ReportGenerator->new(\%main::myconfig, $form);
 
 226   my @hidden_vars    = ('vc', grep { m/^[fl]_/ && $form->{$_} } keys %{ $form });
 
 228   my $href           = build_std_url('action=bank_transfer_list', @hidden_vars);
 
 231     'selected'    => { 'text' => $cgi->checkbox(-name => 'select_all', -id => 'select_all', -label => ''), },
 
 232     'id'          => { 'text' => $locale->text('Number'), },
 
 233     'export_date' => { 'text' => $locale->text('Export date'), },
 
 234     'employee'    => { 'text' => $locale->text('Employee'), },
 
 235     'executed'    => { 'text' => $locale->text('Executed'), },
 
 236     'closed'      => { 'text' => $locale->text('Closed'), },
 
 239   my @columns = qw(selected id export_date employee executed closed);
 
 241   foreach my $name (qw(id export_date employee executed closed)) {
 
 242     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
 243     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
 
 246   $column_defs{selected}->{visible} = $open_available                                ? 'HTML' : 0;
 
 247   $column_defs{executed}->{visible} = $form->{l_executed} && $form->{l_not_executed} ? 1 : 0;
 
 248   $column_defs{closed}->{visible}   = $form->{l_closed}   && $form->{l_open}         ? 1 : 0;
 
 251   push @options, ($vc eq 'customer' ? $::locale->text('Customer') : $locale->text('Vendor')) . ' : ' . $form->{f_vc} if ($form->{f_vc});
 
 252   push @options, $locale->text('Invoice number')                . ' : ' . $form->{f_invnumber}                     if ($form->{f_invnumber});
 
 253   push @options, $locale->text('Export date from')              . ' : ' . $form->{f_export_date_from}              if ($form->{f_export_date_from});
 
 254   push @options, $locale->text('Export date to')                . ' : ' . $form->{f_export_date_to}                if ($form->{f_export_date_to});
 
 255   push @options, $locale->text('Requested execution date from') . ' : ' . $form->{f_requested_execution_date_from} if ($form->{f_requested_execution_date_from});
 
 256   push @options, $locale->text('Requested execution date to')   . ' : ' . $form->{f_requested_execution_date_to}   if ($form->{f_requested_execution_date_to});
 
 257   push @options, $locale->text('Execution date from')           . ' : ' . $form->{f_execution_date_from}           if ($form->{f_execution_date_from});
 
 258   push @options, $locale->text('Execution date to')             . ' : ' . $form->{f_execution_date_to}             if ($form->{f_execution_date_to});
 
 259   push @options, $form->{l_executed} ? $locale->text('executed') : $locale->text('not yet executed')               if ($form->{l_executed} != $form->{l_not_executed});
 
 260   push @options, $form->{l_closed}   ? $locale->text('closed')   : $locale->text('open')                           if ($form->{l_open}     != $form->{l_closed});
 
 262   $report->set_options('top_info_text'         => join("\n", @options),
 
 263                        'raw_top_info_text'     => $form->parse_html_template('sepa/bank_transfer_list_top'),
 
 264                        'raw_bottom_info_text'  => $form->parse_html_template('sepa/bank_transfer_list_bottom', { 'show_buttons' => $open_available, vc => $vc }),
 
 265                        'std_column_visibility' => 1,
 
 266                        'output_format'         => 'HTML',
 
 267                        'title'                 => $form->{title},
 
 268                        'attachment_basename'   => $locale->text('banktransfers') . strftime('_%Y%m%d', localtime time),
 
 270   $report->set_options_from_form();
 
 271   $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 273   $report->set_columns(%column_defs);
 
 274   $report->set_column_order(@columns);
 
 275   $report->set_export_options('bank_transfer_list', @hidden_vars);
 
 276   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 278   my $edit_url = build_std_url('action=bank_transfer_edit', 'callback');
 
 280   foreach my $export (@{ $exports }) {
 
 281     my $row = { map { $_ => { 'data' => $export->{$_} } } keys %{ $export } };
 
 283     map { $row->{$_}->{data} = $export->{$_} ? $locale->text('yes') : $locale->text('no') } qw(executed closed);
 
 285     $row->{id}->{link} = $edit_url . '&id=' . E($export->{id}) . '&vc=' . E($vc);
 
 287     if (!$export->{closed}) {
 
 288       $row->{selected}->{raw_data} =
 
 289           $cgi->hidden(-name => "exports[+].id", -value => $export->{id})
 
 290         . $cgi->checkbox(-name => "exports[].selected", -value => 1, -label => '');
 
 293     $report->add_data($row);
 
 296   $report->generate_with_headers();
 
 298   $main::lxdebug->leave_sub();
 
 301 sub bank_transfer_edit {
 
 302   $main::lxdebug->enter_sub();
 
 304   my $form   = $main::form;
 
 305   my $locale = $main::locale;
 
 306   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
 309   if (!$form->{mode} || ($form->{mode} eq 'single')) {
 
 310     push @ids, $form->{id};
 
 312     @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
 
 315       $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
 
 321   foreach my $id (@ids) {
 
 322     my $curr_export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, 'vc' => $vc);
 
 324     foreach my $item (@{ $curr_export->{items} }) {
 
 325       map { $item->{"export_${_}"} = $curr_export->{$_} } grep { !ref $curr_export->{$_} } keys %{ $curr_export };
 
 329       $export = $curr_export;
 
 331       push @{ $export->{items} }, @{ $curr_export->{items} };
 
 335   if ($form->{mode} && ($form->{mode} eq 'multi')) {
 
 336     $export->{items} = [ grep { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} } ];
 
 338     if (!@{ $export->{items} }) {
 
 339       $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);
 
 343     $form->error($locale->text('That export does not exist.'));
 
 346   $form->{title}    = $locale->text('View SEPA export');
 
 348   print $form->parse_html_template('sepa/bank_transfer_edit',
 
 351                                      'current_date'              => $form->current_date(\%main::myconfig),
 
 352                                      'show_post_payments_button' => any { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} },
 
 355   $main::lxdebug->leave_sub();
 
 358 sub bank_transfer_post_payments {
 
 359   $main::lxdebug->enter_sub();
 
 361   my $form   = $main::form;
 
 362   my $locale = $main::locale;
 
 363   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
 365   my @items  = grep { $_->{selected} } @{ $form->{items} || [] };
 
 368     $form->show_generic_error($locale->text('You have not selected any item.'), 'back_button' => 1);
 
 370   my @export_ids    = uniq map { $_->{sepa_export_id} } @items;
 
 371   my %exports       = map { $_ => SL::SEPA->retrieve_export('id' => $_, 'details' => 1, vc => $vc) } @export_ids;
 
 372   my @items_to_post = ();
 
 374   foreach my $item (@items) {
 
 375     my $export = $exports{ $item->{sepa_export_id} };
 
 376     next if (!$export || $export->{closed} || $export->{executed});
 
 378     push @items_to_post, $item if (none { ($_->{id} == $item->{id}) && $_->{executed} } @{ $export->{items} });
 
 381   if (!@items_to_post) {
 
 382     $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);
 
 385   if (any { !$_->{execution_date} } @items_to_post) {
 
 386     $form->show_generic_error($locale->text('You have to specify an execution date for each antry.'), 'back_button' => 1);
 
 389   SL::SEPA->post_payment('items' => \@items_to_post, vc => $vc);
 
 391   $form->show_generic_information($locale->text('The payments have been posted.'));
 
 393   $main::lxdebug->leave_sub();
 
 396 sub bank_transfer_payment_list_as_pdf {
 
 397   $main::lxdebug->enter_sub();
 
 399   my $form       = $main::form;
 
 400   my %myconfig   = %main::myconfig;
 
 401   my $locale     = $main::locale;
 
 402   my $vc         = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
 404   my @ids        = @{ $form->{items} || [] };
 
 405   my @export_ids = uniq map { $_->{export_id} } @ids;
 
 407   $form->show_generic_error($locale->text('Multi mode not supported.'), 'back_button' => 1) if 1 != scalar @export_ids;
 
 409   my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1, vc => $vc);
 
 412   foreach my $id (@ids) {
 
 413     my $item = first { $_->{id} == $id->{id} } @{ $export->{items} };
 
 414     push @items, $item if $item;
 
 417   $form->show_generic_error($locale->text('No transfers were executed in this export.'), 'back_button' => 1) if 1 > scalar @items;
 
 419   my $report         =  SL::ReportGenerator->new(\%main::myconfig, $form);
 
 422     'invnumber'      => { 'text' => $locale->text('Invoice'),                                                                  },
 
 423     'vc_name'        => { 'text' => $vc eq 'customer' ? $locale->text('Customer')         : $locale->text('Vendor'),           },
 
 424     'our_iban'       => { 'text' => $vc eq 'customer' ? $locale->text('Destination IBAN') : $locale->text('Source IBAN'),      },
 
 425     'our_bic'        => { 'text' => $vc eq 'customer' ? $locale->text('Destination BIC')  : $locale->text('Source BIC'),       },
 
 426     'vc_iban'        => { 'text' => $vc eq 'customer' ? $locale->text('Source IBAN')      : $locale->text('Destination IBAN'), },
 
 427     'vc_bic'         => { 'text' => $vc eq 'customer' ? $locale->text('Source BIC')       : $locale->text('Destination BIC'),  },
 
 428     'amount'         => { 'text' => $locale->text('Amount'),                                                                   },
 
 429     'reference'      => { 'text' => $locale->text('Reference'),                                                                },
 
 430     'execution_date' => { 'text' => $locale->text('Execution date'),                                                           },
 
 433   map { $column_defs{$_}->{align} = 'right' } qw(amount execution_date);
 
 435   my @columns        =  qw(invnumber vc_name our_iban our_bic vc_iban vc_bic amount reference execution_date);
 
 437   $report->set_options('std_column_visibility' => 1,
 
 438                        'output_format'         => 'PDF',
 
 439                        '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}),
 
 440                        '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),
 
 443   $report->set_columns(%column_defs);
 
 444   $report->set_column_order(@columns);
 
 446   foreach my $item (@items) {
 
 447     my $row                = { map { $_ => { 'data' => $item->{$_} } } @columns };
 
 448     $row->{amount}->{data} = $form->format_amount(\%myconfig, $item->{amount}, 2);
 
 450     $report->add_data($row);
 
 453   $report->generate_with_headers();
 
 455   $main::lxdebug->leave_sub();
 
 459 sub bank_transfer_download_sepa_xml {
 
 460   $main::lxdebug->enter_sub();
 
 462   my $form     =  $main::form;
 
 463   my $myconfig = \%main::myconfig;
 
 464   my $locale   =  $main::locale;
 
 465   my $cgi      =  $::request->{cgi};
 
 466   my $vc       = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
 467   my $defaults = SL::DB::Default->get;
 
 469   if (!$defaults->company) {
 
 470     $form->show_generic_error($locale->text('You have to enter a company name in the client configuration.'), 'back_button' => 1);
 
 473   if (($vc eq 'customer') && !$defaults->sepa_creditor_id) {
 
 474     $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID in the client configuration.'), 'back_button' => 1);
 
 478   if ($form->{mode} && ($form->{mode} eq 'multi')) {
 
 479      @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
 
 482     @ids = ($form->{id});
 
 486     $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
 
 491   foreach my $id (@ids) {
 
 492     my $export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, vc => $vc);
 
 493     push @items, grep { !$_->{executed} } @{ $export->{items} } if ($export && !$export->{closed});
 
 497     $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);
 
 500   my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$);
 
 502   my $sepa_xml   = SL::SEPA::XML->new('company'     => $defaults->company,
 
 503                                       'creditor_id' => $defaults->sepa_creditor_id,
 
 504                                       'src_charset' => 'UTF-8',
 
 505                                       'message_id'  => $message_id,
 
 507                                       'collection'  => $vc eq 'customer',
 
 510   foreach my $item (@items) {
 
 511     my $requested_execution_date;
 
 513     if ($item->{requested_execution_date}) {
 
 514       my ($yy, $mm, $dd)        = $locale->parse_date($myconfig, $item->{requested_execution_date});
 
 515       $requested_execution_date = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
 
 518     if ($vc eq 'customer') {
 
 519       my ($yy, $mm, $dd)      = $locale->parse_date($myconfig, $item->{reference_date});
 
 520       $item->{reference_date} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
 
 521       $mandator_id = $item->{mandator_id};
 
 522       if ($item->{mandate_date_of_signature}) {
 
 523         ($yy, $mm, $dd)                    = $locale->parse_date($myconfig, $item->{mandate_date_of_signature});
 
 524         $item->{mandate_date_of_signature} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
 
 528     $sepa_xml->add_transaction({ 'src_iban'       => $item->{our_iban},
 
 529                                  'src_bic'        => $item->{our_bic},
 
 530                                  'dst_iban'       => $item->{vc_iban},
 
 531                                  'dst_bic'        => $item->{vc_bic},
 
 532                                  'company'        => $item->{vc_name},
 
 533                                  'company_number' => $item->{vc_number},
 
 534                                  'amount'         => $item->{amount},
 
 535                                  'reference'      => $item->{reference},
 
 536                                  'mandator_id'    => $mandator_id,
 
 537                                  'reference_date' => $item->{reference_date},
 
 538                                  'execution_date' => $requested_execution_date,
 
 539                                  'end_to_end_id'  => $item->{end_to_end_id},
 
 540                                  'date_of_signature' => $item->{mandate_date_of_signature}, });
 
 543   my $xml = $sepa_xml->to_xml();
 
 545   print $cgi->header('-type'                => 'application/octet-stream',
 
 546                      '-content-disposition' => 'attachment; filename="SEPA_' . $message_id . ($vc eq 'customer' ? '.cdd' : '.cct') . '"',
 
 547                      '-content-length'      => length $xml);
 
 550   $main::lxdebug->leave_sub();
 
 553 sub bank_transfer_mark_as_closed_step1 {
 
 554   $main::lxdebug->enter_sub();
 
 556   my $form       = $main::form;
 
 557   my $locale     = $main::locale;
 
 558   my $vc         = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
 560   my @export_ids = map { $_->{id} } grep { $_->{selected} } @{ $form->{exports} || [] };
 
 563     $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
 
 566   my @open_export_ids = ();
 
 567   foreach my $id (@export_ids) {
 
 568     my $export = SL::SEPA->retrieve_export('id' => $id, vc => $vc);
 
 569     push @open_export_ids, $id if (!$export->{closed});
 
 572   if (!@open_export_ids) {
 
 573     $form->show_generic_error($locale->text('All of the exports you have selected were already closed.'), 'back_button' => 1);
 
 576   $form->{title} = $locale->text('Close SEPA exports');
 
 578   print $form->parse_html_template('sepa/bank_transfer_mark_as_closed_step1', { 'OPEN_EXPORT_IDS' => \@open_export_ids, vc => $vc });
 
 580   $main::lxdebug->leave_sub();
 
 583 sub bank_transfer_mark_as_closed_step2 {
 
 584   $main::lxdebug->enter_sub();
 
 586   my $form       = $main::form;
 
 587   my $locale     = $main::locale;
 
 589   map { SL::SEPA->close_export('id' => $_); } @{ $form->{open_export_ids} || [] };
 
 591   $form->{title} = $locale->text('Close SEPA exports');
 
 593   $form->show_generic_information($locale->text('The selected exports have been closed.'));
 
 595   $main::lxdebug->leave_sub();
 
 599   my $form = $main::form;
 
 601   foreach my $action (qw(bank_transfer_create bank_transfer_edit bank_transfer_list
 
 602                          bank_transfer_post_payments bank_transfer_download_sepa_xml
 
 603                          bank_transfer_mark_as_closed_step1 bank_transfer_mark_as_closed_step2
 
 604                          bank_transfer_payment_list_as_pdf)) {
 
 605     if ($form->{"action_${action}"}) {
 
 611   $form->error($main::locale->text('No action defined.'));