Bei Kunden -> Angebot, gibt es nach dem Speichern des Angebots keinen Funktionsunters...
[kivitendo-erp.git] / SL / Form.pm
index de2028e..71af15e 100644 (file)
@@ -36,6 +36,9 @@
 #======================================================================
 
 package Form;
+
+#use strict;
+
 use Data::Dumper;
 
 use CGI;
@@ -67,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]) = '';
 
@@ -100,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);
@@ -116,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;
@@ -170,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;
       }
@@ -193,6 +194,29 @@ sub _request_to_hash {
   $main::lxdebug->leave_sub(2);
 }
 
+sub _recode_recursively {
+  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]);
+      }
+    }
+  }
+}
+
 sub new {
   $main::lxdebug->enter_sub();
 
@@ -217,7 +241,22 @@ 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;
@@ -609,7 +648,7 @@ sub header {
 
   <script type="text/javascript">
   <!--
-    function fokus() {
+    function focus() {
       $fokus
     }
   //-->
@@ -911,12 +950,12 @@ sub format_amount {
   if ($amount eq "") {
     $amount = 0;
   }
-  
+
   # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
-  
+
   my $neg = ($amount =~ s/^-//);
   my $exp = ($amount =~ m/[e]/) ? 1 : 0;
-  
+
   if (defined($places) && ($places ne '')) {
     if (not $exp) {
       if ($places < 0) {
@@ -1225,7 +1264,7 @@ sub parse_template {
           open(OUT, $self->{OUT})
             or $self->error($self->cleanup . "$self->{OUT} : $!");
         } else {
-          $self->{attachment_filename} = ($self->{attachment_filename}) 
+          $self->{attachment_filename} = ($self->{attachment_filename})
                                        ? $self->{attachment_filename}
                                        : $self->generate_attachment_filename();
 
@@ -1434,7 +1473,7 @@ sub dbconnect_noauto {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig) = @_;
-  
+
   # connect to database
   my $dbh =
     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
@@ -1515,7 +1554,7 @@ sub update_exchangerate {
   if ($curr eq '') {
     $main::lxdebug->leave_sub();
     return;
-  }  
+  }
   $query = qq|SELECT curr FROM defaults|;
 
   my ($currency) = selectrow_query($self, $dbh, $query);
@@ -1556,7 +1595,7 @@ sub update_exchangerate {
                 SET $set
                 WHERE curr = ?
                 AND transdate = ?|;
-    
+
   } else {
     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
                 VALUES (?, $buy, $sell, ?)|;
@@ -1625,6 +1664,10 @@ sub check_exchangerate {
 
   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
+  if ($fld !~/^buy|sell$/) {
+    $self->error('Fatal: check_exchangerate called with invalid buy/sell argument');
+  }
+
   unless ($transdate) {
     $main::lxdebug->leave_sub();
     return "";
@@ -1867,7 +1910,7 @@ sub add_shipto {
                        shiptophone = ? AND
                        shiptofax = ? AND
                        shiptoemail = ? AND
-                       module = ? AND 
+                       module = ? AND
                        trans_id = ?|;
       my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
       if(!$insert_check){
@@ -2074,11 +2117,27 @@ sub _get_charts {
 sub _get_taxcharts {
   $main::lxdebug->enter_sub();
 
-  my ($self, $dbh, $key) = @_;
+  my ($self, $dbh, $params) = @_;
+
+  my $key = "all_taxcharts";
+  my @where;
+
+  if (ref $params eq 'HASH') {
+    $key = $params->{key} if ($params->{key});
+    if ($params->{module} eq 'AR') {
+      push @where, 'taxkey NOT IN (8, 9, 18, 19)';
 
-  $key = "all_taxcharts" unless ($key);
+    } elsif ($params->{module} eq 'AP') {
+      push @where, 'taxkey NOT IN (1, 2, 3, 12, 13)';
+    }
+
+  } elsif ($params) {
+    $key = $params;
+  }
 
-  my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
+  my $where = ' WHERE ' . join(' AND ', map { "($_)" } @where) if (@where);
+
+  my $query = qq|SELECT * FROM tax $where ORDER BY taxkey|;
 
   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
@@ -2158,7 +2217,7 @@ $main::lxdebug->enter_sub();
   $key = "all_currencies" unless ($key);
 
   my $query = qq|SELECT curr AS currency FROM defaults|;
+
   $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
 
   $main::lxdebug->leave_sub();
@@ -2172,7 +2231,7 @@ $main::lxdebug->enter_sub();
   $key = "all_payments" unless ($key);
 
   my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
+
   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
   $main::lxdebug->leave_sub();
@@ -2337,7 +2396,7 @@ sub get_lists {
   if ($params{"employees"}) {
     $self->_get_employees($dbh, "all_employees", $params{"employees"});
   }
-  
+
   if ($params{"salesmen"}) {
     $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
   }
@@ -2349,11 +2408,11 @@ sub get_lists {
   if ($params{"dunning_configs"}) {
     $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
   }
-  
+
   if($params{"currencies"}) {
     $self->_get_currencies($dbh, $params{"currencies"});
   }
-  
+
   if($params{"customers"}) {
     if (ref $params{"customers"} eq 'HASH') {
       $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
@@ -2361,7 +2420,7 @@ sub get_lists {
       $self->_get_customers($dbh, $params{"customers"});
     }
   }
-  
+
   if($params{"vendors"}) {
     if (ref $params{"vendors"} eq 'HASH') {
       $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
@@ -2369,7 +2428,7 @@ sub get_lists {
       $self->_get_vendors($dbh, $params{"vendors"});
     }
   }
-  
+
   if($params{"payments"}) {
     $self->_get_payments($dbh, $params{"payments"});
   }
@@ -3096,7 +3155,7 @@ sub get_history {
       qq|WHERE trans_id = | . $trans_id
       . $restriction . qq| |
       . $order;
-      
+
     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
     $sth->execute() || $self->dberror("$query");
@@ -3107,7 +3166,7 @@ sub get_history {
       $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
       $tempArray[$i++] = $hash_ref;
     }
-    $main::lxdebug->leave_sub() and return \@tempArray 
+    $main::lxdebug->leave_sub() and return \@tempArray
       if ($i > 0 && $tempArray[0] ne "");
   }
   $main::lxdebug->leave_sub();