Bindestriche und ID aus den PopupMenus entfernt und in der FOrm.pm die Speicherung...
[kivitendo-erp.git] / SL / DN.pm
index e4e0d0e..f986421 100644 (file)
--- a/SL/DN.pm
+++ b/SL/DN.pm
@@ -211,10 +211,9 @@ sub save_dunning {
     qq|SELECT
          cfg.interest_rate, cfg.template AS formname,
          cfg.email_subject, cfg.email_body, cfg.email_attachment,
-         (SELECT fee
+         (SELECT SUM(fee)
           FROM dunning
-          WHERE dunning_id = ?
-          LIMIT 1)
+          WHERE dunning_id = ?)
          AS fee,
          (SELECT SUM(interest)
           FROM dunning
@@ -418,8 +417,6 @@ sub get_dunning {
 
   my @values;
 
-  $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
-
   if ($form->{customer_id}) {
     $where .= qq| AND (a.customer_id = ?)|;
     push(@values, $form->{customer_id});
@@ -429,7 +426,6 @@ sub get_dunning {
     push(@values, '%' . $form->{customer} . '%');
   }
 
-
   my %columns = (
     "ordnumber" => "a.ordnumber",
     "invnumber" => "a.invnumber",
@@ -446,7 +442,12 @@ sub get_dunning {
     push(@values, conv_i($form->{dunning_level}));
   }
 
-  $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
+  if ($form->{department_id}) {
+    $where .= qq| AND a.department_id = ?|;
+    push @values, conv_i($form->{department_id});
+  }
+
+  $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
   if ($form->{minamount}) {
     $where .= qq| AND ((a.amount - a.paid) > ?) |;
     push(@values, $form->{minamount});
@@ -500,12 +501,15 @@ sub parse_strings {
 
   my ($myconfig, $form, $userspath, $string) = @_;
 
+  local (*IN, *OUT);
+
   my $format = $form->{format};
   $form->{format} = "html";
 
   $tmpstring = "parse_string.html";
   $tmpfile = "$myconfig->{templates}/$tmpstring";
-  open(OUT, ">$tmpfile") or $form->error("$tmpfile : $!");
+  open(OUT, ">", $tmpfile) or $form->error("$tmpfile : $!");
+
   print(OUT $string);
   close(OUT);
 
@@ -515,12 +519,8 @@ sub parse_strings {
 
   my $fileid = time;
   $form->{tmpfile} = "$userspath/${fileid}.$tmpstring";
-  $out = $form->{OUT};
-  $form->{OUT} = ">$form->{tmpfile}";
 
-  if ($form->{OUT}) {
-    open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
-  }
+  open(OUT, ">", $form->{tmpfile}) or $form->error("$form->{OUT} : $!");
   if (!$template->parse(*OUT)) {
     $form->cleanup();
     $form->error("$form->{IN} : " . $template->get_error());
@@ -528,7 +528,7 @@ sub parse_strings {
 
   close(OUT);
   my $result = "";
-  open(IN, $form->{tmpfile}) or $form->error($form->cleanup . "$form->{tmpfile} : $!");
+  open(IN, "<", $form->{tmpfile}) or $form->error($form->cleanup . "$form->{tmpfile} : $!");
 
   while (<IN>) {
     $result .= $_;
@@ -550,43 +550,34 @@ sub melt_pdfs {
 
   my ($self, $myconfig, $form, $userspath) = @_;
 
-  foreach my $file (@{ $form->{DUNNING_PDFS} }) {
-    $inputfiles .= " $userspath/$file ";
-  }
+  local (*IN, *OUT);
 
+  # Don't allow access outside of $userspath.
+  map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
+
+  my $inputfiles = join " ", map { "$userspath/$_" } @{ $form->{DUNNING_PDFS} };
   my $outputfile = "$userspath/dunning.pdf";
+
   system("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$outputfile $inputfiles");
-  foreach my $file (@{ $form->{DUNNING_PDFS} }) {
-    unlink("$userspath/$file");
-  }
-  $out = "";
 
-  $form->{OUT} = $out;
+  map { unlink("$userspath/$_") } @{ $form->{DUNNING_PDFS} };
 
   my $numbytes = (-s $outputfile);
-  open(IN, $outputfile)
-    or $form->error($self->cleanup . "$outputfile : $!");
+  open(IN, $outputfile) || $form->error($self->cleanup() . "$outputfile : $!");
 
   $form->{copies} = 1 unless $form->{media} eq 'printer';
 
-  chdir("$self->{cwd}");
+  chdir($self->{cwd});
 
   for my $i (1 .. $form->{copies}) {
-    if ($form->{OUT}) {
-      open(OUT, $form->{OUT})
-        or $form->error($form->cleanup . "$form->{OUT} : $!");
-    } else {
-
-      # launch application
-      print qq|Content-Type: Application/PDF
+    # launch application
+    print qq|Content-Type: Application/PDF
 Content-Disposition: attachment; filename="$outputfile"
 Content-Length: $numbytes
 
 |;
 
-      open(OUT, ">-") or $form->error($form->cleanup . "$!: STDOUT");
-
-    }
+    open(OUT, ">-") or $form->error($form->cleanup . "$!: STDOUT");
 
     while (<IN>) {
       print OUT $_;
@@ -598,7 +589,7 @@ Content-Length: $numbytes
   }
 
   close(IN);
-  unlink("$userspath/$outputfile");
+  unlink($outputfile);
 
   $main::lxdebug->leave_sub();
 }
@@ -648,7 +639,11 @@ sub print_dunning {
     qq|SELECT
          cfg.interest_rate, cfg.template AS formname,
          cfg.email_subject, cfg.email_body, cfg.email_attachment,
-         d.fee, d.dunning_date,
+         d.transdate AS dunning_date,
+         (SELECT SUM(fee)
+          FROM dunning
+          WHERE dunning_id = ?)
+         AS fee,
          (SELECT SUM(interest)
           FROM dunning
           WHERE dunning_id = ?)
@@ -664,7 +659,7 @@ sub print_dunning {
        LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
        WHERE d.dunning_id = ?
        LIMIT 1|;
-  $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id);
+  $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
   $form->{interest_rate}     = $form->format_amount($myconfig, $ref->{interest_rate} * 100);