Beim Drucken von Rechnungen mit mehreren Lizenzen wurden die Lizenzen versetzt gedruckt
[kivitendo-erp.git] / SL / Form.pm
index f2fe969..c70e35a 100644 (file)
@@ -1,4 +1,4 @@
-#=====================================================================
+#====================================================================
 # LX-Office ERP
 # Copyright (C) 2004
 # Based on SQL-Ledger Version 2.1.9
@@ -382,8 +382,8 @@ function fokus(){document.$self->{fokus}.focus();}
       $jsscript = qq|
         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
-        <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
-        <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
+        <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
+        <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
         $self->{javascript}
        |;
     }
@@ -450,25 +450,25 @@ sub write_trigger {
 
   $trigger_1 = qq|
        Calendar.setup(
-       {
-         inputField  : "$inputField_1",
-         ifFormat    :"$ifFormat",
-         align    : "$align_1",     
-         button      : "$button_1"
-       }
-       );
+      {
+      inputField : "$inputField_1",
+      ifFormat :"$ifFormat",
+      align : "$align_1", 
+      button : "$button_1"
+      }
+      );
        |;
 
   if ($qty == 2) {
     $trigger_2 = qq|
        Calendar.setup(
        {
-         inputField  : "$inputField_2",
-         ifFormat    :"$ifFormat",
-         align    : "$align_2",     
-         button      : "$button_2"
-       }
-       );
+      inputField : "$inputField_2",
+      ifFormat :"$ifFormat",
+      align : "$align_2", 
+      button : "$button_2"
+      }
+      );
         |;
   }
   $jsscript = qq|
