Debugs in SL/Form.pm eingefuegt fuer konsistentes Tracen
[kivitendo-erp.git] / SL / Form.pm
index 355d60a..456cd2e 100644 (file)
@@ -70,12 +70,10 @@ END {
 sub _store_value {
   $main::lxdebug->enter_sub(2);
 
-  my $self  = shift;
+  my $curr  = shift;
   my $key   = shift;
   my $value = shift;
 
-  my $curr  = $self;
-
   while ($key =~ /\[\+?\]\.|\./) {
     substr($key, 0, $+[0]) = '';
 
@@ -103,14 +101,14 @@ sub _store_value {
 sub _input_to_hash {
   $main::lxdebug->enter_sub(2);
 
-  my $self  = shift;
-  my $input = shift;
+  my $params = shift;
+  my $input  = shift;
 
-  my @pairs = split(/&/, $input);
+  my @pairs  = split(/&/, $input);
 
   foreach (@pairs) {
     my ($key, $value) = split(/=/, $_, 2);
-    $self->_store_value($self->unescape($key), $self->unescape($value));
+    _store_value($params, unescape(undef, $key), unescape(undef, $value));
   }
 
   $main::lxdebug->leave_sub(2);
@@ -119,13 +117,13 @@ sub _input_to_hash {
 sub _request_to_hash {
   $main::lxdebug->enter_sub(2);
 
-  my $self  = shift;
-  my $input = shift;
+  my $params = shift;
+  my $input  = shift;
 
   if (!$ENV{'CONTENT_TYPE'}
       || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
 
-    $self->_input_to_hash($input);
+    _input_to_hash($params, $input);
 
     $main::lxdebug->leave_sub(2);
     return;
@@ -173,8 +171,8 @@ sub _request_to_hash {
           substr $line, $-[0], $+[0] - $-[0], "";
         }
 
-        $previous         = $self->_store_value($name, '');
-        $self->{FILENAME} = $filename if ($filename);
+        $previous           = _store_value($params, $name, '');
+        $params->{FILENAME} = $filename if ($filename);
 
         next;
       }
@@ -196,6 +194,31 @@ sub _request_to_hash {
   $main::lxdebug->leave_sub(2);
 }
 
+sub _recode_recursively {
+  $main::lxdebug->enter_sub();
+  my ($iconv, $param) = @_;
+
+  if (ref $param eq 'HASH') {
+    foreach my $key (keys %{ $param }) {
+      if (!ref $param->{$key}) {
+        $param->{$key} = $iconv->convert($param->{$key});
+      } else {
+        _recode_recursively($iconv, $param->{$key});
+      }
+    }
+
+  } elsif (ref $param eq 'ARRAY') {
+    foreach my $idx (0 .. scalar(@{ $param }) - 1) {
+      if (!ref $param->[$idx]) {
+        $param->[$idx] = $iconv->convert($param->[$idx]);
+      } else {
+        _recode_recursively($iconv, $param->[$idx]);
+      }
+    }
+  }
+  $main::lxdebug->leave_sub();
+}
+
 sub new {
   $main::lxdebug->enter_sub();
 
@@ -220,12 +243,27 @@ sub new {
 
   bless $self, $type;
 
-  $self->_request_to_hash($_);
+  my $parameters = { };
+  _request_to_hash($parameters, $_);
+
+  my $db_charset   = $main::dbcharset;
+  $db_charset    ||= Common::DEFAULT_CHARSET;
+
+  if ($parameters->{INPUT_ENCODING} && (lc $parameters->{INPUT_ENCODING} ne $db_charset)) {
+    require Text::Iconv;
+    my $iconv = Text::Iconv->new($parameters->{INPUT_ENCODING}, $db_charset);
+
+    _recode_recursively($iconv, $parameters);
+
+    delete $parameters{INPUT_ENCODING};
+  }
+
+  map { $self->{$_} = $parameters->{$_}; } keys %{ $parameters };
 
   $self->{action}  =  lc $self->{action};
   $self->{action}  =~ s/( |-|,|\#)/_/g;
 
-  $self->{version} =  "2.6.0 beta 1";
+  $self->{version} =  "2.6.0 beta 2";
 
   $main::lxdebug->leave_sub();
 
@@ -358,6 +396,7 @@ sub unescape {
 }
 
 sub quote {
+  $main::lxdebug->enter_sub();
   my ($self, $str) = @_;
 
   if ($str && !ref($str)) {
@@ -366,9 +405,11 @@ sub quote {
 
   $str;
 
+  $main::lxdebug->leave_sub();
 }
 
 sub unquote {
+  $main::lxdebug->enter_sub();
   my ($self, $str) = @_;
 
   if ($str && !ref($str)) {
@@ -377,9 +418,11 @@ sub unquote {
 
   $str;
 
+  $main::lxdebug->leave_sub();
 }
 
 sub hide_form {
+  $main::lxdebug->enter_sub();
   my $self = shift;
 
   if (@_) {
@@ -390,7 +433,7 @@ sub hide_form {
       print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
     }
   }
-
+  $main::lxdebug->leave_sub();
 }
 
 sub error {
@@ -943,9 +986,9 @@ sub format_amount {
   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
   $amount = do {
-    ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
-    ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
-                        ($neg ? "-$amount"   : "$amount" )    ;
+    ($dash =~ /-/)    ? ($neg ? "($amount)"                            : "$amount" )                              :
+    ($dash =~ /DRCR/) ? ($neg ? "$amount " . $main::locale->text('DR') : "$amount " . $main::locale->text('CR') ) :
+                        ($neg ? "-$amount"                             : "$amount" )                              ;
   };
 
 
@@ -1093,25 +1136,37 @@ sub parse_template {
   $self->{"cwd"} = getcwd();
   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
+  my $ext_for_format;
+
   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
-    $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
+    $template       = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
+    $ext_for_format = 'odt';
+
   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
-    $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
-  } elsif (($self->{"format"} =~ /html/i) ||
-           (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
-    $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
-  } elsif (($self->{"format"} =~ /xml/i) ||
-             (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
-    $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
+    $template         = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
+    $ext_for_format   = 'pdf';
+
+  } elsif (($self->{"format"} =~ /html/i) || (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
+    $template       = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
+    $ext_for_format = 'html';
+
+  } elsif (($self->{"format"} =~ /xml/i) || (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
+    $template       = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
+    $ext_for_format = 'xml';
+
   } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
+
   } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
+
   } elsif ( defined $self->{'format'}) {
     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
+
   } elsif ( $self->{'format'} eq '' ) {
     $self->error("No Outputformat given: $self->{'format'}");
+
   } else { #Catch the rest
     $self->error("Outputformat not defined: $self->{'format'}");
   }
@@ -1196,10 +1251,10 @@ sub parse_template {
       } else {
 
         if (!$self->{"do_not_attach"}) {
-          @{ $mail->{attachments} } =
-            ({ "filename" => $self->{"tmpfile"},
-               "name" => $self->{"attachment_filename"} ?
-                 $self->{"attachment_filename"} : $self->{"tmpfile"} });
+          my $attachment_name  =  $self->{attachment_filename} || $self->{tmpfile};
+          $attachment_name     =~ s/\.(.+?)$/.${ext_for_format}/ if ($ext_for_format);
+          $mail->{attachments} =  [{ "filename" => $self->{tmpfile},
+                                     "name"     => $attachment_name }];
         }
 
         $mail->{message}  =~ s/\r//g;
@@ -1264,6 +1319,7 @@ Content-Length: $numbytes
 }
 
 sub get_formname_translation {
+  $main::lxdebug->enter_sub();
   my ($self, $formname) = @_;
 
   $formname ||= $self->{formname};
@@ -1285,10 +1341,12 @@ sub get_formname_translation {
     purchase_delivery_order => $main::locale->text('Delivery Order'),
   );
 
+  $main::lxdebug->leave_sub();
   return $formname_translations{$formname}
 }
 
 sub get_number_prefix_for_type {
+  $main::lxdebug->enter_sub();
   my ($self) = @_;
 
   my $prefix =
@@ -1297,10 +1355,12 @@ sub get_number_prefix_for_type {
     : ($self->{type} =~ /_delivery_order$/)                   ? 'do'
     :                                                           'ord';
 
+  $main::lxdebug->leave_sub();
   return $prefix;
 }
 
 sub get_extension_for_format {
+  $main::lxdebug->enter_sub();
   my ($self)    = @_;
 
   my $extension = $self->{format} =~ /pdf/i          ? ".pdf"
@@ -1309,10 +1369,12 @@ sub get_extension_for_format {
                 : $self->{format} =~ /html/i         ? ".html"
                 :                                      "";
 
+  $main::lxdebug->leave_sub();
   return $extension;
 }
 
 sub generate_attachment_filename {
+  $main::lxdebug->enter_sub();
   my ($self) = @_;
 
   my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
@@ -1331,10 +1393,12 @@ sub generate_attachment_filename {
   $attachment_filename =  $main::locale->quote_special_chars('filenames', $attachment_filename);
   $attachment_filename =~ s|[\s/\\]+|_|g;
 
+  $main::lxdebug->leave_sub();
   return $attachment_filename;
 }
 
 sub generate_email_subject {
+  $main::lxdebug->enter_sub();
   my ($self) = @_;
 
   my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
@@ -1344,6 +1408,7 @@ sub generate_email_subject {
     $subject .= " " . $self->{"${prefix}number"}
   }
 
+  $main::lxdebug->leave_sub();
   return $subject;
 }
 
@@ -1936,13 +2001,17 @@ sub get_employee_data {
 sub get_duedate {
   $main::lxdebug->enter_sub();
 
-  my ($self, $myconfig) = @_;
+  my ($self, $myconfig, $reference_date) = @_;
 
-  my $dbh = $self->get_standard_dbh($myconfig);
-  my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
-  ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
+  my $reference_date = $reference_date ? conv_dateq($reference_date) . '::DATE' : 'current_date';
+
+  my $dbh            = $self->get_standard_dbh($myconfig);
+  my $query          = qq|SELECT ${reference_date} + terms_netto FROM payment_terms WHERE id = ?|;
+  my ($duedate)      = selectrow_query($self, $dbh, $query, $self->{payment_id});
 
   $main::lxdebug->leave_sub();
+
+  return $duedate;
 }
 
 sub _get_contacts {
@@ -3116,9 +3185,8 @@ sub get_history {
       qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name, h.snumbers, h.trans_id AS id | .
       qq|FROM history_erp h | .
       qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
-      qq|WHERE trans_id = | . $trans_id
-      . $restriction . qq| |
-      . $order;
+      qq|WHERE (trans_id = | . $trans_id . qq|) $restriction | .
+      $order;
 
     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
@@ -3331,7 +3399,7 @@ sub backup_vars {
   my $self = shift;
   my @vars = @_;
 
-  map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if $self->{$_} } @vars;
+  map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if exists $self->{$_} } @vars;
 
   $main::lxdebug->leave_sub();
 }
@@ -3342,7 +3410,7 @@ sub restore_vars {
   my $self = shift;
   my @vars = @_;
 
-  map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if $self->{_VAR_BACKUP}->{$_} } @vars;
+  map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if exists $self->{_VAR_BACKUP}->{$_} } @vars;
 
   $main::lxdebug->leave_sub();
 }