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 #======================================================================
44 $main::lxdebug->enter_sub();
46 my ($self, $myconfig, $form) = @_;
49 my $dbh = $form->dbconnect($myconfig);
53 qq|FROM dunning_config | .
54 qq|ORDER BY dunning_level|;
55 $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
57 foreach my $ref (@{ $form->{DUNNING} }) {
58 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
59 $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
64 $main::lxdebug->leave_sub();
68 $main::lxdebug->enter_sub();
70 my ($self, $myconfig, $form) = @_;
73 my $dbh = $form->dbconnect_noauto($myconfig);
77 for my $i (1 .. $form->{rowcount}) {
78 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
79 $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
81 if (($form->{"dunning_level_$i"} ne "") &&
82 ($form->{"dunning_description_$i"} ne "")) {
83 @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
84 $form->{"email_subject_$i"}, $form->{"email_body_$i"},
85 $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
86 $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
87 $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}));
88 if ($form->{"id_$i"}) {
90 qq|UPDATE dunning_config SET
91 dunning_level = ?, dunning_description = ?,
92 email_subject = ?, email_body = ?,
93 template = ?, fee = ?, interest_rate = ?,
94 active = ?, auto = ?, email = ?,
95 email_attachment = ?, payment_terms = ?, terms = ?
97 push(@values, conv_i($form->{"id_$i"}));
100 qq|INSERT INTO dunning_config
101 (dunning_level, dunning_description, email_subject, email_body,
102 template, fee, interest_rate, active, auto, email,
103 email_attachment, payment_terms, terms)
104 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
106 do_query($form, $dbh, $query, @values);
109 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
110 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
111 do_query($form, $dbh, $query, $form->{"id_$i"});
118 $main::lxdebug->leave_sub();
122 $main::lxdebug->enter_sub();
124 my ($self, $myconfig, $form, $rows, $userspath, $spool, $sendmail) = @_;
125 # connect to database
126 my $dbh = $form->dbconnect_noauto($myconfig);
128 my ($query, @values);
130 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
132 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
133 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
135 my $q_insert_dunning =
136 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level,
137 trans_id, fee, interest, transdate, duedate)
139 (SELECT dunning_level FROM dunning_config WHERE id = ?),
143 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
144 (SELECT (amount - paid) * (current_date - transdate) FROM ar WHERE id = ?)
145 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
148 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
149 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
152 my ($next_dunning_config_id, $customer_id);
155 foreach my $row (@{ $rows }) {
156 push @invoice_ids, $row->{invoice_id};
157 $next_dunning_config_id = $row->{next_dunning_config_id};
158 $customer_id = $row->{customer_id};
160 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
161 do_statement($form, $h_update_ar, $q_update_ar, @values);
163 $send_email |= $row->{email};
165 my $next_config_id = conv_i($row->{next_dunning_config_id});
166 my $invoice_id = conv_i($row->{invoice_id});
168 @values = ($dunning_id, $next_config_id, $next_config_id,
169 $invoice_id, $next_config_id, $invoice_id,
170 $next_config_id, $next_config_id);
171 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
174 $h_update_ar->finish();
175 $h_insert_dunning->finish();
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
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)
186 . join(", ", map { "?" } @invoice_ids) . qq|)|;
188 my $sth = prepare_execute_query($form, $dbh, $query, @invoice_ids);
190 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
192 map({ $form->{"dn_$_"} = []; } keys(%{$ref}));
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 };
204 qq|SELECT id AS customer_id, name, street, zipcode, city, country, department_1, department_2, email
207 $ref = selectfirst_hashref_query($form, $dbh, $query, $customer_id);
208 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
212 cfg.interest_rate, cfg.template AS formname,
213 cfg.email_subject, cfg.email_body, cfg.email_attachment,
219 (SELECT SUM(interest)
221 WHERE dunning_id = ?)
223 (SELECT SUM(amount) - SUM(paid)
225 WHERE id IN (| . join(", ", map { "?" } @invoice_ids) . qq|))
227 FROM dunning_config cfg
229 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, @invoice_ids, $next_dunning_config_id);
230 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
232 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
233 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
234 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
235 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
236 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
238 $form->{templates} = "$myconfig->{templates}";
239 $form->{language} = $form->get_template_language(\%myconfig);
240 $form->{printer_code} = $form->get_printer_code(\%myconfig);
242 if ($form->{language} ne "") {
243 $form->{language} = "_" . $form->{language};
246 if ($form->{printer_code} ne "") {
247 $form->{printer_code} = "_" . $form->{printer_code};
250 $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
251 if ($form->{format} eq 'postscript') {
252 $form->{postscript} = 1;
253 $form->{IN} =~ s/html$/tex/;
254 } elsif ($form->{"format"} =~ /pdf/) {
256 if ($form->{"format"} =~ /opendocument/) {
257 $form->{IN} =~ s/html$/odt/;
259 $form->{IN} =~ s/html$/tex/;
261 } elsif ($form->{"format"} =~ /opendocument/) {
262 $form->{"opendocument"} = 1;
263 $form->{"IN"} =~ s/html$/odt/;
266 if ($send_email && ($form->{email} ne "")) {
267 $form->{media} = 'email';
270 $form->{keep_tmpfile} = 0;
271 if ($form->{media} eq 'email') {
272 $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
273 unless $form->{subject};
274 if (!$form->{email_attachment}) {
275 $form->{do_not_attach} = 1;
277 $form->{do_not_attach} = 0;
279 $form->{subject} = parse_strings($myconfig, $form, $userspath, $form->{email_subject});
280 $form->{message} = parse_strings($myconfig, $form, $userspath, $form->{email_body});
282 $form->{OUT} = "$sendmail";
286 my $filename = Common::unique_id() . $form->{login} . ".pdf";
287 $form->{OUT} = ">$spool/$filename";
288 push(@{ $form->{DUNNING_PDFS} }, $filename);
289 $form->{keep_tmpfile} = 1;
292 delete($form->{tmpfile});
293 $form->parse_template($myconfig, $userspath);
298 $main::lxdebug->leave_sub();
303 $main::lxdebug->enter_sub();
305 my ($self, $myconfig, $form) = @_;
307 # connect to database
308 my $dbh = $form->dbconnect($myconfig);
313 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
315 if ($form->{customer_id}) {
316 $where .= qq| AND (a.customer_id = ?)|;
317 push(@values, $form->{customer_id});
319 } elsif ($form->{customer}) {
320 $where .= qq| AND (ct.name ILIKE ?)|;
321 push(@values, '%' . $form->{customer} . '%');
325 "ordnumber" => "a.ordnumber",
326 "invnumber" => "a.invnumber",
327 "notes" => "a.notes",
329 foreach my $key (keys(%columns)) {
330 next unless ($form->{$key});
331 $where .= qq| AND $columns{$key} ILIKE ?|;
332 push(@values, '%' . $form->{$key} . '%');
335 if ($form->{dunning_level}) {
336 $where .= qq| AND nextcfg.id = ?|;
337 push(@values, conv_i($form->{dunning_level}));
340 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
341 if ($form->{minamount}) {
342 $where .= qq| AND ((a.amount - a.paid) > ?) |;
343 push(@values, $form->{minamount});
348 a.id, a.ordnumber, a.transdate, a.invnumber, a.amount,
349 ct.name AS customername, a.customer_id, a.duedate,
351 cfg.dunning_description, cfg.dunning_level,
353 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
356 a.duedate + cfg.terms - current_date AS nextlevel,
357 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
358 current_date + cfg.payment_terms AS next_duedate,
360 nextcfg.dunning_description AS next_dunning_description,
361 nextcfg.id AS next_dunning_config_id,
362 nextcfg.terms, nextcfg.active, nextcfg.email
366 LEFT JOIN customer ct ON (a.customer_id = ct.id)
367 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
368 LEFT JOIN dunning_config nextcfg ON
372 WHERE dunning_level >
373 COALESCE((SELECT dunning_level
375 WHERE id = a.dunning_config_id
376 ORDER BY dunning_level DESC
380 LEFT JOIN dunning d ON ((d.trans_id = a.id) AND (cfg.dunning_level = d.dunning_level))
382 WHERE (a.paid < a.amount)
383 AND (a.duedate < current_date)
387 ORDER BY a.id, transdate, duedate, name|;
388 my $sth = prepare_execute_query($form, $dbh, $query, @values);
390 $form->{DUNNINGS} = [];
392 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
393 next if !$ref->{terms} || ($ref->{pastdue} < $ref->{terms});
395 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
396 push(@{ $form->{DUNNINGS} }, $ref);
401 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
402 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
405 $main::lxdebug->leave_sub();
410 $main::lxdebug->enter_sub();
412 my ($self, $myconfig, $form) = @_;
414 # connect to database
415 my $dbh = $form->dbconnect($myconfig);
417 $where = qq| WHERE (da.trans_id = a.id)|;
421 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
423 if ($form->{customer_id}) {
424 $where .= qq| AND (a.customer_id = ?)|;
425 push(@values, $form->{customer_id});
427 } elsif ($form->{customer}) {
428 $where .= qq| AND (ct.name ILIKE ?)|;
429 push(@values, '%' . $form->{customer} . '%');
434 "ordnumber" => "a.ordnumber",
435 "invnumber" => "a.invnumber",
436 "notes" => "a.notes",
438 foreach my $key (keys(%columns)) {
439 next unless ($form->{$key});
440 $where .= qq| AND $columns{$key} ILIKE ?|;
441 push(@values, '%' . $form->{$key} . '%');
444 if ($form->{dunning_level}) {
445 $where .= qq| AND a.dunning_config_id = ?|;
446 push(@values, conv_i($form->{dunning_level}));
449 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
450 if ($form->{minamount}) {
451 $where .= qq| AND ((a.amount - a.paid) > ?) |;
452 push(@values, $form->{minamount});
455 if (!$form->{showold}) {
456 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
459 if ($form->{transdatefrom}) {
460 $where .= qq| AND a.transdate >= ?|;
461 push(@values, $form->{transdatefrom});
463 if ($form->{transdateto}) {
464 $where .= qq| AND a.transdate <= ?|;
465 push(@values, $form->{transdateto});
467 if ($form->{dunningfrom}) {
468 $where .= qq| AND da.transdate >= ?|;
469 push(@values, $form->{dunningfrom});
471 if ($form->{dunningto}) {
472 $where .= qq| AND da.transdate >= ?|;
473 push(@values, $form->{dunningto});
477 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount,
478 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
479 da.interest, dn.dunning_description, da.transdate AS dunning_date,
480 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id
482 JOIN customer ct ON (a.customer_id = ct.id), dunning da
483 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
485 ORDER BY name, a.id|;
487 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
489 foreach my $ref (@{ $form->{DUNNINGS} }) {
490 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
494 $main::lxdebug->leave_sub();
499 $main::lxdebug->enter_sub();
501 my ($myconfig, $form, $userspath, $string) = @_;
503 my $format = $form->{format};
504 $form->{format} = "html";
506 $tmpstring = "parse_string.html";
507 $tmpfile = "$myconfig->{templates}/$tmpstring";
508 open(OUT, ">$tmpfile") or $form->error("$tmpfile : $!");
512 my $in = $form->{IN};
513 $form->{IN} = $tmpstring;
514 $template = HTMLTemplate->new($tmpstring, $form, $myconfig, $userspath);
517 $form->{tmpfile} = "$userspath/${fileid}.$tmpstring";
519 $form->{OUT} = ">$form->{tmpfile}";
522 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
524 if (!$template->parse(*OUT)) {
526 $form->error("$form->{IN} : " . $template->get_error());
531 open(IN, $form->{tmpfile}) or $form->error($form->cleanup . "$form->{tmpfile} : $!");
539 # unlink($form->{tmpfile});
541 $form->{format} = $format;
543 $main::lxdebug->leave_sub();
549 $main::lxdebug->enter_sub();
551 my ($self, $myconfig, $form, $userspath) = @_;
553 foreach my $file (@{ $form->{DUNNING_PDFS} }) {
554 $inputfiles .= " $userspath/$file ";
557 my $outputfile = "$userspath/dunning.pdf";
558 system("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$outputfile $inputfiles");
559 foreach my $file (@{ $form->{DUNNING_PDFS} }) {
560 unlink("$userspath/$file");
566 my $numbytes = (-s $outputfile);
567 open(IN, $outputfile)
568 or $form->error($self->cleanup . "$outputfile : $!");
570 $form->{copies} = 1 unless $form->{media} eq 'printer';
572 chdir("$self->{cwd}");
574 for my $i (1 .. $form->{copies}) {
576 open(OUT, $form->{OUT})
577 or $form->error($form->cleanup . "$form->{OUT} : $!");
581 print qq|Content-Type: Application/PDF
582 Content-Disposition: attachment; filename="$outputfile"
583 Content-Length: $numbytes
587 open(OUT, ">-") or $form->error($form->cleanup . "$!: STDOUT");
601 unlink("$userspath/$outputfile");
603 $main::lxdebug->leave_sub();
607 $main::lxdebug->enter_sub();
609 my ($self, $myconfig, $form, $dunning_id, $userspath, $spool, $sendmail) = @_;
610 # connect to database
611 my $dbh = $form->dbconnect_noauto($myconfig);
614 qq|SELECT invnumber, ordnumber, customer_id, amount, netamount,
615 ar.transdate, ar.duedate, paid, amount - paid AS open_amount,
616 template AS formname, email_subject, email_body, email_attachment,
617 da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate
619 LEFT JOIN dunning_config ON (dunning_config.id = da.dunning_config_id)
620 LEFT JOIN ar ON (ar.id = da.trans_id)
621 WHERE (da.dunning_id = ?)|;
623 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
625 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
627 map({ $form->{"dn_$_"} = []; } keys(%{$ref}));
630 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest);
631 map { $form->{$_} = $ref->{$_} } keys %$ref;
632 map { push @{ $form->{"dn_$_"} }, $ref->{$_}} keys %$ref;
637 qq|SELECT id AS customer_id, name, street, zipcode, city, country, department_1, department_2, email
642 LEFT JOIN ar ON (d.trans_id = ar.id)
644 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
645 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
649 cfg.interest_rate, cfg.template AS formname,
650 cfg.email_subject, cfg.email_body, cfg.email_attachment,
651 d.fee, d.transdate AS dunning_date,
652 (SELECT SUM(interest)
654 WHERE dunning_id = ?)
656 (SELECT SUM(amount) - SUM(paid)
661 WHERE dunning_id = ?))
664 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
665 WHERE d.dunning_id = ?
667 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id);
668 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
670 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
671 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
672 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
673 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
674 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
677 $form->{templates} = "$myconfig->{templates}";
679 $form->{language} = $form->get_template_language(\%myconfig);
680 $form->{printer_code} = $form->get_printer_code(\%myconfig);
682 if ($form->{language} ne "") {
683 $form->{language} = "_" . $form->{language};
686 if ($form->{printer_code} ne "") {
687 $form->{printer_code} = "_" . $form->{printer_code};
690 $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
691 if ($form->{format} eq 'postscript') {
692 $form->{postscript} = 1;
693 $form->{IN} =~ s/html$/tex/;
694 } elsif ($form->{"format"} =~ /pdf/) {
696 if ($form->{"format"} =~ /opendocument/) {
697 $form->{IN} =~ s/html$/odt/;
699 $form->{IN} =~ s/html$/tex/;
701 } elsif ($form->{"format"} =~ /opendocument/) {
702 $form->{"opendocument"} = 1;
703 $form->{"IN"} =~ s/html$/odt/;
706 if ($form->{"send_email"} && ($form->{email} ne "")) {
707 $form->{media} = 'email';
710 $form->{keep_tmpfile} = 0;
711 if ($form->{media} eq 'email') {
712 $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
713 unless $form->{subject};
714 if (!$form->{email_attachment}) {
715 $form->{do_not_attach} = 1;
717 $form->{do_not_attach} = 0;
719 $form->{subject} = parse_strings($myconfig, $form, $userspath, $form->{email_subject});
720 $form->{message} = parse_strings($myconfig, $form, $userspath, $form->{email_body});
722 $form->{OUT} = "$sendmail";
726 my $filename = Common::unique_id() . $form->{login} . ".pdf";
728 push(@{ $form->{DUNNING_PDFS} }, $filename);
729 $form->{keep_tmpfile} = 1;
732 $form->parse_template($myconfig, $userspath);
737 $main::lxdebug->leave_sub();