@@ -519,6 +519,12 @@ sub format_amount {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $amount, $places, $dash) = @_;
+  
+  #Workaround for $format_amount calls without $places
+  if (!defined $places){
+  (my $dec) = ($amount =~ /\.(\d+)/);
+  $places           = length $dec;
+  }                    
 
   if ($places =~ /\d/) {
     $amount = $self->round_amount($amount, $places);
@@ -540,19 +546,19 @@ sub format_amount {
         $amount =~ s/\d{3,}?/$&,/g;
         $amount =~ s/,$//;
         $amount = join '', reverse split //, $amount;
-        $amount .= "\.$dec".$fillup;
+        $amount .= "\.$dec".$fillup if ($places ne '' && $places*1 != 0);
       }
 
       if ($myconfig->{numberformat} eq '1.000,00') {
         $amount =~ s/\d{3,}?/$&./g;
         $amount =~ s/\.$//;
         $amount = join '', reverse split //, $amount;
-        $amount .= ",$dec" .$fillup;
+        $amount .= ",$dec".$fillup if ($places ne '' && $places*1 != 0);
       }
 
       if ($myconfig->{numberformat} eq '1000,00') {
         $amount = "$whole";
-        $amount .= ",$dec" .$fillup;
+        $amount .= ",$dec" .$fillup if ($places ne '' && $places*1 != 0);
       }
 
       if ($dash =~ /-/) {
@@ -584,19 +590,44 @@ sub parse_amount {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $amount) = @_;
-
-  if (!(substr($amount, -3, 1) eq ".")) {
-    if (   ($myconfig->{numberformat} eq '1.000,00')
-        || ($myconfig->{numberformat} eq '1000,00')) {
-      $amount =~ s/\.//g;
-      $amount =~ s/,/\./;
-    }
-
-    $amount =~ s/,//g;
-  }
-
+  $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");  
+  if ($myconfig->{in_numberformat} == 1){
+    # Extra input number format 1000.00 or 1000,00
+    $main::lxdebug->message(LXDebug::DEBUG2, "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
+    $amount =~ s/,/\./g;
+    #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
+    $amount = scalar reverse $amount;
+    #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
+    $amount =~ s/\./DOT/;
+    #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
+    $amount =~ s/\.//g;
+    #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
+    $amount =~ s/DOT/\./;
+    #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
+    $amount = scalar reverse $amount ;
+    $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n");
+
+    return ($amount * 1);
+
+  }
+  $main::lxdebug->message(LXDebug::DEBUG2, "in_numberformat: " . $main::locale->text('equal Outputformat'));
+  $main::lxdebug->message(LXDebug::DEBUG2, " = numberformat: $myconfig->{numberformat}");
+  if (   ($myconfig->{numberformat} eq '1.000,00')
+      || ($myconfig->{numberformat} eq '1000,00')) {
+    $amount =~ s/\.//g;
+    $amount =~ s/,/\./;
+  }
+
+  if ($myconfig->{numberformat} eq "1'000.00") {
+      $amount =~ s/'//g;
+  }
+
+  $amount =~ s/,//g;
+  
+  $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount. "\n") if ($amount);
   $main::lxdebug->leave_sub();
-
+  
   return ($amount * 1);
 }
 
@@ -618,6 +649,7 @@ sub round_amount {
   $main::lxdebug->leave_sub();
 
   return $round_amount;
+  
 }
 
 
@@ -631,7 +663,7 @@ sub parse_template {
   # Some variables used for page breaks
   my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
     (0, 0, 0);
-  my ($current_page, $current_line) = (1, 1);
+  my ($current_page, $current_line, $current_row) = (1, 1, 0);
   my $pagebreak = "";
   my $sum       = 0;
 
@@ -735,8 +767,9 @@ sub parse_template {
             $lpp = $lines_on_second_page;
           }
 
-          # Yes we need a manual page break
-          if (($current_line + $lines) > $lpp) {
+          # Yes we need a manual page break -- or the user has forced one
+          if ((($current_line + $lines) > $lpp) ||
+              ($self->{"_forced_pagebreaks"} && grep(/^${current_row}$/, @{$self->{"_forced_pagebreaks"}}))) {
             my $pb = $pagebreak;
 
             # replace the special variables <%sumcarriedforward%>
@@ -757,6 +790,7 @@ sub parse_template {
             $current_line = 1;
           }
           $current_line += $lines;
+          $current_row++;
         }
         $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
 
@@ -843,6 +877,7 @@ sub parse_template {
     }
 
     s/<%(.+?)%>/$self->{$1}/g;
+    s/<nobr><\/nobr>/&nbsp;/g;
     print OUT;
   }
 
@@ -1022,6 +1057,18 @@ sub format_string {
 
   %unique_fields = map({ $_ => 1 } @fields);
   @fields = keys(%unique_fields);
+
+  foreach my $field (@fields) {
+    next unless ($self->{$field} =~ /\<pagebreak\>/);
+    $self->{$field} =~ s/\<pagebreak\>//g;
+    if ($field =~ /.*_(\d+)$/) {
+      if (!$self->{"_forced_pagebreaks"}) {
+        $self->{"_forced_pagebreaks"} = [];
+      }
+      push(@{ $self->{"_forced_pagebreaks"} }, "$1");
+    }
+  }
+
   my $format = $self->{format};
   if ($self->{format} =~ /(postscript|pdf)/) {
     $format = 'tex';
@@ -1070,6 +1117,29 @@ sub format_string {
     map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
   }
 
+  # Allow some HTML markup to be converted into the output format's
+  # corresponding markup code, e.g. bold or italic.
+  if ('html' eq $format) {
+    my @markup_replace = ('b', 'i', 's', 'u');
+
+    foreach my $key (@markup_replace) {
+      map({ $self->{$_} =~ s/\&lt;(\/?)${key}\&gt;/<$1${key}>/g } @fields);
+    }
+
+  } elsif ('tex' eq $format) {
+    my %markup_replace = ('b' => 'textbf',
+                          'i' => 'textit',
+                          'u' => 'underline');
+
+    foreach my $field (@fields) {
+      foreach my $key (keys(%markup_replace)) {
+        my $new = $markup_replace{$key};
+        $self->{$field} =~
+          s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
+      }
+    }
+  }
+
   $main::lxdebug->leave_sub();
 }
 
@@ -1239,6 +1309,11 @@ sub get_exchangerate {
   $main::lxdebug->enter_sub();
 
   my ($self, $dbh, $curr, $transdate, $fld) = @_;
+  
+  unless ($transdate) {
+    $main::lxdebug->leave_sub();
+    return "";
+  }
 
   my $query = qq|SELECT e.$fld FROM exchangerate e
                  WHERE e.curr = '$curr'
@@ -1556,7 +1631,7 @@ sub create_links {
 
   # now get the account numbers
   $query =
-    qq|SELECT c.accno, SUBSTRING(c.description,1,50) as description, c.link, c.taxkey_id
+    qq|SELECT c.accno, c.description, c.link, c.taxkey_id
               FROM chart c
              WHERE c.link LIKE '%$module%'
              ORDER BY c.accno|;
@@ -1629,7 +1704,7 @@ sub create_links {
                LEFT Join tax t ON (a.taxkey = t.taxkey)
                WHERE a.trans_id = $self->{id}
                AND a.fx_transaction = '0'
-               ORDER BY a.transdate|;
+               ORDER BY a.oid,a.transdate|;
     $sth = $dbh->prepare($query);
     $sth->execute || $self->dberror($query);
 
@@ -1639,12 +1714,17 @@ sub create_links {
     $self->{exchangerate} =
       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
                               $fld);
+    my $index=0;
 
     # store amounts in {acc_trans}{$key} for multiple accounts
     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
       $ref->{exchangerate} =
         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
                                 $fld);
+      if ( !($xkeyref{ $ref->{accno} } =~ /tax/)) {
+        $index++;
+      }
+      $ref->{index} = $index;
 
       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
     }
@@ -1722,10 +1802,9 @@ sub lastname_used {
     $where = "quotation = '1'";
   }
 
-  my $query = qq|SELECT id FROM $arap
-                 WHERE id IN (SELECT MAX(id) FROM $arap
+  my $query = qq|SELECT MAX(id) FROM $arap
                              WHERE $where
-                             AND ${table}_id > 0)|;
+                             AND ${table}_id > 0|;
   my $sth = $dbh->prepare($query);
   $sth->execute || $self->dberror($query);
 
@@ -2079,6 +2158,38 @@ sub get_partsgroup {
 }
 
 
+sub get_pricegroup {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig, $p) = @_;
+
+  my $dbh = $self->dbconnect($myconfig);
+
+  my $query = qq|SELECT p.id, p.pricegroup
+                 FROM pricegroup p|;
+
+  $query .= qq|
+                ORDER BY pricegroup|;
+
+  if ($p->{all}) {
+    $query = qq|SELECT id, pricegroup FROM pricegroup
+                ORDER BY pricegroup|;
+  }
+
+  my $sth = $dbh->prepare($query);
+  $sth->execute || $self->dberror($query);
+
+  $self->{all_pricegroup} = ();
+  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+    push @{ $self->{all_pricegroup} }, $ref;
+  }
+  $sth->finish;
+  $dbh->disconnect;
+
+  $main::lxdebug->leave_sub();
+}
+
+
 sub audittrail {
   my ($self, $dbh, $myconfig, $audittrail) = @_;
   
@@ -2139,7 +2250,7 @@ sub audittrail {
        $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
                    formname, action, employee_id, transdate) VALUES (
                    $audittrail->{id}, '$audittrail->{tablename}', |
-                   .$dbh->quote($audittrail->{reference}).qq|',
+                   .$dbh->quote($audittrail->{reference}).qq|,
                    '$audittrail->{formname}', '$audittrail->{action}',
                    $employee_id, '$audittrail->{transdate}')|;
       } else {