3 use List::MoreUtils qw(any none uniq);
4 use List::Util qw(sum first);
5 use POSIX qw(strftime);
7 use SL::DB::BankAccount;
8 use SL::DB::SepaExport;
12 use SL::GenericTranslations;
13 use SL::Locale::String qw(t8);
14 use SL::ReportGenerator;
18 require "bin/mozilla/common.pl";
19 require "bin/mozilla/reportgenerator.pl";
21 sub bank_transfer_add {
22 $main::lxdebug->enter_sub();
24 my $form = $main::form;
25 my $locale = $main::locale;
26 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
27 my $vc_no = $form->{vc} eq 'customer' ? $::locale->text('VN') : $::locale->text('CN');
29 $form->{title} = $vc eq 'customer' ? $::locale->text('Prepare bank collection via SEPA XML') : $locale->text('Prepare bank transfer via SEPA XML');
31 my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
33 if (!scalar @{ $bank_accounts }) {
34 $form->error($locale->text('You have not added bank accounts yet.'));
37 my $invoices = SL::SEPA->retrieve_open_invoices(vc => $vc);
39 if (!scalar @{ $invoices }) {
40 $form->show_generic_information($locale->text('Either there are no open invoices, or you have already initiated bank transfers ' .
41 'with the open amounts for those that are still open.'));
42 $main::lxdebug->leave_sub();
46 # Only include those per default that require manual action from our
47 # side. For sales invoices these are the ones for which direct debit
48 # has been selected. For purchase invoices it's the other way
49 # around: if direct debit is active then the vendor will collect
50 # from us automatically and we don't have to send money manually.
51 $_->{checked} = ($vc eq 'customer' ? $_->{direct_debit} : !$_->{direct_debit}) for @{ $invoices };
53 my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
54 my %translations = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
56 foreach my $invoice (@{ $invoices }) {
57 my $prefix = $translations{ $invoice->{language_id} } || $translations{default} || $::locale->text('Invoice');
58 $prefix .= ' ' unless $prefix =~ m/ $/;
59 $invoice->{reference_prefix} = $prefix;
61 # add c_vendor_id or v_vendor_id as a prefix if a entry exists
62 next unless $invoice->{vc_vc_id};
64 my $prefix_vc_number = $translations{ $invoice->{language_id} } || $translations{default} || $vc_no;
65 $prefix_vc_number .= ' ' unless $prefix_vc_number =~ m/ $/;
66 $invoice->{reference_prefix_vc} = ' ' . $prefix_vc_number unless $prefix_vc_number =~ m/^ /;
69 setup_sepa_add_transfer_action_bar();
72 print $form->parse_html_template('sepa/bank_transfer_add',
73 { 'INVOICES' => $invoices,
74 'BANK_ACCOUNTS' => $bank_accounts,
78 $main::lxdebug->leave_sub();
81 sub bank_transfer_create {
82 $main::lxdebug->enter_sub();
84 my $form = $main::form;
85 my $locale = $main::locale;
86 my $myconfig = \%main::myconfig;
87 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
89 $form->{title} = $vc eq 'customer' ? $::locale->text('Create bank collection via SEPA XML') : $locale->text('Create bank transfer via SEPA XML');
91 my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
92 if (!scalar @{ $bank_accounts }) {
93 $form->error($locale->text('You have not added bank accounts yet.'));
96 my $bank_account = SL::DB::Manager::BankAccount->find_by( id => $form->{bank_account} );
98 unless ( $bank_account ) {
99 $form->error($locale->text('The selected bank account does not exist anymore.'));
102 my $arap_id = $vc eq 'customer' ? 'ar_id' : 'ap_id';
103 my $invoices = SL::SEPA->retrieve_open_invoices(vc => $vc);
105 # 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
106 # all the information from retrieve_open_invoices is then ADDED to what was passed via @{ $form->{bank_transfers} }
107 # parse amount from the entry in the form, but take skonto_amount from PT again
108 # the map inserts the values of invoice_map directly into the array of hashes
109 my %selected_ids = map { ($_ => 1) } @{ $form->{ids} || [] };
110 my %invoices_map = map { $_->{id} => $_ } @{ $invoices };
112 map +{ %{ $invoices_map{ $_->{$arap_id} } }, %{ $_ } },
113 grep { ($_->{selected} || $selected_ids{$_->{$arap_id}}) && (0 < $_->{amount}) && $invoices_map{ $_->{$arap_id} } }
114 map { $_->{amount} = $form->parse_amount($myconfig, $_->{amount}); $_ }
115 @{ $form->{bank_transfers} || [] };
117 # override default payment_type selection and set it to the one chosen by the user
118 # in the previous step, so that we don't need the logic in the template
119 my $subtract_days = $::instance_conf->get_sepa_set_skonto_date_buffer_in_days;
120 my $set_skonto_date = $::instance_conf->get_sepa_set_skonto_date_as_default_exec_date;
121 my $set_duedate = $::instance_conf->get_sepa_set_duedate_as_default_exec_date;
122 foreach my $bt (@bank_transfers) {
123 # add a good recommended exec date
124 # set to skonto date if exists or to duedate
125 # in both cases subtract the same buffer (if configured, default 0)
126 $bt->{recommended_execution_date} =
127 $set_skonto_date && $bt->{payment_type} eq 'with_skonto_pt' ?
128 DateTime->from_kivitendo($bt->{skonto_date})->subtract(days => $subtract_days)->to_kivitendo
129 : $set_duedate && $bt->{duedate} ?
130 DateTime->from_kivitendo($bt->{duedate} )->subtract(days => $subtract_days)->to_kivitendo
134 foreach my $type ( @{$bt->{payment_select_options}} ) {
135 if ( $type->{payment_type} eq $bt->{payment_type} ) {
136 $type->{selected} = 1;
138 $type->{selected} = 0;
143 if (!scalar @bank_transfers) {
144 $form->error($locale->text('You have selected none of the invoices.'));
147 my $total_trans = sum map { $_->{open_amount} } @bank_transfers;
152 my @bank_columns = qw(iban bic);
153 push @bank_columns, qw(mandator_id mandate_date_of_signature) if $vc eq 'customer';
155 if ($form->{confirmation}) {
156 $vc_bank_info = { map { $_->{id} => $_ } @{ $form->{vc_bank_info} || [] } };
158 foreach my $info (values %{ $vc_bank_info }) {
159 if (any { !$info->{$_} } @bank_columns) {
160 $error_message = $locale->text('The bank information must not be empty.');
166 if ($error_message || !$form->{confirmation}) {
167 my @vc_ids = uniq map { $_->{vc_id} } @bank_transfers;
168 $vc_bank_info ||= CT->get_bank_info('vc' => $vc,
170 my @vc_bank_info = sort { lc $a->{name} cmp lc $b->{name} } values %{ $vc_bank_info };
172 setup_sepa_create_transfer_action_bar(is_vendor => $vc eq 'vendor');
175 print $form->parse_html_template('sepa/bank_transfer_create',
176 { 'BANK_TRANSFERS' => \@bank_transfers,
177 'BANK_ACCOUNTS' => $bank_accounts,
178 'VC_BANK_INFO' => \@vc_bank_info,
179 'bank_account' => $bank_account,
180 'error_message' => $error_message,
182 'total_trans' => $total_trans,
186 foreach my $bank_transfer (@bank_transfers) {
187 foreach (@bank_columns) {
188 $bank_transfer->{"vc_${_}"} = $vc_bank_info->{ $bank_transfer->{vc_id} }->{$_};
189 $bank_transfer->{"our_${_}"} = $bank_account->{$_};
192 $bank_transfer->{chart_id} = $bank_account->{chart_id};
195 my $id = SL::SEPA->create_export('employee' => $::myconfig{login},
196 'bank_transfers' => \@bank_transfers,
200 print $form->parse_html_template('sepa/bank_transfer_created', { 'id' => $id, 'vc' => $vc });
203 $main::lxdebug->leave_sub();
206 sub bank_transfer_search {
207 $main::lxdebug->enter_sub();
209 my $form = $main::form;
210 my $locale = $main::locale;
211 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
213 $form->{title} = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
215 setup_sepa_search_transfer_action_bar();
218 print $form->parse_html_template('sepa/bank_transfer_search', { vc => $vc });
220 $main::lxdebug->leave_sub();
224 sub bank_transfer_list {
225 $main::lxdebug->enter_sub();
227 my $form = $main::form;
228 my $locale = $main::locale;
229 my $cgi = $::request->{cgi};
230 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
232 $form->{title} = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
234 $form->{sort} ||= 'id';
235 $form->{sortdir} = '1' if (!defined $form->{sortdir});
237 $form->{callback} = build_std_url('action=bank_transfer_list', 'sort', 'sortdir', 'vc');
239 my %filter = map +( $_ => $form->{"f_${_}"} ),
240 grep { $form->{"f_${_}"} }
241 (qw(vc invnumber message_id),
242 map { ("${_}_date_from", "${_}_date_to") }
243 qw(export requested_execution execution));
244 $filter{executed} = $form->{l_executed} ? 1 : 0 if ($form->{l_executed} != $form->{l_not_executed});
245 $filter{closed} = $form->{l_closed} ? 1 : 0 if ($form->{l_open} != $form->{l_closed});
247 my $exports = SL::SEPA->list_exports('filter' => \%filter,
248 'sortorder' => $form->{sort},
249 'sortdir' => $form->{sortdir},
252 my $open_available = any { !$_->{closed} } @{ $exports };
254 my $report = SL::ReportGenerator->new(\%main::myconfig, $form);
256 my @hidden_vars = ('vc', grep { m/^[fl]_/ && $form->{$_} } keys %{ $form });
258 my $href = build_std_url('action=bank_transfer_list', @hidden_vars);
261 'selected' => { 'text' => $cgi->checkbox(-name => 'select_all', -id => 'select_all', -label => ''), },
262 'id' => { 'text' => $locale->text('Number'), },
263 'export_date' => { 'text' => $locale->text('Export date'), },
264 'employee' => { 'text' => $locale->text('Employee'), },
265 'executed' => { 'text' => $locale->text('Executed'), },
266 'closed' => { 'text' => $locale->text('Closed'), },
267 num_invoices => { 'text' => $locale->text('Number of invoices'), },
268 sum_amounts => { 'text' => $locale->text('Sum of all amounts'), },
269 message_ids => { 'text' => $locale->text('SEPA message IDs'), },
272 my @columns = qw(selected id export_date employee executed closed num_invoices sum_amounts message_ids);
273 my %column_alignment = map { ($_ => 'right') } qw(num_invoices sum_amounts);
275 foreach my $name (qw(id export_date employee executed closed)) {
276 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
277 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
280 $column_defs{selected}->{visible} = $open_available ? 'HTML' : 0;
281 $column_defs{executed}->{visible} = $form->{l_executed} && $form->{l_not_executed} ? 1 : 0;
282 $column_defs{closed}->{visible} = $form->{l_closed} && $form->{l_open} ? 1 : 0;
283 $column_defs{$_}->{align} = $column_alignment{$_} for keys %column_alignment;
286 push @options, ($vc eq 'customer' ? $::locale->text('Customer') : $locale->text('Vendor')) . ' : ' . $form->{f_vc} if ($form->{f_vc});
287 push @options, $locale->text('Invoice number') . ' : ' . $form->{f_invnumber} if ($form->{f_invnumber});
288 push @options, $locale->text('SEPA message ID') . ' : ' . $form->{f_message_id} if (length $form->{f_message_id});
289 push @options, $locale->text('Export date from') . ' : ' . $form->{f_export_date_from} if ($form->{f_export_date_from});
290 push @options, $locale->text('Export date to') . ' : ' . $form->{f_export_date_to} if ($form->{f_export_date_to});
291 push @options, $locale->text('Requested execution date from') . ' : ' . $form->{f_requested_execution_date_from} if ($form->{f_requested_execution_date_from});
292 push @options, $locale->text('Requested execution date to') . ' : ' . $form->{f_requested_execution_date_to} if ($form->{f_requested_execution_date_to});
293 push @options, $locale->text('Execution date from') . ' : ' . $form->{f_execution_date_from} if ($form->{f_execution_date_from});
294 push @options, $locale->text('Execution date to') . ' : ' . $form->{f_execution_date_to} if ($form->{f_execution_date_to});
295 push @options, $form->{l_executed} ? $locale->text('executed') : $locale->text('not yet executed') if ($form->{l_executed} != $form->{l_not_executed});
296 push @options, $form->{l_closed} ? $locale->text('closed') : $locale->text('open') if ($form->{l_open} != $form->{l_closed});
298 $report->set_options('top_info_text' => join("\n", @options),
299 'raw_top_info_text' => $form->parse_html_template('sepa/bank_transfer_list_top'),
300 'raw_bottom_info_text' => $form->parse_html_template('sepa/bank_transfer_list_bottom', { 'show_buttons' => $open_available, vc => $vc }),
301 'std_column_visibility' => 1,
302 'output_format' => 'HTML',
303 'title' => $form->{title},
304 'attachment_basename' => $locale->text('banktransfers') . strftime('_%Y%m%d', localtime time),
306 $report->set_options_from_form();
307 $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
309 $report->set_columns(%column_defs);
310 $report->set_column_order(@columns);
311 $report->set_export_options('bank_transfer_list', @hidden_vars);
312 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
314 my $edit_url = build_std_url('action=bank_transfer_edit', 'callback');
316 foreach my $export (@{ $exports }) {
317 my $row = { map { $_ => { 'data' => $export->{$_}, 'align' => $column_alignment{$_} } } keys %{ $export } };
319 map { $row->{$_}->{data} = $export->{$_} ? $locale->text('yes') : $locale->text('no') } qw(executed closed);
321 $row->{id}->{link} = $edit_url . '&id=' . E($export->{id}) . '&vc=' . E($vc);
323 $row->{$_}->{data} = $::form->format_amount(\%::myconfig, $row->{$_}->{data}, 2) for qw(sum_amounts);
325 if (!$export->{closed}) {
326 $row->{selected}->{raw_data} = $cgi->checkbox(-name => "ids[]", -value => $export->{id}, -label => '');
329 $report->add_data($row);
332 setup_sepa_list_transfers_action_bar(show_buttons => $open_available, is_vendor => $vc eq 'vendor');
334 $report->generate_with_headers();
336 $main::lxdebug->leave_sub();
339 sub bank_transfer_edit {
340 $main::lxdebug->enter_sub();
342 my $form = $main::form;
343 my $locale = $main::locale;
344 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
347 if (!$form->{mode} || ($form->{mode} eq 'single')) {
348 push @ids, $form->{id};
350 @ids = @{ $form->{ids} || [] };
353 $form->show_generic_error($locale->text('You have not selected any export.'));
359 foreach my $id (@ids) {
360 my $curr_export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, 'vc' => $vc);
362 foreach my $item (@{ $curr_export->{items} }) {
363 map { $item->{"export_${_}"} = $curr_export->{$_} } grep { !ref $curr_export->{$_} } keys %{ $curr_export };
367 $export = $curr_export;
369 push @{ $export->{items} }, @{ $curr_export->{items} };
373 if ($form->{mode} && ($form->{mode} eq 'multi')) {
374 $export->{items} = [ grep { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} } ];
376 if (!@{ $export->{items} }) {
377 $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'));
381 $form->error($locale->text('That export does not exist.'));
384 my $show_post_payments_button = any { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} };
385 my $has_executed = any { $_->{executed} } @{ $export->{items} };
387 setup_sepa_edit_transfer_action_bar(
388 show_post_payments_button => $show_post_payments_button,
389 has_executed => $has_executed,
392 $form->{title} = $locale->text('View SEPA export');
394 print $form->parse_html_template('sepa/bank_transfer_edit',
397 current_date => $form->current_date(\%main::myconfig),
398 show_post_payments_button => $show_post_payments_button,
401 $main::lxdebug->leave_sub();
404 sub bank_transfer_post_payments {
405 $main::lxdebug->enter_sub();
407 my $form = $main::form;
408 my $locale = $main::locale;
409 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
411 my %selected = map { ($_ => 1) } @{ $form->{ids} || [] };
412 my @items = grep { $selected{$_->{id}} } @{ $form->{items} || [] };
415 $form->show_generic_error($locale->text('You have not selected any item.'));
417 my @export_ids = uniq map { $_->{sepa_export_id} } @items;
418 my %exports = map { $_ => SL::SEPA->retrieve_export('id' => $_, 'details' => 1, vc => $vc) } @export_ids;
419 my @items_to_post = ();
421 foreach my $item (@items) {
422 my $export = $exports{ $item->{sepa_export_id} };
423 next if (!$export || $export->{closed} || $export->{executed});
425 push @items_to_post, $item if (none { ($_->{id} == $item->{id}) && $_->{executed} } @{ $export->{items} });
428 if (!@items_to_post) {
429 $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'));
432 if (any { !$_->{execution_date} } @items_to_post) {
433 $form->show_generic_error($locale->text('You have to specify an execution date for each antry.'));
436 SL::SEPA->post_payment('items' => \@items_to_post, vc => $vc);
438 $form->show_generic_information($locale->text('The payments have been posted.'));
440 $main::lxdebug->leave_sub();
443 sub bank_transfer_payment_list_as_pdf {
444 $main::lxdebug->enter_sub();
446 my $form = $main::form;
447 my %myconfig = %main::myconfig;
448 my $locale = $main::locale;
449 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
451 my @ids = @{ $form->{ids} || [] };
452 my @export_ids = uniq map { $_->{sepa_export_id} } @{ $form->{items} || [] };
454 $form->show_generic_error($locale->text('Multi mode not supported.')) if 1 != scalar @export_ids;
456 my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1, vc => $vc);
459 foreach my $id (@ids) {
460 my $item = first { $_->{id} == $id } @{ $export->{items} };
461 push @items, $item if $item;
464 $form->show_generic_error($locale->text('No transfers were executed in this export.')) if 1 > scalar @items;
466 my $report = SL::ReportGenerator->new(\%main::myconfig, $form);
469 'invnumber' => { 'text' => $locale->text('Invoice'), },
470 'vc_name' => { 'text' => $vc eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
471 'our_iban' => { 'text' => $vc eq 'customer' ? $locale->text('Destination IBAN') : $locale->text('Source IBAN'), },
472 'our_bic' => { 'text' => $vc eq 'customer' ? $locale->text('Destination BIC') : $locale->text('Source BIC'), },
473 'vc_iban' => { 'text' => $vc eq 'customer' ? $locale->text('Source IBAN') : $locale->text('Destination IBAN'), },
474 'vc_bic' => { 'text' => $vc eq 'customer' ? $locale->text('Source BIC') : $locale->text('Destination BIC'), },
475 'amount' => { 'text' => $locale->text('Amount'), },
476 'reference' => { 'text' => $locale->text('Reference'), },
477 'execution_date' => { 'text' => $locale->text('Execution date'), },
480 map { $column_defs{$_}->{align} = 'right' } qw(amount execution_date);
482 my @columns = qw(invnumber vc_name our_iban our_bic vc_iban vc_bic amount reference execution_date);
484 $report->set_options('std_column_visibility' => 1,
485 'output_format' => 'PDF',
486 '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}),
487 '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),
490 $report->set_columns(%column_defs);
491 $report->set_column_order(@columns);
493 foreach my $item (@items) {
494 my $row = { map { $_ => { 'data' => $item->{$_} } } @columns };
495 $row->{amount}->{data} = $form->format_amount(\%myconfig, $item->{amount}, 2);
497 $report->add_data($row);
500 $report->generate_with_headers();
502 $main::lxdebug->leave_sub();
506 sub bank_transfer_download_sepa_xml {
507 $main::lxdebug->enter_sub();
509 my $form = $main::form;
510 my $myconfig = \%main::myconfig;
511 my $locale = $main::locale;
512 my $cgi = $::request->{cgi};
513 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
514 my $defaults = SL::DB::Default->get;
516 if (!$defaults->company) {
517 $form->show_generic_error($locale->text('You have to enter a company name in the client configuration.'));
520 if (($vc eq 'customer') && !$defaults->sepa_creditor_id) {
521 $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID in the client configuration.'));
525 if ($form->{mode} && ($form->{mode} eq 'multi')) {
526 @ids = @{ $form->{ids} || [] };
529 @ids = ($form->{id});
533 $form->show_generic_error($locale->text('You have not selected any export.'));
538 foreach my $id (@ids) {
539 my $export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, vc => $vc);
540 push @items, grep { !$_->{executed} } @{ $export->{items} } if ($export && !$export->{closed});
544 $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'));
547 my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$);
549 my $sepa_xml = SL::SEPA::XML->new('company' => $defaults->company,
550 'creditor_id' => $defaults->sepa_creditor_id,
551 'src_charset' => 'UTF-8',
552 'message_id' => $message_id,
554 'collection' => $vc eq 'customer',
557 foreach my $item (@items) {
558 my $requested_execution_date;
560 if ($item->{requested_execution_date}) {
561 my ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{requested_execution_date});
562 $requested_execution_date = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
565 if ($vc eq 'customer') {
566 my ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{reference_date});
567 $item->{reference_date} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
568 $mandator_id = $item->{mandator_id};
569 if ($item->{mandate_date_of_signature}) {
570 ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{mandate_date_of_signature});
571 $item->{mandate_date_of_signature} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
575 $sepa_xml->add_transaction({ 'src_iban' => $item->{our_iban},
576 'src_bic' => $item->{our_bic},
577 'dst_iban' => $item->{vc_iban},
578 'dst_bic' => $item->{vc_bic},
579 'company' => $item->{vc_name},
580 'company_number' => $item->{vc_number},
581 'amount' => $item->{amount},
582 'reference' => $item->{reference},
583 'mandator_id' => $mandator_id,
584 'reference_date' => $item->{reference_date},
585 'execution_date' => $requested_execution_date,
586 'end_to_end_id' => $item->{end_to_end_id},
587 'date_of_signature' => $item->{mandate_date_of_signature}, });
590 # Store the message ID used in each of the entries in order to
591 # facilitate finding them by looking at bank statements.
592 foreach my $id (@ids) {
593 SL::DB::SepaExportMessageId->new(
594 sepa_export_id => $id,
595 message_id => $message_id,
599 my $xml = $sepa_xml->to_xml();
601 print $cgi->header('-type' => 'application/octet-stream',
602 '-content-disposition' => 'attachment; filename="SEPA_' . $message_id . ($vc eq 'customer' ? '.cdd' : '.cct') . '"',
603 '-content-length' => length $xml);
606 $main::lxdebug->leave_sub();
609 sub bank_transfer_mark_as_closed {
610 $main::lxdebug->enter_sub();
612 my $form = $main::form;
613 my $locale = $main::locale;
615 map { SL::SEPA->close_export('id' => $_); } @{ $form->{ids} || [] };
617 $form->{title} = $locale->text('Close SEPA exports');
619 $form->show_generic_information($locale->text('The selected exports have been closed.'));
621 $main::lxdebug->leave_sub();
624 sub bank_transfer_undo_sepa_xml {
625 $main::lxdebug->enter_sub();
627 my $form = $main::form;
628 my $locale = $main::locale;
630 map { SL::SEPA->undo_export('id' => $_); } @{ $form->{ids} || [] };
632 $form->{title} = $locale->text('Undo SEPA exports');
634 $form->show_generic_information($locale->text('The selected exports have been undone.'));
636 $main::lxdebug->leave_sub();
640 my $form = $main::form;
642 foreach my $action (qw(bank_transfer_create bank_transfer_edit bank_transfer_list
643 bank_transfer_post_payments bank_transfer_download_sepa_xml
644 bank_transfer_mark_as_closed_step1 bank_transfer_mark_as_closed_step2
645 bank_transfer_payment_list_as_pdf bank_transfer_undo_sepa_xml)) {
646 if ($form->{"action_${action}"}) {
652 $form->error($main::locale->text('No action defined.'));
655 sub setup_sepa_add_transfer_action_bar {
658 for my $bar ($::request->layout->get('actionbar')) {
662 submit => [ '#form', { action => "bank_transfer_create" } ],
663 accesskey => 'enter',
664 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
670 sub setup_sepa_create_transfer_action_bar {
673 for my $bar ($::request->layout->get('actionbar')) {
677 submit => [ '#form', { action => "bank_transfer_create" } ],
678 accesskey => 'enter',
679 tooltip => $params{is_vendor} ? t8('Create bank transfer') : t8('Create bank collection'),
683 call => [ 'kivi.history_back' ],
689 sub setup_sepa_search_transfer_action_bar {
692 for my $bar ($::request->layout->get('actionbar')) {
696 submit => [ '#form', { action => 'bank_transfer_list' } ],
697 accesskey => 'enter',
703 sub setup_sepa_list_transfers_action_bar {
706 return unless $params{show_buttons};
708 for my $bar ($::request->layout->get('actionbar')) {
711 action => [ t8('Actions') ],
713 t8('SEPA XML download'),
714 submit => [ '#form', { action => 'bank_transfer_download_sepa_xml' } ],
715 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
719 submit => [ '#form', { action => 'bank_transfer_edit' } ],
720 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
723 t8('Mark as closed'),
724 submit => [ '#form', { action => 'bank_transfer_mark_as_closed' } ],
725 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
726 confirm => [ $params{is_vendor} ? t8('Do you really want to close the selected SEPA exports? No payment will be recorded for bank transfers that haven\'t been marked as executed yet.')
727 : t8('Do you really want to close the selected SEPA exports? No payment will be recorded for bank collections that haven\'t been marked as executed yet.') ],
730 t8('Undo SEPA exports'),
731 submit => [ '#form', { action => 'bank_transfer_undo_sepa_xml' } ],
732 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
733 confirm => [ t8('Do you really want to undo the selected SEPA exports? You have to reassign the export again.') ],
735 ], # end of combobox "Actions"
740 sub setup_sepa_edit_transfer_action_bar {
743 for my $bar ($::request->layout->get('actionbar')) {
747 submit => [ '#form', { action => 'bank_transfer_post_payments' } ],
748 accesskey => 'enter',
749 tooltip => t8('Post payments for selected invoices'),
750 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
751 only_if => $params{show_post_payments_button},
755 submit => [ '#form', { action => 'bank_transfer_payment_list_as_pdf' } ],
756 accesskey => 'enter',
757 tooltip => t8('Download list of payments as PDF'),
758 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
759 not_if => $params{show_post_payments_button},