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 #======================================================================
42 $main::lxdebug->enter_sub();
44 my ($self, $myconfig, $form) = @_;
47 my $dbh = $form->dbconnect($myconfig);
49 my $query = qq|SELECT dn.*
50 FROM dunning_config dn
51 ORDER BY dn.dunning_level|;
53 $sth = $dbh->prepare($query);
54 $sth->execute || $form->dberror($query);
56 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
57 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
58 $ref->{interest} = $form->format_amount($myconfig, ($ref->{interest} * 100));
59 push @{ $form->{DUNNING} }, $ref;
65 $main::lxdebug->leave_sub();
70 $main::lxdebug->enter_sub();
72 my ($self, $myconfig, $form) = @_;
75 my $dbh = $form->dbconnect_noauto($myconfig);
77 for my $i (1 .. $form->{rowcount}) {
78 $form->{"active_$i"} *= 1;
79 $form->{"auto_$i"} *= 1;
80 $form->{"email_$i"} *= 1;
81 $form->{"terms_$i"} *= 1;
82 $form->{"payment_terms_$i"} *= 1;
83 $form->{"email_attachment_$i"} *= 1;
84 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
85 $form->{"interest_$i"} = $form->parse_amount($myconfig, $form->{"interest_$i"})/100;
87 if (($form->{"dunning_level_$i"} ne "") && ($form->{"dunning_description_$i"} ne "")) {
88 if ($form->{"id_$i"}) {
89 my $query = qq|UPDATE dunning_config SET
90 dunning_level = | . $dbh->quote($form->{"dunning_level_$i"}) . qq|,
91 dunning_description = | . $dbh->quote($form->{"dunning_description_$i"}) . qq|,
92 email_subject = | . $dbh->quote($form->{"email_subject_$i"}) . qq|,
93 email_body = | . $dbh->quote($form->{"email_body_$i"}) . qq|,
94 template = | . $dbh->quote($form->{"template_$i"}) . qq|,
95 fee = '$form->{"fee_$i"}',
96 interest = '$form->{"interest_$i"}',
97 active = '$form->{"active_$i"}',
98 auto = '$form->{"auto_$i"}',
99 email = '$form->{"email_$i"}',
100 email_attachment = '$form->{"email_attachment_$i"}',
101 payment_terms = $form->{"payment_terms_$i"},
102 terms = $form->{"terms_$i"}
103 WHERE id=$form->{"id_$i"}|;
104 $dbh->do($query) || $form->dberror($query);
106 my $query = qq|INSERT INTO dunning_config (dunning_level, dunning_description, email_subject, email_body, template, fee, interest, active, auto, email, email_attachment, terms, payment_terms) VALUES (| . $dbh->quote($form->{"dunning_level_$i"}) . qq|,| . $dbh->quote($form->{"dunning_description_$i"}) . qq|,| . $dbh->quote($form->{"email_subject_$i"}) . qq|,| . $dbh->quote($form->{"email_body_$i"}) . qq|,| . $dbh->quote($form->{"template_$i"}) . qq|,'$form->{"fee_$i"}','$form->{"interest_$i"}','$form->{"active_$i"}','$form->{"auto_$i"}','$form->{"email_$i"}','$form->{"email_attachment_$i"}',$form->{"terms_$i"},$form->{"payment_terms_$i"})|;
107 $dbh->do($query) || $form->dberror($query);
110 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
111 my $query = qq|DELETE FROM dunning_config WHERE id=$form->{"id_$i"}|;
112 $dbh->do($query) || $form->dberror($query);
119 $main::lxdebug->leave_sub();
123 $main::lxdebug->enter_sub();
125 my ($self, $myconfig, $form, $rows, $userspath,$spool, $sendmail) = @_;
126 # connect to database
127 my $dbh = $form->dbconnect_noauto($myconfig);
129 foreach my $row (@{ $rows }) {
131 $form->{"interest_$row"} = $form->parse_amount($myconfig,$form->{"interest_$row"});
132 $form->{"fee_$row"} = $form->parse_amount($myconfig,$form->{"fee_$row"});
133 $form->{send_email} = $form->{"email_$row"};
135 my $query = qq| UPDATE ar set dunning_id = '$form->{"next_dunning_id_$row"}' WHERE id='$form->{"inv_id_$row"}'|;
136 $dbh->do($query) || $form->dberror($query);
137 my $query = qq| INSERT into dunning (dunning_id,dunning_level,trans_id,fee,interest,transdate,duedate) VALUES ($form->{"next_dunning_id_$row"},(select dunning_level from dunning_config WHERE id=$form->{"next_dunning_id_$row"}),$form->{"inv_id_$row"},'$form->{"fee_$row"}', '$form->{"interest_$row"}',current_date, |.$dbh->quote($form->{"next_duedate_$row"}) . qq|)|;
138 $dbh->do($query) || $form->dberror($query);
141 my $query = qq| SELECT invnumber, ordnumber, customer_id, amount, netamount, ar.transdate, ar.duedate, paid, amount-paid AS open_amount, template AS formname, email_subject, email_body, email_attachment, da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate FROM ar LEFT JOIN dunning_config ON (dunning_config.id=ar.dunning_id) LEFT JOIN dunning da ON (ar.id=da.trans_id) where ar.id IN $form->{inv_ids}|;
142 my $sth = $dbh->prepare($query);
143 $sth->execute || $form->dberror($query);
145 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
147 map({ $form->{"dn_$_"} = []; } keys(%{$ref}));
150 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest);
151 map { $form->{$_} = $ref->{$_} } keys %$ref;
152 #print(STDERR Dumper($ref));
153 map { push @{ $form->{"dn_$_"} }, $ref->{$_}} keys %$ref;
157 IS->customer_details($myconfig,$form);
158 #print(STDERR Dumper($form->{dn_invnumber}));
159 $form->{templates} = "$myconfig->{templates}";
163 $form->{language} = $form->get_template_language(\%myconfig);
164 $form->{printer_code} = $form->get_printer_code(\%myconfig);
166 if ($form->{language} ne "") {
167 $form->{language} = "_" . $form->{language};
170 if ($form->{printer_code} ne "") {
171 $form->{printer_code} = "_" . $form->{printer_code};
174 $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
175 if ($form->{format} eq 'postscript') {
176 $form->{postscript} = 1;
177 $form->{IN} =~ s/html$/tex/;
178 } elsif ($form->{"format"} =~ /pdf/) {
180 if ($form->{"format"} =~ /opendocument/) {
181 $form->{IN} =~ s/html$/odt/;
183 $form->{IN} =~ s/html$/tex/;
185 } elsif ($form->{"format"} =~ /opendocument/) {
186 $form->{"opendocument"} = 1;
187 $form->{"IN"} =~ s/html$/odt/;
190 if ($form->{"send_email"} && $form->{email}) {
191 $form->{media} = 'email';
194 $form->{keep_tmpfile} = 0;
195 if ($form->{media} eq 'email') {
196 $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
197 unless $form->{subject};
198 if (!$form->{email_attachment}) {
199 $form->{do_not_attach} = 1;
201 $form->{do_not_attach} = 0;
203 $form->{subject} = parse_strings($myconfig, $form, $userspath, $form->{email_subject});
204 $form->{message} = parse_strings($myconfig, $form, $userspath, $form->{email_body});
206 $form->{OUT} = "$sendmail";
210 my $uid = rand() . time;
212 $uid .= $form->{login};
214 $uid = substr($uid, 2, 75);
218 $form->{OUT} = ">$spool/$filename";
219 push(@{ $form->{DUNNING_PDFS} }, $filename);
220 $form->{keep_tmpfile} = 1;
223 $form->parse_template($myconfig, $userspath);
228 $main::lxdebug->leave_sub();
233 $main::lxdebug->enter_sub();
235 my ($self, $myconfig, $form) = @_;
237 # connect to database
238 my $dbh = $form->dbconnect($myconfig);
240 $where = qq| WHERE 1=1 AND a.paid < a.amount AND a.duedate < current_date AND dnn.id = (select id from dunning_config WHERE dunning_level>(select case when a.dunning_id is null then 0 else (select dunning_level from dunning_config where id=a.dunning_id order by dunning_level limit 1 ) end from dunning_config limit 1) limit 1) |;
242 if ($form->{"$form->{vc}_id"}) {
243 $where .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
245 if ($form->{ $form->{vc} }) {
246 $where .= " AND lower(ct.name) LIKE '$name'";
250 my $sortorder = join ', ',
251 ("a.id", $form->sort_columns(transdate, duedate, name));
252 $sortorder = $form->{sort} if $form->{sort};
254 $where .= " AND lower(ordnumber) LIKE '$form->{ordnumber}'" if $form->{ordnumber};
255 $where .= " AND lower(invnumber) LIKE '$form->{invnumber}'" if $form->{invnumber};
258 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
259 $where .= " AND a.dunning_id='$form->{dunning_level}'"
260 if $form->{dunning_level};
261 $where .= " AND a.ordnumber ilike '%$form->{ordnumber}%'"
262 if $form->{ordnumber};
263 $where .= " AND a.invnumber ilike '%$form->{invnumber}%'"
264 if $form->{invnumber};
265 $where .= " AND a.notes ilike '%$form->{notes}%'"
267 $where .= " AND ct.name ilike '%$form->{customer}%'"
268 if $form->{customer};
270 $where .= " AND a.amount-a.paid>'$form->{minamount}'"
271 if $form->{minamount};
273 $where .= " ORDER by $sortorder";
275 $paymentdate = ($form->{paymentuntil}) ? "'$form->{paymentuntil}'" : current_date;
277 $query = qq|SELECT a.id, a.ordnumber, a.transdate, a.invnumber,a.amount, ct.name AS customername, a.customer_id, a.duedate,da.fee AS old_fee, dnn.fee as fee, dn.dunning_description, da.transdate AS dunning_date, da.duedate AS dunning_duedate, a.duedate + dnn.terms - current_date AS nextlevel, $paymentdate - a.duedate AS pastdue, dn.dunning_level, current_date + dnn.payment_terms AS next_duedate, dnn.dunning_description AS next_dunning_description, dnn.id AS next_dunning_id, dnn.interest AS interest_rate, dnn.terms
278 FROM dunning_config dnn, ar a
279 JOIN customer ct ON (a.customer_id = ct.id)
280 LEFT JOIN dunning_config dn ON (dn.id = a.dunning_id)
281 LEFT JOIN dunning da ON (da.trans_id=a.id)
284 my $sth = $dbh->prepare($query);
285 $sth->execute || $form->dberror($query);
288 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
289 $ref->{fee} += $ref->{old_fee};
290 $ref->{interest} = ($ref->{amount} * $ref->{pastdue} * $ref->{interest_rate}) /360;
291 $ref->{interest} = $form->round_amount($ref->{interest},2);
292 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
293 if ($ref->{pastdue} >= $ref->{terms}) {
294 push @{ $form->{DUNNINGS} }, $ref;
300 $query = qq|select id, dunning_description FROM dunning_config order by dunning_level|;
301 my $sth = $dbh->prepare($query);
302 $sth->execute || $form->dberror($query);
304 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
305 push @{ $form->{DUNNING_CONFIG} }, $ref;
311 $main::lxdebug->leave_sub();
316 $main::lxdebug->enter_sub();
318 my ($self, $myconfig, $form) = @_;
320 # connect to database
321 my $dbh = $form->dbconnect($myconfig);
323 $where = qq| WHERE 1=1 AND da.trans_id=a.id|;
325 if ($form->{"$form->{vc}_id"}) {
326 $where .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
328 if ($form->{ $form->{vc} }) {
329 $where .= " AND lower(ct.name) LIKE '$name'";
333 my $sortorder = join ', ',
334 ("a.id", $form->sort_columns(transdate, duedate, name));
335 $sortorder = $form->{sort} if $form->{sort};
337 $where .= " AND lower(ordnumber) LIKE '$form->{ordnumber}'" if $form->{ordnumber};
338 $where .= " AND lower(invnumber) LIKE '$form->{invnumber}'" if $form->{invnumber};
341 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
342 $where .= " AND a.dunning_id='$form->{dunning_level}'"
343 if $form->{dunning_level};
344 $where .= " AND a.ordnumber ilike '%$form->{ordnumber}%'"
345 if $form->{ordnumber};
346 $where .= " AND a.invnumber ilike '%$form->{invnumber}%'"
347 if $form->{invnumber};
348 $where .= " AND a.notes ilike '%$form->{notes}%'"
350 $where .= " AND ct.name ilike '%$form->{customer}%'"
351 if $form->{customer};
352 $where .= " AND a.amount > a.paid AND da.dunning_id=a.dunning_id " unless ($form->{showold});
354 $where .= " AND a.transdate >='$form->{transdatefrom}' " if ($form->{transdatefrom});
355 $where .= " AND a.transdate <='$form->{transdateto}' " if ($form->{transdateto});
356 $where .= " AND da.transdate >='$form->{dunningfrom}' " if ($form->{dunningfrom});
357 $where .= " AND da.transdate <='$form->{dunningto}' " if ($form->{dunningto});
359 $where .= " ORDER by $sortorder";
362 $query = qq|SELECT a.id, a.ordnumber, a.transdate, a.invnumber,a.amount, ct.name AS customername, a.duedate,da.fee ,da.interest, dn.dunning_description, da.transdate AS dunning_date, da.duedate AS dunning_duedate
364 JOIN customer ct ON (a.customer_id = ct.id),
365 dunning da LEFT JOIN dunning_config dn ON (da.dunning_id=dn.id)
368 my $sth = $dbh->prepare($query);
369 $sth->execute || $form->dberror($query);
372 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
374 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
375 push @{ $form->{DUNNINGS} }, $ref;
383 $main::lxdebug->leave_sub();
389 $main::lxdebug->enter_sub();
391 my ($myconfig, $form, $userspath, $string) = @_;
393 my $format = $form->{format};
394 $form->{format} = "html";
396 $tmpstring = "parse_string.html";
397 $tmpfile = "$myconfig->{templates}/$tmpstring";
398 open(OUT, ">$tmpfile") or $form->error("$tmpfile : $!");
402 my $in = $form->{IN};
403 $form->{IN} = $tmpstring;
404 $template = HTMLTemplate->new($tmpstring, $form, $myconfig, $userspath);
407 $form->{tmpfile} = "$userspath/${fileid}.$tmpstring";
409 $form->{OUT} = ">$form->{tmpfile}";
412 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
414 if (!$template->parse(*OUT)) {
416 $form->error("$form->{IN} : " . $template->get_error());
421 open(IN, $form->{tmpfile}) or $form->error($form->cleanup . "$form->{tmpfile} : $!");
429 # unlink($form->{tmpfile});
431 $form->{format} = $format;
433 $main::lxdebug->leave_sub();
439 $main::lxdebug->enter_sub();
441 my ($self, $myconfig, $form, $userspath) = @_;
443 foreach my $file (@{ $form->{DUNNING_PDFS} }) {
444 $inputfiles .= " $userspath/$file ";
447 my $outputfile = "$userspath/dunning.pdf";
448 system("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$outputfile $inputfiles");
449 foreach my $file (@{ $form->{DUNNING_PDFS} }) {
450 unlink("$userspath/$file");
457 my $numbytes = (-s $outputfile);
458 open(IN, $outputfile)
459 or $form->error($self->cleanup . "$outputfile : $!");
461 $form->{copies} = 1 unless $form->{media} eq 'printer';
463 chdir("$self->{cwd}");
465 for my $i (1 .. $form->{copies}) {
467 open(OUT, $form->{OUT})
468 or $form->error($form->cleanup . "$form->{OUT} : $!");
472 print qq|Content-Type: Application/PDF
473 Content-Disposition: attachment; filename="$outputfile"
474 Content-Length: $numbytes
478 open(OUT, ">-") or $form->error($form->cleanup . "$!: STDOUT");
492 unlink("$userspath/$outputfile");
494 $main::lxdebug->leave_sub();