Datenbankupdate aus Admin.pl heraus deaktiviert. Workaround Bug 435
[kivitendo-erp.git] / SL / Form.pm
index 3b683f4..381d815 100644 (file)
@@ -219,6 +219,25 @@ sub unquote {
 
 }
 
+sub quote_html {
+  $main::lxdebug->enter_sub(2);
+
+  my ($self, $str) = @_;
+
+  my %replace =
+    ('order' => ['"', '<', '>'],
+     '<'             => '&lt;',
+     '>'             => '&gt;',
+     '"'             => '&quot;',
+    );
+
+  map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
+
+  $main::lxdebug->leave_sub(2);
+
+  return $str;
+}
+
 sub hide_form {
   my $self = shift;
 
@@ -635,6 +654,10 @@ sub format_amount {
   $main::lxdebug->enter_sub(2);
 
   my ($self, $myconfig, $amount, $places, $dash) = @_;
+  
+  if ($amount eq "") {
+    $amount = 0;
+  }
   my $neg = ($amount =~ s/-//);
 
   $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
@@ -646,13 +669,14 @@ sub format_amount {
 
   $amount = $p[0];
   $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" )    ;
   };
-  
+    
+
   $main::lxdebug->leave_sub(2);
   return $amount;
 }
@@ -1610,40 +1634,47 @@ sub create_links {
   my ($query, $sth);
 
   my $dbh = $self->dbconnect($myconfig);
-
   my %xkeyref = ();
 
-  # now get the account numbers
-  $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
-              FROM chart c
-             WHERE c.link LIKE '%$module%'
-             ORDER BY c.accno|;
-
-  $sth = $dbh->prepare($query);
-  $sth->execute || $self->dberror($query);
-
-  $self->{accounts} = "";
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-
-    foreach my $key (split /:/, $ref->{link}) {
-      if ($key =~ /$module/) {
+  if (!$self->{id}) {
 
-        # cross reference for keys
-        $xkeyref{ $ref->{accno} } = $key;
-
-        push @{ $self->{"${module}_links"}{$key} },
-          { accno       => $ref->{accno},
-            description => $ref->{description},
-            taxkey      => $ref->{taxkey_id} };
-
-        $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
+    my $transdate = "current_date";
+    if ($self->{transdate}) {
+      $transdate = qq|'$self->{transdate}'|;
+    }
+  
+    # now get the account numbers
+    $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
+                FROM chart c, taxkeys tk
+                WHERE c.link LIKE '%$module%' AND c.id=tk.chart_id AND tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)
+                ORDER BY c.accno|;
+  
+    $sth = $dbh->prepare($query);
+    $sth->execute || $self->dberror($query);
+  
+    $self->{accounts} = "";
+    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  
+      foreach my $key (split /:/, $ref->{link}) {
+        if ($key =~ /$module/) {
+  
+          # cross reference for keys
+          $xkeyref{ $ref->{accno} } = $key;
+  
+          push @{ $self->{"${module}_links"}{$key} },
+            { accno       => $ref->{accno},
+              description => $ref->{description},
+              taxkey      => $ref->{taxkey_id},
+              tax_id      => $ref->{tax_id} };
+  
+          $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
+        }
       }
     }
   }
-  $sth->finish;
 
   # get taxkeys and description
-  $query = qq|SELECT taxkey, taxdescription
+  $query = qq|SELECT id, taxkey, taxdescription
               FROM tax|;
   $sth = $dbh->prepare($query);
   $sth->execute || $self->dberror($query);
@@ -1705,14 +1736,50 @@ sub create_links {
     }
     $sth->finish;
 
+
+    my $transdate = "current_date";
+    if ($self->{transdate}) {
+      $transdate = qq|'$self->{transdate}'|;
+    }
+  
+    # now get the account numbers
+    $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
+                FROM chart c, taxkeys tk
+                WHERE c.link LIKE '%$module%' AND (((tk.chart_id=c.id) AND NOT(c.link like '%_tax%')) OR (NOT(tk.chart_id=c.id) AND (c.link like '%_tax%'))) AND (((tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)) AND NOT(c.link like '%_tax%')) OR (c.link like '%_tax%'))
+                ORDER BY c.accno|;
+  
+    $sth = $dbh->prepare($query);
+    $sth->execute || $self->dberror($query);
+  
+    $self->{accounts} = "";
+    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  
+      foreach my $key (split /:/, $ref->{link}) {
+        if ($key =~ /$module/) {
+  
+          # cross reference for keys
+          $xkeyref{ $ref->{accno} } = $key;
+  
+          push @{ $self->{"${module}_links"}{$key} },
+            { accno       => $ref->{accno},
+              description => $ref->{description},
+              taxkey      => $ref->{taxkey_id},
+              tax_id      => $ref->{tax_id} };
+  
+          $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
+        }
+      }
+    }
+
+
     # get amounts from individual entries
     $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
-                a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
+                a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate, t.id
                FROM acc_trans a
                JOIN chart c ON (c.id = a.chart_id)
                LEFT JOIN project p ON (p.id = a.project_id)
-               LEFT Join tax t ON (a.taxkey = t.taxkey)
-               WHERE a.trans_id = $self->{id}
+                LEFT JOIN tax t ON (t.id=(SELECT tk.tax_id from taxkeys tk WHERE (tk.taxkey_id=a.taxkey) AND ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id=a.taxkey) THEN tk.chart_id=a.chart_id ELSE 1=1 END) OR (c.link='%tax%')) AND startdate <=a.transdate ORDER BY startdate DESC LIMIT 1)) 
+                WHERE a.trans_id = $self->{id}
                AND a.fx_transaction = '0'
                ORDER BY a.oid,a.transdate|;
     $sth = $dbh->prepare($query);
@@ -1741,8 +1808,8 @@ sub create_links {
 
       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
     }
-    $sth->finish;
 
+    $sth->finish;
     $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
                   (SELECT c.accno FROM chart c
                   WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
@@ -1793,6 +1860,8 @@ sub create_links {
 
   }
 
+  $sth->finish;
+
   $dbh->disconnect;
 
   $main::lxdebug->leave_sub();