Favicon inserted.
[kivitendo-erp.git] / SL / Form.pm
index e0a3d23..32ff29e 100644 (file)
@@ -146,7 +146,7 @@ sub new {
   $self->{action} = lc $self->{action};
   $self->{action} =~ s/( |-|,|\#)/_/g;
 
-  $self->{version}   = "2.3.9";
+  $self->{version}   = "2.4.0";
 
   $main::lxdebug->leave_sub();
 
@@ -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;
 
@@ -347,6 +366,8 @@ sub header {
  |;
     }
 
+    $self->{favicon}    = "favicon.ico" unless $self->{favicon};
+
     if ($self->{favicon} && (-f "$self->{favicon}")) {
       $favicon =
         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
@@ -404,6 +425,7 @@ function fokus(){document.$self->{fokus}.focus();}
   $jsscript
   $ajax
   $fokus
+  <meta name="robots" content="noindex,nofollow" />
   <script type="text/javascript" src="js/highlight_input.js"></script>
   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
   
@@ -635,24 +657,29 @@ 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/);
 
   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
-  my @p = split /\./, $amount ; # split amount at decimal point
+  my @p = split(/\./, $amount); # split amount at decimal point
 
   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
   $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;
 }
@@ -729,20 +756,36 @@ sub parse_template {
   } 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);
+  } 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'}");  
   }
 
   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
-      qw(email tel fax name signature company address businessnumber));
+      qw(email tel fax name signature company address businessnumber
+         co_ustid taxnumber duns));
+  map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
+      qw(company address signature));
 
   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
   # OUT is used for the media, screen, printer, email
   # for postscript we store a copy in a temporary file
   my $fileid = time;
