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., 51 Franklin Street, Fifth Floor, Boston,
30 #======================================================================
32 # Dunning process module
34 #======================================================================
41 use SL::GenericTranslations;
49 use SL::Util qw(trim);
55 $main::lxdebug->enter_sub();
57 my ($self, $myconfig, $form) = @_;
60 my $dbh = SL::DB->client->dbh;
64 qq|FROM dunning_config | .
65 qq|ORDER BY dunning_level|;
66 $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
68 foreach my $ref (@{ $form->{DUNNING} }) {
69 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
70 $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
74 qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar
76 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
78 $main::lxdebug->leave_sub();
82 my ($self, $myconfig, $form) = @_;
83 $main::lxdebug->enter_sub();
85 my $rc = SL::DB->client->with_transaction(\&_save_config, $self, $myconfig, $form);
87 $::lxdebug->leave_sub;
92 my ($self, $myconfig, $form) = @_;
94 my $dbh = SL::DB->client->dbh;
98 for my $i (1 .. $form->{rowcount}) {
99 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
100 $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
102 if (($form->{"dunning_level_$i"} ne "") &&
103 ($form->{"dunning_description_$i"} ne "")) {
104 @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
105 $form->{"email_subject_$i"}, $form->{"email_body_$i"},
106 $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
107 $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
108 $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}),
109 $form->{"create_invoices_for_fees_$i"} ? 't' : 'f');
110 if ($form->{"id_$i"}) {
112 qq|UPDATE dunning_config SET
113 dunning_level = ?, dunning_description = ?,
114 email_subject = ?, email_body = ?,
115 template = ?, fee = ?, interest_rate = ?,
116 active = ?, auto = ?, email = ?,
117 email_attachment = ?, payment_terms = ?, terms = ?,
118 create_invoices_for_fees = ?
120 push(@values, conv_i($form->{"id_$i"}));
123 qq|INSERT INTO dunning_config
124 (dunning_level, dunning_description, email_subject, email_body,
125 template, fee, interest_rate, active, auto, email,
126 email_attachment, payment_terms, terms, create_invoices_for_fees)
127 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
129 do_query($form, $dbh, $query, @values);
132 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
133 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
134 do_query($form, $dbh, $query, $form->{"id_$i"});
138 $query = qq|UPDATE defaults SET dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?|;
139 @values = (conv_i($form->{AR_amount_fee}), conv_i($form->{AR_amount_interest}), conv_i($form->{AR}));
140 do_query($form, $dbh, $query, @values);
145 sub create_invoice_for_fees {
146 $main::lxdebug->enter_sub();
148 my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
150 my ($query, @values, $sth, $ref);
152 $query = qq|SELECT dcfg.create_invoices_for_fees
154 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
155 WHERE d.dunning_id = ?|;
156 my ($create_invoices_for_fees) = selectrow_query($form, $dbh, $query, $dunning_id);
158 if (!$create_invoices_for_fees) {
159 $main::lxdebug->leave_sub();
163 $query = qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar FROM defaults|;
164 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
170 SELECT MAX(d_fee.fee)
172 WHERE (d_fee.trans_id = d.trans_id)
173 AND (d_fee.dunning_id <> ?)
174 AND NOT (d_fee.fee_interest_ar_id ISNULL)
179 SELECT MAX(d_interest.interest)
180 FROM dunning d_interest
181 WHERE (d_interest.trans_id = d.trans_id)
182 AND (d_interest.dunning_id <> ?)
183 AND NOT (d_interest.fee_interest_ar_id ISNULL)
185 AS max_previous_interest
187 WHERE dunning_id = ?|;
188 @values = ($dunning_id, $dunning_id, $dunning_id);
189 $sth = prepare_execute_query($form, $dbh, $query, @values);
191 my ($fee_remaining, $interest_remaining) = (0, 0);
192 my ($fee_total, $interest_total) = (0, 0);
194 while (my $ref = $sth->fetchrow_hashref()) {
195 $fee_remaining += $form->round_amount($ref->{fee}, 2);
196 $fee_remaining -= $form->round_amount($ref->{max_previous_fee}, 2);
197 $fee_total += $form->round_amount($ref->{fee}, 2);
198 $interest_remaining += $form->round_amount($ref->{interest}, 2);
199 $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
200 $interest_total += $form->round_amount($ref->{interest}, 2);
205 my $amount = $fee_remaining + $interest_remaining;
208 $main::lxdebug->leave_sub();
212 my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
213 my $curr = $form->get_default_currency($myconfig);
214 my $trans_number = SL::TransNumber->new(type => 'invoice', dbh => $dbh);
217 qq|INSERT INTO ar (id, invnumber, transdate, gldate, customer_id,
218 taxincluded, amount, netamount, paid, duedate,
219 invoice, currency_id, taxzone_id, notes,
224 current_date, -- transdate
225 current_date, -- gldate
227 (SELECT ar.customer_id
229 LEFT JOIN ar ON (dn.trans_id = ar.id)
230 WHERE dn.dunning_id = ?
237 (SELECT duedate FROM dunning WHERE dunning_id = ? LIMIT 1),
239 (SELECT id FROM currencies WHERE name = ?), -- curr
241 (SELECT taxzone_id FROM customer WHERE id =
242 (SELECT ar.customer_id
244 LEFT JOIN ar ON (dn.trans_id = ar.id)
245 WHERE dn.dunning_id = ?
250 (SELECT id FROM employee WHERE login = ?)
252 @values = ($ar_id, # id
253 $trans_number->create_unique, # invnumber
254 $dunning_id, # customer_id
257 $dunning_id, # duedate
258 $curr, # default currency
259 $dunning_id, # taxzone_id
260 sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
261 $::myconfig{login}); # employee_id
262 do_query($form, $dbh, $query, @values);
265 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey, tax_id, chart_link)
266 VALUES (?, ?, ?, current_date, current_date, 0,
267 (SELECT id FROM tax WHERE (taxkey = 0) AND (rate = 0)),
268 (SELECT link FROM chart WHERE id = ?))|;
269 $sth = prepare_query($form, $dbh, $query);
271 @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining, conv_i($form->{AR_amount_fee}));
272 do_statement($form, $sth, $query, @values);
274 if ($interest_remaining) {
275 @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining, conv_i($form->{AR_amount_interest}));
276 do_statement($form, $sth, $query, @values);
279 @values = ($ar_id, conv_i($form->{AR}), -1 * $amount, conv_i($form->{AR}));
280 do_statement($form, $sth, $query, @values);
284 $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
285 do_query($form, $dbh, $query, $ar_id, $dunning_id);
287 $main::lxdebug->leave_sub();
292 my ($self, $myconfig, $form, $rows) = @_;
293 $main::lxdebug->enter_sub();
295 my $rc = SL::DB->client->with_transaction(\&_save_dunning, $self, $myconfig, $form, $rows);
298 die SL::DB->client->error
300 $::lxdebug->leave_sub;
307 my ($self, $myconfig, $form, $rows) = @_;
309 my $dbh = SL::DB->client->dbh;
311 my ($query, @values);
313 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
315 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
316 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
318 my $q_insert_dunning =
319 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
320 fee, interest, transdate, duedate)
322 (SELECT dunning_level FROM dunning_config WHERE id = ?),
326 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
327 (SELECT (amount - paid) * (current_date - duedate) FROM ar WHERE id = ?)
328 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
331 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
332 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
335 my ($next_dunning_config_id, $customer_id);
338 foreach my $row (@{ $rows }) {
339 push @invoice_ids, $row->{invoice_id};
340 $next_dunning_config_id = $row->{next_dunning_config_id};
341 $customer_id = $row->{customer_id};
343 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
344 do_statement($form, $h_update_ar, $q_update_ar, @values);
346 $send_email |= $row->{email};
348 my $next_config_id = conv_i($row->{next_dunning_config_id});
349 my $invoice_id = conv_i($row->{invoice_id});
351 @values = ($dunning_id, $next_config_id, $next_config_id,
352 $invoice_id, $next_config_id, $invoice_id,
353 $next_config_id, $next_config_id);
354 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
357 $h_update_ar->finish();
358 $h_insert_dunning->finish();
360 $form->{DUNNING_PDFS_EMAIL} = [];
362 $form->{dunning_id} = $dunning_id;
364 $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
366 $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
367 $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
371 $self->send_email($myconfig, $form, $dunning_id, $dbh);
378 $main::lxdebug->enter_sub();
380 my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
384 dcfg.email_body, dcfg.email_subject, dcfg.email_attachment,
388 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
389 LEFT JOIN ar ON (d.trans_id = ar.id)
390 LEFT JOIN customer c ON (ar.customer_id = c.id)
391 WHERE (d.dunning_id = ?)
393 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
395 if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
396 $main::lxdebug->leave_sub();
400 my $template = SL::Template::create(type => 'PlainText', form => $form, myconfig => $myconfig);
401 my $mail = Mailer->new();
402 $mail->{bcc} = $form->get_bcc_defaults($myconfig, $form->{bcc});
403 $mail->{from} = $myconfig->{email};
404 $mail->{to} = $ref->{recipient};
405 $mail->{subject} = $template->parse_block($ref->{email_subject});
406 $mail->{message} = $template->parse_block($ref->{email_body});
408 $mail->{message} .= $form->create_email_signature();
410 $mail->{message} =~ s/\r\n/\n/g;
412 if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
413 $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
418 $main::lxdebug->leave_sub();
421 sub set_template_options {
422 $main::lxdebug->enter_sub();
424 my ($self, $myconfig, $form) = @_;
426 my $defaults = SL::DB::Default->get;
427 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
428 $form->{templates} = $defaults->templates;
429 $form->{language} = $form->get_template_language($myconfig);
430 $form->{printer_code} = $form->get_printer_code($myconfig);
432 if ($form->{language} ne "") {
433 $form->{language} = "_" . $form->{language};
436 if ($form->{printer_code} ne "") {
437 $form->{printer_code} = "_" . $form->{printer_code};
440 my $extension = 'html';
441 if ($form->{format} eq 'postscript') {
442 $form->{postscript} = 1;
445 } elsif ($form->{"format"} =~ /pdf/) {
447 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
449 } elsif ($form->{"format"} =~ /opendocument/) {
450 $form->{opendocument} = 1;
452 } elsif ($form->{"format"} =~ /excel/) {
458 # search for the template
460 push @template_files, "$form->{formname}_email$form->{language}$form->{printer_code}.$extension" if $form->{media} eq 'email';
461 push @template_files, "$form->{formname}$form->{language}$form->{printer_code}.$extension";
462 push @template_files, "$form->{formname}.$extension";
463 push @template_files, "default.$extension";
466 for my $filename (@template_files) {
467 if (-f ($defaults->templates . "/$filename")) {
468 $form->{IN} = $filename;
473 if (!defined $form->{IN}) {
474 $::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));
477 # prepare meta information for template introspection
478 $form->{template_meta} = {
479 formname => $form->{formname},
480 language => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id} || undef),
481 format => $form->{format},
482 media => $form->{media},
483 extension => $extension,
484 printer => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id} || undef),
485 today => DateTime->today,
488 $main::lxdebug->leave_sub();
493 $main::lxdebug->enter_sub();
495 my ($self, $myconfig, $form) = @_;
497 # connect to database
498 my $dbh = SL::DB->client->dbh;
503 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
505 if ($form->{customer_id}) {
506 $where .= qq| AND (a.customer_id = ?)|;
507 push(@values, $form->{customer_id});
509 } elsif ($form->{customer}) {
510 $where .= qq| AND (ct.name ILIKE ?)|;
511 push(@values, like($form->{customer}));
515 "ordnumber" => "a.ordnumber",
516 "invnumber" => "a.invnumber",
517 "notes" => "a.notes",
518 "country" => "ct.country",
520 foreach my $key (keys(%columns)) {
521 next unless ($form->{$key});
522 $where .= qq| AND $columns{$key} ILIKE ?|;
523 push(@values, like($form->{$key}));
526 if ($form->{dunning_level}) {
527 $where .= qq| AND nextcfg.id = ?|;
528 push(@values, conv_i($form->{dunning_level}));
531 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
532 if ($form->{minamount}) {
533 $where .= qq| AND ((a.amount - a.paid) > ?) |;
534 push(@values, trim($form->{minamount}));
540 WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
541 my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
543 if (!$form->{l_include_direct_debit}) {
544 $where .= qq| AND NOT COALESCE(a.direct_debit, FALSE) |;
549 a.id, a.invoice, a.ordnumber, a.transdate, a.invnumber, a.amount, a.language_id,
550 ct.name AS customername, a.customer_id, a.duedate,
551 a.amount - a.paid AS open_amount,
554 cfg.dunning_description, cfg.dunning_level,
556 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
559 a.duedate + cfg.terms - current_date AS nextlevel,
560 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
561 current_date + cfg.payment_terms AS next_duedate,
563 nextcfg.dunning_description AS next_dunning_description,
564 nextcfg.id AS next_dunning_config_id,
565 nextcfg.terms, nextcfg.active, nextcfg.email
569 LEFT JOIN customer ct ON (a.customer_id = ct.id)
570 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
571 LEFT JOIN dunning_config nextcfg ON
576 WHERE dunning_level >
577 COALESCE((SELECT dunning_level
579 WHERE id = a.dunning_config_id
580 ORDER BY dunning_level DESC
583 ORDER BY dunning_level ASC
586 LEFT JOIN dunning d ON (d.id = (
589 WHERE (d2.trans_id = a.id)
590 AND (d2.dunning_level = cfg.dunning_level)
593 WHERE (a.paid < a.amount)
594 AND (a.duedate < current_date)
598 ORDER BY a.id, transdate, duedate, name|;
599 my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
601 $form->{DUNNINGS} = [];
603 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
604 next if ($ref->{pastdue} < $ref->{terms});
606 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
607 push(@{ $form->{DUNNINGS} }, $ref);
612 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
613 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
615 $main::lxdebug->leave_sub();
620 $main::lxdebug->enter_sub();
622 my ($self, $myconfig, $form) = @_;
624 # connect to database
625 my $dbh = SL::DB->client->dbh;
627 my $where = qq| WHERE (da.trans_id = a.id)|;
631 if ($form->{customer_id}) {
632 $where .= qq| AND (a.customer_id = ?)|;
633 push(@values, $form->{customer_id});
635 } elsif ($form->{customer}) {
636 $where .= qq| AND (ct.name ILIKE ?)|;
637 push(@values, like($form->{customer}));
641 "ordnumber" => "a.ordnumber",
642 "invnumber" => "a.invnumber",
643 "notes" => "a.notes",
645 foreach my $key (keys(%columns)) {
646 next unless ($form->{$key});
647 $where .= qq| AND $columns{$key} ILIKE ?|;
648 push(@values, like($form->{$key}));
651 if ($form->{dunning_level}) {
652 $where .= qq| AND a.dunning_config_id = ?|;
653 push(@values, conv_i($form->{dunning_level}));
656 if ($form->{department_id}) {
657 $where .= qq| AND a.department_id = ?|;
658 push @values, conv_i($form->{department_id});
661 $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
662 if ($form->{minamount}) {
663 $where .= qq| AND ((a.amount - a.paid) > ?) |;
664 push(@values, $form->{minamount});
667 if (!$form->{showold}) {
668 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
671 if ($form->{transdatefrom}) {
672 $where .= qq| AND a.transdate >= ?|;
673 push(@values, $form->{transdatefrom});
675 if ($form->{transdateto}) {
676 $where .= qq| AND a.transdate <= ?|;
677 push(@values, $form->{transdateto});
679 if ($form->{dunningfrom}) {
680 $where .= qq| AND da.transdate >= ?|;
681 push(@values, $form->{dunningfrom});
683 if ($form->{dunningto}) {
684 $where .= qq| AND da.transdate >= ?|;
685 push(@values, $form->{dunningto});
688 if ($form->{salesman_id}) {
689 $where .= qq| AND a.salesman_id = ?|;
690 push(@values, conv_i($form->{salesman_id}));
694 'dunning_description' => [ qw(dn.dunning_description customername invnumber) ],
695 'customername' => [ qw(customername invnumber) ],
696 'invnumber' => [ qw(a.invnumber) ],
697 'transdate' => [ qw(a.transdate a.invnumber) ],
698 'duedate' => [ qw(a.duedate a.invnumber) ],
699 'dunning_date' => [ qw(dunning_date a.invnumber) ],
700 'dunning_duedate' => [ qw(dunning_duedate a.invnumber) ],
701 'salesman' => [ qw(salesman) ],
704 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
705 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername';
706 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
709 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount, a.language_id,
710 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
711 da.interest, dn.dunning_description, da.transdate AS dunning_date,
712 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id,
715 JOIN customer ct ON (a.customer_id = ct.id)
716 LEFT JOIN employee e2 ON (a.salesman_id = e2.id), dunning da
717 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
719 ORDER BY $sortorder|;
721 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
723 foreach my $ref (@{ $form->{DUNNINGS} }) {
724 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
727 $main::lxdebug->leave_sub();
732 $main::lxdebug->enter_sub();
734 my ($self, $myconfig, $form, $copies) = @_;
736 # Don't allow access outside of $spool.
737 map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
740 $copies = 1 unless $copies;
741 my $spool = $::lx_office_conf{paths}->{spool};
742 my $inputfiles = join " ", map { "$spool/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
743 my $dunning_id = $form->{dunning_id};
745 $dunning_id =~ s|[^\d]||g;
747 my $in = IO::File->new($::lx_office_conf{applications}->{ghostscript} . " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
748 $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
750 if ($form->{media} eq 'printer') {
751 $form->get_printer_code($myconfig);
753 if ($form->{printer_command}) {
754 $out = IO::File->new("| $form->{printer_command}");
757 $::locale->with_raw_io($out, sub { $out->print($_) while <$in> });
759 $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
762 my $dunning_filename = $form->get_formname_translation('dunning');
763 print qq|Content-Type: Application/PDF\n| .
764 qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|;
766 $::locale->with_raw_io(\*STDOUT, sub { print while <$in> });
771 map { unlink("$spool/$_") } @{ $form->{DUNNING_PDFS} };
773 $main::lxdebug->leave_sub();
777 $main::lxdebug->enter_sub();
779 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
781 # connect to database
782 my $dbh = $provided_dbh || SL::DB->client->dbh;
784 $dunning_id =~ s|[^\d]||g;
786 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
787 if ($form->{"language_id"}) {
788 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
789 AM->get_language_details($myconfig, $form, $form->{language_id});
791 $output_dateformat = $myconfig->{dateformat};
792 $output_numberformat = $myconfig->{numberformat};
793 $output_longdates = 1;
799 da.transdate AS dunning_date,
800 da.duedate AS dunning_duedate,
802 dcfg.template AS formname,
803 dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
805 ar.transdate, ar.duedate, ar.customer_id,
806 ar.invnumber, ar.ordnumber, ar.cp_id,
807 ar.amount, ar.netamount, ar.paid,
808 ar.employee_id, ar.salesman_id,
809 (SELECT cu.name FROM currencies cu WHERE cu.id = ar.currency_id) AS curr,
810 (SELECT description from department WHERE id = ar.department_id) AS department,
811 ar.amount - ar.paid AS open_amount,
812 ar.amount - ar.paid + da.fee + da.interest AS linetotal
815 LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
816 LEFT JOIN ar ON (ar.id = da.trans_id)
817 WHERE (da.dunning_id = ?)|;
819 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
821 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
823 $form->{TEMPLATE_ARRAYS} = {};
824 map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref}));
827 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
828 map { $form->{$_} = $ref->{$_} } keys %$ref;
829 map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
835 c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
836 c.country, c.department_1, c.department_2, c.email, c.customernumber,
837 c.greeting, c.contact, c.phone, c.fax, c.homepage,
838 c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban,
842 LEFT JOIN ar ON (d.trans_id = ar.id)
843 LEFT JOIN customer c ON (ar.customer_id = c.id)
844 LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
845 LEFT JOIN employee e ON (ar.salesman_id = e.id)
846 WHERE (d.dunning_id = ?)
848 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
849 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
853 cfg.interest_rate, cfg.template AS formname,
854 cfg.email_subject, cfg.email_body, cfg.email_attachment,
855 d.transdate AS dunning_date,
858 WHERE dunning_id = ?)
860 (SELECT SUM(interest)
862 WHERE dunning_id = ?)
864 (SELECT SUM(amount) - SUM(paid)
869 WHERE dunning_id = ?))
872 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
873 WHERE d.dunning_id = ?
875 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
876 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
878 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
879 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
880 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
881 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
882 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
884 $::form->format_dates($output_dateformat, $output_longdates,
885 qw(dn_dunning_date dn_dunning_duedate dn_transdate dn_duedate
886 dunning_date dunning_duedate transdate duedate)
888 $::form->reformat_numbers($output_numberformat, 2, qw(
889 dn_amount dn_netamount dn_paid dn_open_amount dn_fee dn_interest dn_linetotal
890 amount netamount paid open_amount fee interest linetotal
891 total_interest total_open_interest total_amount total_open_amount
893 $::form->reformat_numbers($output_numberformat, undef, qw(interest_rate));
895 $self->set_customer_cvars($myconfig, $form);
896 $self->set_template_options($myconfig, $form);
898 my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
899 my $spool = $::lx_office_conf{paths}->{spool};
900 $form->{OUT} = "${spool}/$filename";
901 $form->{keep_tmpfile} = 1;
903 delete $form->{tmpfile};
905 push @{ $form->{DUNNING_PDFS} }, $filename;
906 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
907 'name' => $form->get_formname_translation('dunning') . "_${dunning_id}.pdf" };
909 $form->get_employee_data('prefix' => 'employee', 'id' => $form->{employee_id});
910 $form->get_employee_data('prefix' => 'salesman', 'id' => $form->{salesman_id});
912 $form->parse_template($myconfig);
914 $main::lxdebug->leave_sub();
917 sub print_invoice_for_fees {
918 $main::lxdebug->enter_sub();
920 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
922 my $dbh = $provided_dbh || SL::DB->client->dbh;
924 my ($query, @values, $sth);
928 d.fee_interest_ar_id,
931 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
932 WHERE d.dunning_id = ?|;
933 my ($ar_id, $template) = selectrow_query($form, $dbh, $query, $dunning_id);
936 $main::lxdebug->leave_sub();
940 my $saved_form = save_form();
942 $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
943 my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
947 ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
948 ar.duedate, ar.notes, ar.notes AS invoicenotes, ar.customer_id,
950 c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country,
951 c.contact, c.customernumber, c.phone, c.fax, c.email,
952 c.taxnumber, c.greeting
955 LEFT JOIN customer c ON (ar.customer_id = c.id)
957 my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
958 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
960 $query = qq|SELECT * FROM employee WHERE login = ?|;
961 $ref = selectfirst_hashref_query($form, $dbh, $query, $::myconfig{login});
962 map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
964 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|;
965 $sth = prepare_execute_query($form, $dbh, $query, $ar_id);
967 my ($row, $fee, $interest) = (0, 0, 0);
969 while ($ref = $sth->fetchrow_hashref()) {
970 next if ($ref->{amount} < 0);
975 $fee = $ref->{amount};
977 $interest = $ref->{amount};
981 $form->{fee} = $form->round_amount($fee, 2);
982 $form->{interest} = $form->round_amount($interest, 2);
983 $form->{invamount} = $form->round_amount($fee + $interest, 2);
984 $form->{dunning_id} = $dunning_id;
985 $form->{formname} = "${template}_invoice";
987 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
989 $self->set_customer_cvars($myconfig, $form);
990 $self->set_template_options($myconfig, $form);
992 my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
994 my $spool = $::lx_office_conf{paths}->{spool};
995 $form->{OUT} = "$spool/$filename";
996 $form->{keep_tmpfile} = 1;
997 delete $form->{tmpfile};
999 map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
1001 $form->parse_template($myconfig);
1003 restore_form($saved_form);
1005 push @{ $form->{DUNNING_PDFS} }, $filename;
1006 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
1007 'name' => "dunning_invoice_${dunning_id}.pdf" };
1009 $main::lxdebug->leave_sub();
1012 sub set_customer_cvars {
1013 my ($self, $myconfig, $form) = @_;
1015 my $custom_variables = CVar->get_custom_variables(dbh => $form->get_standard_dbh,
1017 trans_id => $form->{customer_id});
1018 map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
1020 $form->{cp_greeting} = GenericTranslations->get(dbh => $form->get_standard_dbh,
1021 translation_type => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
1022 language_id => $form->{language_id},
1023 allow_fallback => 1);