3 use List::MoreUtils qw(any none uniq);
4 use List::Util qw(sum first);
5 use POSIX qw(strftime);
11 use SL::GenericTranslations;
12 use SL::ReportGenerator;
16 require "bin/mozilla/common.pl";
17 require "bin/mozilla/reportgenerator.pl";
19 sub bank_transfer_add {
20 $main::lxdebug->enter_sub();
22 my $form = $main::form;
23 my $locale = $main::locale;
24 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
26 $form->{title} = $vc eq 'customer' ? $::locale->text('Prepare bank collection via SEPA XML') : $locale->text('Prepare bank transfer via SEPA XML');
28 my $bank_accounts = SL::BankAccount->list();
30 if (!scalar @{ $bank_accounts }) {
31 $form->error($locale->text('You have not added bank accounts yet.'));
34 my $invoices = SL::SEPA->retrieve_open_invoices(vc => $vc);
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();
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 };
50 my $bank_account_label_sub = sub { $locale->text('Account number #1, bank code #2, #3', $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank}) };
52 my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
53 my %translations = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
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;
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,
69 $main::lxdebug->leave_sub();
72 sub bank_transfer_create {
73 $main::lxdebug->enter_sub();
75 my $form = $main::form;
76 my $locale = $main::locale;
77 my $myconfig = \%main::myconfig;
78 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
80 $form->{title} = $vc eq 'customer' ? $::locale->text('Create bank collection via SEPA XML') : $locale->text('Create bank transfer via SEPA XML');
82 my $bank_accounts = SL::BankAccount->list();
84 if (!scalar @{ $bank_accounts }) {
85 $form->error($locale->text('You have not added bank accounts yet.'));
88 my $bank_account = first { $form->{bank_account}->{id} == $_->{id} } @{ $bank_accounts };
91 $form->error($locale->text('The selected bank account does not exist anymore.'));
94 my $arap_id = $vc eq 'customer' ? 'ar_id' : 'ap_id';
95 my $invoices = SL::SEPA->retrieve_open_invoices(vc => $vc);
97 my %invoices_map = map { $_->{id} => $_ } @{ $invoices };
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} || [] };
104 if (!scalar @bank_transfers) {
105 $form->error($locale->text('You have selected none of the invoices.'));
108 my $total_trans = sum map { $_->{open_amount} } @bank_transfers;
113 my @bank_columns = qw(iban bic);
114 push @bank_columns, qw(mandator_id mandate_date_of_signature) if $vc eq 'customer';
116 if ($form->{confirmation}) {
117 $vc_bank_info = { map { $_->{id} => $_ } @{ $form->{vc_bank_info} || [] } };
119 foreach my $info (values %{ $vc_bank_info }) {
120 if (any { !$info->{$_} } @bank_columns) {
121 $error_message = $locale->text('The bank information must not be empty.');
127 if ($error_message || !$form->{confirmation}) {
128 my @vc_ids = uniq map { $_->{vc_id} } @bank_transfers;
129 $vc_bank_info ||= CT->get_bank_info('vc' => $vc,
131 my @vc_bank_info = sort { lc $a->{name} cmp lc $b->{name} } values %{ $vc_bank_info };
133 my $bank_account_label_sub = sub { $locale->text('Account number #1, bank code #2, #3', $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank}) };
136 print $form->parse_html_template('sepa/bank_transfer_create',
137 { 'BANK_TRANSFERS' => \@bank_transfers,
138 'BANK_ACCOUNTS' => $bank_accounts,
139 'VC_BANK_INFO' => \@vc_bank_info,
140 'bank_account' => $bank_account,
141 'bank_account_label' => $bank_account_label_sub,
142 'error_message' => $error_message,
144 'total_trans' => $total_trans,
148 foreach my $bank_transfer (@bank_transfers) {
149 foreach (@bank_columns) {
150 $bank_transfer->{"vc_${_}"} = $vc_bank_info->{ $bank_transfer->{vc_id} }->{$_};
151 $bank_transfer->{"our_${_}"} = $bank_account->{$_};
154 $bank_transfer->{chart_id} = $bank_account->{chart_id};
157 my $id = SL::SEPA->create_export('employee' => $form->{login},
158 'bank_transfers' => \@bank_transfers,
162 print $form->parse_html_template('sepa/bank_transfer_created', { 'id' => $id, 'vc' => $vc });
165 $main::lxdebug->leave_sub();
168 sub bank_transfer_search {
169 $main::lxdebug->enter_sub();
171 my $form = $main::form;
172 my $locale = $main::locale;
173 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
175 $form->{title} = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
178 print $form->parse_html_template('sepa/bank_transfer_search', { vc => $vc });
180 $main::lxdebug->leave_sub();
184 sub bank_transfer_list {
185 $main::lxdebug->enter_sub();
187 my $form = $main::form;
188 my $locale = $main::locale;
189 my $cgi = $::request->{cgi};
190 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
192 $form->{title} = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
194 $form->{sort} ||= 'id';
195 $form->{sortdir} = '1' if (!defined $form->{sortdir});
197 $form->{callback} = build_std_url('action=bank_transfer_list', 'sort', 'sortdir', 'vc');
199 my %filter = map +( $_ => $form->{"f_${_}"} ),
200 grep { $form->{"f_${_}"} }
202 map { ("${_}_date_from", "${_}_date_to") }
203 qw(export requested_execution execution));
204 $filter{executed} = $form->{l_executed} ? 1 : 0 if ($form->{l_executed} != $form->{l_not_executed});
205 $filter{closed} = $form->{l_closed} ? 1 : 0 if ($form->{l_open} != $form->{l_closed});
207 my $exports = SL::SEPA->list_exports('filter' => \%filter,
208 'sortorder' => $form->{sort},
209 'sortdir' => $form->{sortdir},
212 my $open_available = any { !$_->{closed} } @{ $exports };
214 my $report = SL::ReportGenerator->new(\%main::myconfig, $form);
216 my @hidden_vars = ('vc', grep { m/^[fl]_/ && $form->{$_} } keys %{ $form });
218 my $href = build_std_url('action=bank_transfer_list', @hidden_vars);
221 'selected' => { 'text' => $cgi->checkbox(-name => 'select_all', -id => 'select_all', -label => ''), },
222 'id' => { 'text' => $locale->text('Number'), },
223 'export_date' => { 'text' => $locale->text('Export date'), },
224 'employee' => { 'text' => $locale->text('Employee'), },
225 'executed' => { 'text' => $locale->text('Executed'), },
226 'closed' => { 'text' => $locale->text('Closed'), },
229 my @columns = qw(selected id export_date employee executed closed);
231 foreach my $name (qw(id export_date employee executed closed)) {
232 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
233 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
236 $column_defs{selected}->{visible} = $open_available ? 'HTML' : 0;
237 $column_defs{executed}->{visible} = $form->{l_executed} && $form->{l_not_executed} ? 1 : 0;
238 $column_defs{closed}->{visible} = $form->{l_closed} && $form->{l_open} ? 1 : 0;
241 push @options, ($vc eq 'customer' ? $::locale->text('Customer') : $locale->text('Vendor')) . ' : ' . $form->{f_vc} if ($form->{f_vc});
242 push @options, $locale->text('Invoice number') . ' : ' . $form->{f_invnumber} if ($form->{f_invnumber});
243 push @options, $locale->text('Export date from') . ' : ' . $form->{f_export_date_from} if ($form->{f_export_date_from});
244 push @options, $locale->text('Export date to') . ' : ' . $form->{f_export_date_to} if ($form->{f_export_date_to});
245 push @options, $locale->text('Requested execution date from') . ' : ' . $form->{f_requested_execution_date_from} if ($form->{f_requested_execution_date_from});
246 push @options, $locale->text('Requested execution date to') . ' : ' . $form->{f_requested_execution_date_to} if ($form->{f_requested_execution_date_to});
247 push @options, $locale->text('Execution date from') . ' : ' . $form->{f_execution_date_from} if ($form->{f_execution_date_from});
248 push @options, $locale->text('Execution date to') . ' : ' . $form->{f_execution_date_to} if ($form->{f_execution_date_to});
249 push @options, $form->{l_executed} ? $locale->text('executed') : $locale->text('not yet executed') if ($form->{l_executed} != $form->{l_not_executed});
250 push @options, $form->{l_closed} ? $locale->text('closed') : $locale->text('open') if ($form->{l_open} != $form->{l_closed});
252 $report->set_options('top_info_text' => join("\n", @options),
253 'raw_top_info_text' => $form->parse_html_template('sepa/bank_transfer_list_top'),
254 'raw_bottom_info_text' => $form->parse_html_template('sepa/bank_transfer_list_bottom', { 'show_buttons' => $open_available, vc => $vc }),
255 'std_column_visibility' => 1,
256 'output_format' => 'HTML',
257 'title' => $form->{title},
258 'attachment_basename' => $locale->text('banktransfers') . strftime('_%Y%m%d', localtime time),
260 $report->set_options_from_form();
261 $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
263 $report->set_columns(%column_defs);
264 $report->set_column_order(@columns);
265 $report->set_export_options('bank_transfer_list', @hidden_vars);
266 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
268 my $edit_url = build_std_url('action=bank_transfer_edit', 'callback');
270 foreach my $export (@{ $exports }) {
271 my $row = { map { $_ => { 'data' => $export->{$_} } } keys %{ $export } };
273 map { $row->{$_}->{data} = $export->{$_} ? $locale->text('yes') : $locale->text('no') } qw(executed closed);
275 $row->{id}->{link} = $edit_url . '&id=' . E($export->{id}) . '&vc=' . E($vc);
277 if (!$export->{closed}) {
278 $row->{selected}->{raw_data} =
279 $cgi->hidden(-name => "exports[+].id", -value => $export->{id})
280 . $cgi->checkbox(-name => "exports[].selected", -value => 1, -label => '');
283 $report->add_data($row);
286 $report->generate_with_headers();
288 $main::lxdebug->leave_sub();
291 sub bank_transfer_edit {
292 $main::lxdebug->enter_sub();
294 my $form = $main::form;
295 my $locale = $main::locale;
296 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
299 if (!$form->{mode} || ($form->{mode} eq 'single')) {
300 push @ids, $form->{id};
302 @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
305 $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
311 foreach my $id (@ids) {
312 my $curr_export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, 'vc' => $vc);
314 foreach my $item (@{ $curr_export->{items} }) {
315 map { $item->{"export_${_}"} = $curr_export->{$_} } grep { !ref $curr_export->{$_} } keys %{ $curr_export };
319 $export = $curr_export;
321 push @{ $export->{items} }, @{ $curr_export->{items} };
325 if ($form->{mode} && ($form->{mode} eq 'multi')) {
326 $export->{items} = [ grep { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} } ];
328 if (!@{ $export->{items} }) {
329 $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'), 'back_button' => 1);
333 $form->error($locale->text('That export does not exist.'));
336 $form->{title} = $locale->text('View SEPA export');
338 print $form->parse_html_template('sepa/bank_transfer_edit',
341 'current_date' => $form->current_date(\%main::myconfig),
342 'show_post_payments_button' => any { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} },
345 $main::lxdebug->leave_sub();
348 sub bank_transfer_post_payments {
349 $main::lxdebug->enter_sub();
351 my $form = $main::form;
352 my $locale = $main::locale;
353 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
355 my @items = grep { $_->{selected} } @{ $form->{items} || [] };
358 $form->show_generic_error($locale->text('You have not selected any item.'), 'back_button' => 1);
360 my @export_ids = uniq map { $_->{sepa_export_id} } @items;
361 my %exports = map { $_ => SL::SEPA->retrieve_export('id' => $_, 'details' => 1, vc => $vc) } @export_ids;
362 my @items_to_post = ();
364 foreach my $item (@items) {
365 my $export = $exports{ $item->{sepa_export_id} };
366 next if (!$export || $export->{closed} || $export->{executed});
368 push @items_to_post, $item if (none { ($_->{id} == $item->{id}) && $_->{executed} } @{ $export->{items} });
371 if (!@items_to_post) {
372 $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'), 'back_button' => 1);
375 if (any { !$_->{execution_date} } @items_to_post) {
376 $form->show_generic_error($locale->text('You have to specify an execution date for each antry.'), 'back_button' => 1);
379 SL::SEPA->post_payment('items' => \@items_to_post, vc => $vc);
381 $form->show_generic_information($locale->text('The payments have been posted.'));
383 $main::lxdebug->leave_sub();
386 sub bank_transfer_payment_list_as_pdf {
387 $main::lxdebug->enter_sub();
389 my $form = $main::form;
390 my %myconfig = %main::myconfig;
391 my $locale = $main::locale;
392 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
394 my @ids = @{ $form->{items} || [] };
395 my @export_ids = uniq map { $_->{export_id} } @ids;
397 $form->show_generic_error($locale->text('Multi mode not supported.'), 'back_button' => 1) if 1 != scalar @export_ids;
399 my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1, vc => $vc);
402 foreach my $id (@ids) {
403 my $item = first { $_->{id} == $id->{id} } @{ $export->{items} };
404 push @items, $item if $item;
407 $form->show_generic_error($locale->text('No transfers were executed in this export.'), 'back_button' => 1) if 1 > scalar @items;
409 my $report = SL::ReportGenerator->new(\%main::myconfig, $form);
412 'invnumber' => { 'text' => $locale->text('Invoice'), },
413 'vc_name' => { 'text' => $vc eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
414 'our_iban' => { 'text' => $vc eq 'customer' ? $locale->text('Destination IBAN') : $locale->text('Source IBAN'), },
415 'our_bic' => { 'text' => $vc eq 'customer' ? $locale->text('Destination BIC') : $locale->text('Source BIC'), },
416 'vc_iban' => { 'text' => $vc eq 'customer' ? $locale->text('Source IBAN') : $locale->text('Destination IBAN'), },
417 'vc_bic' => { 'text' => $vc eq 'customer' ? $locale->text('Source BIC') : $locale->text('Destination BIC'), },
418 'amount' => { 'text' => $locale->text('Amount'), },
419 'reference' => { 'text' => $locale->text('Reference'), },
420 'execution_date' => { 'text' => $locale->text('Execution date'), },
423 map { $column_defs{$_}->{align} = 'right' } qw(amount execution_date);
425 my @columns = qw(invnumber vc_name our_iban our_bic vc_iban vc_bic amount reference execution_date);
427 $report->set_options('std_column_visibility' => 1,
428 'output_format' => 'PDF',
429 '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}),
430 'attachment_basename' => ($vc eq 'customer' ? $locale->text('bank_collection_payment_list_#1', $export->{id}) : $locale->text('bank_transfer_payment_list_#1', $export->{id})) . strftime('_%Y%m%d', localtime time),
433 $report->set_columns(%column_defs);
434 $report->set_column_order(@columns);
436 foreach my $item (@items) {
437 my $row = { map { $_ => { 'data' => $item->{$_} } } @columns };
438 $row->{amount}->{data} = $form->format_amount(\%myconfig, $item->{amount}, 2);
440 $report->add_data($row);
443 $report->generate_with_headers();
445 $main::lxdebug->leave_sub();
449 sub bank_transfer_download_sepa_xml {
450 $main::lxdebug->enter_sub();
452 my $form = $main::form;
453 my $myconfig = \%main::myconfig;
454 my $locale = $main::locale;
455 my $cgi = $::request->{cgi};
456 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
457 my $defaults = SL::DB::Default->get;
459 if (!$defaults->company) {
460 $form->show_generic_error($locale->text('You have to enter a company name in the client configuration.'), 'back_button' => 1);
463 if (($vc eq 'customer') && !$defaults->sepa_creditor_id) {
464 $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID in the client configuration.'), 'back_button' => 1);
468 if ($form->{mode} && ($form->{mode} eq 'multi')) {
469 @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
472 @ids = ($form->{id});
476 $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
481 foreach my $id (@ids) {
482 my $export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, vc => $vc);
483 push @items, grep { !$_->{executed} } @{ $export->{items} } if ($export && !$export->{closed});
487 $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);
490 my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$);
492 my $sepa_xml = SL::SEPA::XML->new('company' => $defaults->company,
493 'creditor_id' => $defaults->sepa_creditor_id,
494 'src_charset' => 'UTF-8',
495 'message_id' => $message_id,
497 'collection' => $vc eq 'customer',
500 foreach my $item (@items) {
501 my $requested_execution_date;
503 if ($item->{requested_execution_date}) {
504 my ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{requested_execution_date});
505 $requested_execution_date = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
508 if ($vc eq 'customer') {
509 my ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{reference_date});
510 $item->{reference_date} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
511 $mandator_id = $item->{mandator_id};
512 if ($item->{mandate_date_of_signature}) {
513 ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{mandate_date_of_signature});
514 $item->{mandate_date_of_signature} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
518 $sepa_xml->add_transaction({ 'src_iban' => $item->{our_iban},
519 'src_bic' => $item->{our_bic},
520 'dst_iban' => $item->{vc_iban},
521 'dst_bic' => $item->{vc_bic},
522 'company' => $item->{vc_name},
523 'company_number' => $item->{vc_number},
524 'amount' => $item->{amount},
525 'reference' => $item->{reference},
526 'mandator_id' => $mandator_id,
527 'reference_date' => $item->{reference_date},
528 'execution_date' => $requested_execution_date,
529 'end_to_end_id' => $item->{end_to_end_id},
530 'date_of_signature' => $item->{mandate_date_of_signature}, });
533 my $xml = $sepa_xml->to_xml();
535 print $cgi->header('-type' => 'application/octet-stream',
536 '-content-disposition' => 'attachment; filename="SEPA_' . $message_id . ($vc eq 'customer' ? '.cdd' : '.cct') . '"',
537 '-content-length' => length $xml);
540 $main::lxdebug->leave_sub();
543 sub bank_transfer_mark_as_closed_step1 {
544 $main::lxdebug->enter_sub();
546 my $form = $main::form;
547 my $locale = $main::locale;
548 my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
550 my @export_ids = map { $_->{id} } grep { $_->{selected} } @{ $form->{exports} || [] };
553 $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
556 my @open_export_ids = ();
557 foreach my $id (@export_ids) {
558 my $export = SL::SEPA->retrieve_export('id' => $id, vc => $vc);
559 push @open_export_ids, $id if (!$export->{closed});
562 if (!@open_export_ids) {
563 $form->show_generic_error($locale->text('All of the exports you have selected were already closed.'), 'back_button' => 1);
566 $form->{title} = $locale->text('Close SEPA exports');
568 print $form->parse_html_template('sepa/bank_transfer_mark_as_closed_step1', { 'OPEN_EXPORT_IDS' => \@open_export_ids, vc => $vc });
570 $main::lxdebug->leave_sub();
573 sub bank_transfer_mark_as_closed_step2 {
574 $main::lxdebug->enter_sub();
576 my $form = $main::form;
577 my $locale = $main::locale;
579 map { SL::SEPA->close_export('id' => $_); } @{ $form->{open_export_ids} || [] };
581 $form->{title} = $locale->text('Close SEPA exports');
583 $form->show_generic_information($locale->text('The selected exports have been closed.'));
585 $main::lxdebug->leave_sub();
589 my $form = $main::form;
591 foreach my $action (qw(bank_transfer_create bank_transfer_edit bank_transfer_list
592 bank_transfer_post_payments bank_transfer_download_sepa_xml
593 bank_transfer_mark_as_closed_step1 bank_transfer_mark_as_closed_step2
594 bank_transfer_payment_list_as_pdf)) {
595 if ($form->{"action_${action}"}) {
601 $form->error($main::locale->text('No action defined.'));