item_selected: Multiselect bzw. Mengeneingabe beim Artikelselektor
[kivitendo-erp.git] / bin / mozilla / sepa.pl
index 518b9a8..c9db071 100755 (executable)
@@ -1,7 +1,7 @@
 use strict;
 
 use List::MoreUtils qw(any none uniq);
-use List::Util qw(first);
+use List::Util qw(sum first);
 use POSIX qw(strftime);
 
 use SL::BankAccount;
@@ -47,7 +47,7 @@ sub bank_transfer_add {
   # from us automatically and we don't have to send money manually.
   $_->{checked} = ($vc eq 'customer' ? $_->{direct_debit} : !$_->{direct_debit}) for @{ $invoices };
 
-  my $bank_account_label_sub = sub { $locale->text('Account number #1, bank code #2, #3', $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank}) };
+  my $bank_account_label_sub = sub { $locale->text('#1 - Account number #2, bank code #3, #4', $_[0]->{name}, $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank} ) };
 
   my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
   my %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
@@ -105,14 +105,19 @@ sub bank_transfer_create {
     $form->error($locale->text('You have selected none of the invoices.'));
   }
 
+  my $total_trans = sum map { $_->{open_amount} } @bank_transfers;
+
   my ($vc_bank_info);
   my $error_message;
 
+  my @bank_columns    = qw(iban bic);
+  push @bank_columns, qw(mandator_id mandate_date_of_signature) if $vc eq 'customer';
+
   if ($form->{confirmation}) {
     $vc_bank_info = { map { $_->{id} => $_ } @{ $form->{vc_bank_info} || [] } };
 
     foreach my $info (values %{ $vc_bank_info }) {
-      if (any { !$info->{$_} } qw(iban bic)) {
+      if (any { !$info->{$_} } @bank_columns) {
         $error_message = $locale->text('The bank information must not be empty.');
         last;
       }
@@ -125,9 +130,7 @@ sub bank_transfer_create {
                                                    'id' => \@vc_ids);
     my @vc_bank_info           = sort { lc $a->{name} cmp lc $b->{name} } values %{ $vc_bank_info };
 
-    my $bank_account_label_sub = sub { $locale->text('Account number #1, bank code #2, #3', $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank}) };
-
-    $form->{jsscript}          = 1;
+    my $bank_account_label_sub = sub { $locale->text('#1 - Account number #2, bank code #3, #4', $_[0]->{name}, $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank} ) };
 
     $form->header();
     print $form->parse_html_template('sepa/bank_transfer_create',
@@ -138,11 +141,12 @@ sub bank_transfer_create {
                                        'bank_account_label' => $bank_account_label_sub,
                                        'error_message'      => $error_message,
                                        'vc'                 => $vc,
+                                       'total_trans'        => $total_trans,
                                      });
 
   } else {
     foreach my $bank_transfer (@bank_transfers) {
-      foreach (qw(iban bic)) {
+      foreach (@bank_columns) {
         $bank_transfer->{"vc_${_}"}  = $vc_bank_info->{ $bank_transfer->{vc_id} }->{$_};
         $bank_transfer->{"our_${_}"} = $bank_account->{$_};
       }
@@ -150,7 +154,7 @@ sub bank_transfer_create {
       $bank_transfer->{chart_id} = $bank_account->{chart_id};
     }
 
-    my $id = SL::SEPA->create_export('employee'       => $form->{login},
+    my $id = SL::SEPA->create_export('employee'       => $::myconfig{login},
                                      'bank_transfers' => \@bank_transfers,
                                      'vc'             => $vc);
 
@@ -169,7 +173,6 @@ sub bank_transfer_search {
   my $vc     = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
 
   $form->{title}    = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
-  $form->{jsscript} = 1;
 
   $form->header();
   print $form->parse_html_template('sepa/bank_transfer_search', { vc => $vc });
@@ -330,7 +333,6 @@ sub bank_transfer_edit {
     $form->error($locale->text('That export does not exist.'));
   }
 
-  $form->{jsscript} = 1;
   $form->{title}    = $locale->text('View SEPA export');
   $form->header();
   print $form->parse_html_template('sepa/bank_transfer_edit',
@@ -452,13 +454,14 @@ sub bank_transfer_download_sepa_xml {
   my $locale   =  $main::locale;
   my $cgi      =  $::request->{cgi};
   my $vc       = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
+  my $defaults = SL::DB::Default->get;
 
-  if (!$myconfig->{company}) {
-    $form->show_generic_error($locale->text('You have to enter a company name in your user preferences (see the "Program" menu, "Preferences").'), 'back_button' => 1);
+  if (!$defaults->company) {
+    $form->show_generic_error($locale->text('You have to enter a company name in the client configuration.'), 'back_button' => 1);
   }
 
-  if (($vc eq 'customer') && !$myconfig->{sepa_creditor_id}) {
-    $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID in your user preferences (see the "Program" menu, "Preferences").'), 'back_button' => 1);
+  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);
   }
 
   my @ids;
@@ -486,9 +489,9 @@ sub bank_transfer_download_sepa_xml {
 
   my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$);
 
-  my $sepa_xml   = SL::SEPA::XML->new('company'     => $myconfig->{company},
-                                      'creditor_id' => $myconfig->{sepa_creditor_id},
-                                      'src_charset' => $::lx_office_conf{system}->{dbcharset} || 'ISO-8859-15',
+  my $sepa_xml   = SL::SEPA::XML->new('company'     => $defaults->company,
+                                      'creditor_id' => $defaults->sepa_creditor_id,
+                                      'src_charset' => 'UTF-8',
                                       'message_id'  => $message_id,
                                       'grouped'     => 1,
                                       'collection'  => $vc eq 'customer',
@@ -496,6 +499,7 @@ sub bank_transfer_download_sepa_xml {
 
   foreach my $item (@items) {
     my $requested_execution_date;
+    my $mandator_id;
     if ($item->{requested_execution_date}) {
       my ($yy, $mm, $dd)        = $locale->parse_date($myconfig, $item->{requested_execution_date});
       $requested_execution_date = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
@@ -504,6 +508,11 @@ sub bank_transfer_download_sepa_xml {
     if ($vc eq 'customer') {
       my ($yy, $mm, $dd)      = $locale->parse_date($myconfig, $item->{reference_date});
       $item->{reference_date} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
+      $mandator_id = $item->{mandator_id};
+      if ($item->{mandate_date_of_signature}) {
+        ($yy, $mm, $dd)                    = $locale->parse_date($myconfig, $item->{mandate_date_of_signature});
+        $item->{mandate_date_of_signature} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
+      }
     }
 
     $sepa_xml->add_transaction({ 'src_iban'       => $item->{our_iban},
@@ -511,11 +520,14 @@ sub bank_transfer_download_sepa_xml {
                                  'dst_iban'       => $item->{vc_iban},
                                  'dst_bic'        => $item->{vc_bic},
                                  'company'        => $item->{vc_name},
+                                 'company_number' => $item->{vc_number},
                                  'amount'         => $item->{amount},
                                  'reference'      => $item->{reference},
+                                 'mandator_id'    => $mandator_id,
                                  'reference_date' => $item->{reference_date},
                                  'execution_date' => $requested_execution_date,
-                                 'end_to_end_id'  => $item->{end_to_end_id} });
+                                 'end_to_end_id'  => $item->{end_to_end_id},
+                                 'date_of_signature' => $item->{mandate_date_of_signature}, });
   }
 
   my $xml = $sepa_xml->to_xml();