Stichtagsbezogene Auswertungen von offenen Forderungen und Verbindlichkeiten
[kivitendo-erp.git] / SL / Form.pm
index 98fd7b2..a6ce5af 100644 (file)
@@ -53,7 +53,7 @@ use SL::Menu;
 use SL::Template;
 use SL::User;
 use Template;
-use List::Util qw(max min sum);
+use List::Util qw(first max min sum);
 
 my $standard_dbh;
 
@@ -376,53 +376,6 @@ sub unquote {
 
 }
 
-sub quote_html {
-  $main::lxdebug->enter_sub(2);
-
-  my ($self, $str) = @_;
-
-  my %replace =
-    ('order' => ['&', '"', '<', '>'],
-     '<'     => '&lt;',
-     '>'     => '&gt;',
-     '"'     => '&quot;',
-     '&'     => '&amp;',
-    );
-
-  map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
-
-  $main::lxdebug->leave_sub(2);
-
-  return $str;
-}
-
-sub unquote_html {
-  $main::lxdebug->enter_sub(2);
-
-  my ($self, $str) = @_;
-
-  my %replace  =
-    ('&auml;'  => 'ä',
-     '&ouml;'  => 'ö',
-     '&uuml;'  => 'ü',
-     '&Auml;'  => 'Ä',
-     '&Ouml;'  => 'Ö',
-     '&Uuml;'  => 'Ü',
-     '&szlig;' => 'ß',
-     '&gt;'    => '>',
-     '&lt;'    => '<',
-     '&quot;'  => '"',
-    );
-
-  map { $str =~ s/\Q$_\E/$replace{$_}/g; } keys %replace;
-  $str =~ s/\&amp;/\&/g;
-
-  $main::lxdebug->leave_sub(2);
-
-  return $str;
-}
-
-
 sub hide_form {
   my $self = shift;
 
@@ -528,6 +481,48 @@ sub isblank {
   $main::lxdebug->leave_sub();
 }
 
+sub create_http_response {
+  $main::lxdebug->enter_sub();
+
+  my $self     = shift;
+  my %params   = @_;
+
+  my $cgi      = $main::cgi;
+  $cgi       ||= CGI->new('');
+
+  my $base_path;
+
+  if ($ENV{HTTP_X_FORWARDED_FOR}) {
+    $base_path =  $ENV{HTTP_REFERER};
+    $base_path =~ s|^.*?://.*?/|/|;
+  } else {
+    $base_path =  $ENV{REQUEST_URI};
+  }
+  $base_path =~ s|[^/]+$||;
+  $base_path =~ s|/$||;
+
+  my $session_cookie;
+  if (defined $main::auth) {
+    my $session_cookie_value   = $main::auth->get_session_id();
+    $session_cookie_value    ||= 'NO_SESSION';
+
+    $session_cookie = $cgi->cookie('-name'  => $main::auth->get_session_cookie_name(),
+                                   '-value' => $session_cookie_value,
+                                   '-path'  => $base_path);
+  }
+
+  my %cgi_params = ('-type' => $params{content_type});
+  $cgi_params{'-charset'} = $params{charset} if ($parmas{charset});
+
+  my $output = $cgi->header('-cookie' => $session_cookie,
+                            %cgi_params);
+
+  $main::lxdebug->leave_sub();
+
+  return $output;
+}
+
+
 sub header {
   $main::lxdebug->enter_sub();
 
@@ -538,9 +533,6 @@ sub header {
     return;
   }
 
-  my $cgi   = $main::cgi;
-  $cgi    ||= CGI->new('');
-
   my ($stylesheet, $favicon);
 
   if ($ENV{HTTP_USER_AGENT}) {
@@ -602,30 +594,8 @@ sub header {
       $ajax .= $item->show_javascript();
     }
 
-    my $base_path;
-
-    if ($ENV{HTTP_X_FORWARDED_FOR}) {
-      $base_path =  $ENV{HTTP_REFERER};
-      $base_path =~ s|^.*?://.*?/|/|;
-    } else {
-      $base_path =  $ENV{REQUEST_URI};
-    }
-    $base_path =~ s|[^/]+$||;
-    $base_path =~ s|/$||;
-
-    my $session_cookie;
-    if (defined $main::auth) {
-      my $session_cookie_value   = $main::auth->get_session_id();
-      $session_cookie_value    ||= 'NO_SESSION';
-
-      $session_cookie = $cgi->cookie('-name'  => $main::auth->get_session_cookie_name(),
-                                     '-value' => $session_cookie_value,
-                                     '-path'  => $base_path);
-    }
-
-    print $cgi->header('-type'    => 'text/html',
-                       '-charset' => $db_charset,
-                       '-cookie'  => $session_cookie);
+    print $self->create_http_response('content_type' => 'text/html',
+                                      'charset'      => $db_charset,);
     print qq|${doctype}<html>
 <head>
   <title>$self->{titlebar}</title>
@@ -772,23 +742,26 @@ sub parse_html_template {
 }
 
 sub show_generic_error {
-  my ($self, $error, $title, $action) = @_;
+  my ($self, $error, %params) = @_;
 
   my $add_params = {
-    'title_error' => $title,
+    'title_error' => $params{title},
     'label_error' => $error,
   };
 
-  my @vars;
-  if ($action) {
-    map({ delete($self->{$_}); } qw(action));
-    map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
-            if (!ref($self->{$_})); }
-        keys(%{$self}));
-    $add_params->{"SHOW_BUTTON"} = 1;
-    $add_params->{"BUTTON_LABEL"} = $action;
+  if ($params{action}) {
+    my @vars;
+
+    map { delete($self->{$_}); } qw(action);
+    map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
+
+    $add_params->{SHOW_BUTTON}  = 1;
+    $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
+    $add_params->{VARIABLES}    = \@vars;
+
+  } elsif ($params{back_button}) {
+    $add_params->{SHOW_BACK_BUTTON} = 1;
   }
-  $add_params->{"VARIABLES"} = \@vars;
 
   $self->{title} = $title if ($title);
 
@@ -1254,43 +1227,59 @@ sub get_formname_translation {
   $formname ||= $self->{formname};
 
   my %formname_translations = (
-    bin_list            => $main::locale->text('Bin List'),
-    credit_note         => $main::locale->text('Credit Note'),
-    invoice             => $main::locale->text('Invoice'),
-    packing_list        => $main::locale->text('Packing List'),
-    pick_list           => $main::locale->text('Pick List'),
-    proforma            => $main::locale->text('Proforma Invoice'),
-    purchase_order      => $main::locale->text('Purchase Order'),
-    request_quotation   => $main::locale->text('RFQ'),
-    sales_order         => $main::locale->text('Confirmation'),
-    sales_quotation     => $main::locale->text('Quotation'),
-    storno_invoice      => $main::locale->text('Storno Invoice'),
-    storno_packing_list => $main::locale->text('Storno Packing List'),
+    bin_list                => $main::locale->text('Bin List'),
+    credit_note             => $main::locale->text('Credit Note'),
+    invoice                 => $main::locale->text('Invoice'),
+    packing_list            => $main::locale->text('Packing List'),
+    pick_list               => $main::locale->text('Pick List'),
+    proforma                => $main::locale->text('Proforma Invoice'),
+    purchase_order          => $main::locale->text('Purchase Order'),
+    request_quotation       => $main::locale->text('RFQ'),
+    sales_order             => $main::locale->text('Confirmation'),
+    sales_quotation         => $main::locale->text('Quotation'),
+    storno_invoice          => $main::locale->text('Storno Invoice'),
+    storno_packing_list     => $main::locale->text('Storno Packing List'),
+    sales_delivery_order    => $main::locale->text('Delivery Order'),
+    purchase_delivery_order => $main::locale->text('Delivery Order'),
   );
 
   return $formname_translations{$formname}
 }
 
+sub get_number_prefix_for_type {
+  my ($self) = @_;
+
+  my $prefix =
+      (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
+    : ($self->{type} =~ /_quotation$/)                        ? 'quo'
+    : ($self->{type} =~ /_delivery_order$/)                   ? 'do'
+    :                                                           'ord';
+
+  return $prefix;
+}
+
+sub get_extension_for_format {
+  my ($self)    = @_;
+
+  my $extension = $self->{format} =~ /pdf/i          ? ".pdf"
+                : $self->{format} =~ /postscript/i   ? ".ps"
+                : $self->{format} =~ /opendocument/i ? ".odt"
+                : $self->{format} =~ /html/i         ? ".html"
+                :                                      "";
+
+  return $extension;
+}
+
 sub generate_attachment_filename {
   my ($self) = @_;
 
-  my $attachment_filename = $self->unquote_html($self->get_formname_translation());
-  my $prefix = 
-      (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
-    : ($self->{"type"} =~ /_quotation$/)                       ? "quo"
-    :                                                            "ord";
+  my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
+  my $prefix              = $self->get_number_prefix_for_type();
 
   if ($attachment_filename && $self->{"${prefix}number"}) {
-    $attachment_filename .= "_" . $self->{"${prefix}number"}
-                            . (  $self->{format} =~ /pdf/i          ? ".pdf"
-                               : $self->{format} =~ /postscript/i   ? ".ps"
-                               : $self->{format} =~ /opendocument/i ? ".odt"
-                               : $self->{format} =~ /html/i         ? ".html"
-                               :                                      "");
-    $attachment_filename =~ s/ /_/g;
-    my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue", 
-                    "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
-    map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
+    $attachment_filename .=  "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
+    $attachment_filename  =  $main::locale->quote_special_chars('filenames', $attachment_filename);
+    $attachment_filename  =~ s|[\s/\\]+|_|g;
   } else {
     $attachment_filename = "";
   }
@@ -1298,6 +1287,19 @@ sub generate_attachment_filename {
   return $attachment_filename;
 }
 
+sub generate_email_subject {
+  my ($self) = @_;
+
+  my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
+  my $prefix  = $self->get_number_prefix_for_type();
+
+  if ($subject && $self->{"${prefix}number"}) {
+    $subject .= " " . $self->{"${prefix}number"}
+  }
+
+  return $subject;
+}
+
 sub cleanup {
   $main::lxdebug->enter_sub();
 
@@ -2213,6 +2215,20 @@ sub _get_simple {
   $main::lxdebug->leave_sub();
 }
 
+sub _get_groups {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $dbh, $key) = @_;
+
+  $key ||= "all_groups";
+
+  my $groups = $main::auth->read_groups();
+
+  $self->{$key} = selectall_hashref_query($self, $dbh, $query);
+
+  $main::lxdebug->leave_sub();
+}
+
 sub get_lists {
   $main::lxdebug->enter_sub();
 
@@ -2311,6 +2327,10 @@ sub get_lists {
     $self->_get_warehouses($dbh, $params{warehouses});
   }
 
+  if ($params{groups}) {
+    $self->_get_groups($dbh, $params{groups});
+  }
+
   $main::lxdebug->leave_sub();
 }