"path" wird nicht mehr benötigt.
[kivitendo-erp.git] / bin / mozilla / dn.pl
index 50c602b..ee77c82 100644 (file)
@@ -36,8 +36,8 @@ use SL::PE;
 use SL::DN;
 use Data::Dumper;
 
-require "$form->{path}/io.pl";
-require "$form->{path}/arap.pl";
+require "bin/mozilla/io.pl";
+require "bin/mozilla/arap.pl";
 
 1;
 
@@ -51,10 +51,10 @@ sub edit_config {
   $form->{title} = $locale->text('Edit Dunning Process Config');
   
   $form->{callback} =
-    "$form->{script}?action=edit_config&path=$form->{path}&login=$form->{login}&password=$form->{password}"
+    "$form->{script}?action=edit_config&login=$form->{login}&password=$form->{password}"
     unless $form->{callback};
 
-  @column_index = qw(dunning_level dunning_description active auto email payment_terms terms fee interest template);
+  @column_index = qw(dunning_level dunning_description active auto email payment_terms terms fee interest_rate template);
 
   $column_header{dunning_level} =
       qq|<th class=listheading>|
@@ -88,7 +88,7 @@ sub edit_config {
       qq|<th class=listheading>|
     . $locale->text('Fee')
     . qq|</th>|;
-  $column_header{interest} =
+  $column_header{interest_rate} =
       qq|<th class=listheading>|
     . $locale->text('Interest Rate')
     . qq|</th>|;
@@ -105,7 +105,7 @@ sub edit_config {
 
 <table width=100%>
   <tr>
-    <th class=listtop colspan=9>$form->{title}</th>
+    <th class=listtop colspan=10>$form->{title}</th>
   </tr>
   <tr height="5"></tr>
   <tr>|;
@@ -141,7 +141,7 @@ sub edit_config {
     $column_data{payment_terms}           = qq|<td><input name=payment_terms_$i size=3 value="$ref->{payment_terms}"></td>|;
     $column_data{terms}           = qq|<td><input name=terms_$i size=3 value="$ref->{terms}"></td>|;
     $column_data{fee}           = qq|<td><input name=fee_$i size=5 value="$ref->{fee}"></td>|;
-    $column_data{interest}           = qq|<td><input name=interest_$i size=4 value="$ref->{interest}">%</td>|;
+    $column_data{interest_rate}           = qq|<td><input name=interest_rate_$i size=4 value="$ref->{interest}">%</td>|;
     $column_data{template}           = qq|<td><input name=template_$i value="$ref->{template}"></td>|;
 
 
@@ -163,9 +163,8 @@ sub edit_config {
   $column_data{dunning_level} =
     qq|<td><input type=hidden size=2 name=dunning_level_$i value=$i>$i</td>|;
   $column_data{dunning_description}           = qq|<td><input name=dunning_description_$i ></td>|;
-  my $active = "";
   $column_data{active} =
-    qq|<td><input type=checkbox name=active_$i value=1 $active></td>|;
+    qq|<td><input type=checkbox name=active_$i value=1></td>|;
   my $email = "";
   $column_data{email} =
     qq|<td><input type=checkbox name=email_$i value=1 $email><button type="button" onclick="set_email_window('email_subject_$i', 'email_body_$i', 'email_attachment_$i')">| . $locale->text('L') . qq|</button><input type=hidden name=email_body_$i><input type=hidden name=email_subject_$i><input type=hidden name=email_attachment_$i></td>|;
@@ -175,7 +174,7 @@ sub edit_config {
   $column_data{payment_terms}           = qq|<td><input  size=3 name=payment_terms_$i></td>|;
   $column_data{terms}           = qq|<td><input  size=3 name=terms_$i></td>|;
   $column_data{fee}           = qq|<td><input  size=5 name=fee_$i></td>|;
-  $column_data{interest}           = qq|<td><input  size=4 name=interest_$i>%</td>|;
+  $column_data{interest_rate}           = qq|<td><input  size=4 name=interest_rate_$i>%</td>|;
   $column_data{template}           = qq|<td><input name=template_$i></td>|;
 
 
@@ -202,19 +201,11 @@ sub edit_config {
 <input name=callback type=hidden value="$form->{callback}">
 <input name=rowcount type=hidden value="$form->{rowcount}">
 
-<input type=hidden name=path value=$form->{path}>
 <input type=hidden name=login value=$form->{login}>
 <input type=hidden name=password value=$form->{password}>
 
 <input class=submit type=submit name=action value="|
-    . $locale->text('Save') . qq|">|;
-
-  if ($form->{menubar}) {
-    require "$form->{path}/menu.pl";
-    &menubar;
-  }
-
-  print qq|
+    . $locale->text('Save') . qq|">
 
   </form>
 
@@ -269,34 +260,21 @@ sub add {
     <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
     </tr>
     | if $form->{selectdepartment};
-  $form->{title}   = $locale->text('Start Dunning Process');
-  $form->{nextsub} = "show_invoices";
 
-  # use JavaScript Calendar or not
-  $form->{jsscript} = $jscalendar;
-  $jsscript = "";
-  if ($form->{jsscript}) {
+  $form->{title}       = $locale->text('Start Dunning Process');
+  $form->{nextsub}     = "show_invoices";
 
-    # with JavaScript Calendar
-    $button1 = qq|
-      <td><input name=paymentuntil id=paymentuntil size=11 title="$myconfig{dateformat}">
-      <input type=button name=paymentuntil id="trigger1" value=|
-      . $locale->text('button') . qq|></td>
-      |;
+  $form->{jsscript}    = 1;
+  $form->{fokus}       = "search.customer";
+  $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
+  $form->header();
 
-    #write Trigger
-    $jsscript =
-      Form->write_trigger(\%myconfig, "1", "paymentuntil", "BR", "trigger1");
-  } else {
+  $onload = qq|focus()|;
+  $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
+  $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
 
-    # without JavaScript Calendar
-    $button1 =
-      qq|<td><input name=paymentuntil id=paymentuntil size=11 title="$myconfig{dateformat}"></td>|;
-  }
-  $form->{fokus} = "search.customer";
-  $form->header;
   print qq|
-<body onLoad="fokus()">
+<body onLoad="$onload">
 
 <form method=post name="search" action=$form->{script}>
 
@@ -324,11 +302,6 @@ sub add {
           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
           <td colspan=3><input name=notes size=40></td>
         </tr>
-        <tr>
-          <th align=right nowrap>| . $locale->text('Payment until') . qq|</th>
-          $button1
-        </tr>
-        <input type=hidden name=sort value=transdate>
       </table>
     </td>
   </tr>
@@ -353,7 +326,6 @@ sub add {
 
 <input type=hidden name=nextsub value=$form->{nextsub}>
 
-<input type=hidden name=path value=$form->{path}>
 <input type=hidden name=login value=$form->{login}>
 <input type=hidden name=password value=$form->{password}>
 
@@ -389,24 +361,33 @@ sub show_invoices {
 
 
   $form->{nextsub} = "save_dunning";
-  
+
+  $form->{jsscript} = 1;
+  $form->{javascript} .= qq|<script type="text/javascript" src="js/checkbox_utils.js"></script>|;
+
   $form->{callback} =
-    "$form->{script}?action=show_invoices&path=$form->{path}&login=$form->{login}&password=$form->{password}&customer=$form->{customer}&invnumber=$form->{invnumber}&ordnumber=$form->{ordnumber}&paymentuntil=$form->{paymentuntil}&groupinvoices=$form->{groupinvoices}&minamount=$form->{minamount}&dunning_level=$form->{dunning_level}&notes=$form->{notes}"
+    "$form->{script}?action=show_invoices&login=$form->{login}&password=$form->{password}&customer=$form->{customer}&invnumber=$form->{invnumber}&ordnumber=$form->{ordnumber}&paymentuntil=$form->{paymentuntil}&groupinvoices=$form->{groupinvoices}&minamount=$form->{minamount}&dunning_level=$form->{dunning_level}&notes=$form->{notes}"
     unless $form->{callback};
 
-  @column_index = qw(dunning_description active email customername invnumber invdate inv_duedate invamount next_duedate fee interest );
+  @column_index = qw(dunning_description dunning_description_next active email customername invnumber invdate inv_duedate amount next_duedate fee interest );
 
   $column_header{dunning_description} =
-      qq|<th class=listheading>|
+      qq|<th class="listheading" colspan="2">|
     . $locale->text('Current / Next Level')
     . qq|</th>|;
   $column_header{active} =
       qq|<th class=listheading>|
-    . $locale->text('Active?')
+    . NTI($cgi->checkbox('-name' => 'selectall_active',
+                         '-label' => $locale->text('Active?'),
+                         '-checked' => 0,
+                         '-onclick' => "checkbox_check_all('selectall_active', 'active_', 1, " . scalar(@{ $form->{DUNNINGS} }) . ")"))
     . qq|</th>|;
   $column_header{email} =
       qq|<th class=listheading>|
-    . $locale->text('eMail?')
+    . NTI($cgi->checkbox('-name' => 'selectall_email',
+                         '-label' => $locale->text('eMail?'),
+                         '-checked' => 0,
+                         '-onclick' => "checkbox_check_all('selectall_email', 'email_', 1, " . scalar(@{ $form->{DUNNINGS} }) . ")"))
     . qq|</th>|;
   $column_header{customername} =
       qq|<th class=listheading>|
@@ -428,7 +409,7 @@ sub show_invoices {
       qq|<th class=listheading>|
     . $locale->text('Invdate')
     . qq|</th>|;
-  $column_header{invamount} =
+  $column_header{amount} =
       qq|<th class=listheading>|
     . $locale->text('Amount')
     . qq|</th>|;
@@ -438,7 +419,7 @@ sub show_invoices {
     . qq|</th>|;
   $column_header{interest} =
       qq|<th class=listheading>|
-    . $locale->text('Total Interest')
+    . $locale->text('Interest')
     . qq|</th>|;
 
   $form->header;
@@ -453,11 +434,11 @@ sub show_invoices {
 
 <table width=100%>
   <tr>
-    <th class=listtop colspan=9>$form->{title}</th>
+    <th class=listtop colspan=12>$form->{title}</th>
   </tr>
   <tr height="5"></tr>
   <tr>|;
-  map { print "$column_header{$_}\n" } @column_index;
+  map { print "$column_header{$_}\n" if $column_header{$_}; } @column_index;
 
   print qq|
         </tr>
@@ -472,33 +453,39 @@ sub show_invoices {
         <tr valign=top class=listrow$j>
 |;
 
-  $form->{selectdunning} =~ s/ selected//g;
-  if ($ref->{next_dunning_id} ne "") {
-     $form->{selectdunning} =~ s/value=$ref->{next_dunning_id}/value=$ref->{next_dunning_id} selected/;
-  }
-  
-
-  $dunning = qq|<select name=next_dunning_id_$i>$form->{selectdunning}</select>|;
-
+    $form->{selectdunning} =~ s/ selected//g;
+    if ($ref->{next_dunning_config_id} ne "") {
+      $form->{selectdunning} =~ s/value=$ref->{next_dunning_config_id}/value=$ref->{next_dunning_config_id} selected/;
+    }
 
-    $column_data{dunning_description}           = qq|<td><input type=hidden name=inv_id_$i size=2 value="$ref->{id}"><input type=hidden name=customer_id_$i size=2 value="$ref->{customer_id}">$ref->{dunning_level}:&nbsp;$dunning</td>|;
-    my $active = "checked";
+    $column_data{dunning_description} =
+      qq|<td>|
+      . qq|<input type=hidden name=inv_id_$i size=2 value="$ref->{id}">|
+      . qq|<input type=hidden name=customer_id_$i size=2 value="$ref->{customer_id}">|
+      . ($ref->{dunning_level} ? $ref->{dunning_level} : "&nbsp;")
+      . qq|</td>|;
+    $column_data{dunning_description_next} =
+      qq|<td>|
+      . qq|<select name=next_dunning_config_id_$i>$form->{selectdunning}</select>|
+      . qq|</td>|;
+    my $active = ($ref->{active}) ? "checked" : "";
     $column_data{active} =
       qq|<td><input type=checkbox name=active_$i value=1 $active></td>|;
-    my $email = "checked";
-  $column_data{email} =
-    qq|<td><input type=checkbox name=email_$i value=1 $email></td>|;
-    $column_data{next_duedate}           = qq|<td><input type=hidden name=next_duedate_$i size=6 value="$ref->{next_duedate}">$ref->{next_duedate}</td>|;
+    my $email = ($ref->{email}) ? "checked" : "";
+    $column_data{email} =
+      qq|<td><input type=checkbox name=email_$i value=1 $email></td>|;
+    $column_data{next_duedate}           = qq|<td>$ref->{next_duedate}</td>|;
 
     $column_data{inv_duedate}           = qq|<td><input type=hidden name=inv_duedate_$i size=6 value="$ref->{duedate}">$ref->{duedate}</td>|;
     $column_data{invdate}           = qq|<td><input type=hidden name=invdate_$i size=6 value="$ref->{transdate}">$ref->{transdate}</td>|;
     $column_data{invnumber}           = qq|<td><input type=hidden name=invnumber_$i size=6 value="$ref->{invnumber}">$ref->{invnumber}</td>|;
     $column_data{customername}           = qq|<td><input type=hidden name=customername_$i size=6 value="$ref->{customername}">$ref->{customername}</td>|;
-    $column_data{invamount}           = qq|<td><input type=hidden name=invamount_$i size=6 value="$ref->{amount}">$ref->{amount}</td>|;
-    $column_data{fee}           = qq|<td><input type=hidden name=fee_$i size=5 value="$ref->{fee}">$ref->{fee}</td>|;
-    $column_data{interest}           = qq|<td><input type=hidden name=interest_$i size=4 value="$ref->{interest}">$ref->{interest}</td>|;
-
 
+    map { $column_data{$_} =
+            qq|<td align="right">|
+            . H($form->format_amount(\%myconfig, $ref->{$_} * 1, -2))
+            . qq|</td>|
+    } qw(amount fee interest);
 
     map { print "$column_data{$_}\n" } @column_index;
 
@@ -517,8 +504,8 @@ sub show_invoices {
   <td><hr size=3 noshade></td>
   </tr>
 </table>|;
-&print_options;
-print qq|
+  print_options();
+  print qq|
 <br>
 <form method=post action=$form->{script}>
 
@@ -528,22 +515,11 @@ print qq|
 <input name=groupinvoices type=hidden value="$form->{groupinvoices}">
 
 
-<input type=hidden name=path value=$form->{path}>
 <input type=hidden name=login value=$form->{login}>
-<input type=hidden name=password value=$form->{password}>|;
-#print qq|
-# <input type="submit" name="action" value="|
-#     . $locale->text('Continue') . qq|">|;
-print qq|
+<input type=hidden name=password value=$form->{password}>
 <input type="hidden" name="action">
 <input type="submit" name="dummy" value="|
-    . $locale->text('Continue') . qq|" onclick="this.disabled=true; this.value='| . $locale->text("The dunning process started") . qq|'; document.Form.action.value='| . $locale->text('Continue') . qq|'; document.Form.submit()">|;
-  if ($form->{menubar}) {
-    require "$form->{path}/menu.pl";
-    &menubar;
-  }
-
-  print qq|
+    . $locale->text('Continue') . qq|" onclick="this.disabled=true; this.value='| . $locale->text("The dunning process started") . qq|'; document.Form.action.value='| . $locale->text('Continue') . qq|'; document.Form.submit()">
 
   </form>
 
@@ -569,6 +545,13 @@ sub save {
   }
 
   DN->save_config(\%myconfig, \%$form);
+  # saving the history
+  if(!exists $form->{addition} && $form->{id} ne "") {
+       $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
+    $form->{addition} = "SAVED FOR DUNNING";
+       $form->save_history($form->dbconnect(\%myconfig));
+  }
+  # /saving the history 
   $form->redirect($locale->text('Dunning Process Config saved!'));
 
   $lxdebug->leave_sub();
@@ -580,61 +563,71 @@ sub save_dunning {
   my $active=1;
   my @rows = ();
   undef($form->{DUNNING_PDFS});
+
   if ($form->{groupinvoices}) {
-    while ($active) {
-      $lastcustomer = 0;
-      $form->{inv_ids} = "";
-      $active = 0;
-      @rows = ();
-      for my $i (1 .. $form->{rowcount}) {
-        $form->{"active_$i"} *= 1;
-        $lastcustomer = $form->{"customer_id_$i"} unless ($lastcustomer);
-        if ($form->{"active_$i"} && ($form->{"customer_id_$i"} == $lastcustomer)) {
-          if ($form->{inv_ids}) {
-            $form->{inv_ids} .= qq|,$form->{"inv_id_$i"}|;
-          } else {
-            $form->{inv_ids} = qq|($form->{"inv_id_$i"}|;
-          }
-          $form->{"active_$i"} = 0;
-          $form->{"customer_id_$i"} = 0;
-          push(@rows, $i);
-        } elsif ($form->{"active_$i"}) {
-          $active = 1;
-        } else {
-          $form->{"customer_id_$i"} = 0;
-        }
-      }
-      if ($form->{inv_ids} ne "") {
-        $form->{inv_ids} .= ")";
-        DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath,$spool, $sendmail);
+    my %dunnings_for;
+
+    for my $i (1 .. $form->{rowcount}) {
+      next unless ($form->{"active_$i"});
+
+      $dunnings_for{$form->{"customer_id_$i"}} ||= {};
+      my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
+
+      $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
+      my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
+
+      push @{ $level }, { "row"                    => $i,
+                          "invoice_id"             => $form->{"inv_id_$i"},
+                          "customer_id"            => $form->{"customer_id_$i"},
+                          "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
+                          "email"                  => $form->{"email_$i"}, };
+    }
+
+    foreach my $levels (values %dunnings_for) {
+      foreach my $level (values %{ $levels }) {
+        next unless scalar @{ $level };
+
+        DN->save_dunning(\%myconfig, \%$form, $level, $userspath, $spool, $sendmail);
       }
     }
+
   } else {
     for my $i (1 .. $form->{rowcount}) {
-      if ($form->{"active_$i"}) {
-        @rows = ();
-        $form->{inv_ids} = qq|($form->{"inv_id_$i"})|;
-        push(@rows, $i);
-        DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath,$spool, $sendmail);
-      }
+      next unless $form->{"active_$i"};
+
+      my $level = [ { "row"                    => $i,
+                      "invoice_id"             => $form->{"inv_id_$i"},
+                      "customer_id"            => $form->{"customer_id_$i"},
+                      "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
+                      "email"                  => $form->{"email_$i"}, } ];
+      DN->save_dunning(\%myconfig, \%$form, $level, $userspath, $spool, $sendmail);
     }
   }
+
   if($form->{DUNNING_PDFS}) {
     DN->melt_pdfs(\%myconfig, \%$form,$spool);
   }
 
+  # saving the history
+  if(!exists $form->{addition} && $form->{id} ne "") {
+       $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
+    $form->{addition} = "DUNNING STARTED";
+       $form->save_history($form->dbconnect(\%myconfig));
+  }
+  # /saving the history
+
   $form->redirect($locale->text('Dunning Process started for selected invoices!'));
 
   $lxdebug->leave_sub();
 }
-  
+
 sub set_email {
   $lxdebug->enter_sub();
 
 
   my $callback = "$form->{script}?action=set_email&";
   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
-      (qw(login path password name input_subject input_body input_attachment email_subject email_body email_attachment), grep({ /^[fl]_/ } keys %$form)));
+      (qw(login password name input_subject input_body input_attachment email_subject email_body email_attachment), grep({ /^[fl]_/ } keys %$form)));
 
   if ($form->{email_attachment}) {
     $form->{email_attachment} = "checked";
@@ -700,22 +693,22 @@ sub search {
 
     # with JavaScript Calendar
     $button1 = qq|
-      <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
+      <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
       <input type=button name=transdatefrom id="trigger1" value=|
       . $locale->text('button') . qq|></td>
       |;
     $button2 = qq|
-      <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
+      <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
       <input type=button name=transdateto id="trigger2" value=|
       . $locale->text('button') . qq|></td>
       |;
     $button3 = qq|
-      <td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}">
+      <td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
       <input type=button name=dunningfrom id="trigger3" value=|
       . $locale->text('button') . qq|></td>
       |;
     $button4 = qq|
-      <td><input name=dunningto id=dunningto size=11 title="$myconfig{dateformat}">
+      <td><input name=dunningto id=dunningto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
       <input type=button name=dunningto id="trigger4" value=|
       . $locale->text('button') . qq|></td>
       |;
@@ -727,19 +720,24 @@ sub search {
 
     # without JavaScript Calendar
     $button1 =
-      qq|<td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
+      qq|<td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
     $button2 =
-      qq|<td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
-    $button1 =
-      qq|<td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}"></td>|;
-    $button1 =
-      qq|<td><input name=dunningfrom id=dunningto size=11 title="$myconfig{dateformat}"></td>|;
+      qq|<td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
+    $button3 =
+      qq|<td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
+    $button4 =
+      qq|<td><input name=dunningfrom id=dunningto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
   }
 
   $form->{fokus} = "search.customer";
+  $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
   $form->header;
+  $onload = qq|focus()|;
+  $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
+  $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
+
   print qq|
-<body onLoad="fokus()">
+<body onLoad="$onload">
 
 <form method=post name="search" action=$form->{script}>
 
@@ -780,7 +778,6 @@ sub search {
           $button4
         </tr>
 
-        <input type=hidden name=sort value=transdate>
       </table>
     </td>
   </tr>
@@ -801,7 +798,6 @@ sub search {
 
 <input type=hidden name=nextsub value=$form->{nextsub}>
 
-<input type=hidden name=path value=$form->{path}>
 <input type=hidden name=login value=$form->{login}>
 <input type=hidden name=password value=$form->{password}>
 
@@ -833,7 +829,7 @@ sub show_dunning {
 
   
   $form->{callback} =
-    "$form->{script}?action=show_dunning&path=$form->{path}&login=$form->{login}&password=$form->{password}&customer=$form->{customer}&invnumber=$form->{invnumber}&ordnumber=$form->{ordnumber}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&dunningfrom=$form->{dunningfrom}&dunningto=$form->{dunningto}&notes=$form->{notes}&showold=$form->{showold}&dunning_level=$form->{dunning_level}"
+    "$form->{script}?action=show_dunning&login=$form->{login}&password=$form->{password}&customer=$form->{customer}&invnumber=$form->{invnumber}&ordnumber=$form->{ordnumber}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&dunningfrom=$form->{dunningfrom}&dunningto=$form->{dunningto}&notes=$form->{notes}&showold=$form->{showold}&dunning_level=$form->{dunning_level}"
     unless $form->{callback};
 
   @column_index = qw(dunning_description customername invnumber invdate inv_duedate invamount dunning_date next_duedate fee interest );
@@ -876,7 +872,7 @@ sub show_dunning {
     . qq|</th>|;
   $column_header{interest} =
       qq|<th class=listheading>|
-    . $locale->text('Total Interest')
+    . $locale->text('Interest')
     . qq|</th>|;
 
   $form->header;
@@ -891,7 +887,7 @@ sub show_dunning {
 
 <table width=100%>
   <tr>
-    <th class=listtop colspan=9>$form->{title}</th>
+    <th class=listtop colspan=10>$form->{title}</th>
   </tr>
   <tr height="5"></tr>
   <tr>|;
@@ -900,11 +896,28 @@ sub show_dunning {
   print qq|
         </tr>
 |;
+
+  my %columns = (
+    "dunning_duedate" => "next_duedate",
+    "duedate" => "inv_duedate",
+    "transdate" => "invdate",
+    "amount" => "invamount",
+    );
+
   my $i = 0;
+  my $j = 0;
+  my ($previous_dunning_id, $first_row_for_dunning);
   foreach $ref (@{ $form->{DUNNINGS} }) {
-
     $i++;
-    my $j = $i % 2;
+
+    if ($previous_dunning_id != $ref->{dunning_id}) {
+      $j++;
+      $j = $j % 2;
+      $first_row_for_dunning = 1;
+    } else {
+      $first_row_for_dunning = 0;
+    }
+    $previous_dunning_id = $ref->{dunning_id};
 
     print qq|
         <tr valign=top class=listrow$j>
@@ -912,23 +925,27 @@ sub show_dunning {
 
   
 
-  $dunning = qq|<select name=next_dunning_id_$i>$form->{selectdunning}</select>|;
-
-
-    $column_data{dunning_description}           = qq|<td>$ref->{dunning_description}</td>|;
-    my $active = "checked";
-    $column_data{dunning_date}           = qq|<td>$ref->{dunning_date}</td>|;
-    $column_data{next_duedate}           = qq|<td>$ref->{dunning_duedate}</td>|;
-
-    $column_data{inv_duedate}           = qq|<td>$ref->{duedate}</td>|;
-    $column_data{invdate}           = qq|<td>$ref->{transdate}</td>|;
-    $column_data{invnumber}           = qq|<td><a href=ar.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$form->{callback}>$ref->{invnumber}</a></td>|;
-    $column_data{customername}           = qq|<td>$ref->{customername}</td>|;
-    $column_data{invamount}           = qq|<td>$ref->{amount}</td>|;
-    $column_data{fee}           = qq|<td>$ref->{fee}</td>|;
-    $column_data{interest}           = qq|<td>$ref->{interest}</td>|;
+    foreach (qw(dunning_date dunning_duedate duedate transdate customername amount fee interest)) {
+      my $col = $columns{$_} ? $columns{$_} : $_;
+      $column_data{$col} = "<td>" . H($ref->{$_}) . "</td>";
+    }
 
+    if ($first_row_for_dunning) {
+      $column_data{dunning_description} =
+        qq|<td><a href="dn.pl?action=print_dunning&format=pdf&media=screen&| .
+        qq|dunning_id=| . E($ref->{dunning_id}) .
+        join("", map({ "&${_}=" . E($form->{$_}) } qw(login password callback))) .
+        qq|">| . H($ref->{dunning_description}) . qq|</a></td>|;
+    } else {
+      $column_data{dunning_description} = qq|<td>&nbsp;</td>|;
+      $column_data{customername} = qq|<td>&nbsp;</td>|;
+    }
 
+    $column_data{invnumber} =
+      qq|<td><a href="| . ($ref->{invoice} ? "is.pl" : "ar.pl" ) .
+      qq|?action=edit&id=| . H($ref->{id}) .
+      join("", map({ "&${_}=" . E($form->{$_}) } qw(login password callback))) .
+      qq|">| . H($ref->{invnumber}) . qq|</a></td>|;
 
     map { print "$column_data{$_}\n" } @column_index;
 
@@ -956,17 +973,8 @@ sub show_dunning {
 <input name=nextsub type=hidden value="$form->{nextsub}">
 
 
-<input type=hidden name=path value=$form->{path}>
 <input type=hidden name=login value=$form->{login}>
 <input type=hidden name=password value=$form->{password}>
-|;
-
-  if ($form->{menubar}) {
-    require "$form->{path}/menu.pl";
-    &menubar;
-  }
-
-  print qq|
 
   </form>
 
@@ -978,5 +986,21 @@ sub show_dunning {
   $lxdebug->leave_sub();
 
 }
+
+sub print_dunning {
+  $lxdebug->enter_sub();
+
+  DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $userspath, $spool, $sendmail);
+
+  if($form->{DUNNING_PDFS}) {
+    DN->melt_pdfs(\%myconfig, \%$form,$spool);
+  } else {
+    $form->redirect($locale->text('Could not create dunning copy!'));
+  }
+
+  $lxdebug->leave_sub();
+
+}
+
 # end of main