Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 232a9153

Von Moritz Bunkus vor mehr als 17 Jahren hinzugefügt

  • ID 232a9153b591c815dacc4b41c15135166be550f0
  • Vorgänger c03fba33
  • Nachfolger bdc416d1

Mahnwesen:
1. Neues Feature: Automatisches Erzeugen von Debitorenrechnungen über die Mahngebühren und -zinsen. Diese werden ebenfalls als PDFs ausgegeben.
2. Neues Feature: Beim Bericht über aktive Mahnungen ermöglichen, dass mehrere Mahnungen und die eventuell dazu erstellen Debitorenrechnungen auf einmal ausgedruckt werden können.
3. Neues Feature: Mahnungen können wahlweise am Bildschirm oder direkt auf Druckern ausgegeben werden. Zusätzlich können andere Sprachen ausgewählt werden.
4. Bugfix: Beim Bearbeiten von Emaileinstellungen in der Mahnkonfiguration wurden +-Zeichen falsch escapet und verschwanden.
5. Code zum Ersetzen von $form-Variablen in Strings entfernt und durch die Verwendung von PlainTextTemplate ersetzt.
Fixes für Bugs 473 und 553.

Unterschiede anzeigen:

SL/DN.pm
34 34

  
35 35
package DN;
36 36

  
37
use SL::Template;
38
use SL::IS;
39 37
use SL::Common;
40 38
use SL::DBUtils;
41
use Data::Dumper;
39
use SL::IS;
40
use SL::Mailer;
41
use SL::MoreCommon;
42
use SL::Template;
42 43

  
43 44
sub get_config {
44 45
  $main::lxdebug->enter_sub();
......
59 60
    $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
60 61
  }
61 62

  
63
  $query =
64
    qq|SELECT
65
         dunning_create_invoices_for_fees, dunning_ar_amount_fee,
66
         dunning_ar_amount_interest,       dunning_ar
67
       FROM defaults|;
68
  ($form->{create_invoices_for_fees}, $form->{AR_amount_fee},
69
   $form->{AR_amount_interest},       $form->{AR}           ) = selectrow_query($form, $dbh, $query);
70

  
62 71
  $dbh->disconnect();
63 72

  
64 73
  $main::lxdebug->leave_sub();
......
112 121
    }
113 122
  }
114 123

  
115
  $dbh->commit;
116
  $dbh->disconnect;
124
  $query  = qq|UPDATE defaults SET dunning_create_invoices_for_fees = ?|;
125
  @values = ($form->{create_invoices_for_fees} ? 't' : 'f');
126

  
127
  if ($form->{create_invoices_for_fees}) {
128
    $query .= qq|, dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?|;
129
    push @values, conv_i($form->{AR_amount_fee}), conv_i($form->{AR_amount_interest}), conv_i($form->{AR});
130
  }
131

  
132
  do_query($form, $dbh, $query, @values);
133

  
134
  $dbh->commit();
135
  $dbh->disconnect();
136

  
137
  $main::lxdebug->leave_sub();
