]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Form.pm
Einkaufs- und Verkaufsrechnungen: Filtermöglichkeiten nach Projektnummer sowie Anzeig...
[mfinanz.git] / SL / Form.pm
index 90b7d3d2f08847aade321df8e67aca28e54ccb92..842afe8af1b2fabe1b2c8812b3ecbcdbceaf2079 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();
 
@@ -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;
@@ -1149,11 +1148,11 @@ sub set_payment_options {
       selectrow_query($self, $dbh, $query);
 
     my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
+    my $skonto_amount = $self->parse_amount($myconfig, $total) *
+      $self->{percent_skonto};
 
     $self->{skonto_amount} =
-      $self->format_amount($myconfig,
-                           $self->parse_amount($myconfig, $total) *
-                           $self->{percent_skonto}, 2);
+      $self->format_amount($myconfig, $skonto_amount, 2);
 
     if ($self->{"language_id"}) {
       $query =
@@ -1183,9 +1182,7 @@ sub set_payment_options {
         my $saved_numberformat = $myconfig->{"numberformat"};
         $myconfig->{"numberformat"} = $output_numberformat;
         $self->{skonto_amount} =
-          $self->format_amount($myconfig,
-                               $self->parse_amount($myconfig, $total) *
-                               $self->{percent_skonto}, 2);
+          $self->format_amount($myconfig, $skonto_amount, 2);
         $myconfig->{"numberformat"} = $saved_numberformat;
       }
     }
@@ -1398,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();