1 #======================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (C) 1998-2002
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  17 # This program is free software; you can redistribute it and/or modify
 
  18 # it under the terms of the GNU General Public License as published by
 
  19 # the Free Software Foundation; either version 2 of the License, or
 
  20 # (at your option) any later version.
 
  22 # This program is distributed in the hope that it will be useful,
 
  23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  25 # GNU General Public License for more details.
 
  26 # You should have received a copy of the GNU General Public License
 
  27 # along with this program; if not, write to the Free Software
 
  28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  29 #======================================================================
 
  31 # Dunning process module
 
  33 #======================================================================
 
  47   $main::lxdebug->enter_sub();
 
  49   my ($self, $myconfig, $form) = @_;
 
  52   my $dbh = $form->dbconnect($myconfig);
 
  56     qq|FROM dunning_config | .
 
  57     qq|ORDER BY dunning_level|;
 
  58   $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
 
  60   foreach my $ref (@{ $form->{DUNNING} }) {
 
  61     $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
 
  62     $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
 
  66     qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar
 
  68   ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
 
  72   $main::lxdebug->leave_sub();
 
  76   $main::lxdebug->enter_sub();
 
  78   my ($self, $myconfig, $form) = @_;
 
  81   my $dbh = $form->dbconnect_noauto($myconfig);
 
  85   for my $i (1 .. $form->{rowcount}) {
 
  86     $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
 
  87     $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
 
  89     if (($form->{"dunning_level_$i"} ne "") &&
 
  90         ($form->{"dunning_description_$i"} ne "")) {
 
  91       @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
 
  92                  $form->{"email_subject_$i"}, $form->{"email_body_$i"},
 
  93                  $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
 
  94                  $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
 
  95                  $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}),
 
  96                  $form->{"create_invoices_for_fees_$i"} ? 't' : 'f');
 
  97       if ($form->{"id_$i"}) {
 
  99           qq|UPDATE dunning_config SET
 
 100                dunning_level = ?, dunning_description = ?,
 
 101                email_subject = ?, email_body = ?,
 
 102                template = ?, fee = ?, interest_rate = ?,
 
 103                active = ?, auto = ?, email = ?,
 
 104                email_attachment = ?, payment_terms = ?, terms = ?,
 
 105                create_invoices_for_fees = ?
 
 107         push(@values, conv_i($form->{"id_$i"}));
 
 110           qq|INSERT INTO dunning_config
 
 111                (dunning_level, dunning_description, email_subject, email_body,
 
 112                 template, fee, interest_rate, active, auto, email,
 
 113                 email_attachment, payment_terms, terms, create_invoices_for_fees)
 
 114              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 116       do_query($form, $dbh, $query, @values);
 
 119     if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
 
 120       $query = qq|DELETE FROM dunning_config WHERE id = ?|;
 
 121       do_query($form, $dbh, $query, $form->{"id_$i"});
 
 125   $query  = qq|UPDATE defaults SET dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?|;
 
 126   @values = (conv_i($form->{AR_amount_fee}), conv_i($form->{AR_amount_interest}), conv_i($form->{AR}));
 
 127   do_query($form, $dbh, $query, @values);
 
 132   $main::lxdebug->leave_sub();
 
 135 sub create_invoice_for_fees {
 
 136   $main::lxdebug->enter_sub();
 
 138   my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
 
 140   my ($query, @values, $sth, $ref);
 
 142   $query = qq|SELECT dcfg.create_invoices_for_fees
 
 144               LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
 
 145               WHERE d.dunning_id = ?|;
 
 146   my ($create_invoices_for_fees) = selectrow_query($form, $dbh, $query, $dunning_id);
 
 148   if (!$create_invoices_for_fees) {
 
 149     $main::lxdebug->leave_sub();
 
 153   $query = qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar FROM defaults|;
 
 154   ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
 
 160            SELECT MAX(d_fee.fee)
 
 162            WHERE (d_fee.trans_id   =  d.trans_id)
 
 163              AND (d_fee.dunning_id <> ?)
 
 164              AND NOT (d_fee.fee_interest_ar_id ISNULL)
 
 169            SELECT MAX(d_interest.interest)
 
 170            FROM dunning d_interest
 
 171            WHERE (d_interest.trans_id   =  d.trans_id)
 
 172              AND (d_interest.dunning_id <> ?)
 
 173              AND NOT (d_interest.fee_interest_ar_id ISNULL)
 
 175          AS max_previous_interest
 
 177        WHERE dunning_id = ?|;
 
 178   @values = ($dunning_id, $dunning_id, $dunning_id);
 
 179   $sth = prepare_execute_query($form, $dbh, $query, @values);
 
 181   my ($fee_remaining, $interest_remaining) = (0, 0);
 
 182   my ($fee_total, $interest_total) = (0, 0);
 
 184   while (my $ref = $sth->fetchrow_hashref()) {
 
 185     $fee_remaining      += $form->round_amount($ref->{fee}, 2);
 
 186     $fee_remaining      -= $form->round_amount($ref->{max_previous_fee}, 2);
 
 187     $fee_total          += $form->round_amount($ref->{fee}, 2);
 
 188     $interest_remaining += $form->round_amount($ref->{interest}, 2);
 
 189     $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
 
 190     $interest_total     += $form->round_amount($ref->{interest}, 2);
 
 195   my $amount = $fee_remaining + $interest_remaining;
 
 198     $main::lxdebug->leave_sub();
 
 202   my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
 
 203   my $curr = $form->get_default_currency($myconfig);
 
 206     qq|INSERT INTO ar (id,          invnumber, transdate, gldate, customer_id,
 
 207                        taxincluded, amount,    netamount, paid,   duedate,
 
 208                        invoice,     curr,      notes,
 
 213          current_date,          -- transdate
 
 214          current_date,          -- gldate
 
 216          (SELECT ar.customer_id
 
 218           LEFT JOIN ar ON (dn.trans_id = ar.id)
 
 219           WHERE dn.dunning_id = ?
 
 226          (SELECT duedate FROM dunning WHERE dunning_id = ? LIMIT 1),
 
 231          (SELECT id FROM employee WHERE login = ?)
 
 233   @values = ($ar_id,            # id
 
 234              $form->update_defaults($myconfig, 'invnumber', $dbh), # invnumber
 
 235              $dunning_id,       # customer_id
 
 238              $dunning_id,       # duedate
 
 239              $curr,             # default currency
 
 240              sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
 
 241              $form->{login});   # employee_id
 
 242   do_query($form, $dbh, $query, @values);
 
 245     qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey)
 
 246        VALUES (?, ?, ?, current_date, current_date, 0)|;
 
 247   $sth = prepare_query($form, $dbh, $query);
 
 249   @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining);
 
 250   do_statement($form, $sth, $query, @values);
 
 252   if ($interest_remaining) {
 
 253     @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining);
 
 254     do_statement($form, $sth, $query, @values);
 
 257   @values = ($ar_id, conv_i($form->{AR}), -1 * $amount);
 
 258   do_statement($form, $sth, $query, @values);
 
 262   $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
 
 263   do_query($form, $dbh, $query, $ar_id, $dunning_id);
 
 265   $main::lxdebug->leave_sub();
 
 269   $main::lxdebug->enter_sub();
 
 271   my ($self, $myconfig, $form, $rows, $userspath, $spool) = @_;
 
 272   # connect to database
 
 273   my $dbh = $form->dbconnect_noauto($myconfig);
 
 275   my ($query, @values);
 
 277   my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
 
 279   my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
 
 280   my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
 
 282   my $q_insert_dunning =
 
 283     qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
 
 284                             fee,        interest,          transdate,     duedate)
 
 286                (SELECT dunning_level FROM dunning_config WHERE id = ?),
 
 290                 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
 
 291                (SELECT (amount - paid) * (current_date - transdate) FROM ar WHERE id = ?)
 
 292                  * (SELECT interest_rate FROM dunning_config WHERE id = ?)
 
 295                current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
 
 296   my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
 
 299   my ($next_dunning_config_id, $customer_id);
 
 302   foreach my $row (@{ $rows }) {
 
 303     push @invoice_ids, $row->{invoice_id};
 
 304     $next_dunning_config_id = $row->{next_dunning_config_id};
 
 305     $customer_id            = $row->{customer_id};
 
 307     @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
 
 308     do_statement($form, $h_update_ar, $q_update_ar, @values);
 
 310     $send_email |= $row->{email};
 
 312     my $next_config_id = conv_i($row->{next_dunning_config_id});
 
 313     my $invoice_id     = conv_i($row->{invoice_id});
 
 315     @values = ($dunning_id,     $next_config_id, $next_config_id,
 
 316                $invoice_id,     $next_config_id, $invoice_id,
 
 317                $next_config_id, $next_config_id);
 
 318     do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
 
 321   $h_update_ar->finish();
 
 322   $h_insert_dunning->finish();
 
 324   $form->{DUNNING_PDFS_EMAIL} = [];
 
 326   $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
 
 328   $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
 
 329   $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
 
 331   $form->{dunning_id} = $dunning_id;
 
 334     $self->send_email($myconfig, $form, $dunning_id, $dbh);
 
 340   $main::lxdebug->leave_sub();
 
 344   $main::lxdebug->enter_sub();
 
 346   my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
 
 350          dcfg.email_body,     dcfg.email_subject, dcfg.email_attachment,
 
 354        LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
 
 355        LEFT JOIN ar                  ON (d.trans_id          = ar.id)
 
 356        LEFT JOIN customer c          ON (ar.customer_id      = c.id)
 
 357        WHERE (d.dunning_id = ?)
 
 359   my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
 
 361   if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
 
 362     $main::lxdebug->leave_sub();
 
 366   my $template     = PlainTextTemplate->new(undef, $form, $myconfig);
 
 367   my $mail         = Mailer->new();
 
 368   $mail->{from}    = $myconfig->{email};
 
 369   $mail->{to}      = $ref->{recipient};
 
 370   $mail->{subject} = $template->parse_block($ref->{email_subject});
 
 371   $mail->{message} = $template->parse_block($ref->{email_body});
 
 373   if ($myconfig->{signature}) {
 
 374     $mail->{message} .= "\n-- \n$myconfig->{signature}";
 
 377   $mail->{message} =~ s/\r\n/\n/g;
 
 379   if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
 
 380     $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
 
 385   $main::lxdebug->leave_sub();
 
 388 sub set_template_options {
 
 389   $main::lxdebug->enter_sub();
 
 391   my ($self, $myconfig, $form) = @_;
 
 393   $form->{templates}    = "$myconfig->{templates}";
 
 394   $form->{language}     = $form->get_template_language($myconfig);
 
 395   $form->{printer_code} = $form->get_printer_code($myconfig);
 
 397   if ($form->{language} ne "") {
 
 398     $form->{language} = "_" . $form->{language};
 
 401   if ($form->{printer_code} ne "") {
 
 402     $form->{printer_code} = "_" . $form->{printer_code};
 
 405   $form->{IN}  = "$form->{formname}$form->{language}$form->{printer_code}.html";
 
 408   if ($form->{"format"} =~ /opendocument/) {
 
 409     $form->{IN} =~ s/html$/odt/;
 
 411     $form->{IN} =~ s/html$/tex/;
 
 414   $main::lxdebug->leave_sub();
 
 419   $main::lxdebug->enter_sub();
 
 421   my ($self, $myconfig, $form) = @_;
 
 423   # connect to database
 
 424   my $dbh = $form->dbconnect($myconfig);
 
 429   $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
 
 431   if ($form->{customer_id}) {
 
 432     $where .= qq| AND (a.customer_id = ?)|;
 
 433     push(@values, $form->{customer_id});
 
 435   } elsif ($form->{customer}) {
 
 436     $where .= qq| AND (ct.name ILIKE ?)|;
 
 437     push(@values, '%' . $form->{customer} . '%');
 
 441     "ordnumber" => "a.ordnumber",
 
 442     "invnumber" => "a.invnumber",
 
 443     "notes"     => "a.notes",
 
 445   foreach my $key (keys(%columns)) {
 
 446     next unless ($form->{$key});
 
 447     $where .= qq| AND $columns{$key} ILIKE ?|;
 
 448     push(@values, '%' . $form->{$key} . '%');
 
 451   if ($form->{dunning_level}) {
 
 452     $where .= qq| AND nextcfg.id = ?|;
 
 453     push(@values, conv_i($form->{dunning_level}));
 
 456   $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
 
 457   if ($form->{minamount}) {
 
 458     $where .= qq| AND ((a.amount - a.paid) > ?) |;
 
 459     push(@values, $form->{minamount});
 
 465        WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
 
 466   my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
 
 470          a.id, a.ordnumber, a.transdate, a.invnumber, a.amount,
 
 471          ct.name AS customername, a.customer_id, a.duedate,
 
 473          cfg.dunning_description, cfg.dunning_level,
 
 475          d.transdate AS dunning_date, d.duedate AS dunning_duedate,
 
 478          a.duedate + cfg.terms - current_date AS nextlevel,
 
 479          current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
 
 480          current_date + cfg.payment_terms AS next_duedate,
 
 482          nextcfg.dunning_description AS next_dunning_description,
 
 483          nextcfg.id AS next_dunning_config_id,
 
 484          nextcfg.terms, nextcfg.active, nextcfg.email
 
 488        LEFT JOIN customer ct ON (a.customer_id = ct.id)
 
 489        LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
 
 490        LEFT JOIN dunning_config nextcfg ON
 
 495               WHERE dunning_level >
 
 496                 COALESCE((SELECT dunning_level
 
 498                           WHERE id = a.dunning_config_id
 
 499                           ORDER BY dunning_level DESC
 
 502               ORDER BY dunning_level ASC
 
 505        LEFT JOIN dunning d ON ((d.trans_id = a.id) AND (cfg.dunning_level = d.dunning_level))
 
 507        WHERE (a.paid < a.amount)
 
 508          AND (a.duedate < current_date)
 
 512        ORDER BY a.id, transdate, duedate, name|;
 
 513   my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
 
 515   $form->{DUNNINGS} = [];
 
 517   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 518     next if ($ref->{pastdue} < $ref->{terms});
 
 520     $ref->{interest} = $form->round_amount($ref->{interest}, 2);
 
 521     push(@{ $form->{DUNNINGS} }, $ref);
 
 526   $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
 
 527   $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
 
 530   $main::lxdebug->leave_sub();
 
 535   $main::lxdebug->enter_sub();
 
 537   my ($self, $myconfig, $form) = @_;
 
 539   # connect to database
 
 540   my $dbh = $form->dbconnect($myconfig);
 
 542   my $where = qq| WHERE (da.trans_id = a.id)|;
 
 546   if ($form->{customer_id}) {
 
 547     $where .= qq| AND (a.customer_id = ?)|;
 
 548     push(@values, $form->{customer_id});
 
 550   } elsif ($form->{customer}) {
 
 551     $where .= qq| AND (ct.name ILIKE ?)|;
 
 552     push(@values, '%' . $form->{customer} . '%');
 
 556     "ordnumber" => "a.ordnumber",
 
 557     "invnumber" => "a.invnumber",
 
 558     "notes" => "a.notes",
 
 560   foreach my $key (keys(%columns)) {
 
 561     next unless ($form->{$key});
 
 562     $where .= qq| AND $columns{$key} ILIKE ?|;
 
 563     push(@values, '%' . $form->{$key} . '%');
 
 566   if ($form->{dunning_level}) {
 
 567     $where .= qq| AND a.dunning_config_id = ?|;
 
 568     push(@values, conv_i($form->{dunning_level}));
 
 571   if ($form->{department_id}) {
 
 572     $where .= qq| AND a.department_id = ?|;
 
 573     push @values, conv_i($form->{department_id});
 
 576   $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
 
 577   if ($form->{minamount}) {
 
 578     $where .= qq| AND ((a.amount - a.paid) > ?) |;
 
 579     push(@values, $form->{minamount});
 
 582   if (!$form->{showold}) {
 
 583     $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
 
 586   if ($form->{transdatefrom}) {
 
 587     $where .= qq| AND a.transdate >= ?|;
 
 588     push(@values, $form->{transdatefrom});
 
 590   if ($form->{transdateto}) {
 
 591     $where .= qq| AND a.transdate <= ?|;
 
 592     push(@values, $form->{transdateto});
 
 594   if ($form->{dunningfrom}) {
 
 595     $where .= qq| AND da.transdate >= ?|;
 
 596     push(@values, $form->{dunningfrom});
 
 598   if ($form->{dunningto}) {
 
 599     $where .= qq| AND da.transdate >= ?|;
 
 600     push(@values, $form->{dunningto});
 
 604     'dunning_description' => [ qw(dn.dunning_description customername invnumber) ],
 
 605     'customername'        => [ qw(customername invnumber) ],
 
 606     'invnumber'           => [ qw(a.invnumber) ],
 
 607     'transdate'           => [ qw(a.transdate a.invnumber) ],
 
 608     'duedate'             => [ qw(a.duedate a.invnumber) ],
 
 609     'dunning_date'        => [ qw(dunning_date a.invnumber) ],
 
 610     'dunning_duedate'     => [ qw(dunning_duedate a.invnumber) ],
 
 613   my $sortdir   = !defined $form->{sortdir}    ? 'ASC'         : $form->{sortdir} ? 'ASC' : 'DESC';
 
 614   my $sortkey   = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername';
 
 615   my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
 
 618     qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount,
 
 619          ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
 
 620          da.interest, dn.dunning_description, da.transdate AS dunning_date,
 
 621          da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id
 
 623        JOIN customer ct ON (a.customer_id = ct.id), dunning da
 
 624        LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
 
 626        ORDER BY $sortorder|;
 
 628   $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
 
 630   foreach my $ref (@{ $form->{DUNNINGS} }) {
 
 631     map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
 
 635   $main::lxdebug->leave_sub();
 
 640   $main::lxdebug->enter_sub();
 
 642   my ($self, $myconfig, $form, $copies) = @_;
 
 644   # Don't allow access outside of $spool.
 
 645   map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
 
 648   $copies         = 1 unless $copies;
 
 649   my $inputfiles  = join " ", map { "${main::spool}/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
 
 650   my $dunning_id  = $form->{dunning_id};
 
 652   $dunning_id     =~ s|[^\d]||g;
 
 654   my $in = IO::File->new("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
 
 655   $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
 
 659   if ($form->{media} eq 'printer') {
 
 660     $form->get_printer_code($myconfig);
 
 661     if ($form->{printer_command}) {
 
 662       $out = IO::File->new("| $form->{printer_command}");
 
 665     $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
 
 668     my $dunning_filename = $form->get_formname_translation('dunning');
 
 669     $out = IO::File->new('>-');
 
 670     $out->print(qq|Content-Type: Application/PDF\n| .
 
 671                 qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|);
 
 674   while (my $line = <$in>) {
 
 681   map { unlink("${main::spool}/$_") } @{ $form->{DUNNING_PDFS} };
 
 683   $main::lxdebug->leave_sub();
 
 687   $main::lxdebug->enter_sub();
 
 689   my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
 
 691   # connect to database
 
 692   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
 
 694   $dunning_id =~ s|[^\d]||g;
 
 699          da.transdate  AS dunning_date,
 
 700          da.duedate    AS dunning_duedate,
 
 702          dcfg.template AS formname,
 
 703          dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
 
 705          ar.transdate,       ar.duedate,      ar.customer_id,
 
 706          ar.invnumber,       ar.ordnumber,    ar.cp_id,
 
 707          ar.amount,          ar.netamount,    ar.paid,
 
 708          ar.amount - ar.paid AS open_amount,
 
 709          ar.amount - ar.paid + da.fee + da.interest AS linetotal
 
 712        LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
 
 713        LEFT JOIN ar ON (ar.id = da.trans_id)
 
 714        WHERE (da.dunning_id = ?)|;
 
 716   my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
 
 718   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 720       $form->{TEMPLATE_ARRAYS} = {};
 
 721       map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref}));
 
 724     map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
 
 725     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 726     map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
 
 732          c.id AS customer_id, c.name,         c.street,       c.zipcode,   c.city,
 
 733          c.country,           c.department_1, c.department_2, c.email,     c.customernumber,
 
 734          c.greeting,          c.contact,      c.phone,        c.fax,       c.homepage,
 
 735          c.email,             c.taxincluded,  c.business_id,  c.taxnumber, c.iban,
 
 738        LEFT JOIN ar          ON (d.trans_id = ar.id)
 
 739        LEFT JOIN customer c  ON (ar.customer_id = c.id)
 
 740        LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
 
 741        WHERE (d.dunning_id = ?)
 
 743   my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
 
 744   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 748          cfg.interest_rate, cfg.template AS formname,
 
 749          cfg.email_subject, cfg.email_body, cfg.email_attachment,
 
 750          d.transdate AS dunning_date,
 
 753           WHERE dunning_id = ?)
 
 755          (SELECT SUM(interest)
 
 757           WHERE dunning_id = ?)
 
 759          (SELECT SUM(amount) - SUM(paid)
 
 764              WHERE dunning_id = ?))
 
 767        LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
 
 768        WHERE d.dunning_id = ?
 
 770   $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
 
 771   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 773   $form->{interest_rate}     = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
 
 774   $form->{fee}               = $form->format_amount($myconfig, $ref->{fee}, 2);
 
 775   $form->{total_interest}    = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
 
 776   $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
 
 777   $form->{total_amount}      = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
 
 779   $self->set_template_options($myconfig, $form);
 
 781   my $filename          = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
 
 782   $form->{OUT}          = ">${main::spool}/$filename";
 
 783   $form->{keep_tmpfile} = 1;
 
 785   delete $form->{tmpfile};
 
 787   push @{ $form->{DUNNING_PDFS} }, $filename;
 
 788   push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${main::spool}/$filename",
 
 789                                            'name'     => "dunning_${dunning_id}.pdf" };
 
 791   $form->parse_template($myconfig, $main::userspath);
 
 793   $dbh->disconnect() unless $provided_dbh;
 
 795   $main::lxdebug->leave_sub();
 
 798 sub print_invoice_for_fees {
 
 799   $main::lxdebug->enter_sub();
 
 801   my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
 
 803   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
 
 805   my ($query, @values, $sth);
 
 809          d.fee_interest_ar_id,
 
 812        LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
 
 813        WHERE d.dunning_id = ?|;
 
 814   my ($ar_id, $template) = selectrow_query($form, $dbh, $query, $dunning_id);
 
 817     $main::lxdebug->leave_sub();
 
 821   my $saved_form = save_form();
 
 823   $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
 
 824   my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
 
 828          ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
 
 829          ar.duedate,   ar.notes,     ar.notes AS invoicenotes,
 
 831          c.name,      c.department_1,   c.department_2, c.street, c.zipcode, c.city, c.country,
 
 832          c.contact,   c.customernumber, c.phone,        c.fax,    c.email,
 
 833          c.taxnumber, c.greeting
 
 836        LEFT JOIN customer c ON (ar.customer_id = c.id)
 
 838   my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
 
 839   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 841   $query = qq|SELECT * FROM employee WHERE login = ?|;
 
 842   $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{login});
 
 843   map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
 
 845   $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|;
 
 846   $sth   = prepare_execute_query($form, $dbh, $query, $ar_id);
 
 848   my ($row, $fee, $interest) = (0, 0, 0);
 
 850   while ($ref = $sth->fetchrow_hashref()) {
 
 851     next if ($ref->{amount} < 0);
 
 856       $fee = $ref->{amount};
 
 858       $interest = $ref->{amount};
 
 862   $form->{fee}        = $form->round_amount($fee,             2);
 
 863   $form->{interest}   = $form->round_amount($interest,        2);
 
 864   $form->{invamount}  = $form->round_amount($fee + $interest, 2);
 
 865   $form->{dunning_id} = $dunning_id;
 
 866   $form->{formname}   = "${template}_invoice";
 
 868   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
 
 870   $self->set_template_options($myconfig, $form);
 
 872   my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
 
 874   $form->{OUT}          = ">$main::spool/$filename";
 
 875   $form->{keep_tmpfile} = 1;
 
 876   delete $form->{tmpfile};
 
 878   map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
 
 880   $form->parse_template($myconfig, $main::userspath);
 
 882   restore_form($saved_form);
 
 884   push @{ $form->{DUNNING_PDFS} }, $filename;
 
 885   push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${main::spool}/$filename",
 
 886                                            'name'     => "dunning_invoice_${dunning_id}.pdf" };
 
 888   $dbh->disconnect() unless $provided_dbh;
 
 890   $main::lxdebug->leave_sub();