-  $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
+  $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
   if ($template->uses_temp_file() || $self->{media} eq 'email') {
     $out = $self->{OUT};
     $self->{OUT} = ">$self->{tmpfile}";
@@ -761,9 +804,6 @@ sub parse_template {
   }
 
   close(OUT);
-  
-  use Data::Dumper;
-  #print(STDERR Dumper($self));
 
   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
@@ -778,6 +818,7 @@ sub parse_template {
       $mail->{to}     = qq|$self->{email}|;
       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
       $mail->{fileid} = "$fileid.";
+      $myconfig->{signature} =~ s/\\r\\n/\\n/g;
 
       # if we send html or plain text inline
       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
@@ -785,7 +826,7 @@ sub parse_template {
 
         $mail->{message}       =~ s/\r\n/<br>\n/g;
         $myconfig->{signature} =~ s/\\n/<br>\n/g;
-        $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
+        $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
         open(IN, $self->{tmpfile})
           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
@@ -799,8 +840,9 @@ sub parse_template {
 
         @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
 
-        $myconfig->{signature} =~ s/\\n/\r\n/g;
-        $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
+        $mail->{message}       =~ s/\r\n/\n/g;
+        $myconfig->{signature} =~ s/\\n/\n/g;
+        $mail->{message} .= "\n-- \n$myconfig->{signature}";
 
       }
 
@@ -1106,6 +1148,13 @@ sub set_payment_options {
     $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
     $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
     $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
+    $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
+    $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
+    $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
+    $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
+    $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
+    $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
+    $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
     $dbh->disconnect;
   }
@@ -1208,7 +1257,7 @@ sub get_shipto {
 
 
     my $query = qq|SELECT s.* FROM shipto s
-                  WHERE s.id = $self->{shipto_id}|;
+                  WHERE s.shipto_id = $self->{shipto_id}|;
     my $sth = $dbh->prepare($query);
     $sth->execute || $self->dberror($query);
     $ref = $sth->fetchrow_hashref(NAME_lc);
@@ -1235,7 +1284,6 @@ sub add_shipto {
     }
     $self->{"shipto$item"} =~ s/\'/\'\'/g;
   }
-
   if ($shipto) {
     if ($self->{shipto_id}) {
       my $query = qq| UPDATE shipto set
@@ -1250,7 +1298,7 @@ sub add_shipto {
                       shiptophone = '$self->{shiptophone}',
                       shiptofax = '$self->{shiptofax}',
                       shiptoemail = '$self->{shiptoemail}'
-                      WHERE id = $self->{shipto_id}|;
+                      WHERE shipto_id = $self->{shipto_id}|;
       $dbh->do($query) || $self->dberror($query);
     } else {
       my $query =
@@ -1287,6 +1335,24 @@ sub get_employee {
   $main::lxdebug->leave_sub();
 }
 
+sub get_duedate {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig) = @_;
+
+  my $dbh = $self->dbconnect($myconfig);
+  my $query = qq|SELECT current_date+terms_netto FROM payment_terms
+                 WHERE id = '$self->{payment_id}'|;
+  my $sth = $dbh->prepare($query);
+  $sth->execute || $self->dberror($query);
+
+  ($self->{duedate}) = $sth->fetchrow_array;
+
+  $sth->finish;
+
+  $main::lxdebug->leave_sub();
+}
+
 # get other contact for transaction and form - html/tex
 sub get_contact {
   $main::lxdebug->enter_sub();
@@ -1562,18 +1628,6 @@ sub language_payment {
   }
   $sth->finish;
 
-  # get adr
-  $query = qq|SELECT id, adr_description, adr_code
-              FROM adr|;
-  $sth = $dbh->prepare($query);
-  $sth->execute || $form->dberror($query);
-
-
-  $self->{ADR} = [];
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-    push @{ $self->{ADR} }, $ref;
-  }
-  $sth->finish;
   $dbh->disconnect;
   $main::lxdebug->leave_sub();
 }
@@ -1621,40 +1675,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/) {
-
-        # cross reference for keys
-        $xkeyref{ $ref->{accno} } = $key;
-
-        push @{ $self->{"${module}_links"}{$key} },
-          { accno       => $ref->{accno},
-            description => $ref->{description},
-            taxkey      => $ref->{taxkey_id} };
+  if (!$self->{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);
@@ -1716,14 +1777,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);
@@ -1752,8 +1849,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,
@@ -1787,7 +1884,7 @@ sub create_links {
     if ($self->{"$self->{vc}_id"}) {
 
       # only setup currency
-      ($self->{currency}) = split /:/, $self->{currencies};
+      ($self->{currency}) = split(/:/, $self->{currencies});
 
     } else {
 
@@ -1804,6 +1901,8 @@ sub create_links {
 
   }
 
+  $sth->finish;
+
   $dbh->disconnect;
 
   $main::lxdebug->leave_sub();
@@ -2311,10 +2410,14 @@ sub new {
   my ($type, $country, $NLS_file) = @_;
   my $self = {};
 
-  %self = ();
   if ($country && -d "locale/$country") {
+    local *IN;
     $self->{countrycode} = $country;
-    eval { require "locale/$country/$NLS_file"; };
+    if (open(IN, "locale/$country/$NLS_file")) {
+      my $code = join("", <IN>);
+      eval($code);
+      close(IN);
+    }
   }
 
   $self->{NLS_file} = $NLS_file;
@@ -2334,7 +2437,7 @@ sub new {
 sub text {
   my ($self, $text) = @_;
 
-  return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
+  return (exists $self->{texts}{$text}) ? $self->{texts}{$text} : $text;
 }
 
 sub findsub {
@@ -2342,8 +2445,8 @@ sub findsub {
 
   my ($self, $text) = @_;
 
-  if (exists $self{subs}{$text}) {
-    $text = $self{subs}{$text};
+  if (exists $self->{subs}{$text}) {
+    $text = $self->{subs}{$text};
   } else {
     if ($self->{countrycode} && $self->{NLS_file}) {
       Form->error(
@@ -2470,4 +2573,33 @@ sub parse_date {
   return ($yy, $mm, $dd);
 }
 
+sub reformat_date {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig, $date, $output_format, $longformat) = @_;
+
+  $main::lxdebug->leave_sub() and return "" unless ($date);
+
+  my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
+
+  $output_format =~ /d+/;
+  substr($output_format, $-[0], $+[0] - $-[0]) =
+    sprintf("%0" . (length($&)) . "d", $dd);
+
+  $output_format =~ /m+/;
+  substr($output_format, $-[0], $+[0] - $-[0]) =
+    sprintf("%0" . (length($&)) . "d", $mm);
+
+  $output_format =~ /y+/;
+  if (length($&) == 2) {
+    $yy -= $yy >= 2000 ? 2000 : 1900;
+  }
+  substr($output_format, $-[0], $+[0] - $-[0]) =
+    sprintf("%0" . (length($&)) . "d", $yy);
+
+  $main::lxdebug->leave_sub();
+
+  return $output_format;
+}
+
 1;