Falsche Funktionsaufrufe. Fix für Bugzilla ID 560.
[kivitendo-erp.git] / SL / Form.pm
index 57bd84b..7636b16 100644 (file)
@@ -147,7 +147,7 @@ sub new {
   $self->{action} = lc $self->{action};
   $self->{action} =~ s/( |-|,|\#)/_/g;
 
-  $self->{version}   = "2.4.1";
+  $self->{version}   = "2.4.2";
 
   $main::lxdebug->leave_sub();
 
@@ -433,7 +433,7 @@ function fokus(){document.$self->{fokus}.focus();}
   <script type="text/javascript" src="js/tabcontent.js">
   
   /***********************************************
-  * Tab Content script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
+  * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
   * This notice MUST stay intact for legal use
   * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
   ***********************************************/
@@ -653,7 +653,18 @@ sub format_amount {
   }
   my $neg = ($amount =~ s/-//);
 
-  $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
+  if (defined($places) && ($places ne '')) {
+    if ($places < 0) {
+      $amount *= 1;
+      $places *= -1;
+
+      my ($actual_places) = ($amount =~ /\.(\d+)/);
+      $actual_places = length($actual_places);
+      $places = $actual_places > $places ? $actual_places : $places;
+    }
+
+    $amount = $self->round_amount($amount, $places);
+  }
 
   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
   my @p = split(/\./, $amount); # split amount at decimal point
@@ -679,18 +690,6 @@ sub parse_amount {
 
   my ($self, $myconfig, $amount) = @_;
 
-  if ($myconfig->{in_numberformat} == 1) {
-    # Extra input number format 1000.00 or 1000,00
-    $amount =~ s/,/\./g;
-    $amount = scalar reverse $amount;
-    $amount =~ s/\./DOT/;
-    $amount =~ s/\.//g;
-    $amount =~ s/DOT/\./;
-    $amount = scalar reverse $amount;
-    $main::lxdebug->leave_sub(2);
-    return ($amount * 1);
-  }
-
   if (   ($myconfig->{numberformat} eq '1.000,00')
       || ($myconfig->{numberformat} eq '1000,00')) {
     $amount =~ s/\.//g;
@@ -1093,7 +1092,7 @@ sub get_exchangerate {
 
   unless ($transdate) {
     $main::lxdebug->leave_sub();
-    return "";
+    return 1;
   }
 
   my $query = qq|SELECT e.$fld FROM exchangerate e
@@ -1105,7 +1104,7 @@ sub get_exchangerate {
   my ($exchangerate) = $sth->fetchrow_array;
   $sth->finish;
 
-  if ($exchangerate == 0) {
+  if (!$exchangerate) {
     $exchangerate = 1;
   }
 
@@ -1396,51 +1395,136 @@ sub get_duedate {
   $main::lxdebug->leave_sub();
 }
 
-# get other contact for transaction and form - html/tex
-sub get_contact {
+# get contacts for id, if no contact return {"","","","",""}
+sub get_contacts {
   $main::lxdebug->enter_sub();
 
-  my ($self, $dbh, $id) = @_;
+  my ($self, $dbh, $id, $key) = @_;
 
-  my $query = qq|SELECT c.*
-              FROM contacts c
-              WHERE cp_id=$id|;
-  $sth = $dbh->prepare($query);
-  $sth->execute || $self->dberror($query);
+  $key = "all_contacts" unless ($key);
+  $self->{$key} = [];
 
-  $ref = $sth->fetchrow_hashref(NAME_lc);
+  my $query =
+    qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung | .
+    qq|FROM contacts c | .
+    qq|WHERE cp_cv_id = ? | .
+    qq|ORDER BY lower(c.cp_name)|;
+  my $sth = $dbh->prepare($query);
+  $sth->execute($id) || $self->dberror($query . " ($id)");
 
-  push @{ $self->{$_} }, $ref;
+  my $i = 0;
+  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+    push @{ $self->{$key} }, $ref;
+    $i++;
+  }
 
+  if ($i == 0) {
+    push @{ $self->{$key} }, { { "", "", "", "", "", "" } };
+  }
   $sth->finish;
   $main::lxdebug->leave_sub();
 }
 
-# get contacts for id, if no contact return {"","","","",""}
-sub get_contacts {
+sub get_projects {
   $main::lxdebug->enter_sub();
 
-  my ($self, $dbh, $id) = @_;
+  my ($self, $dbh, $key) = @_;
+
+  my ($all, $old_id, $where, @values);
+
+  if (ref($key) eq "HASH") {
+    my $params = $key;
+
+    $key = "ALL_PROJECTS";
+
+    foreach my $p (keys(%{$params})) {
+      if ($p eq "all") {
+        $all = $params->{$p};
+      } elsif ($p eq "old_id") {
+        $old_id = $params->{$p};
+      } elsif ($p eq "key") {
+        $key = $params->{$p};
+      }
+    }
+  }
+
+  if (!$all) {
+    $where = "WHERE active ";
+    if ($old_id) {
+      if (ref($old_id) eq "ARRAY") {
+        my @ids = grep({ $_ } @{$old_id});
+        if (@ids) {
+          $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
+          push(@values, @ids);
+        }
+      } else {
+        $where .= " OR (id = ?) ";
+        push(@values, $old_id);
+      }
+    }
+  }
 
-  my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
-              FROM contacts c
-              WHERE cp_cv_id=$id|;
+  my $query =
+    qq|SELECT id, projectnumber, description, active | .
+    qq|FROM project | .
+    $where .
+    qq|ORDER BY lower(projectnumber)|;
   my $sth = $dbh->prepare($query);
-  $sth->execute || $self->dberror($query);
+  $sth->execute(@values) ||
+    $self->dberror($query . " (" . join(", ", @values) . ")");
 
-  my $i = 0;
+  $self->{$key} = [];
   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-    push @{ $self->{all_contacts} }, $ref;
-    $i++;
+    push(@{ $self->{$key} }, $ref);
   }
 
-  if ($i == 0) {
-    push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
-  }
   $sth->finish;
   $main::lxdebug->leave_sub();
 }
 
+sub get_lists {
+  $main::lxdebug->enter_sub();
+
+  my $self = shift;
+  my %params = @_;
+
+  my $dbh = $self->dbconnect(\%main::myconfig);
+  my ($sth, $query, $ref);
+
+  my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
+  my $vc_id = $self->{"${vc}_id"};
+
+  if ($params{"contacts"}) {
+    $self->get_contacts($dbh, $vc_id, $params{"contacts"});
+  }
+
+  if ($params{"shipto"}) {
+    # get shipping addresses
+    $query =
+      qq|SELECT s.shipto_id,s.shiptoname,s.shiptodepartment_1 | .
+      qq|FROM shipto s | .
+      qq|WHERE s.trans_id = ?|;
+    $sth = $dbh->prepare($query);
+    $sth->execute($vc_id) || $self->dberror($query . " ($vc_id)");
+
+    $self->{$params{"shipto"}} = [];
+    while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
+      push(@{ $self->{$params{"shipto"}} }, $ref);
+    }
+    $sth->finish;
+  }
+
+  if ($params{"projects"} || $params{"all_projects"}) {
+    $self->get_projects($dbh, $params{"all_projects"} ?
+                        $params{"all_projects"} : $params{"projects"},
+                        $params{"all_projects"} ? 1 : 0);
+  }
+
+  $dbh->disconnect();
+
+  $main::lxdebug->leave_sub();
+}
+
 # this sub gets the id and name from $table
 sub get_name {
   $main::lxdebug->enter_sub();
@@ -1612,7 +1696,6 @@ sub all_vc {
   $main::lxdebug->leave_sub();
 }
 
-
 sub language_payment {
   $main::lxdebug->enter_sub();
 
@@ -2132,6 +2215,83 @@ sub update_status {
   $main::lxdebug->leave_sub();
 }
 
+#--- 4 locale ---#
+# $main::locale->text('SAVED') 
+# $main::locale->text('DELETED') 
+# $main::locale->text('ADDED')
+# $main::locale->text('PAYMENT POSTED')
+# $main::locale->text('POSTED')
+# $main::locale->text('POSTED AS NEW')
+# $main::locale->text('ELSE')
+# $main::locale->text('SAVED FOR DUNNING')
+# $main::locale->text('DUNNING STARTED')
+# $main::locale->text('PRINTED')
+# $main::locale->text('MAILED')
+# $main::locale->text('SCREENED')
+# $main::locale->text('invoice')
+# $main::locale->text('proforma')
+# $main::locale->text('sales_order')
+# $main::locale->text('packing_list')
+# $main::locale->text('pick_list')
+# $main::locale->text('purchase_order')
+# $main::locale->text('bin_list')
+# $main::locale->text('sales_quotation')
+# $main::locale->text('request_quotation')
+
+sub save_history {
+       $main::lxdebug->enter_sub();
+       
+       my $self = shift();
+       my $dbh = shift();
+       
+       if(!exists $self->{employee_id}) {
+               &get_employee($self, $dbh);
+       }
+       
+       my $query =
+    qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done) | .
+    qq|VALUES (?, ?, ?, ?)|;
+  my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
+                $self->{addition}, $self->{what_done});
+  do_query($self, $dbh, $query, @values);
+       
+       $main::lxdebug->leave_sub();
+}
+
+sub get_history {
+       $main::lxdebug->enter_sub();
+       
+       my $self = shift();
+       my $dbh = shift();
+       my $trans_id = shift();
+       my $restriction = shift();
+       my @tempArray;
+       my $i = 0;
+       if ($trans_id ne "") {
+               my $query =
+      qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name | .
+      qq|FROM history_erp h | .
+      qq|LEFT JOIN employee emp | .
+      qq|ON emp.id = h.employee_id | .
+      qq|WHERE trans_id = ? |
+      . $restriction;
+       
+               my $sth = $dbh->prepare($query) || $self->dberror($query);
+       
+               $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
+
+               while(my $hash_ref = $sth->fetchrow_hashref()) {
+                       $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
+                       $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
+                       $tempArray[$i++] = $hash_ref; 
+               }
+    $main::lxdebug->leave_sub() and return \@tempArray
+      if ($i > 0 && $tempArray[0] ne "");
+       }
+       $main::lxdebug->leave_sub();
+       return 0;
+}
+
 sub save_status {
   $main::lxdebug->enter_sub();
 
@@ -2142,9 +2302,9 @@ sub save_status {
   my $formnames  = $self->{printed};
   my $emailforms = $self->{emailed};
 
-  my $query = qq|DELETE FROM status
-                 WHERE formname = '$self->{formname}'
-                AND trans_id = $self->{id}|;
+  $query = qq|DELETE FROM status
+              WHERE formname = '$self->{formname}'
+                     AND trans_id = $self->{id}|;
   $dbh->do($query) || $self->dberror($query);
 
   # this only applies to the forms