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 #======================================================================
39 use SL::GenericTranslations;
48 $main::lxdebug->enter_sub();
50 my ($self, $myconfig, $form) = @_;
53 my $dbh = $form->dbconnect($myconfig);
57 qq|FROM dunning_config | .
58 qq|ORDER BY dunning_level|;
59 $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
61 foreach my $ref (@{ $form->{DUNNING} }) {
62 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
63 $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
67 qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar
69 ($form->{AR_amount_fee}, $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 $form->{"create_invoices_for_fees_$i"} ? 't' : 'f');
98 if ($form->{"id_$i"}) {
100 qq|UPDATE dunning_config SET
101 dunning_level = ?, dunning_description = ?,
102 email_subject = ?, email_body = ?,
103 template = ?, fee = ?, interest_rate = ?,
104 active = ?, auto = ?, email = ?,
105 email_attachment = ?, payment_terms = ?, terms = ?,
106 create_invoices_for_fees = ?
108 push(@values, conv_i($form->{"id_$i"}));
111 qq|INSERT INTO dunning_config
112 (dunning_level, dunning_description, email_subject, email_body,
113 template, fee, interest_rate, active, auto, email,
114 email_attachment, payment_terms, terms, create_invoices_for_fees)
115 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
117 do_query($form, $dbh, $query, @values);
120 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
121 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
122 do_query($form, $dbh, $query, $form->{"id_$i"});
126 $query = qq|UPDATE defaults SET dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?|;
127 @values = (conv_i($form->{AR_amount_fee}), conv_i($form->{AR_amount_interest}), conv_i($form->{AR}));
128 do_query($form, $dbh, $query, @values);
133 $main::lxdebug->leave_sub();
136 sub create_invoice_for_fees {
137 $main::lxdebug->enter_sub();
139 my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
141 my ($query, @values, $sth, $ref);
143 $query = qq|SELECT dcfg.create_invoices_for_fees
145 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
146 WHERE d.dunning_id = ?|;
147 my ($create_invoices_for_fees) = selectrow_query($form, $dbh, $query, $dunning_id);
149 if (!$create_invoices_for_fees) {
150 $main::lxdebug->leave_sub();
154 $query = qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar FROM defaults|;
155 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
161 SELECT MAX(d_fee.fee)
163 WHERE (d_fee.trans_id = d.trans_id)
164 AND (d_fee.dunning_id <> ?)
165 AND NOT (d_fee.fee_interest_ar_id ISNULL)
170 SELECT MAX(d_interest.interest)
171 FROM dunning d_interest
172 WHERE (d_interest.trans_id = d.trans_id)
173 AND (d_interest.dunning_id <> ?)
174 AND NOT (d_interest.fee_interest_ar_id ISNULL)
176 AS max_previous_interest
178 WHERE dunning_id = ?|;
179 @values = ($dunning_id, $dunning_id, $dunning_id);
180 $sth = prepare_execute_query($form, $dbh, $query, @values);
182 my ($fee_remaining, $interest_remaining) = (0, 0);
183 my ($fee_total, $interest_total) = (0, 0);
185 while (my $ref = $sth->fetchrow_hashref()) {
186 $fee_remaining += $form->round_amount($ref->{fee}, 2);
187 $fee_remaining -= $form->round_amount($ref->{max_previous_fee}, 2);
188 $fee_total += $form->round_amount($ref->{fee}, 2);
189 $interest_remaining += $form->round_amount($ref->{interest}, 2);
190 $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
191 $interest_total += $form->round_amount($ref->{interest}, 2);
196 my $amount = $fee_remaining + $interest_remaining;
199 $main::lxdebug->leave_sub();
203 my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
204 my $curr = $form->get_default_currency($myconfig);
207 qq|INSERT INTO ar (id, invnumber, transdate, gldate, customer_id,
208 taxincluded, amount, netamount, paid, duedate,
209 invoice, curr, notes,
214 current_date, -- transdate
215 current_date, -- gldate
217 (SELECT ar.customer_id
219 LEFT JOIN ar ON (dn.trans_id = ar.id)
220 WHERE dn.dunning_id = ?
227 (SELECT duedate FROM dunning WHERE dunning_id = ? LIMIT 1),
232 (SELECT id FROM employee WHERE login = ?)
234 @values = ($ar_id, # id
235 $form->update_defaults($myconfig, 'invnumber', $dbh), # invnumber
236 $dunning_id, # customer_id
239 $dunning_id, # duedate
240 $curr, # default currency
241 sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
242 $form->{login}); # employee_id
243 do_query($form, $dbh, $query, @values);
246 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey)
247 VALUES (?, ?, ?, current_date, current_date, 0)|;
248 $sth = prepare_query($form, $dbh, $query);
250 @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining);
251 do_statement($form, $sth, $query, @values);
253 if ($interest_remaining) {
254 @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining);
255 do_statement($form, $sth, $query, @values);
258 @values = ($ar_id, conv_i($form->{AR}), -1 * $amount);
259 do_statement($form, $sth, $query, @values);
263 $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
264 do_query($form, $dbh, $query, $ar_id, $dunning_id);
266 $main::lxdebug->leave_sub();
270 $main::lxdebug->enter_sub();
272 my ($self, $myconfig, $form, $rows) = @_;
273 # connect to database
274 my $dbh = $form->dbconnect_noauto($myconfig);
276 my ($query, @values);
278 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
280 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
281 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
283 my $q_insert_dunning =
284 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
285 fee, interest, transdate, duedate)
287 (SELECT dunning_level FROM dunning_config WHERE id = ?),
291 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
292 (SELECT (amount - paid) * (current_date - duedate) FROM ar WHERE id = ?)
293 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
296 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
297 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
300 my ($next_dunning_config_id, $customer_id);
303 foreach my $row (@{ $rows }) {
304 push @invoice_ids, $row->{invoice_id};
305 $next_dunning_config_id = $row->{next_dunning_config_id};
306 $customer_id = $row->{customer_id};
308 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
309 do_statement($form, $h_update_ar, $q_update_ar, @values);
311 $send_email |= $row->{email};
313 my $next_config_id = conv_i($row->{next_dunning_config_id});
314 my $invoice_id = conv_i($row->{invoice_id});
316 @values = ($dunning_id, $next_config_id, $next_config_id,
317 $invoice_id, $next_config_id, $invoice_id,
318 $next_config_id, $next_config_id);
319 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
322 $h_update_ar->finish();
323 $h_insert_dunning->finish();
325 $form->{DUNNING_PDFS_EMAIL} = [];
327 $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
329 $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
330 $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
332 $form->{dunning_id} = $dunning_id;
335 $self->send_email($myconfig, $form, $dunning_id, $dbh);
341 $main::lxdebug->leave_sub();
345 $main::lxdebug->enter_sub();
347 my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
351 dcfg.email_body, dcfg.email_subject, dcfg.email_attachment,
355 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
356 LEFT JOIN ar ON (d.trans_id = ar.id)
357 LEFT JOIN customer c ON (ar.customer_id = c.id)
358 WHERE (d.dunning_id = ?)
360 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
362 if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
363 $main::lxdebug->leave_sub();
367 my $template = SL::Template::create(type => 'PlainText', form => $form, myconfig => $myconfig);
368 my $mail = Mailer->new();
369 $mail->{from} = $myconfig->{email};
370 $mail->{to} = $ref->{recipient};
371 $mail->{subject} = $template->parse_block($ref->{email_subject});
372 $mail->{message} = $template->parse_block($ref->{email_body});
374 if ($myconfig->{signature}) {
375 $mail->{message} .= "\n-- \n$myconfig->{signature}";
378 $mail->{message} =~ s/\r\n/\n/g;
380 if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
381 $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
386 $main::lxdebug->leave_sub();
389 sub set_template_options {
390 $main::lxdebug->enter_sub();
392 my ($self, $myconfig, $form) = @_;
394 $form->{templates} = "$myconfig->{templates}";
395 $form->{language} = $form->get_template_language($myconfig);
396 $form->{printer_code} = $form->get_printer_code($myconfig);
398 if ($form->{language} ne "") {
399 $form->{language} = "_" . $form->{language};
402 if ($form->{printer_code} ne "") {
403 $form->{printer_code} = "_" . $form->{printer_code};
406 $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
409 if ($form->{"format"} =~ /opendocument/) {
410 $form->{IN} =~ s/html$/odt/;
412 $form->{IN} =~ s/html$/tex/;
415 $main::lxdebug->leave_sub();
420 $main::lxdebug->enter_sub();
422 my ($self, $myconfig, $form) = @_;
424 # connect to database
425 my $dbh = $form->dbconnect($myconfig);
430 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
432 if ($form->{customer_id}) {
433 $where .= qq| AND (a.customer_id = ?)|;
434 push(@values, $form->{customer_id});
436 } elsif ($form->{customer}) {
437 $where .= qq| AND (ct.name ILIKE ?)|;
438 push(@values, '%' . $form->{customer} . '%');
442 "ordnumber" => "a.ordnumber",
443 "invnumber" => "a.invnumber",
444 "notes" => "a.notes",
446 foreach my $key (keys(%columns)) {
447 next unless ($form->{$key});
448 $where .= qq| AND $columns{$key} ILIKE ?|;
449 push(@values, '%' . $form->{$key} . '%');
452 if ($form->{dunning_level}) {
453 $where .= qq| AND nextcfg.id = ?|;
454 push(@values, conv_i($form->{dunning_level}));
457 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
458 if ($form->{minamount}) {
459 $where .= qq| AND ((a.amount - a.paid) > ?) |;
460 push(@values, $form->{minamount});
466 WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
467 my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
471 a.id, a.ordnumber, a.transdate, a.invnumber, a.amount,
472 ct.name AS customername, a.customer_id, a.duedate,
473 a.amount - a.paid AS open_amount,
475 cfg.dunning_description, cfg.dunning_level,
477 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
480 a.duedate + cfg.terms - current_date AS nextlevel,
481 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
482 current_date + cfg.payment_terms AS next_duedate,
484 nextcfg.dunning_description AS next_dunning_description,
485 nextcfg.id AS next_dunning_config_id,
486 nextcfg.terms, nextcfg.active, nextcfg.email
490 LEFT JOIN customer ct ON (a.customer_id = ct.id)
491 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
492 LEFT JOIN dunning_config nextcfg ON
497 WHERE dunning_level >
498 COALESCE((SELECT dunning_level
500 WHERE id = a.dunning_config_id
501 ORDER BY dunning_level DESC
504 ORDER BY dunning_level ASC
507 LEFT JOIN dunning d ON (d.id = (
510 WHERE (d2.trans_id = a.id)
511 AND (d2.dunning_level = cfg.dunning_level)
514 WHERE (a.paid < a.amount)
515 AND (a.duedate < current_date)
519 ORDER BY a.id, transdate, duedate, name|;
520 my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
522 $form->{DUNNINGS} = [];
524 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
525 next if ($ref->{pastdue} < $ref->{terms});
527 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
528 push(@{ $form->{DUNNINGS} }, $ref);
533 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
534 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
537 $main::lxdebug->leave_sub();
542 $main::lxdebug->enter_sub();
544 my ($self, $myconfig, $form) = @_;
546 # connect to database
547 my $dbh = $form->dbconnect($myconfig);
549 my $where = qq| WHERE (da.trans_id = a.id)|;
553 if ($form->{customer_id}) {
554 $where .= qq| AND (a.customer_id = ?)|;
555 push(@values, $form->{customer_id});
557 } elsif ($form->{customer}) {
558 $where .= qq| AND (ct.name ILIKE ?)|;
559 push(@values, '%' . $form->{customer} . '%');
563 "ordnumber" => "a.ordnumber",
564 "invnumber" => "a.invnumber",
565 "notes" => "a.notes",
567 foreach my $key (keys(%columns)) {
568 next unless ($form->{$key});
569 $where .= qq| AND $columns{$key} ILIKE ?|;
570 push(@values, '%' . $form->{$key} . '%');
573 if ($form->{dunning_level}) {
574 $where .= qq| AND a.dunning_config_id = ?|;
575 push(@values, conv_i($form->{dunning_level}));
578 if ($form->{department_id}) {
579 $where .= qq| AND a.department_id = ?|;
580 push @values, conv_i($form->{department_id});
583 $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
584 if ($form->{minamount}) {
585 $where .= qq| AND ((a.amount - a.paid) > ?) |;
586 push(@values, $form->{minamount});
589 if (!$form->{showold}) {
590 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
593 if ($form->{transdatefrom}) {
594 $where .= qq| AND a.transdate >= ?|;
595 push(@values, $form->{transdatefrom});
597 if ($form->{transdateto}) {
598 $where .= qq| AND a.transdate <= ?|;
599 push(@values, $form->{transdateto});
601 if ($form->{dunningfrom}) {
602 $where .= qq| AND da.transdate >= ?|;
603 push(@values, $form->{dunningfrom});
605 if ($form->{dunningto}) {
606 $where .= qq| AND da.transdate >= ?|;
607 push(@values, $form->{dunningto});
610 if ($form->{salesman_id}) {
611 $where .= qq| AND a.salesman_id = ?|;
612 push(@values, conv_i($form->{salesman_id}));
616 'dunning_description' => [ qw(dn.dunning_description customername invnumber) ],
617 'customername' => [ qw(customername invnumber) ],
618 'invnumber' => [ qw(a.invnumber) ],
619 'transdate' => [ qw(a.transdate a.invnumber) ],
620 'duedate' => [ qw(a.duedate a.invnumber) ],
621 'dunning_date' => [ qw(dunning_date a.invnumber) ],
622 'dunning_duedate' => [ qw(dunning_duedate a.invnumber) ],
623 'salesman' => [ qw(salesman) ],
626 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
627 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername';
628 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
631 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount,
632 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
633 da.interest, dn.dunning_description, da.transdate AS dunning_date,
634 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id,
637 JOIN customer ct ON (a.customer_id = ct.id)
638 LEFT JOIN employee e2 ON (a.salesman_id = e2.id), dunning da
639 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
641 ORDER BY $sortorder|;
643 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
645 foreach my $ref (@{ $form->{DUNNINGS} }) {
646 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
650 $main::lxdebug->leave_sub();
655 $main::lxdebug->enter_sub();
657 my ($self, $myconfig, $form, $copies) = @_;
659 # Don't allow access outside of $spool.
660 map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
663 $copies = 1 unless $copies;
664 my $spool = $::lx_office_conf{paths}->{spool};
665 my $inputfiles = join " ", map { "$spool/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
666 my $dunning_id = $form->{dunning_id};
668 $dunning_id =~ s|[^\d]||g;
670 my $in = IO::File->new($::lx_office_conf{applications}->{ghostscript} . " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
671 $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
673 if ($form->{media} eq 'printer') {
674 $form->get_printer_code($myconfig);
676 if ($form->{printer_command}) {
677 $out = IO::File->new("| $form->{printer_command}");
680 $::locale->with_raw_io($out, sub { $out->print($_) while <$in> });
682 $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
685 my $dunning_filename = $form->get_formname_translation('dunning');
686 print qq|Content-Type: Application/PDF\n| .
687 qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|;
689 $::locale->with_raw_io(\*STDOUT, sub { print while <$in> });
694 map { unlink("$spool/$_") } @{ $form->{DUNNING_PDFS} };
696 $main::lxdebug->leave_sub();
700 $main::lxdebug->enter_sub();
702 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
704 # connect to database
705 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
707 $dunning_id =~ s|[^\d]||g;
712 da.transdate AS dunning_date,
713 da.duedate AS dunning_duedate,
715 dcfg.template AS formname,
716 dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
718 ar.transdate, ar.duedate, ar.customer_id,
719 ar.invnumber, ar.ordnumber, ar.cp_id,
720 ar.amount, ar.netamount, ar.paid,
721 ar.amount - ar.paid AS open_amount,
722 ar.amount - ar.paid + da.fee + da.interest AS linetotal
725 LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
726 LEFT JOIN ar ON (ar.id = da.trans_id)
727 WHERE (da.dunning_id = ?)|;
729 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
731 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
733 $form->{TEMPLATE_ARRAYS} = {};
734 map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref}));
737 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
738 map { $form->{$_} = $ref->{$_} } keys %$ref;
739 map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
745 c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
746 c.country, c.department_1, c.department_2, c.email, c.customernumber,
747 c.greeting, c.contact, c.phone, c.fax, c.homepage,
748 c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban,
751 LEFT JOIN ar ON (d.trans_id = ar.id)
752 LEFT JOIN customer c ON (ar.customer_id = c.id)
753 LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
754 WHERE (d.dunning_id = ?)
756 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
757 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
761 cfg.interest_rate, cfg.template AS formname,
762 cfg.email_subject, cfg.email_body, cfg.email_attachment,
763 d.transdate AS dunning_date,
766 WHERE dunning_id = ?)
768 (SELECT SUM(interest)
770 WHERE dunning_id = ?)
772 (SELECT SUM(amount) - SUM(paid)
777 WHERE dunning_id = ?))
780 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
781 WHERE d.dunning_id = ?
783 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
784 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
786 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
787 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
788 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
789 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
790 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
792 $self->set_customer_cvars($myconfig, $form);
793 $self->set_template_options($myconfig, $form);
795 my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
796 my $spool = $::lx_office_conf{paths}->{spool};
797 $form->{OUT} = ">${spool}/$filename";
798 $form->{keep_tmpfile} = 1;
800 delete $form->{tmpfile};
802 push @{ $form->{DUNNING_PDFS} }, $filename;
803 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
804 'name' => "dunning_${dunning_id}.pdf" };
806 $form->parse_template($myconfig);
808 $dbh->disconnect() unless $provided_dbh;
810 $main::lxdebug->leave_sub();
813 sub print_invoice_for_fees {
814 $main::lxdebug->enter_sub();
816 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
818 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
820 my ($query, @values, $sth);
824 d.fee_interest_ar_id,
827 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
828 WHERE d.dunning_id = ?|;
829 my ($ar_id, $template) = selectrow_query($form, $dbh, $query, $dunning_id);
832 $main::lxdebug->leave_sub();
836 my $saved_form = save_form();
838 $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
839 my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
843 ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
844 ar.duedate, ar.notes, ar.notes AS invoicenotes, ar.customer_id,
846 c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country,
847 c.contact, c.customernumber, c.phone, c.fax, c.email,
848 c.taxnumber, c.greeting
851 LEFT JOIN customer c ON (ar.customer_id = c.id)
853 my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
854 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
856 $query = qq|SELECT * FROM employee WHERE login = ?|;
857 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{login});
858 map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
860 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|;
861 $sth = prepare_execute_query($form, $dbh, $query, $ar_id);
863 my ($row, $fee, $interest) = (0, 0, 0);
865 while ($ref = $sth->fetchrow_hashref()) {
866 next if ($ref->{amount} < 0);
871 $fee = $ref->{amount};
873 $interest = $ref->{amount};
877 $form->{fee} = $form->round_amount($fee, 2);
878 $form->{interest} = $form->round_amount($interest, 2);
879 $form->{invamount} = $form->round_amount($fee + $interest, 2);
880 $form->{dunning_id} = $dunning_id;
881 $form->{formname} = "${template}_invoice";
883 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
885 $self->set_customer_cvars($myconfig, $form);
886 $self->set_template_options($myconfig, $form);
888 my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
890 my $spool = $::lx_office_conf{paths}->{spool};
891 $form->{OUT} = ">$spool/$filename";
892 $form->{keep_tmpfile} = 1;
893 delete $form->{tmpfile};
895 map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
897 $form->parse_template($myconfig);
899 restore_form($saved_form);
901 push @{ $form->{DUNNING_PDFS} }, $filename;
902 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
903 'name' => "dunning_invoice_${dunning_id}.pdf" };
905 $dbh->disconnect() unless $provided_dbh;
907 $main::lxdebug->leave_sub();
910 sub set_customer_cvars {
911 my ($self, $myconfig, $form) = @_;
913 my $custom_variables = CVar->get_custom_variables(dbh => $form->get_standard_dbh,
915 trans_id => $form->{customer_id});
916 map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
918 $form->{cp_greeting} = GenericTranslations->get(dbh => $form->get_standard_dbh,
919 translation_type => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
920 language_id => $form->{language_id},
921 allow_fallback => 1);