138
}
139

  
140
sub create_invoice_for_fees {
141
  $main::lxdebug->enter_sub();
142

  
143
  my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
144

  
145
  my ($query, @values, $sth, $ref);
146

  
147
  $query =
148
    qq|SELECT
149
         dunning_create_invoices_for_fees, dunning_ar_amount_fee,
150
         dunning_ar_amount_interest, dunning_ar
151
       FROM defaults|;
152
  ($form->{create_invoices_for_fees}, $form->{AR_amount_fee},
153
   $form->{AR_amount_interest},       $form->{AR}           ) = selectrow_query($form, $dbh, $query);
154

  
155
  if (!$form->{create_invoices_for_fees}) {
156
    $main::lxdebug->leave_sub();
157
    return;
158
  }
159

  
160
  $query =
161
    qq|SELECT
162
         fee,
163
         COALESCE((
164
           SELECT MAX(d_fee.fee)
165
           FROM dunning d_fee
166
           WHERE (d_fee.trans_id   =  d.trans_id)
167
             AND (d_fee.dunning_id <> ?)
168
             AND NOT (d_fee.fee_interest_ar_id ISNULL)
169
         ), 0)
170
         AS max_previous_fee,
171
         interest,
172
         COALESCE((
173
           SELECT MAX(d_interest.interest)
174
           FROM dunning d_interest
175
           WHERE (d_interest.trans_id   =  d.trans_id)
176
             AND (d_interest.dunning_id <> ?)
177
             AND NOT (d_interest.fee_interest_ar_id ISNULL)
178
         ), 0)
179
         AS max_previous_interest
180
       FROM dunning d
181
       WHERE dunning_id = ?|;
182
  @values = ($dunning_id, $dunning_id, $dunning_id);
183
  $sth = prepare_execute_query($form, $dbh, $query, @values);
184

  
185
  my ($fee_remaining, $interest_remaining) = (0, 0);
186
  my ($fee_total, $interest_total) = (0, 0);
187

  
188
  while (my $ref = $sth->fetchrow_hashref()) {
189
    $fee_remaining      += $form->round_amount($ref->{fee}, 2);
190
    $fee_remaining      -= $form->round_amount($ref->{max_previous_fee}, 2);
191
    $fee_total          += $form->round_amount($ref->{fee}, 2);
192
    $interest_remaining += $form->round_amount($ref->{interest}, 2);
193
    $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
194
    $interest_total     += $form->round_amount($ref->{interest}, 2);
195
  }
196

  
197
  $sth->finish();
198

  
199
  my $amount = $fee_remaining + $interest_remaining;
200

  
201
  if (!$amount) {
202
    $main::lxdebug->leave_sub();
203
    return;
204
  }
205

  
206
  my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
207

  
208
  $query =
209
    qq|INSERT INTO ar (id,          invnumber, transdate, gldate, customer_id,
210
                       taxincluded, amount,    netamount, paid,   duedate,
211
                       invoice,     curr,      notes,
212
                       employee_id)
213
       VALUES (
214
         ?,                     -- id
215
         ?,                     -- invnumber
216
         current_date,          -- transdate
217
         current_date,          -- gldate
218
         -- customer_id:
219
         (SELECT ar.customer_id
220
          FROM dunning dn
221
          LEFT JOIN ar ON (dn.trans_id = ar.id)
222
          WHERE dn.dunning_id = ?
223
          LIMIT 1),
224
         'f',                   -- taxincluded
225
         ?,                     -- amount
226
         ?,                     -- netamount
227
         0,                     -- paid
228
         -- duedate:
229
         (SELECT duedate FROM dunning WHERE dunning_id = ?),
230
         'f',                   -- invoice
231
         ?,                     -- curr
232
         ?,                     -- notes
233
         -- employee_id:
234
         (SELECT id FROM employee WHERE login = ?)
235
       )|;
236
  @values = ($ar_id,            # id
237
             $form->update_defaults($myconfig, 'invnumber', $dbh), # invnumber
238
             $dunning_id,       # customer_id
239
             $amount,
240
             $amount,
241
             $dunning_id,       # duedate
242
             (split m/:/, $myconfig->{currency})[0], # currency
243
             sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
244
             $form->{login});   # employee_id
245
  do_query($form, $dbh, $query, @values);
246

  
247
  $query =
248
    qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey)
249
       VALUES (?, ?, ?, current_date, current_date, 0)|;
250
  $sth = prepare_query($form, $dbh, $query);
251

  
252
  @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining);
253
  do_statement($form, $sth, $query, @values);
254

  
255
  if ($interest_remaining) {
256
    @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining);
257
    do_statement($form, $sth, $query, @values);
258
  }
259

  
260
  @values = ($ar_id, conv_i($form->{AR}), -1 * $amount);
261
  do_statement($form, $sth, $query, @values);
262

  
263
  $sth->finish();
264

  
265
  $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
266
  do_query($form, $dbh, $query, $ar_id, $dunning_id);
117 267

  
118 268
  $main::lxdebug->leave_sub();
119 269
}
......
133 283
  my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
134 284

  
135 285
  my $q_insert_dunning =
136
    qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level,
137
                            trans_id, fee, interest, transdate, duedate)
286
    qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
287
                            fee,        interest,          transdate,     duedate)
