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 #======================================================================
45 $main::lxdebug->enter_sub();
47 my ($self, $myconfig, $form) = @_;
50 my $dbh = $form->dbconnect($myconfig);
54 qq|FROM dunning_config | .
55 qq|ORDER BY dunning_level|;
56 $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
58 foreach my $ref (@{ $form->{DUNNING} }) {
59 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
60 $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
65 dunning_create_invoices_for_fees, dunning_ar_amount_fee,
66 dunning_ar_amount_interest, dunning_ar
68 ($form->{create_invoices_for_fees}, $form->{AR_amount_fee},
69 $form->{AR_amount_interest}, $form->{AR} ) = selectrow_query($form, $dbh, $query);
73 $main::lxdebug->leave_sub();
77 $main::lxdebug->enter_sub();
79 my ($self, $myconfig, $form) = @_;
82 my $dbh = $form->dbconnect_noauto($myconfig);
86 for my $i (1 .. $form->{rowcount}) {
87 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
88 $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
90 if (($form->{"dunning_level_$i"} ne "") &&
91 ($form->{"dunning_description_$i"} ne "")) {
92 @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
93 $form->{"email_subject_$i"}, $form->{"email_body_$i"},
94 $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
95 $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
96 $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}));
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 = ?
106 push(@values, conv_i($form->{"id_$i"}));
109 qq|INSERT INTO dunning_config
110 (dunning_level, dunning_description, email_subject, email_body,
111 template, fee, interest_rate, active, auto, email,
112 email_attachment, payment_terms, terms)
113 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
115 do_query($form, $dbh, $query, @values);
118 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
119 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
120 do_query($form, $dbh, $query, $form->{"id_$i"});
124 $query = qq|UPDATE defaults SET dunning_create_invoices_for_fees = ?|;
125 @values = ($form->{create_invoices_for_fees} ? 't' : 'f');
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});
132 do_query($form, $dbh, $query, @values);
137 $main::lxdebug->leave_sub();
140 sub create_invoice_for_fees {
141 $main::lxdebug->enter_sub();
143 my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
145 my ($query, @values, $sth, $ref);
149 dunning_create_invoices_for_fees, dunning_ar_amount_fee,
150 dunning_ar_amount_interest, dunning_ar
152 ($form->{create_invoices_for_fees}, $form->{AR_amount_fee},
153 $form->{AR_amount_interest}, $form->{AR} ) = selectrow_query($form, $dbh, $query);
155 if (!$form->{create_invoices_for_fees}) {
156 $main::lxdebug->leave_sub();
164 SELECT MAX(d_fee.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)
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)
179 AS max_previous_interest
181 WHERE dunning_id = ?|;
182 @values = ($dunning_id, $dunning_id, $dunning_id);
183 $sth = prepare_execute_query($form, $dbh, $query, @values);
185 my ($fee_remaining, $interest_remaining) = (0, 0);
186 my ($fee_total, $interest_total) = (0, 0);
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);
199 my $amount = $fee_remaining + $interest_remaining;
202 $main::lxdebug->leave_sub();
206 my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
209 qq|INSERT INTO ar (id, invnumber, transdate, gldate, customer_id,
210 taxincluded, amount, netamount, paid, duedate,
211 invoice, curr, notes,
216 current_date, -- transdate
217 current_date, -- gldate
219 (SELECT ar.customer_id
221 LEFT JOIN ar ON (dn.trans_id = ar.id)
222 WHERE dn.dunning_id = ?
229 (SELECT duedate FROM dunning WHERE dunning_id = ?),
234 (SELECT id FROM employee WHERE login = ?)
236 @values = ($ar_id, # id
237 $form->update_defaults($myconfig, 'invnumber', $dbh), # invnumber
238 $dunning_id, # customer_id
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);
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);
252 @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining);
253 do_statement($form, $sth, $query, @values);
255 if ($interest_remaining) {
256 @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining);
257 do_statement($form, $sth, $query, @values);
260 @values = ($ar_id, conv_i($form->{AR}), -1 * $amount);
261 do_statement($form, $sth, $query, @values);
265 $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
266 do_query($form, $dbh, $query, $ar_id, $dunning_id);
268 $main::lxdebug->leave_sub();
272 $main::lxdebug->enter_sub();
274 my ($self, $myconfig, $form, $rows, $userspath, $spool, $sendmail) = @_;
275 # connect to database
276 my $dbh = $form->dbconnect_noauto($myconfig);
278 my ($query, @values);
280 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
282 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
283 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
285 my $q_insert_dunning =
286 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
287 fee, interest, transdate, duedate)
289 (SELECT dunning_level FROM dunning_config WHERE id = ?),
293 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
294 (SELECT (amount - paid) * (current_date - transdate) FROM ar WHERE id = ?)
295 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
298 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
299 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
302 my ($next_dunning_config_id, $customer_id);
305 foreach my $row (@{ $rows }) {
306 push @invoice_ids, $row->{invoice_id};
307 $next_dunning_config_id = $row->{next_dunning_config_id};
308 $customer_id = $row->{customer_id};
310 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
311 do_statement($form, $h_update_ar, $q_update_ar, @values);
313 $send_email |= $row->{email};
315 my $next_config_id = conv_i($row->{next_dunning_config_id});
316 my $invoice_id = conv_i($row->{invoice_id});
318 @values = ($dunning_id, $next_config_id, $next_config_id,
319 $invoice_id, $next_config_id, $invoice_id,
320 $next_config_id, $next_config_id);
321 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
324 $h_update_ar->finish();
325 $h_insert_dunning->finish();
327 $form->{DUNNING_PDFS_EMAIL} = [];
329 $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
331 $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
332 $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
334 $form->{dunning_id} = $dunning_id;
337 $self->send_email($myconfig, $form, $dunning_id, $dbh);
343 $main::lxdebug->leave_sub();
347 $main::lxdebug->enter_sub();
349 my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
353 dcfg.email_body, dcfg.email_subject, dcfg.email_attachment,
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 = ?)
362 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
364 if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
365 $main::lxdebug->leave_sub();
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});
376 if ($myconfig->{signature}) {
377 $mail->{message} .= "\n-- \n$myconfig->{signature}";
380 $mail->{message} =~ s/\r\n/\n/g;
382 if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
383 $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
388 $main::lxdebug->leave_sub();
391 sub set_template_options {
392 $main::lxdebug->enter_sub();
394 my ($self, $myconfig, $form) = @_;
396 $form->{templates} = "$myconfig->{templates}";
397 $form->{language} = $form->get_template_language($myconfig);
398 $form->{printer_code} = $form->get_printer_code($myconfig);
400 if ($form->{language} ne "") {
401 $form->{language} = "_" . $form->{language};
404 if ($form->{printer_code} ne "") {
405 $form->{printer_code} = "_" . $form->{printer_code};
408 $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
411 if ($form->{"format"} =~ /opendocument/) {
412 $form->{IN} =~ s/html$/odt/;
414 $form->{IN} =~ s/html$/tex/;
417 $main::lxdebug->leave_sub();
422 $main::lxdebug->enter_sub();
424 my ($self, $myconfig, $form) = @_;
426 # connect to database
427 my $dbh = $form->dbconnect($myconfig);
432 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
434 if ($form->{customer_id}) {
435 $where .= qq| AND (a.customer_id = ?)|;
436 push(@values, $form->{customer_id});
438 } elsif ($form->{customer}) {
439 $where .= qq| AND (ct.name ILIKE ?)|;
440 push(@values, '%' . $form->{customer} . '%');
444 "ordnumber" => "a.ordnumber",
445 "invnumber" => "a.invnumber",
446 "notes" => "a.notes",
448 foreach my $key (keys(%columns)) {
449 next unless ($form->{$key});
450 $where .= qq| AND $columns{$key} ILIKE ?|;
451 push(@values, '%' . $form->{$key} . '%');
454 if ($form->{dunning_level}) {
455 $where .= qq| AND nextcfg.id = ?|;
456 push(@values, conv_i($form->{dunning_level}));
459 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
460 if ($form->{minamount}) {
461 $where .= qq| AND ((a.amount - a.paid) > ?) |;
462 push(@values, $form->{minamount});
468 WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
469 my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
473 a.id, a.ordnumber, a.transdate, a.invnumber, a.amount,
474 ct.name AS customername, a.customer_id, a.duedate,
476 cfg.dunning_description, cfg.dunning_level,
478 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
481 a.duedate + cfg.terms - current_date AS nextlevel,
482 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
483 current_date + cfg.payment_terms AS next_duedate,
485 nextcfg.dunning_description AS next_dunning_description,
486 nextcfg.id AS next_dunning_config_id,
487 nextcfg.terms, nextcfg.active, nextcfg.email
491 LEFT JOIN customer ct ON (a.customer_id = ct.id)
492 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
493 LEFT JOIN dunning_config nextcfg ON
498 WHERE dunning_level >
499 COALESCE((SELECT dunning_level
501 WHERE id = a.dunning_config_id
502 ORDER BY dunning_level DESC
505 ORDER BY dunning_level ASC
508 LEFT JOIN dunning d ON ((d.trans_id = a.id) AND (cfg.dunning_level = d.dunning_level))
510 WHERE (a.paid < a.amount)
511 AND (a.duedate < current_date)
515 ORDER BY a.id, transdate, duedate, name|;
516 my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
518 $form->{DUNNINGS} = [];
520 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
521 next if !$ref->{terms} || ($ref->{pastdue} < $ref->{terms});
523 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
524 push(@{ $form->{DUNNINGS} }, $ref);
529 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
530 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
533 $main::lxdebug->leave_sub();
538 $main::lxdebug->enter_sub();
540 my ($self, $myconfig, $form) = @_;
542 # connect to database
543 my $dbh = $form->dbconnect($myconfig);
545 $where = qq| WHERE (da.trans_id = a.id)|;
549 if ($form->{customer_id}) {
550 $where .= qq| AND (a.customer_id = ?)|;
551 push(@values, $form->{customer_id});
553 } elsif ($form->{customer}) {
554 $where .= qq| AND (ct.name ILIKE ?)|;
555 push(@values, '%' . $form->{customer} . '%');
559 "ordnumber" => "a.ordnumber",
560 "invnumber" => "a.invnumber",
561 "notes" => "a.notes",
563 foreach my $key (keys(%columns)) {
564 next unless ($form->{$key});
565 $where .= qq| AND $columns{$key} ILIKE ?|;
566 push(@values, '%' . $form->{$key} . '%');
569 if ($form->{dunning_level}) {
570 $where .= qq| AND a.dunning_config_id = ?|;
571 push(@values, conv_i($form->{dunning_level}));
574 if ($form->{department_id}) {
575 $where .= qq| AND a.department_id = ?|;
576 push @values, conv_i($form->{department_id});
579 $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
580 if ($form->{minamount}) {
581 $where .= qq| AND ((a.amount - a.paid) > ?) |;
582 push(@values, $form->{minamount});
585 if (!$form->{showold}) {
586 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
589 if ($form->{transdatefrom}) {
590 $where .= qq| AND a.transdate >= ?|;
591 push(@values, $form->{transdatefrom});
593 if ($form->{transdateto}) {
594 $where .= qq| AND a.transdate <= ?|;
595 push(@values, $form->{transdateto});
597 if ($form->{dunningfrom}) {
598 $where .= qq| AND da.transdate >= ?|;
599 push(@values, $form->{dunningfrom});
601 if ($form->{dunningto}) {
602 $where .= qq| AND da.transdate >= ?|;
603 push(@values, $form->{dunningto});
607 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount,
608 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
609 da.interest, dn.dunning_description, da.transdate AS dunning_date,
610 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id
612 JOIN customer ct ON (a.customer_id = ct.id), dunning da
613 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
615 ORDER BY name, a.id|;
617 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
619 foreach my $ref (@{ $form->{DUNNINGS} }) {
620 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
624 $main::lxdebug->leave_sub();
629 $main::lxdebug->enter_sub();
631 my ($self, $myconfig, $form, $copies) = @_;
633 # Don't allow access outside of $spool.
634 map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
637 $copies = 1 unless $copies;
638 my $inputfiles = join " ", map { "${main::spool}/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
639 my $dunning_id = $form->{dunning_id};
641 $dunning_id =~ s|[^\d]||g;
643 my $in = IO::File->new("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
644 $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
648 if ($form->{media} eq 'printer') {
649 $form->get_printer_code($myconfig);
650 if ($form->{printer_command}) {
651 $out = IO::File->new("| $form->{printer_command}");
654 $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
657 $out = IO::File->new('>-');
658 $out->print(qq|Content-Type: Application/PDF\n| .
659 qq|Content-Disposition: attachment; filename="dunning_${dunning_id}.pdf"\n\n|);
662 while (my $line = <$in>) {
669 map { unlink("${main::spool}/$_") } @{ $form->{DUNNING_PDFS} };
671 $main::lxdebug->leave_sub();
675 $main::lxdebug->enter_sub();
677 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
679 # connect to database
680 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
682 $dunning_id =~ s|[^\d]||g;
687 da.transdate AS dunning_date,
688 da.duedate AS dunning_duedate,
690 dcfg.template AS formname,
691 dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
693 ar.transdate, ar.duedate, ar.customer_id,
694 ar.invnumber, ar.ordnumber,
695 ar.amount, ar.netamount, ar.paid,
696 ar.amount - ar.paid AS open_amount
699 LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
700 LEFT JOIN ar ON (ar.id = da.trans_id)
701 WHERE (da.dunning_id = ?)|;
703 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
705 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
707 map({ $form->{"dn_$_"} = []; } keys(%{$ref}));
710 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest);
711 map { $form->{$_} = $ref->{$_} } keys %$ref;
712 map { push @{ $form->{"dn_$_"} }, $ref->{$_}} keys %$ref;
718 c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
719 c.country, c.department_1, c.department_2, c.email
721 LEFT JOIN ar ON (d.trans_id = ar.id)
722 LEFT JOIN customer c ON (ar.customer_id = c.id)
723 WHERE (d.dunning_id = ?)
725 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
726 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
730 cfg.interest_rate, cfg.template AS formname,
731 cfg.email_subject, cfg.email_body, cfg.email_attachment,
732 d.transdate AS dunning_date,
735 WHERE dunning_id = ?)
737 (SELECT SUM(interest)
739 WHERE dunning_id = ?)
741 (SELECT SUM(amount) - SUM(paid)
746 WHERE dunning_id = ?))
749 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
750 WHERE d.dunning_id = ?
752 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
753 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
755 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
756 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
757 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
758 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
759 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
761 $self->set_template_options($myconfig, $form);
763 my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
764 $form->{OUT} = ">${main::spool}/$filename";
765 $form->{keep_tmpfile} = 1;
767 delete $form->{tmpfile};
769 push @{ $form->{DUNNING_PDFS} }, $filename;
770 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${main::spool}/$filename",
771 'name' => "dunning_${dunning_id}.pdf" };
773 $form->parse_template($myconfig, $main::userspath);
775 $dbh->disconnect() unless $provided_dbh;
777 $main::lxdebug->leave_sub();
780 sub print_invoice_for_fees {
781 $main::lxdebug->enter_sub();
783 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
785 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
787 my ($query, @values, $sth);
791 d.fee_interest_ar_id,
794 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
795 WHERE d.dunning_id = ?|;
796 my ($ar_id, $template) = selectrow_query($form, $dbh, $query, $dunning_id);
799 $main::lxdebug->leave_sub();
803 my $saved_form = save_form();
805 $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
806 my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
810 ar.invnumber, ar.transdate, ar.amount, ar.netamount,
811 ar.duedate, ar.notes, ar.notes AS invoicenotes,
813 c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country,
814 c.contact, c.customernumber, c.phone, c.fax, c.email,
815 c.taxnumber, c.sic_code, c.greeting
818 LEFT JOIN customer c ON (ar.customer_id = c.id)
820 $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
821 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
823 $query = qq|SELECT * FROM employee WHERE login = ?|;
824 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{login});
825 map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
827 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid ASC|;
828 $sth = prepare_execute_query($form, $dbh, $query, $ar_id);
830 my ($row, $fee, $interest) = (0, 0, 0);
832 while ($ref = $sth->fetchrow_hashref()) {
833 next if ($ref->{amount} < 0);
838 $fee = $ref->{amount};
840 $interest = $ref->{amount};
844 $form->{fee} = $form->round_amount($fee, 2);
845 $form->{interest} = $form->round_amount($interest, 2);
846 $form->{invamount} = $form->round_amount($fee + $interest, 2);
847 $form->{dunning_id} = $dunning_id;
848 $form->{formname} = "${template}_invoice";
850 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
852 $self->set_template_options($myconfig, $form);
854 my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
856 $form->{OUT} = ">$main::spool/$filename";
857 $form->{keep_tmpfile} = 1;
858 delete $form->{tmpfile};
860 map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
862 $form->parse_template($myconfig, $main::userspath);
864 restore_form($saved_form);
866 push @{ $form->{DUNNING_PDFS} }, $filename;
867 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${main::spool}/$filename",
868 'name' => "dunning_invoice_${dunning_id}.pdf" };
870 $dbh->disconnect() unless $provided_dbh;
872 $main::lxdebug->leave_sub();