Proformarechnung geht bei Drucken und Buchen verloren
[kivitendo-erp.git] / bin / mozilla / sepa.pl
index ac018e2..78b5eea 100755 (executable)
@@ -6,6 +6,7 @@ use POSIX qw(strftime);
 
 use Data::Dumper;
 use SL::DB::BankAccount;
+use SL::DB::SepaExport;
 use SL::Chart;
 use SL::CT;
 use SL::Form;
@@ -23,6 +24,7 @@ sub bank_transfer_add {
   my $form          = $main::form;
   my $locale        = $main::locale;
   my $vc            = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
+  my $vc_no         = $form->{vc} eq 'customer' ? $::locale->text('VN') : $::locale->text('CN');
 
   $form->{title}    = $vc eq 'customer' ? $::locale->text('Prepare bank collection via SEPA XML') : $locale->text('Prepare bank transfer via SEPA XML');
 
@@ -55,6 +57,13 @@ sub bank_transfer_add {
     my $prefix                    = $translations{ $invoice->{language_id} } || $translations{default} || $::locale->text('Invoice');
     $prefix                      .= ' ' unless $prefix =~ m/ $/;
     $invoice->{reference_prefix}  = $prefix;
+
+    # add c_vendor_id or v_vendor_id as a prefix if a entry exists
+    next unless $invoice->{vc_vc_id};
+
+    my $prefix_vc_number             = $translations{ $invoice->{language_id} } || $translations{default} || $vc_no;
+    $prefix_vc_number               .= ' ' unless $prefix_vc_number =~ m/ $/;
+    $invoice->{reference_prefix_vc}  = ' '  . $prefix_vc_number unless $prefix_vc_number =~ m/^ /;
   }
 
   $form->header();
@@ -208,7 +217,7 @@ sub bank_transfer_list {
 
   my %filter         = map  +( $_ => $form->{"f_${_}"} ),
                        grep  { $form->{"f_${_}"} }
-                             (qw(vc invnumber),
+                             (qw(vc invnumber message_id),
                               map { ("${_}_date_from", "${_}_date_to") }
                                   qw(export requested_execution execution));
   $filter{executed}  = $form->{l_executed} ? 1 : 0 if ($form->{l_executed} != $form->{l_not_executed});
@@ -236,9 +245,10 @@ sub bank_transfer_list {
     'closed'      => { 'text' => $locale->text('Closed'), },
     num_invoices  => { 'text' => $locale->text('Number of invoices'), },
     sum_amounts   => { 'text' => $locale->text('Sum of all amounts'), },
+    message_ids   => { 'text' => $locale->text('SEPA message IDs'), },
   );
 
-  my @columns = qw(selected id export_date employee executed closed invoices netamount);
+  my @columns = qw(selected id export_date employee executed closed num_invoices sum_amounts message_ids);
   my %column_alignment = map { ($_ => 'right') } qw(num_invoices sum_amounts);
 
   foreach my $name (qw(id export_date employee executed closed)) {
@@ -254,6 +264,7 @@ sub bank_transfer_list {
   my @options = ();
   push @options, ($vc eq 'customer' ? $::locale->text('Customer') : $locale->text('Vendor')) . ' : ' . $form->{f_vc} if ($form->{f_vc});
   push @options, $locale->text('Invoice number')                . ' : ' . $form->{f_invnumber}                     if ($form->{f_invnumber});
+  push @options, $locale->text('SEPA message ID')               . ' : ' . $form->{f_message_id}                    if (length $form->{f_message_id});
   push @options, $locale->text('Export date from')              . ' : ' . $form->{f_export_date_from}              if ($form->{f_export_date_from});
   push @options, $locale->text('Export date to')                . ' : ' . $form->{f_export_date_to}                if ($form->{f_export_date_to});
   push @options, $locale->text('Requested execution date from') . ' : ' . $form->{f_requested_execution_date_from} if ($form->{f_requested_execution_date_from});
@@ -318,7 +329,7 @@ sub bank_transfer_edit {
     @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] };
 
     if (!@ids) {
-      $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
+      $form->show_generic_error($locale->text('You have not selected any export.'));
     }
   }
 
@@ -342,7 +353,7 @@ sub bank_transfer_edit {
     $export->{items} = [ grep { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} } ];
 
     if (!@{ $export->{items} }) {
-      $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);
+      $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'));
     }
 
   } elsif (!$export) {
@@ -371,7 +382,7 @@ sub bank_transfer_post_payments {
   my @items  = grep { $_->{selected} } @{ $form->{items} || [] };
 
   if (!@items) {
-    $form->show_generic_error($locale->text('You have not selected any item.'), 'back_button' => 1);
+    $form->show_generic_error($locale->text('You have not selected any item.'));
   }
   my @export_ids    = uniq map { $_->{sepa_export_id} } @items;
   my %exports       = map { $_ => SL::SEPA->retrieve_export('id' => $_, 'details' => 1, vc => $vc) } @export_ids;
@@ -385,11 +396,11 @@ sub bank_transfer_post_payments {
   }
 
   if (!@items_to_post) {
-    $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);
+    $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'));
   }
 
   if (any { !$_->{execution_date} } @items_to_post) {
-    $form->show_generic_error($locale->text('You have to specify an execution date for each antry.'), 'back_button' => 1);
+    $form->show_generic_error($locale->text('You have to specify an execution date for each antry.'));
   }
 
   SL::SEPA->post_payment('items' => \@items_to_post, vc => $vc);
@@ -410,7 +421,7 @@ sub bank_transfer_payment_list_as_pdf {
   my @ids        = @{ $form->{items} || [] };
   my @export_ids = uniq map { $_->{export_id} } @ids;
 
-  $form->show_generic_error($locale->text('Multi mode not supported.'), 'back_button' => 1) if 1 != scalar @export_ids;
+  $form->show_generic_error($locale->text('Multi mode not supported.')) if 1 != scalar @export_ids;
 
   my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1, vc => $vc);
   my @items  = ();
@@ -420,7 +431,7 @@ sub bank_transfer_payment_list_as_pdf {
     push @items, $item if $item;
   }
 
-  $form->show_generic_error($locale->text('No transfers were executed in this export.'), 'back_button' => 1) if 1 > scalar @items;
+  $form->show_generic_error($locale->text('No transfers were executed in this export.')) if 1 > scalar @items;
 
   my $report         =  SL::ReportGenerator->new(\%main::myconfig, $form);
 
@@ -473,11 +484,11 @@ sub bank_transfer_download_sepa_xml {
   my $defaults = SL::DB::Default->get;
 
   if (!$defaults->company) {
-    $form->show_generic_error($locale->text('You have to enter a company name in the client configuration.'), 'back_button' => 1);
+    $form->show_generic_error($locale->text('You have to enter a company name in the client configuration.'));
   }
 
   if (($vc eq 'customer') && !$defaults->sepa_creditor_id) {
-    $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID in the client configuration.'), 'back_button' => 1);
+    $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID in the client configuration.'));
   }
 
   my @ids;
@@ -489,7 +500,7 @@ sub bank_transfer_download_sepa_xml {
   }
 
   if (!@ids) {
-    $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
+    $form->show_generic_error($locale->text('You have not selected any export.'));
   }
 
   my @items = ();
@@ -500,7 +511,7 @@ sub bank_transfer_download_sepa_xml {
   }
 
   if (!@items) {
-    $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);
+    $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'));
   }
 
   my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$);
@@ -546,6 +557,15 @@ sub bank_transfer_download_sepa_xml {
                                  'date_of_signature' => $item->{mandate_date_of_signature}, });
   }
 
+  # Store the message ID used in each of the entries in order to
+  # facilitate finding them by looking at bank statements.
+  foreach my $id (@ids) {
+    SL::DB::SepaExportMessageId->new(
+      sepa_export_id => $id,
+      message_id     => $message_id,
+    )->save;
+  }
+
   my $xml = $sepa_xml->to_xml();
 
   print $cgi->header('-type'                => 'application/octet-stream',
@@ -566,7 +586,7 @@ sub bank_transfer_mark_as_closed_step1 {
   my @export_ids = map { $_->{id} } grep { $_->{selected} } @{ $form->{exports} || [] };
 
   if (!@export_ids) {
-    $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1);
+    $form->show_generic_error($locale->text('You have not selected any export.'));
   }
 
   my @open_export_ids = ();
@@ -576,7 +596,7 @@ sub bank_transfer_mark_as_closed_step1 {
   }
 
   if (!@open_export_ids) {
-    $form->show_generic_error($locale->text('All of the exports you have selected were already closed.'), 'back_button' => 1);
+    $form->show_generic_error($locale->text('All of the exports you have selected were already closed.'));
   }
 
   $form->{title} = $locale->text('Close SEPA exports');