138 288
       VALUES (?, ?,
139 289
               (SELECT dunning_level FROM dunning_config WHERE id = ?),
140 290
               ?,
......
174 324
  $h_update_ar->finish();
175 325
  $h_insert_dunning->finish();
176 326

  
327
  $form->{DUNNING_PDFS_EMAIL} = [];
328

  
329
  $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
330

  
331
  $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
332
  $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
333

  
334
  $form->{dunning_id} = $dunning_id;
335

  
336
  if ($send_email) {
337
    $self->send_email($myconfig, $form, $dunning_id, $dbh);
338
  }
339

  
340
#   $dbh->commit();
341
  $dbh->disconnect();
342

  
343
  $main::lxdebug->leave_sub();
344
}
345

  
346
sub send_email {
347
  $main::lxdebug->enter_sub();
348

  
349
  my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
350

  
177 351
  my $query =
178 352
    qq|SELECT
179
         ar.invnumber, ar.ordnumber, ar.amount, ar.netamount,
180
         ar.transdate, ar.duedate, ar.paid, ar.amount - ar.paid AS open_amount,
181
         da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate
182
       FROM ar
183
       LEFT JOIN dunning_config cfg ON (cfg.id = ar.dunning_config_id)
184
       LEFT JOIN dunning da ON (ar.id = da.trans_id AND cfg.dunning_level = da.dunning_level)
185
       WHERE ar.id IN (|
186
       . join(", ", map { "?" } @invoice_ids) . qq|)|;
353
         dcfg.email_body,     dcfg.email_subject, dcfg.email_attachment,
354
         c.email AS recipient
187 355

  
188
  my $sth = prepare_execute_query($form, $dbh, $query, @invoice_ids);
189
  my $first = 1;
190
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
191
    if ($first) {
192
      map({ $form->{"dn_$_"} = []; } keys(%{$ref}));
193
      $first = 0;
194
    }
356
       FROM dunning d
357
       LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
358
       LEFT JOIN ar                  ON (d.trans_id          = ar.id)
359
       LEFT JOIN customer c          ON (ar.customer_id      = c.id)
360
       WHERE (d.dunning_id = ?)
361
       LIMIT 1|;
362
  my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
195 363

  
196
    $ref->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
197
    map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest);
198
    map { push(@{ $form->{"dn_$_"} }, $ref->{$_})} keys %$ref;
199
    map { $form->{$_} = $ref->{$_} } keys %{ $ref };
364
  if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
365
    $main::lxdebug->leave_sub();
366
    return;
200 367
  }
201
  $sth->finish;
202 368

  
203
  $query =
204
    qq|SELECT id AS customer_id, name, street, zipcode, city, country, department_1, department_2, email
205
       FROM customer
206
       WHERE id = ?|;
207
  $ref = selectfirst_hashref_query($form, $dbh, $query, $customer_id);
208
  map { $form->{$_} = $ref->{$_} } keys %{ $ref };
369
  my $template     = PlainTextTemplate->new(undef, $form, $myconfig);
370
  my $mail         = Mailer->new();
371
  $mail->{from}    = $myconfig->{email};
372
  $mail->{to}      = $ref->{recipient};
373
  $mail->{subject} = $template->parse_block($ref->{email_subject});
374
  $mail->{message} = $template->parse_block($ref->{email_body});
209 375

  
210
  $query =
211
    qq|SELECT
212
         cfg.interest_rate, cfg.template AS formname,
213
         cfg.email_subject, cfg.email_body, cfg.email_attachment,
214
         (SELECT SUM(fee)
215
          FROM dunning
216
          WHERE dunning_id = ?)
217
         AS fee,
218
         (SELECT SUM(interest)
219
          FROM dunning
220
          WHERE dunning_id = ?)
221
         AS total_interest,
222
         (SELECT SUM(amount) - SUM(paid)
223
          FROM ar
224
          WHERE id IN (| . join(", ", map { "?" } @invoice_ids) . qq|))
225
         AS total_open_amount
226
       FROM dunning_config cfg
227
       WHERE id = ?|;
228
  $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, @invoice_ids, $next_dunning_config_id);
229
  map { $form->{$_} = $ref->{$_} } keys %{ $ref };
376
  if ($myconfig->{signature}) {
377
    $mail->{message} .= "\n-- \n$myconfig->{signature}";
378
  }
230 379

  
231
  $form->{interest_rate}     = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
