Sammelcommit Bankerweiterung und Skonto
[kivitendo-erp.git] / bin / mozilla / sepa.pl
1 use strict;
2
3 use List::MoreUtils qw(any none uniq);
4 use List::Util qw(sum first);
5 use POSIX qw(strftime);
6
7 use Data::Dumper;
8 use SL::DB::BankAccount;
9 use SL::Chart;
10 use SL::CT;
11 use SL::Form;
12 use SL::GenericTranslations;
13 use SL::ReportGenerator;
14 use SL::SEPA;
15 use SL::SEPA::XML;
16
17 require "bin/mozilla/common.pl";
18 require "bin/mozilla/reportgenerator.pl";
19
20 sub bank_transfer_add {
21   $main::lxdebug->enter_sub();
22
23   my $form          = $main::form;
24   my $locale        = $main::locale;
25   my $vc            = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
26
27   $form->{title}    = $vc eq 'customer' ? $::locale->text('Prepare bank collection via SEPA XML') : $locale->text('Prepare bank transfer via SEPA XML');
28
29   my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
30
31   if (!scalar @{ $bank_accounts }) {
32     $form->error($locale->text('You have not added bank accounts yet.'));
33   }
34
35   my $invoices = SL::SEPA->retrieve_open_invoices(vc => $vc);
36
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();
41     return;
42   }
43
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 };
50
51   my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
52   my %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
53
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;
58   }
59
60   $form->header();
61   print $form->parse_html_template('sepa/bank_transfer_add',
62                                    { 'INVOICES'           => $invoices,
63                                      'BANK_ACCOUNTS'      => $bank_accounts,
64                                      'vc'                 => $vc,
65                                    });
66
67   $main::lxdebug->leave_sub();
68 }
69
70 sub bank_transfer_create {
71   $main::lxdebug->enter_sub();
72
73   my $form          = $main::form;
74   my $locale        = $main::locale;
75   my $myconfig      = \%main::myconfig;
76   my $vc            = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
77
78   $form->{title}    = $vc eq 'customer' ? $::locale->text('Create bank collection via SEPA XML') : $locale->text('Create bank transfer via SEPA XML');
79
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.'));
83   }
84
85   my $bank_account = SL::DB::Manager::BankAccount->find_by( id => $form->{bank_account} );
86
87   unless ( $bank_account ) {
88     $form->error($locale->text('The selected bank account does not exist anymore.'));
89   }
90
91   my $arap_id        = $vc eq 'customer' ? 'ar_id' : 'ap_id';
92   my $invoices       = SL::SEPA->retrieve_open_invoices(vc => $vc);
93
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 };
99   my @bank_transfers =
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} || [] };
104
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;
111       } else {
112         $type->{selected} = 0;
113       };
114     };
115   };
116
117   if (!scalar @bank_transfers) {
118     $form->error($locale->text('You have selected none of the invoices.'));
119   }
120
121   my $total_trans = sum map { $_->{open_amount} } @bank_transfers;
122
123   my ($vc_bank_info);
124   my $error_message;
125
126   my @bank_columns    = qw(iban bic);
127   push @bank_columns, qw(mandator_id mandate_date_of_signature) if $vc eq 'customer';
128
129   if ($form->{confirmation}) {
130     $vc_bank_info = { map { $_->{id} => $_ } @{ $form->{vc_bank_info} || [] } };
131
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.');
135         last;
136       }
137     }
138   }
139
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,
143                                                    'id' => \@vc_ids);
144     my @vc_bank_info           = sort { lc $a->{name} cmp lc $b->{name} } values %{ $vc_bank_info };
145
146     $form->header();
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,
153                                        'vc'                 => $vc,
154                                        'total_trans'        => $total_trans,
155                                      });
156
157   } else {
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->{$_};
162       }
163
164       $bank_transfer->{chart_id} = $bank_account->{chart_id};
165     }
166
167     my $id = SL::SEPA->create_export('employee'       => $::myconfig{login},
168                                      'bank_transfers' => \@bank_transfers,
169                                      'vc'             => $vc);
170
171     $form->header();
172     print $form->parse_html_template('sepa/bank_transfer_created', { 'id' => $id, 'vc' => $vc });
173   }
174
175   $main::lxdebug->leave_sub();
176 }
177
178 sub bank_transfer_search {
179   $main::lxdebug->enter_sub();
180
181   my $form   = $main::form;
182   my $locale = $main::locale;
183   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
184
185   $form->{title}    = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
186
187   $form->header();
188   print $form->parse_html_template('sepa/bank_transfer_search', { vc => $vc });
189
190   $main::lxdebug->leave_sub();
191 }
192
193
194 sub bank_transfer_list {
195   $main::lxdebug->enter_sub();
196
197   my $form   = $main::form;
198   my $locale = $main::locale;
199   my $cgi    = $::request->{cgi};
200   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
201
202   $form->{title}     = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
203
204   $form->{sort}    ||= 'id';
205   $form->{sortdir}   = '1' if (!defined $form->{sortdir});
206
207   $form->{callback}  = build_std_url('action=bank_transfer_list', 'sort', 'sortdir', 'vc');
208
209   my %filter         = map  +( $_ => $form->{"f_${_}"} ),
210                        grep  { $form->{"f_${_}"} }
211                              (qw(vc invnumber),
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});
216
217   my $exports        = SL::SEPA->list_exports('filter'    => \%filter,
218                                               'sortorder' => $form->{sort},
219                                               'sortdir'   => $form->{sortdir},
220                                               'vc'        => $vc);
221
222   my $open_available = any { !$_->{closed} } @{ $exports };
223
224   my $report         = SL::ReportGenerator->new(\%main::myconfig, $form);
225
226   my @hidden_vars    = ('vc', grep { m/^[fl]_/ && $form->{$_} } keys %{ $form });
227
228   my $href           = build_std_url('action=bank_transfer_list', @hidden_vars);
229
230   my %column_defs = (
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'), },
237   );
238
239   my @columns = qw(selected id export_date employee executed closed);
240
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";
244   }
245
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;
249
250   my @options = ();
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});
261
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),
269     );
270   $report->set_options_from_form();
271   $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
272
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});
277
278   my $edit_url = build_std_url('action=bank_transfer_edit', 'callback');
279
280   foreach my $export (@{ $exports }) {
281     my $row = { map { $_ => { 'data' => $export->{$_} } } keys %{ $export } };
282
283     map { $row->{$_}->{data} = $export->{$_} ? $locale->text('yes') : $locale->text('no') } qw(executed closed);
284
285     $row->{id}->{link} = $edit_url . '&id=' . E($export->{id}) . '&vc=' . E($vc);
286
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 => '');
291     }
292
293     $report->add_data($row);
294   }
295
296   $report->generate_with_headers();
297
298   $main::lxdebug->leave_sub();
299 }
300
301 sub bank_transfer_edit {
302   $main::lxdebug->enter_sub();
303
304   my $form   = $main::form;
305   my $locale = $main::locale;
306   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
307
308   my @ids    = ();
309   if (!$form->{mode} || ($form->{mode} eq 'single')) {
310     push @ids, $form->{id};
311   } else {
312     @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
313
314     if (!@ids) {
315       $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
316     }
317   }
318
319   my $export;
320
321   foreach my $id (@ids) {
322     my $curr_export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, 'vc' => $vc);
323
324     foreach my $item (@{ $curr_export->{items} }) {
325       map { $item->{"export_${_}"} = $curr_export->{$_} } grep { !ref $curr_export->{$_} } keys %{ $curr_export };
326     }
327
328     if (!$export) {
329       $export = $curr_export;
330     } else {
331       push @{ $export->{items} }, @{ $curr_export->{items} };
332     }
333   }
334
335   if ($form->{mode} && ($form->{mode} eq 'multi')) {
336     $export->{items} = [ grep { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} } ];
337
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);
340     }
341
342   } elsif (!$export) {
343     $form->error($locale->text('That export does not exist.'));
344   }
345
346   $form->{title}    = $locale->text('View SEPA export');
347   $form->header();
348   print $form->parse_html_template('sepa/bank_transfer_edit',
349                                    { 'ids'                       => \@ids,
350                                      'export'                    => $export,
351                                      'current_date'              => $form->current_date(\%main::myconfig),
352                                      'show_post_payments_button' => any { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} },
353                                    });
354
355   $main::lxdebug->leave_sub();
356 }
357
358 sub bank_transfer_post_payments {
359   $main::lxdebug->enter_sub();
360
361   my $form   = $main::form;
362   my $locale = $main::locale;
363   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
364
365   my @items  = grep { $_->{selected} } @{ $form->{items} || [] };
366
367   if (!@items) {
368     $form->show_generic_error($locale->text('You have not selected any item.'), 'back_button' => 1);
369   }
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 = ();
373
374   foreach my $item (@items) {
375     my $export = $exports{ $item->{sepa_export_id} };
376     next if (!$export || $export->{closed} || $export->{executed});
377
378     push @items_to_post, $item if (none { ($_->{id} == $item->{id}) && $_->{executed} } @{ $export->{items} });
379   }
380
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);
383   }
384
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);
387   }
388
389   SL::SEPA->post_payment('items' => \@items_to_post, vc => $vc);
390
391   $form->show_generic_information($locale->text('The payments have been posted.'));
392
393   $main::lxdebug->leave_sub();
394 }
395
396 sub bank_transfer_payment_list_as_pdf {
397   $main::lxdebug->enter_sub();
398
399   my $form       = $main::form;
400   my %myconfig   = %main::myconfig;
401   my $locale     = $main::locale;
402   my $vc         = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
403
404   my @ids        = @{ $form->{items} || [] };
405   my @export_ids = uniq map { $_->{export_id} } @ids;
406
407   $form->show_generic_error($locale->text('Multi mode not supported.'), 'back_button' => 1) if 1 != scalar @export_ids;
408
409   my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1, vc => $vc);
410   my @items  = ();
411
412   foreach my $id (@ids) {
413     my $item = first { $_->{id} == $id->{id} } @{ $export->{items} };
414     push @items, $item if $item;
415   }
416
417   $form->show_generic_error($locale->text('No transfers were executed in this export.'), 'back_button' => 1) if 1 > scalar @items;
418
419   my $report         =  SL::ReportGenerator->new(\%main::myconfig, $form);
420
421   my %column_defs    =  (
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'),                                                           },
431   );
432
433   map { $column_defs{$_}->{align} = 'right' } qw(amount execution_date);
434
435   my @columns        =  qw(invnumber vc_name our_iban our_bic vc_iban vc_bic amount reference execution_date);
436
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),
441     );
442
443   $report->set_columns(%column_defs);
444   $report->set_column_order(@columns);
445
446   foreach my $item (@items) {
447     my $row                = { map { $_ => { 'data' => $item->{$_} } } @columns };
448     $row->{amount}->{data} = $form->format_amount(\%myconfig, $item->{amount}, 2);
449
450     $report->add_data($row);
451   }
452
453   $report->generate_with_headers();
454
455   $main::lxdebug->leave_sub();
456 }
457
458 # TODO
459 sub bank_transfer_download_sepa_xml {
460   $main::lxdebug->enter_sub();
461
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;
468
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);
471   }
472
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);
475   }
476
477   my @ids;
478   if ($form->{mode} && ($form->{mode} eq 'multi')) {
479      @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
480
481   } else {
482     @ids = ($form->{id});
483   }
484
485   if (!@ids) {
486     $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
487   }
488
489   my @items = ();
490
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});
494   }
495
496   if (!@items) {
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);
498   }
499
500   my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$);
501
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,
506                                       'grouped'     => 1,
507                                       'collection'  => $vc eq 'customer',
508     );
509
510   foreach my $item (@items) {
511     my $requested_execution_date;
512     my $mandator_id;
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;
516     }
517
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;
525       }
526     }
527
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}, });
541   }
542
543   my $xml = $sepa_xml->to_xml();
544
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);
548   print $xml;
549
550   $main::lxdebug->leave_sub();
551 }
552
553 sub bank_transfer_mark_as_closed_step1 {
554   $main::lxdebug->enter_sub();
555
556   my $form       = $main::form;
557   my $locale     = $main::locale;
558   my $vc         = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
559
560   my @export_ids = map { $_->{id} } grep { $_->{selected} } @{ $form->{exports} || [] };
561
562   if (!@export_ids) {
563     $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
564   }
565
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});
570   }
571
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);
574   }
575
576   $form->{title} = $locale->text('Close SEPA exports');
577   $form->header();
578   print $form->parse_html_template('sepa/bank_transfer_mark_as_closed_step1', { 'OPEN_EXPORT_IDS' => \@open_export_ids, vc => $vc });
579
580   $main::lxdebug->leave_sub();
581 }
582
583 sub bank_transfer_mark_as_closed_step2 {
584   $main::lxdebug->enter_sub();
585
586   my $form       = $main::form;
587   my $locale     = $main::locale;
588
589   map { SL::SEPA->close_export('id' => $_); } @{ $form->{open_export_ids} || [] };
590
591   $form->{title} = $locale->text('Close SEPA exports');
592   $form->header();
593   $form->show_generic_information($locale->text('The selected exports have been closed.'));
594
595   $main::lxdebug->leave_sub();
596 }
597
598 sub dispatcher {
599   my $form = $main::form;
600
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}"}) {
606       call_sub($action);
607       return;
608     }
609   }
610
611   $form->error($main::locale->text('No action defined.'));
612 }
613
614 1;