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));
64 qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar
66 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
70 $main::lxdebug->leave_sub();
74 $main::lxdebug->enter_sub();
76 my ($self, $myconfig, $form) = @_;
79 my $dbh = $form->dbconnect_noauto($myconfig);
83 for my $i (1 .. $form->{rowcount}) {
84 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
85 $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
87 if (($form->{"dunning_level_$i"} ne "") &&
88 ($form->{"dunning_description_$i"} ne "")) {
89 @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
90 $form->{"email_subject_$i"}, $form->{"email_body_$i"},
91 $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
92 $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
93 $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}),
94 $form->{"create_invoices_for_fees_$i"} ? 't' : 'f');
95 if ($form->{"id_$i"}) {
97 qq|UPDATE dunning_config SET
98 dunning_level = ?, dunning_description = ?,
99 email_subject = ?, email_body = ?,
100 template = ?, fee = ?, interest_rate = ?,
101 active = ?, auto = ?, email = ?,
102 email_attachment = ?, payment_terms = ?, terms = ?,
103 create_invoices_for_fees = ?
105 push(@values, conv_i($form->{"id_$i"}));
108 qq|INSERT INTO dunning_config
109 (dunning_level, dunning_description, email_subject, email_body,
110 template, fee, interest_rate, active, auto, email,
111 email_attachment, payment_terms, terms, create_invoices_for_fees)
112 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
114 do_query($form, $dbh, $query, @values);
117 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
118 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
119 do_query($form, $dbh, $query, $form->{"id_$i"});
123 $query = qq|UPDATE defaults SET dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?|;
124 @values = (conv_i($form->{AR_amount_fee}), conv_i($form->{AR_amount_interest}), conv_i($form->{AR}));
125 do_query($form, $dbh, $query, @values);
130 $main::lxdebug->leave_sub();
133 sub create_invoice_for_fees {
134 $main::lxdebug->enter_sub();
136 my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
138 my ($query, @values, $sth, $ref);
140 $query = qq|SELECT dcfg.create_invoices_for_fees
142 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
143 WHERE d.dunning_id = ?|;
144 my ($create_invoices_for_fees) = selectrow_query($form, $dbh, $query, $dunning_id);
146 if (!$create_invoices_for_fees) {
147 $main::lxdebug->leave_sub();
151 $query = qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar FROM defaults|;
152 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
158 SELECT MAX(d_fee.fee)
160 WHERE (d_fee.trans_id = d.trans_id)
161 AND (d_fee.dunning_id <> ?)
162 AND NOT (d_fee.fee_interest_ar_id ISNULL)
167 SELECT MAX(d_interest.interest)
168 FROM dunning d_interest
169 WHERE (d_interest.trans_id = d.trans_id)
170 AND (d_interest.dunning_id <> ?)
171 AND NOT (d_interest.fee_interest_ar_id ISNULL)
173 AS max_previous_interest
175 WHERE dunning_id = ?|;
176 @values = ($dunning_id, $dunning_id, $dunning_id);
177 $sth = prepare_execute_query($form, $dbh, $query, @values);
179 my ($fee_remaining, $interest_remaining) = (0, 0);
180 my ($fee_total, $interest_total) = (0, 0);
182 while (my $ref = $sth->fetchrow_hashref()) {
183 $fee_remaining += $form->round_amount($ref->{fee}, 2);
184 $fee_remaining -= $form->round_amount($ref->{max_previous_fee}, 2);
185 $fee_total += $form->round_amount($ref->{fee}, 2);
186 $interest_remaining += $form->round_amount($ref->{interest}, 2);
187 $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
188 $interest_total += $form->round_amount($ref->{interest}, 2);
193 my $amount = $fee_remaining + $interest_remaining;
196 $main::lxdebug->leave_sub();
200 my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
201 my $curr = $form->get_default_currency($myconfig);
204 qq|INSERT INTO ar (id, invnumber, transdate, gldate, customer_id,
205 taxincluded, amount, netamount, paid, duedate,
206 invoice, curr, notes,
211 current_date, -- transdate
212 current_date, -- gldate
214 (SELECT ar.customer_id
216 LEFT JOIN ar ON (dn.trans_id = ar.id)
217 WHERE dn.dunning_id = ?
224 (SELECT duedate FROM dunning WHERE dunning_id = ? LIMIT 1),
229 (SELECT id FROM employee WHERE login = ?)
231 @values = ($ar_id, # id
232 $form->update_defaults($myconfig, 'invnumber', $dbh), # invnumber
233 $dunning_id, # customer_id
236 $dunning_id, # duedate
237 $curr, # default currency
238 sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
239 $form->{login}); # employee_id
240 do_query($form, $dbh, $query, @values);
243 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey)
244 VALUES (?, ?, ?, current_date, current_date, 0)|;
245 $sth = prepare_query($form, $dbh, $query);
247 @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining);
248 do_statement($form, $sth, $query, @values);
250 if ($interest_remaining) {
251 @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining);
252 do_statement($form, $sth, $query, @values);
255 @values = ($ar_id, conv_i($form->{AR}), -1 * $amount);
256 do_statement($form, $sth, $query, @values);
260 $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
261 do_query($form, $dbh, $query, $ar_id, $dunning_id);
263 $main::lxdebug->leave_sub();
267 $main::lxdebug->enter_sub();
269 my ($self, $myconfig, $form, $rows, $userspath, $spool) = @_;
270 # connect to database
271 my $dbh = $form->dbconnect_noauto($myconfig);
273 my ($query, @values);
275 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
277 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
278 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
280 my $q_insert_dunning =
281 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
282 fee, interest, transdate, duedate)
284 (SELECT dunning_level FROM dunning_config WHERE id = ?),
288 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
289 (SELECT (amount - paid) * (current_date - transdate) FROM ar WHERE id = ?)
290 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
293 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
294 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
297 my ($next_dunning_config_id, $customer_id);
300 foreach my $row (@{ $rows }) {
301 push @invoice_ids, $row->{invoice_id};
302 $next_dunning_config_id = $row->{next_dunning_config_id};
303 $customer_id = $row->{customer_id};
305 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
306 do_statement($form, $h_update_ar, $q_update_ar, @values);
308 $send_email |= $row->{email};
310 my $next_config_id = conv_i($row->{next_dunning_config_id});
311 my $invoice_id = conv_i($row->{invoice_id});
313 @values = ($dunning_id, $next_config_id, $next_config_id,
314 $invoice_id, $next_config_id, $invoice_id,
315 $next_config_id, $next_config_id);
316 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
319 $h_update_ar->finish();
320 $h_insert_dunning->finish();
322 $form->{DUNNING_PDFS_EMAIL} = [];
324 $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
326 $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
327 $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
329 $form->{dunning_id} = $dunning_id;
332 $self->send_email($myconfig, $form, $dunning_id, $dbh);
338 $main::lxdebug->leave_sub();
342 $main::lxdebug->enter_sub();
344 my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
348 dcfg.email_body, dcfg.email_subject, dcfg.email_attachment,
352 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
353 LEFT JOIN ar ON (d.trans_id = ar.id)
354 LEFT JOIN customer c ON (ar.customer_id = c.id)
355 WHERE (d.dunning_id = ?)
357 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
359 if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
360 $main::lxdebug->leave_sub();
364 my $template = PlainTextTemplate->new(undef, $form, $myconfig);
365 my $mail = Mailer->new();
366 $mail->{from} = $myconfig->{email};
367 $mail->{to} = $ref->{recipient};
368 $mail->{subject} = $template->parse_block($ref->{email_subject});
369 $mail->{message} = $template->parse_block($ref->{email_body});
371 if ($myconfig->{signature}) {
372 $mail->{message} .= "\n-- \n$myconfig->{signature}";
375 $mail->{message} =~ s/\r\n/\n/g;
377 if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
378 $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
383 $main::lxdebug->leave_sub();
386 sub set_template_options {
387 $main::lxdebug->enter_sub();
389 my ($self, $myconfig, $form) = @_;
391 $form->{templates} = "$myconfig->{templates}";
392 $form->{language} = $form->get_template_language($myconfig);
393 $form->{printer_code} = $form->get_printer_code($myconfig);
395 if ($form->{language} ne "") {
396 $form->{language} = "_" . $form->{language};
399 if ($form->{printer_code} ne "") {
400 $form->{printer_code} = "_" . $form->{printer_code};
403 $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
406 if ($form->{"format"} =~ /opendocument/) {
407 $form->{IN} =~ s/html$/odt/;
409 $form->{IN} =~ s/html$/tex/;
412 $main::lxdebug->leave_sub();
417 $main::lxdebug->enter_sub();
419 my ($self, $myconfig, $form) = @_;
421 # connect to database
422 my $dbh = $form->dbconnect($myconfig);
427 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
429 if ($form->{customer_id}) {
430 $where .= qq| AND (a.customer_id = ?)|;
431 push(@values, $form->{customer_id});
433 } elsif ($form->{customer}) {
434 $where .= qq| AND (ct.name ILIKE ?)|;
435 push(@values, '%' . $form->{customer} . '%');
439 "ordnumber" => "a.ordnumber",
440 "invnumber" => "a.invnumber",
441 "notes" => "a.notes",
443 foreach my $key (keys(%columns)) {
444 next unless ($form->{$key});
445 $where .= qq| AND $columns{$key} ILIKE ?|;
446 push(@values, '%' . $form->{$key} . '%');
449 if ($form->{dunning_level}) {
450 $where .= qq| AND nextcfg.id = ?|;
451 push(@values, conv_i($form->{dunning_level}));
454 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
455 if ($form->{minamount}) {
456 $where .= qq| AND ((a.amount - a.paid) > ?) |;
457 push(@values, $form->{minamount});
463 WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
464 my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
468 a.id, a.ordnumber, a.transdate, a.invnumber, a.amount,
469 ct.name AS customername, a.customer_id, a.duedate,
471 cfg.dunning_description, cfg.dunning_level,
473 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
476 a.duedate + cfg.terms - current_date AS nextlevel,
477 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
478 current_date + cfg.payment_terms AS next_duedate,
480 nextcfg.dunning_description AS next_dunning_description,
481 nextcfg.id AS next_dunning_config_id,
482 nextcfg.terms, nextcfg.active, nextcfg.email
486 LEFT JOIN customer ct ON (a.customer_id = ct.id)
487 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
488 LEFT JOIN dunning_config nextcfg ON
493 WHERE dunning_level >
494 COALESCE((SELECT dunning_level
496 WHERE id = a.dunning_config_id
497 ORDER BY dunning_level DESC
500 ORDER BY dunning_level ASC
503 LEFT JOIN dunning d ON ((d.trans_id = a.id) AND (cfg.dunning_level = d.dunning_level))
505 WHERE (a.paid < a.amount)
506 AND (a.duedate < current_date)
510 ORDER BY a.id, transdate, duedate, name|;
511 my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
513 $form->{DUNNINGS} = [];
515 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
516 next if ($ref->{pastdue} < $ref->{terms});
518 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
519 push(@{ $form->{DUNNINGS} }, $ref);
524 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
525 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
528 $main::lxdebug->leave_sub();
533 $main::lxdebug->enter_sub();
535 my ($self, $myconfig, $form) = @_;
537 # connect to database
538 my $dbh = $form->dbconnect($myconfig);
540 my $where = qq| WHERE (da.trans_id = a.id)|;
544 if ($form->{customer_id}) {
545 $where .= qq| AND (a.customer_id = ?)|;
546 push(@values, $form->{customer_id});
548 } elsif ($form->{customer}) {
549 $where .= qq| AND (ct.name ILIKE ?)|;
550 push(@values, '%' . $form->{customer} . '%');
554 "ordnumber" => "a.ordnumber",
555 "invnumber" => "a.invnumber",
556 "notes" => "a.notes",
558 foreach my $key (keys(%columns)) {
559 next unless ($form->{$key});
560 $where .= qq| AND $columns{$key} ILIKE ?|;
561 push(@values, '%' . $form->{$key} . '%');
564 if ($form->{dunning_level}) {
565 $where .= qq| AND a.dunning_config_id = ?|;
566 push(@values, conv_i($form->{dunning_level}));
569 if ($form->{department_id}) {
570 $where .= qq| AND a.department_id = ?|;
571 push @values, conv_i($form->{department_id});
574 $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
575 if ($form->{minamount}) {
576 $where .= qq| AND ((a.amount - a.paid) > ?) |;
577 push(@values, $form->{minamount});
580 if (!$form->{showold}) {
581 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
584 if ($form->{transdatefrom}) {
585 $where .= qq| AND a.transdate >= ?|;
586 push(@values, $form->{transdatefrom});
588 if ($form->{transdateto}) {
589 $where .= qq| AND a.transdate <= ?|;
590 push(@values, $form->{transdateto});
592 if ($form->{dunningfrom}) {
593 $where .= qq| AND da.transdate >= ?|;
594 push(@values, $form->{dunningfrom});
596 if ($form->{dunningto}) {
597 $where .= qq| AND da.transdate >= ?|;
598 push(@values, $form->{dunningto});
602 'dunning_description' => [ qw(dn.dunning_description customername invnumber) ],
603 'customername' => [ qw(customername invnumber) ],
604 'invnumber' => [ qw(a.invnumber) ],
605 'transdate' => [ qw(a.transdate a.invnumber) ],
606 'duedate' => [ qw(a.duedate a.invnumber) ],
607 'dunning_date' => [ qw(dunning_date a.invnumber) ],
608 'dunning_duedate' => [ qw(dunning_duedate a.invnumber) ],
611 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
612 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername';
613 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
616 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount,
617 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
618 da.interest, dn.dunning_description, da.transdate AS dunning_date,
619 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id
621 JOIN customer ct ON (a.customer_id = ct.id), dunning da
622 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
624 ORDER BY $sortorder|;
626 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
628 foreach my $ref (@{ $form->{DUNNINGS} }) {
629 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
633 $main::lxdebug->leave_sub();
638 $main::lxdebug->enter_sub();
640 my ($self, $myconfig, $form, $copies) = @_;
642 # Don't allow access outside of $spool.
643 map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
646 $copies = 1 unless $copies;
647 my $inputfiles = join " ", map { "${main::spool}/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
648 my $dunning_id = $form->{dunning_id};
650 $dunning_id =~ s|[^\d]||g;
652 my $in = IO::File->new("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
653 $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
657 if ($form->{media} eq 'printer') {
658 $form->get_printer_code($myconfig);
659 if ($form->{printer_command}) {
660 $out = IO::File->new("| $form->{printer_command}");
663 $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
666 my $dunning_filename = $form->get_formname_translation('dunning');
667 $out = IO::File->new('>-');
668 $out->print(qq|Content-Type: Application/PDF\n| .
669 qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|);
672 while (my $line = <$in>) {
679 map { unlink("${main::spool}/$_") } @{ $form->{DUNNING_PDFS} };
681 $main::lxdebug->leave_sub();
685 $main::lxdebug->enter_sub();
687 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
689 # connect to database
690 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
692 $dunning_id =~ s|[^\d]||g;
697 da.transdate AS dunning_date,
698 da.duedate AS dunning_duedate,
700 dcfg.template AS formname,
701 dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
703 ar.transdate, ar.duedate, ar.customer_id,
704 ar.invnumber, ar.ordnumber, ar.cp_id,
705 ar.amount, ar.netamount, ar.paid,
706 ar.amount - ar.paid AS open_amount,
707 ar.amount - ar.paid + da.fee + da.interest AS linetotal
710 LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
711 LEFT JOIN ar ON (ar.id = da.trans_id)
712 WHERE (da.dunning_id = ?)|;
714 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
716 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
718 $form->{TEMPLATE_ARRAYS} = {};
719 map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref}));
722 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
723 map { $form->{$_} = $ref->{$_} } keys %$ref;
724 map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
730 c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
731 c.country, c.department_1, c.department_2, c.email, c.customernumber,
732 c.greeting, c.contact, c.phone, c.fax, c.homepage,
733 c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban,
736 LEFT JOIN ar ON (d.trans_id = ar.id)
737 LEFT JOIN customer c ON (ar.customer_id = c.id)
738 LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
739 WHERE (d.dunning_id = ?)
741 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
742 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
746 cfg.interest_rate, cfg.template AS formname,
747 cfg.email_subject, cfg.email_body, cfg.email_attachment,
748 d.transdate AS dunning_date,
751 WHERE dunning_id = ?)
753 (SELECT SUM(interest)
755 WHERE dunning_id = ?)
757 (SELECT SUM(amount) - SUM(paid)
762 WHERE dunning_id = ?))
765 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
766 WHERE d.dunning_id = ?
768 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
769 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
771 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
772 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
773 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
774 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
775 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
777 $self->set_template_options($myconfig, $form);
779 my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
780 $form->{OUT} = ">${main::spool}/$filename";
781 $form->{keep_tmpfile} = 1;
783 delete $form->{tmpfile};
785 push @{ $form->{DUNNING_PDFS} }, $filename;
786 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${main::spool}/$filename",
787 'name' => "dunning_${dunning_id}.pdf" };
789 $form->parse_template($myconfig, $main::userspath);
791 $dbh->disconnect() unless $provided_dbh;
793 $main::lxdebug->leave_sub();
796 sub print_invoice_for_fees {
797 $main::lxdebug->enter_sub();
799 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
801 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
803 my ($query, @values, $sth);
807 d.fee_interest_ar_id,
810 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
811 WHERE d.dunning_id = ?|;
812 my ($ar_id, $template) = selectrow_query($form, $dbh, $query, $dunning_id);
815 $main::lxdebug->leave_sub();
819 my $saved_form = save_form();
821 $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
822 my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
826 ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
827 ar.duedate, ar.notes, ar.notes AS invoicenotes,
829 c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country,
830 c.contact, c.customernumber, c.phone, c.fax, c.email,
831 c.taxnumber, c.greeting
834 LEFT JOIN customer c ON (ar.customer_id = c.id)
836 my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
837 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
839 $query = qq|SELECT * FROM employee WHERE login = ?|;
840 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{login});
841 map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
843 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|;
844 $sth = prepare_execute_query($form, $dbh, $query, $ar_id);
846 my ($row, $fee, $interest) = (0, 0, 0);
848 while ($ref = $sth->fetchrow_hashref()) {
849 next if ($ref->{amount} < 0);
854 $fee = $ref->{amount};
856 $interest = $ref->{amount};
860 $form->{fee} = $form->round_amount($fee, 2);
861 $form->{interest} = $form->round_amount($interest, 2);
862 $form->{invamount} = $form->round_amount($fee + $interest, 2);
863 $form->{dunning_id} = $dunning_id;
864 $form->{formname} = "${template}_invoice";
866 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
868 $self->set_template_options($myconfig, $form);
870 my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
872 $form->{OUT} = ">$main::spool/$filename";
873 $form->{keep_tmpfile} = 1;
874 delete $form->{tmpfile};
876 map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
878 $form->parse_template($myconfig, $main::userspath);
880 restore_form($saved_form);
882 push @{ $form->{DUNNING_PDFS} }, $filename;
883 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${main::spool}/$filename",
884 'name' => "dunning_invoice_${dunning_id}.pdf" };
886 $dbh->disconnect() unless $provided_dbh;
888 $main::lxdebug->leave_sub();