232
  $form->{fee}               = $form->format_amount($myconfig, $ref->{fee}, 2);
233
  $form->{total_interest}    = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
234
  $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
235
  $form->{total_amount}      = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
380
  $mail->{message} =~ s/\r\n/\n/g;
381

  
382
  if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
383
    $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
384
  }
385

  
386
  $mail->send();
387

  
388
  $main::lxdebug->leave_sub();
389
}
390

  
391
sub set_template_options {
392
  $main::lxdebug->enter_sub();
393

  
394
  my ($self, $myconfig, $form) = @_;
236 395

  
237 396
  $form->{templates}    = "$myconfig->{templates}";
238
  $form->{language}     = $form->get_template_language(\%myconfig);
239
  $form->{printer_code} = $form->get_printer_code(\%myconfig);
397
  $form->{language}     = $form->get_template_language($myconfig);
398
  $form->{printer_code} = $form->get_printer_code($myconfig);
240 399

  
241 400
  if ($form->{language} ne "") {
242 401
    $form->{language} = "_" . $form->{language};
......
246 405
    $form->{printer_code} = "_" . $form->{printer_code};
247 406
  }
248 407

  
249
  $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
250
  if ($form->{format} eq 'postscript') {
251
    $form->{postscript} = 1;
252
    $form->{IN} =~ s/html$/tex/;
253
  } elsif ($form->{"format"} =~ /pdf/) {
254
    $form->{pdf} = 1;
255
    if ($form->{"format"} =~ /opendocument/) {
256
      $form->{IN} =~ s/html$/odt/;
257
    } else {
258
      $form->{IN} =~ s/html$/tex/;
259
    }
260
  } elsif ($form->{"format"} =~ /opendocument/) {
261
    $form->{"opendocument"} = 1;
262
    $form->{"IN"} =~ s/html$/odt/;
263
  }
264

  
265
  if ($send_email && ($form->{email} ne "")) {
266
    $form->{media} = 'email';
267
  }
268

  
269
  $form->{keep_tmpfile} = 0;
270
  if ($form->{media} eq 'email') {
271
    $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
272
      unless $form->{subject};
273
    if (!$form->{email_attachment}) {
274
      $form->{do_not_attach} = 1;
275
    } else {
276
      $form->{do_not_attach} = 0;
277
    }
278
    $form->{subject} = parse_strings($myconfig, $form, $userspath, $form->{email_subject});
279
    $form->{message} = parse_strings($myconfig, $form, $userspath, $form->{email_body});
280

  
281
    $form->{OUT} = "$sendmail";
408
  $form->{IN}  = "$form->{formname}$form->{language}$form->{printer_code}.html";
409
  $form->{pdf} = 1;
282 410

  
411
  if ($form->{"format"} =~ /opendocument/) {
412
    $form->{IN} =~ s/html$/odt/;
283 413
  } else {
284

  
285
    my $filename = Common::unique_id() . $form->{login} . ".pdf";
286
    $form->{OUT} = ">$spool/$filename";
287
    push(@{ $form->{DUNNING_PDFS} }, $filename);
288
    $form->{keep_tmpfile} = 1;
414
    $form->{IN} =~ s/html$/tex/;
289 415
  }
290 416

  
291
  delete($form->{tmpfile});
292
  $form->parse_template($myconfig, $userspath);
293

  
294
  $dbh->commit;
295
  $dbh->disconnect;
296

  
297 417
  $main::lxdebug->leave_sub();
298 418
}
299 419

  
......
495 615
  $main::lxdebug->leave_sub();
496 616
}
497 617

  
498
sub parse_strings {
499

  
500
  $main::lxdebug->enter_sub();
501

  
502
  my ($myconfig, $form, $userspath, $string) = @_;
503

  
504
  local (*IN, *OUT);
505

  
506
  my $format = $form->{format};
507
  $form->{format} = "html";
508

  
509
  $tmpstring = "parse_string.html";
510
  $tmpfile = "$myconfig->{templates}/$tmpstring";
511
  open(OUT, ">", $tmpfile) or $form->error("$tmpfile : $!");
512

  
513
  print(OUT $string);
514
  close(OUT);
515

  
516
  my $in = $form->{IN};
517
  $form->{IN} = $tmpstring;
518
  $template = HTMLTemplate->new($tmpstring, $form, $myconfig, $userspath);
519

  
520
  my $fileid = time;
521
  $form->{tmpfile} = "$userspath/${fileid}.$tmpstring";
522

  
523
  open(OUT, ">", $form->{tmpfile}) or $form->error("$form->{OUT} : $!");
524
  if (!$template->parse(*OUT)) {
525
    $form->cleanup();
526
    $form->error("$form->{IN} : " . $template->get_error());
527
  }
528

  
529
  close(OUT);
530
  my $result = "";
531
  open(IN, "<", $form->{tmpfile}) or $form->error($form->cleanup . "$form->{tmpfile} : $!");
532

  
533
  while (<IN>) {
534
    $result .= $_;
535
  }
536

  
537
  close(IN);
538
#   unlink($tmpfile);
539
#   unlink($form->{tmpfile});
540
  $form->{IN} = $in;
541
  $form->{format} = $format;
542

  
543
  $main::lxdebug->leave_sub();
544
  return $result;
545
}
546

  
547 618
sub melt_pdfs {
548 619

  
549 620
  $main::lxdebug->enter_sub();
550 621

  
551
  my ($self, $myconfig, $form, $userspath) = @_;
552

  
553
  local (*IN, *OUT);
622
  my ($self, $myconfig, $form, $copies) = @_;
554 623

  
555
  # Don't allow access outside of $userspath.
624
  # Don't allow access outside of $spool.
556 625
  map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
557 626

  
558
  my $inputfiles = join " ", map { "$userspath/$_" } @{ $form->{DUNNING_PDFS} };
559
  my $outputfile = "$userspath/dunning.pdf";
627
  $copies        *= 1;
628
  $copies         = 1 unless $copies;
629
  my $inputfiles  = join " ", map { "${main::spool}/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
630
  my $dunning_id  = $form->{dunning_id};
560 631

  
561
  system("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$outputfile $inputfiles");
632
  $dunning_id     =~ s|[^\d]||g;
562 633

  
563
  map { unlink("$userspath/$_") } @{ $form->{DUNNING_PDFS} };
634
  my $in = IO::File->new("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
635
  $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
564 636

  
565
  my $numbytes = (-s $outputfile);
566
  open(IN, $outputfile) || $form->error($self->cleanup() . "$outputfile : $!");
637
  my $out;
567 638

  
568
  $form->{copies} = 1 unless $form->{media} eq 'printer';
569

  
570
  chdir($self->{cwd});
571

  
572
  for my $i (1 .. $form->{copies}) {
573
    # launch application
574
    print qq|Content-Type: Application/PDF
575
Content-Disposition: attachment; filename="$outputfile"
576
Content-Length: $numbytes
577

  
578
|;
579

  
580
    open(OUT, ">-") or $form->error($form->cleanup . "$!: STDOUT");
581

  
582
    while (<IN>) {
583
      print OUT $_;
639
  if ($form->{media} eq 'printer') {
640
    $form->get_printer_code($myconfig);
641
    if ($form->{printer_command}) {
642
      $out = IO::File->new("| $form->{printer_command}");
584 643
    }
585 644

  
586
    close(OUT);
645
    $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
646

  
647
  } else {
648
    $out = IO::File->new('>-');
649
    $out->print(qq|Content-Type: Application/PDF\n| .
650
                qq|Content-Disposition: attachment; filename="dunning_${dunning_id}.pdf"\n\n|);
651
  }
587 652

  
588
    seek(IN, 0, 0);
653
  while (my $line = <$in>) {
654
    $out->print($line);
589 655
  }
590 656

  
591
  close(IN);
592
  unlink($outputfile);
657
  $in->close();
658
  $out->close();
659

  
660
  map { unlink("${main::spool}/$_") } @{ $form->{DUNNING_PDFS} };
593 661

  
594 662
  $main::lxdebug->leave_sub();
595 663
}
......
597 665
sub print_dunning {
598 666
  $main::lxdebug->enter_sub();
599 667

  
600
  my ($self, $myconfig, $form, $dunning_id, $userspath, $spool, $sendmail) = @_;
668
  my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
669

  
601 670
  # connect to database
602
  my $dbh = $form->dbconnect_noauto($myconfig);
671
  my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
672

  
673
  $dunning_id =~ s|[^\d]||g;
603 674

  
604 675
  my $query =
605
    qq|SELECT invnumber, ordnumber, customer_id, amount, netamount,
606
         ar.transdate, ar.duedate, paid, amount - paid AS open_amount,
607
         template AS formname, email_subject, email_body, email_attachment,
608
         da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate
676
    qq|SELECT
677
         da.fee, da.interest,
678
         da.transdate  AS dunning_date,
679
         da.duedate    AS dunning_duedate,
680

  
681
         dcfg.template AS formname,
682
         dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
683

  
684
         ar.transdate,       ar.duedate,      ar.customer_id,
685
         ar.invnumber,       ar.ordnumber,
686
         ar.amount,          ar.netamount,    ar.paid,
687
         ar.amount - ar.paid AS open_amount
688

  
609 689
       FROM dunning da
610
       LEFT JOIN dunning_config ON (dunning_config.id = da.dunning_config_id)
690
       LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
611 691
       LEFT JOIN ar ON (ar.id = da.trans_id)
612 692
       WHERE (da.dunning_id = ?)|;
613 693

  
......
622 702
    map { $form->{$_} = $ref->{$_} } keys %$ref;
623 703
    map { push @{ $form->{"dn_$_"} }, $ref->{$_}} keys %$ref;
624 704
  }
625
  $sth->finish;
705
  $sth->finish();
626 706

  
627 707
  $query =
628
    qq|SELECT id AS customer_id, name, street, zipcode, city, country, department_1, department_2, email
629
       FROM customer
630
       WHERE id =
631
         (SELECT customer_id
632
          FROM dunning d
633
          LEFT JOIN ar ON (d.trans_id = ar.id)
634
          WHERE d.id = ?)|;
708
    qq|SELECT
709
         c.id AS customer_id, c.name,         c.street,       c.zipcode, c.city,
710
         c.country,           c.department_1, c.department_2, c.email
711
       FROM dunning d
712
       LEFT JOIN ar         ON (d.trans_id = ar.id)
713
       LEFT JOIN customer c ON (ar.customer_id = c.id)
714
       WHERE (d.dunning_id = ?)
715
       LIMIT 1|;
635 716
  $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
636 717
  map { $form->{$_} = $ref->{$_} } keys %{ $ref };
637 718

  
......
668 749
  $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
669 750
  $form->{total_amount}      = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
670 751

  
752
  $self->set_template_options($myconfig, $form);
671 753

  
672
  $form->{templates} = "$myconfig->{templates}";
754
  my $filename          = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
755
  $form->{OUT}          = ">${main::spool}/$filename";
756
  $form->{keep_tmpfile} = 1;
673 757

  
674
  $form->{language} = $form->get_template_language(\%myconfig);
675
  $form->{printer_code} = $form->get_printer_code(\%myconfig);
758
  delete $form->{tmpfile};
676 759

  
677
  if ($form->{language} ne "") {
678
    $form->{language} = "_" . $form->{language};
679
  }
760
  push @{ $form->{DUNNING_PDFS} }, $filename;
761
  push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => $filename,
762
                                           'name'     => "dunning_${dunning_id}.pdf" };
680 763

  
681
  if ($form->{printer_code} ne "") {
682
    $form->{printer_code} = "_" . $form->{printer_code};
764
  $form->parse_template($myconfig, $main::userspath);
765

  
766
  $dbh->disconnect() unless $provided_dbh;
767

  
768
  $main::lxdebug->leave_sub();
769
}
770

  
771
sub print_invoice_for_fees {
772
  $main::lxdebug->enter_sub();
773

  
774
  my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
775

  
776
  my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
777

  
778
  my ($query, @values, $sth);
779

  
780
  $query =
781
    qq|SELECT
782
         d.fee_interest_ar_id,
783
         dcfg.template
784
       FROM dunning d
785
       LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
786
       WHERE d.dunning_id = ?|;
787
  my ($ar_id, $template) = selectrow_query($form, $dbh, $query, $dunning_id);
788

  
789
  if (!$ar_id) {
790
    $main::lxdebug->leave_sub();
791
    return;
683 792
  }
684 793

  
685
  $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
686
  if ($form->{format} eq 'postscript') {
687
    $form->{postscript} = 1;
688
    $form->{IN} =~ s/html$/tex/;
689
  } elsif ($form->{"format"} =~ /pdf/) {
690
    $form->{pdf} = 1;
691
    if ($form->{"format"} =~ /opendocument/) {
692
      $form->{IN} =~ s/html$/odt/;
794
  my $saved_form = save_form();
795

  
796
  $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
797
  my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
798

  
799
  $query =
800
    qq|SELECT
801
         ar.invnumber, ar.transdate, ar.amount, ar.netamount,
802
         ar.duedate,   ar.notes,     ar.notes AS invoicenotes,
803

  
804
         c.name,      c.department_1,   c.department_2, c.street, c.zipcode, c.city, c.country,
805
         c.contact,   c.customernumber, c.phone,        c.fax,    c.email,
806
         c.taxnumber, c.sic_code,       c.greeting
807

  
808
       FROM ar
809
       LEFT JOIN customer c ON (ar.customer_id = c.id)
810
       WHERE ar.id = ?|;
811
  $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
812
  map { $form->{$_} = $ref->{$_} } keys %{ $ref };
813

  
814
  $query = qq|SELECT * FROM employee WHERE login = ?|;
815
  $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{login});
816
  map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
817

  
818
  $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid ASC|;
819
  $sth   = prepare_execute_query($form, $dbh, $query, $ar_id);
820

  
821
  my ($row, $fee, $interest) = (0, 0, 0);
822

  
823
  while ($ref = $sth->fetchrow_hashref()) {
824
    next if ($ref->{amount} < 0);
825

  
826
    $row++;
827

  
828
    if ($row == 1) {
829
      $fee = $ref->{amount};
693 830
    } else {
694
      $form->{IN} =~ s/html$/tex/;
831
      $interest = $ref->{amount};
695 832
    }
696
  } elsif ($form->{"format"} =~ /opendocument/) {
697
    $form->{"opendocument"} = 1;
698
    $form->{"IN"} =~ s/html$/odt/;
699 833
  }
700 834

  
701
  if ($form->{"send_email"} && ($form->{email} ne "")) {
702
    $form->{media} = 'email';
703
  }
835
  $form->{fee}        = $form->round_amount($fee,             2);
836
  $form->{interest}   = $form->round_amount($interest,        2);
837
  $form->{invamount}  = $form->round_amount($fee + $interest, 2);
838
  $form->{dunning_id} = $dunning_id;
839
  $form->{formname}   = "${template}_invoice";
704 840

  
705
  $form->{keep_tmpfile} = 0;
706
  if ($form->{media} eq 'email') {
707
    $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
708
      unless $form->{subject};
709
    if (!$form->{email_attachment}) {
710
      $form->{do_not_attach} = 1;
711
    } else {
712
      $form->{do_not_attach} = 0;
713
    }
714
    $form->{subject} = parse_strings($myconfig, $form, $userspath, $form->{email_subject});
715
    $form->{message} = parse_strings($myconfig, $form, $userspath, $form->{email_body});
841
  map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
716 842

  
717
    $form->{OUT} = "$sendmail";
843
  $self->set_template_options($myconfig, $form);
718 844

  
719
  } else {
845
  my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
720 846

  
721
    my $filename = Common::unique_id() . $form->{login} . ".pdf";
847
  $form->{OUT}          = ">$main::spool/$filename";
848
  $form->{keep_tmpfile} = 1;
849
  delete $form->{tmpfile};
722 850

  
723
    push(@{ $form->{DUNNING_PDFS} }, $filename);
724
    $form->{keep_tmpfile} = 1;
725
  }
851
  map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
726 852

  
727
  $form->parse_template($myconfig, $userspath);
853
  $form->parse_template($myconfig, $main::userspath);
728 854

  
729
  $dbh->commit;
730
  $dbh->disconnect;
855
  restore_form($saved_form);
856

  
857
  push @{ $form->{DUNNING_PDFS} }, $filename;
858
  push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => $filename,
859
                                           'name'     => "dunning_invoice_${dunning_id}.pdf" };
860

  
861
  $dbh->disconnect() unless $provided_dbh;
731 862

  
732 863
  $main::lxdebug->leave_sub();
733 864
}

Auch abrufbar als: Unified diff