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 #======================================================================
40 use SL::GenericTranslations;
52 $main::lxdebug->enter_sub();
54 my ($self, $myconfig, $form) = @_;
57 my $dbh = $form->dbconnect($myconfig);
61 qq|FROM dunning_config | .
62 qq|ORDER BY dunning_level|;
63 $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
65 foreach my $ref (@{ $form->{DUNNING} }) {
66 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
67 $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
71 qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar
73 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
77 $main::lxdebug->leave_sub();
81 $main::lxdebug->enter_sub();
83 my ($self, $myconfig, $form) = @_;
86 my $dbh = $form->dbconnect_noauto($myconfig);
90 for my $i (1 .. $form->{rowcount}) {
91 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
92 $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
94 if (($form->{"dunning_level_$i"} ne "") &&
95 ($form->{"dunning_description_$i"} ne "")) {
96 @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
97 $form->{"email_subject_$i"}, $form->{"email_body_$i"},
98 $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
99 $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
100 $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}),
101 $form->{"create_invoices_for_fees_$i"} ? 't' : 'f');
102 if ($form->{"id_$i"}) {
104 qq|UPDATE dunning_config SET
105 dunning_level = ?, dunning_description = ?,
106 email_subject = ?, email_body = ?,
107 template = ?, fee = ?, interest_rate = ?,
108 active = ?, auto = ?, email = ?,
109 email_attachment = ?, payment_terms = ?, terms = ?,
110 create_invoices_for_fees = ?
112 push(@values, conv_i($form->{"id_$i"}));
115 qq|INSERT INTO dunning_config
116 (dunning_level, dunning_description, email_subject, email_body,
117 template, fee, interest_rate, active, auto, email,
118 email_attachment, payment_terms, terms, create_invoices_for_fees)
119 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
121 do_query($form, $dbh, $query, @values);
124 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
125 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
126 do_query($form, $dbh, $query, $form->{"id_$i"});
130 $query = qq|UPDATE defaults SET dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?|;
131 @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);
147 $query = qq|SELECT dcfg.create_invoices_for_fees
149 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
150 WHERE d.dunning_id = ?|;
151 my ($create_invoices_for_fees) = selectrow_query($form, $dbh, $query, $dunning_id);
153 if (!$create_invoices_for_fees) {
154 $main::lxdebug->leave_sub();
158 $query = qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar FROM defaults|;
159 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
165 SELECT MAX(d_fee.fee)
167 WHERE (d_fee.trans_id = d.trans_id)
168 AND (d_fee.dunning_id <> ?)
169 AND NOT (d_fee.fee_interest_ar_id ISNULL)
174 SELECT MAX(d_interest.interest)
175 FROM dunning d_interest
176 WHERE (d_interest.trans_id = d.trans_id)
177 AND (d_interest.dunning_id <> ?)
178 AND NOT (d_interest.fee_interest_ar_id ISNULL)
180 AS max_previous_interest
182 WHERE dunning_id = ?|;
183 @values = ($dunning_id, $dunning_id, $dunning_id);
184 $sth = prepare_execute_query($form, $dbh, $query, @values);
186 my ($fee_remaining, $interest_remaining) = (0, 0);
187 my ($fee_total, $interest_total) = (0, 0);
189 while (my $ref = $sth->fetchrow_hashref()) {
190 $fee_remaining += $form->round_amount($ref->{fee}, 2);
191 $fee_remaining -= $form->round_amount($ref->{max_previous_fee}, 2);
192 $fee_total += $form->round_amount($ref->{fee}, 2);
193 $interest_remaining += $form->round_amount($ref->{interest}, 2);
194 $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
195 $interest_total += $form->round_amount($ref->{interest}, 2);
200 my $amount = $fee_remaining + $interest_remaining;
203 $main::lxdebug->leave_sub();
207 my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
208 my $curr = $form->get_default_currency($myconfig);
209 my $trans_number = SL::TransNumber->new(type => 'invoice', dbh => $dbh);
212 qq|INSERT INTO ar (id, invnumber, transdate, gldate, customer_id,
213 taxincluded, amount, netamount, paid, duedate,
214 invoice, currency_id, notes,
219 current_date, -- transdate
220 current_date, -- gldate
222 (SELECT ar.customer_id
224 LEFT JOIN ar ON (dn.trans_id = ar.id)
225 WHERE dn.dunning_id = ?
232 (SELECT duedate FROM dunning WHERE dunning_id = ? LIMIT 1),
234 (SELECT id FROM currencies WHERE name = ?), -- curr
237 (SELECT id FROM employee WHERE login = ?)
239 @values = ($ar_id, # id
240 $trans_number->create_unique, # invnumber
241 $dunning_id, # customer_id
244 $dunning_id, # duedate
245 $curr, # default currency
246 sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
247 $::myconfig{login}); # employee_id
248 do_query($form, $dbh, $query, @values);
251 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey, tax_id, chart_link)
252 VALUES (?, ?, ?, current_date, current_date, 0,
253 (SELECT id FROM tax WHERE (taxkey = 0) AND (rate = 0)),
254 (SELECT link FROM chart WHERE id = ?))|;
255 $sth = prepare_query($form, $dbh, $query);
257 @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining, conv_i($form->{AR_amount_fee}));
258 do_statement($form, $sth, $query, @values);
260 if ($interest_remaining) {
261 @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining, conv_i($form->{AR_amount_interest}));
262 do_statement($form, $sth, $query, @values);
265 @values = ($ar_id, conv_i($form->{AR}), -1 * $amount, conv_i($form->{AR}));
266 do_statement($form, $sth, $query, @values);
270 $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
271 do_query($form, $dbh, $query, $ar_id, $dunning_id);
273 $main::lxdebug->leave_sub();
277 $main::lxdebug->enter_sub();
279 my ($self, $myconfig, $form, $rows) = @_;
280 # connect to database
281 my $dbh = $form->dbconnect_noauto($myconfig);
283 my ($query, @values);
285 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
287 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
288 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
290 my $q_insert_dunning =
291 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
292 fee, interest, transdate, duedate)
294 (SELECT dunning_level FROM dunning_config WHERE id = ?),
298 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
299 (SELECT (amount - paid) * (current_date - duedate) FROM ar WHERE id = ?)
300 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
303 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
304 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
307 my ($next_dunning_config_id, $customer_id);
310 foreach my $row (@{ $rows }) {
311 push @invoice_ids, $row->{invoice_id};
312 $next_dunning_config_id = $row->{next_dunning_config_id};
313 $customer_id = $row->{customer_id};
315 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
316 do_statement($form, $h_update_ar, $q_update_ar, @values);
318 $send_email |= $row->{email};
320 my $next_config_id = conv_i($row->{next_dunning_config_id});
321 my $invoice_id = conv_i($row->{invoice_id});
323 @values = ($dunning_id, $next_config_id, $next_config_id,
324 $invoice_id, $next_config_id, $invoice_id,
325 $next_config_id, $next_config_id);
326 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
329 $h_update_ar->finish();
330 $h_insert_dunning->finish();
332 $form->{DUNNING_PDFS_EMAIL} = [];
334 $form->{dunning_id} = $dunning_id;
336 $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
338 $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
339 $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
343 $self->send_email($myconfig, $form, $dunning_id, $dbh);
349 $main::lxdebug->leave_sub();
353 $main::lxdebug->enter_sub();
355 my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
359 dcfg.email_body, dcfg.email_subject, dcfg.email_attachment,
363 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
364 LEFT JOIN ar ON (d.trans_id = ar.id)
365 LEFT JOIN customer c ON (ar.customer_id = c.id)
366 WHERE (d.dunning_id = ?)
368 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
370 if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
371 $main::lxdebug->leave_sub();
375 my $template = SL::Template::create(type => 'PlainText', form => $form, myconfig => $myconfig);
376 my $mail = Mailer->new();
377 $mail->{from} = $myconfig->{email};
378 $mail->{to} = $ref->{recipient};
379 $mail->{subject} = $template->parse_block($ref->{email_subject});
380 $mail->{message} = $template->parse_block($ref->{email_body});
382 $mail->{message} .= $form->create_email_signature();
384 $mail->{message} =~ s/\r\n/\n/g;
386 if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
387 $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
392 $main::lxdebug->leave_sub();
395 sub set_template_options {
396 $main::lxdebug->enter_sub();
398 my ($self, $myconfig, $form) = @_;
400 my $defaults = SL::DB::Default->get;
401 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
402 $form->{templates} = $defaults->templates;
403 $form->{language} = $form->get_template_language($myconfig);
404 $form->{printer_code} = $form->get_printer_code($myconfig);
406 if ($form->{language} ne "") {
407 $form->{language} = "_" . $form->{language};
410 if ($form->{printer_code} ne "") {
411 $form->{printer_code} = "_" . $form->{printer_code};
414 my $extension = 'html';
415 if ($form->{format} eq 'postscript') {
416 $form->{postscript} = 1;
419 } elsif ($form->{"format"} =~ /pdf/) {
421 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
423 } elsif ($form->{"format"} =~ /opendocument/) {
424 $form->{opendocument} = 1;
426 } elsif ($form->{"format"} =~ /excel/) {
432 # search for the template
434 push @template_files, "$form->{formname}_email$form->{language}$form->{printer_code}.$extension" if $form->{media} eq 'email';
435 push @template_files, "$form->{formname}$form->{language}$form->{printer_code}.$extension";
436 push @template_files, "$form->{formname}.$extension";
437 push @template_files, "default.$extension";
440 for my $filename (@template_files) {
441 if (-f ($defaults->templates . "/$filename")) {
442 $form->{IN} = $filename;
447 if (!defined $form->{IN}) {
448 $::form->error($::locale->text('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files));
451 # prepare meta information for template introspection
452 $form->{template_meta} = {
453 formname => $form->{formname},
454 language => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id}),
455 format => $form->{format},
456 media => $form->{media},
457 extension => $extension,
458 printer => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id}),
459 today => DateTime->today,
462 $main::lxdebug->leave_sub();
467 $main::lxdebug->enter_sub();
469 my ($self, $myconfig, $form) = @_;
471 # connect to database
472 my $dbh = $form->dbconnect($myconfig);
477 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
479 if ($form->{customer_id}) {
480 $where .= qq| AND (a.customer_id = ?)|;
481 push(@values, $form->{customer_id});
483 } elsif ($form->{customer}) {
484 $where .= qq| AND (ct.name ILIKE ?)|;
485 push(@values, '%' . $form->{customer} . '%');
489 "ordnumber" => "a.ordnumber",
490 "invnumber" => "a.invnumber",
491 "notes" => "a.notes",
492 "country" => "ct.country",
494 foreach my $key (keys(%columns)) {
495 next unless ($form->{$key});
496 $where .= qq| AND $columns{$key} ILIKE ?|;
497 push(@values, '%' . $form->{$key} . '%');
500 if ($form->{dunning_level}) {
501 $where .= qq| AND nextcfg.id = ?|;
502 push(@values, conv_i($form->{dunning_level}));
505 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
506 if ($form->{minamount}) {
507 $where .= qq| AND ((a.amount - a.paid) > ?) |;
508 push(@values, $form->{minamount});
514 WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
515 my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
519 a.id, a.ordnumber, a.transdate, a.invnumber, a.amount, a.language_id,
520 ct.name AS customername, a.customer_id, a.duedate,
521 a.amount - a.paid AS open_amount,
523 cfg.dunning_description, cfg.dunning_level,
525 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
528 a.duedate + cfg.terms - current_date AS nextlevel,
529 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
530 current_date + cfg.payment_terms AS next_duedate,
532 nextcfg.dunning_description AS next_dunning_description,
533 nextcfg.id AS next_dunning_config_id,
534 nextcfg.terms, nextcfg.active, nextcfg.email
538 LEFT JOIN customer ct ON (a.customer_id = ct.id)
539 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
540 LEFT JOIN dunning_config nextcfg ON
545 WHERE dunning_level >
546 COALESCE((SELECT dunning_level
548 WHERE id = a.dunning_config_id
549 ORDER BY dunning_level DESC
552 ORDER BY dunning_level ASC
555 LEFT JOIN dunning d ON (d.id = (
558 WHERE (d2.trans_id = a.id)
559 AND (d2.dunning_level = cfg.dunning_level)
562 WHERE (a.paid < a.amount)
563 AND (a.duedate < current_date)
567 ORDER BY a.id, transdate, duedate, name|;
568 my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
570 $form->{DUNNINGS} = [];
572 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
573 next if ($ref->{pastdue} < $ref->{terms});
575 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
576 push(@{ $form->{DUNNINGS} }, $ref);
581 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
582 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
585 $main::lxdebug->leave_sub();
590 $main::lxdebug->enter_sub();
592 my ($self, $myconfig, $form) = @_;
594 # connect to database
595 my $dbh = $form->dbconnect($myconfig);
597 my $where = qq| WHERE (da.trans_id = a.id)|;
601 if ($form->{customer_id}) {
602 $where .= qq| AND (a.customer_id = ?)|;
603 push(@values, $form->{customer_id});
605 } elsif ($form->{customer}) {
606 $where .= qq| AND (ct.name ILIKE ?)|;
607 push(@values, '%' . $form->{customer} . '%');
611 "ordnumber" => "a.ordnumber",
612 "invnumber" => "a.invnumber",
613 "notes" => "a.notes",
615 foreach my $key (keys(%columns)) {
616 next unless ($form->{$key});
617 $where .= qq| AND $columns{$key} ILIKE ?|;
618 push(@values, '%' . $form->{$key} . '%');
621 if ($form->{dunning_level}) {
622 $where .= qq| AND a.dunning_config_id = ?|;
623 push(@values, conv_i($form->{dunning_level}));
626 if ($form->{department_id}) {
627 $where .= qq| AND a.department_id = ?|;
628 push @values, conv_i($form->{department_id});
631 $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
632 if ($form->{minamount}) {
633 $where .= qq| AND ((a.amount - a.paid) > ?) |;
634 push(@values, $form->{minamount});
637 if (!$form->{showold}) {
638 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
641 if ($form->{transdatefrom}) {
642 $where .= qq| AND a.transdate >= ?|;
643 push(@values, $form->{transdatefrom});
645 if ($form->{transdateto}) {
646 $where .= qq| AND a.transdate <= ?|;
647 push(@values, $form->{transdateto});
649 if ($form->{dunningfrom}) {
650 $where .= qq| AND da.transdate >= ?|;
651 push(@values, $form->{dunningfrom});
653 if ($form->{dunningto}) {
654 $where .= qq| AND da.transdate >= ?|;
655 push(@values, $form->{dunningto});
658 if ($form->{salesman_id}) {
659 $where .= qq| AND a.salesman_id = ?|;
660 push(@values, conv_i($form->{salesman_id}));
664 'dunning_description' => [ qw(dn.dunning_description customername invnumber) ],
665 'customername' => [ qw(customername invnumber) ],
666 'invnumber' => [ qw(a.invnumber) ],
667 'transdate' => [ qw(a.transdate a.invnumber) ],
668 'duedate' => [ qw(a.duedate a.invnumber) ],
669 'dunning_date' => [ qw(dunning_date a.invnumber) ],
670 'dunning_duedate' => [ qw(dunning_duedate a.invnumber) ],
671 'salesman' => [ qw(salesman) ],
674 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
675 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername';
676 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
679 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount, a.language_id,
680 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
681 da.interest, dn.dunning_description, da.transdate AS dunning_date,
682 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id,
685 JOIN customer ct ON (a.customer_id = ct.id)
686 LEFT JOIN employee e2 ON (a.salesman_id = e2.id), dunning da
687 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
689 ORDER BY $sortorder|;
691 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
693 foreach my $ref (@{ $form->{DUNNINGS} }) {
694 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
698 $main::lxdebug->leave_sub();
703 $main::lxdebug->enter_sub();
705 my ($self, $myconfig, $form, $copies) = @_;
707 # Don't allow access outside of $spool.
708 map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
711 $copies = 1 unless $copies;
712 my $spool = $::lx_office_conf{paths}->{spool};
713 my $inputfiles = join " ", map { "$spool/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
714 my $dunning_id = $form->{dunning_id};
716 $dunning_id =~ s|[^\d]||g;
718 my $in = IO::File->new($::lx_office_conf{applications}->{ghostscript} . " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
719 $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
721 if ($form->{media} eq 'printer') {
722 $form->get_printer_code($myconfig);
724 if ($form->{printer_command}) {
725 $out = IO::File->new("| $form->{printer_command}");
728 $::locale->with_raw_io($out, sub { $out->print($_) while <$in> });
730 $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
733 my $dunning_filename = $form->get_formname_translation('dunning');
734 print qq|Content-Type: Application/PDF\n| .
735 qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|;
737 $::locale->with_raw_io(\*STDOUT, sub { print while <$in> });
742 map { unlink("$spool/$_") } @{ $form->{DUNNING_PDFS} };
744 $main::lxdebug->leave_sub();
748 $main::lxdebug->enter_sub();
750 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
752 # connect to database
753 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
755 $dunning_id =~ s|[^\d]||g;
757 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
758 if ($form->{"language_id"}) {
759 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
760 AM->get_language_details($myconfig, $form, $form->{language_id});
762 $output_dateformat = $myconfig->{dateformat};
763 $output_numberformat = $myconfig->{numberformat};
764 $output_longdates = 1;
770 da.transdate AS dunning_date,
771 da.duedate AS dunning_duedate,
773 dcfg.template AS formname,
774 dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
776 ar.transdate, ar.duedate, ar.customer_id,
777 ar.invnumber, ar.ordnumber, ar.cp_id,
778 ar.amount, ar.netamount, ar.paid,
779 (SELECT cu.name FROM currencies cu WHERE cu.id=ar.currency_id) AS curr,
780 ar.amount - ar.paid AS open_amount,
781 ar.amount - ar.paid + da.fee + da.interest AS linetotal
784 LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
785 LEFT JOIN ar ON (ar.id = da.trans_id)
786 WHERE (da.dunning_id = ?)|;
788 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
790 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
792 $form->{TEMPLATE_ARRAYS} = {};
793 map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref}));
796 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
797 map { $form->{$_} = $ref->{$_} } keys %$ref;
798 map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
804 c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
805 c.country, c.department_1, c.department_2, c.email, c.customernumber,
806 c.greeting, c.contact, c.phone, c.fax, c.homepage,
807 c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban,
808 c,ustid, e.name as salesman_name,
811 LEFT JOIN ar ON (d.trans_id = ar.id)
812 LEFT JOIN customer c ON (ar.customer_id = c.id)
813 LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
814 LEFT JOIN employee e ON (ar.salesman_id = e.id)
815 WHERE (d.dunning_id = ?)
817 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
818 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
822 cfg.interest_rate, cfg.template AS formname,
823 cfg.email_subject, cfg.email_body, cfg.email_attachment,
824 d.transdate AS dunning_date,
827 WHERE dunning_id = ?)
829 (SELECT SUM(interest)
831 WHERE dunning_id = ?)
833 (SELECT SUM(amount) - SUM(paid)
838 WHERE dunning_id = ?))
841 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
842 WHERE d.dunning_id = ?
844 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
845 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
847 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
848 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
849 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
850 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
851 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
853 $::form->format_dates($output_dateformat, $output_longdates,
854 qw(dn_dunning_date dn_dunning_duedate dn_transdate dn_duedate
855 dunning_date dunning_duedate transdate duedate)
857 $::form->reformat_numbers($output_numberformat, 2, qw(
858 dn_amount dn_netamount dn_paid dn_open_amount dn_fee dn_interest dn_linetotal
859 amount netamount paid open_amount fee interest linetotal
860 total_interest total_open_interest total_amount total_open_amount
862 $::form->reformat_numbers($output_numberformat, undef, qw(interest_rate));
864 $self->set_customer_cvars($myconfig, $form);
865 $self->set_template_options($myconfig, $form);
867 my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
868 my $spool = $::lx_office_conf{paths}->{spool};
869 $form->{OUT} = "${spool}/$filename";
870 $form->{keep_tmpfile} = 1;
872 delete $form->{tmpfile};
874 push @{ $form->{DUNNING_PDFS} }, $filename;
875 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
876 'name' => $form->get_formname_translation('dunning') . "_${dunning_id}.pdf" };
878 $form->parse_template($myconfig);
880 $dbh->disconnect() unless $provided_dbh;
882 $main::lxdebug->leave_sub();
885 sub print_invoice_for_fees {
886 $main::lxdebug->enter_sub();
888 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
890 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
892 my ($query, @values, $sth);
896 d.fee_interest_ar_id,
899 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
900 WHERE d.dunning_id = ?|;
901 my ($ar_id, $template) = selectrow_query($form, $dbh, $query, $dunning_id);
904 $main::lxdebug->leave_sub();
908 my $saved_form = save_form();
910 $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
911 my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
915 ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
916 ar.duedate, ar.notes, ar.notes AS invoicenotes, ar.customer_id,
918 c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country,
919 c.contact, c.customernumber, c.phone, c.fax, c.email,
920 c.taxnumber, c.greeting
923 LEFT JOIN customer c ON (ar.customer_id = c.id)
925 my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
926 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
928 $query = qq|SELECT * FROM employee WHERE login = ?|;
929 $ref = selectfirst_hashref_query($form, $dbh, $query, $::myconfig{login});
930 map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
932 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|;
933 $sth = prepare_execute_query($form, $dbh, $query, $ar_id);
935 my ($row, $fee, $interest) = (0, 0, 0);
937 while ($ref = $sth->fetchrow_hashref()) {
938 next if ($ref->{amount} < 0);
943 $fee = $ref->{amount};
945 $interest = $ref->{amount};
949 $form->{fee} = $form->round_amount($fee, 2);
950 $form->{interest} = $form->round_amount($interest, 2);
951 $form->{invamount} = $form->round_amount($fee + $interest, 2);
952 $form->{dunning_id} = $dunning_id;
953 $form->{formname} = "${template}_invoice";
955 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
957 $self->set_customer_cvars($myconfig, $form);
958 $self->set_template_options($myconfig, $form);
960 my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
962 my $spool = $::lx_office_conf{paths}->{spool};
963 $form->{OUT} = "$spool/$filename";
964 $form->{keep_tmpfile} = 1;
965 delete $form->{tmpfile};
967 map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
969 $form->parse_template($myconfig);
971 restore_form($saved_form);
973 push @{ $form->{DUNNING_PDFS} }, $filename;
974 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
975 'name' => "dunning_invoice_${dunning_id}.pdf" };
977 $dbh->disconnect() unless $provided_dbh;
979 $main::lxdebug->leave_sub();
982 sub set_customer_cvars {
983 my ($self, $myconfig, $form) = @_;
985 my $custom_variables = CVar->get_custom_variables(dbh => $form->get_standard_dbh,
987 trans_id => $form->{customer_id});
988 map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
990 $form->{cp_greeting} = GenericTranslations->get(dbh => $form->get_standard_dbh,
991 translation_type => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
992 language_id => $form->{language_id},
993 allow_fallback => 1);