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 #======================================================================
43 use SL::GenericTranslations;
51 use SL::Util qw(trim);
57 $main::lxdebug->enter_sub();
59 my ($self, $myconfig, $form) = @_;
62 my $dbh = SL::DB->client->dbh;
66 qq|FROM dunning_config | .
67 qq|ORDER BY dunning_level|;
68 $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
70 foreach my $ref (@{ $form->{DUNNING} }) {
71 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
72 $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
76 qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar, dunning_creator
78 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}, $form->{dunning_creator})
79 = selectrow_query($form, $dbh, $query);
81 $main::lxdebug->leave_sub();
85 my ($self, $myconfig, $form) = @_;
86 $main::lxdebug->enter_sub();
88 my $rc = SL::DB->client->with_transaction(\&_save_config, $self, $myconfig, $form);
90 $::lxdebug->leave_sub;
95 my ($self, $myconfig, $form) = @_;
97 my $dbh = SL::DB->client->dbh;
101 for my $i (1 .. $form->{rowcount}) {
102 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
103 $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
105 if (($form->{"dunning_level_$i"} ne "") &&
106 ($form->{"dunning_description_$i"} ne "")) {
107 @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
108 $form->{"email_subject_$i"}, $form->{"email_body_$i"},
109 $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
110 $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
111 $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}),
112 $form->{"create_invoices_for_fees_$i"} ? 't' : 'f');
113 if ($form->{"id_$i"}) {
115 qq|UPDATE dunning_config SET
116 dunning_level = ?, dunning_description = ?,
117 email_subject = ?, email_body = ?,
118 template = ?, fee = ?, interest_rate = ?,
119 active = ?, auto = ?, email = ?,
120 email_attachment = ?, payment_terms = ?, terms = ?,
121 create_invoices_for_fees = ?
123 push(@values, conv_i($form->{"id_$i"}));
126 qq|INSERT INTO dunning_config
127 (dunning_level, dunning_description, email_subject, email_body,
128 template, fee, interest_rate, active, auto, email,
129 email_attachment, payment_terms, terms, create_invoices_for_fees)
130 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
132 do_query($form, $dbh, $query, @values);
135 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
136 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
137 do_query($form, $dbh, $query, $form->{"id_$i"});
141 $query = qq|UPDATE defaults SET dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?,
142 dunning_creator = ?|;
143 @values = (conv_i($form->{AR_amount_fee}), conv_i($form->{AR_amount_interest}), conv_i($form->{AR}),
144 $form->{dunning_creator});
145 do_query($form, $dbh, $query, @values);
150 sub create_invoice_for_fees {
151 $main::lxdebug->enter_sub();
153 my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
155 my ($query, @values, $sth, $ref);
157 $query = qq|SELECT dcfg.create_invoices_for_fees
159 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
160 WHERE d.dunning_id = ?|;
161 my ($create_invoices_for_fees) = selectrow_query($form, $dbh, $query, $dunning_id);
163 if (!$create_invoices_for_fees) {
164 $main::lxdebug->leave_sub();
168 $query = qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar FROM defaults|;
169 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
175 SELECT MAX(d_fee.fee)
177 WHERE (d_fee.trans_id = d.trans_id)
178 AND (d_fee.dunning_id <> ?)
179 AND NOT (d_fee.fee_interest_ar_id ISNULL)
184 SELECT MAX(d_interest.interest)
185 FROM dunning d_interest
186 WHERE (d_interest.trans_id = d.trans_id)
187 AND (d_interest.dunning_id <> ?)
188 AND NOT (d_interest.fee_interest_ar_id ISNULL)
190 AS max_previous_interest
192 WHERE dunning_id = ?|;
193 @values = ($dunning_id, $dunning_id, $dunning_id);
194 $sth = prepare_execute_query($form, $dbh, $query, @values);
196 my ($fee_remaining, $interest_remaining) = (0, 0);
197 my ($fee_total, $interest_total) = (0, 0);
199 while (my $ref = $sth->fetchrow_hashref()) {
200 $fee_remaining += $form->round_amount($ref->{fee}, 2);
201 $fee_remaining -= $form->round_amount($ref->{max_previous_fee}, 2);
202 $fee_total += $form->round_amount($ref->{fee}, 2);
203 $interest_remaining += $form->round_amount($ref->{interest}, 2);
204 $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
205 $interest_total += $form->round_amount($ref->{interest}, 2);
210 my $amount = $fee_remaining + $interest_remaining;
213 $main::lxdebug->leave_sub();
217 my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
218 my $curr = $form->get_default_currency($myconfig);
219 my $trans_number = SL::TransNumber->new(type => 'invoice', dbh => $dbh);
222 qq|INSERT INTO ar (id, invnumber, transdate, gldate, customer_id,
223 taxincluded, amount, netamount, paid, duedate,
224 invoice, currency_id, taxzone_id, notes,
229 current_date, -- transdate
230 current_date, -- gldate
232 (SELECT ar.customer_id
234 LEFT JOIN ar ON (dn.trans_id = ar.id)
235 WHERE dn.dunning_id = ?
242 (SELECT duedate FROM dunning WHERE dunning_id = ? LIMIT 1),
244 (SELECT id FROM currencies WHERE name = ?), -- curr
246 (SELECT taxzone_id FROM customer WHERE id =
247 (SELECT ar.customer_id
249 LEFT JOIN ar ON (dn.trans_id = ar.id)
250 WHERE dn.dunning_id = ?
255 (SELECT id FROM employee WHERE login = ?)
257 @values = ($ar_id, # id
258 $trans_number->create_unique, # invnumber
259 $dunning_id, # customer_id
262 $dunning_id, # duedate
263 $curr, # default currency
264 $dunning_id, # taxzone_id
265 sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
266 $::myconfig{login}); # employee_id
267 do_query($form, $dbh, $query, @values);
270 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey, tax_id, chart_link)
271 VALUES (?, ?, ?, current_date, current_date, 0,
272 (SELECT id FROM tax WHERE (taxkey = 0) AND (rate = 0)),
273 (SELECT link FROM chart WHERE id = ?))|;
274 $sth = prepare_query($form, $dbh, $query);
276 @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining, conv_i($form->{AR_amount_fee}));
277 do_statement($form, $sth, $query, @values);
279 if ($interest_remaining) {
280 @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining, conv_i($form->{AR_amount_interest}));
281 do_statement($form, $sth, $query, @values);
284 @values = ($ar_id, conv_i($form->{AR}), -1 * $amount, conv_i($form->{AR}));
285 do_statement($form, $sth, $query, @values);
289 $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
290 do_query($form, $dbh, $query, $ar_id, $dunning_id);
292 $main::lxdebug->leave_sub();
297 my ($self, $myconfig, $form, $rows) = @_;
298 $main::lxdebug->enter_sub();
300 my $rc = SL::DB->client->with_transaction(\&_save_dunning, $self, $myconfig, $form, $rows);
303 die SL::DB->client->error
305 $::lxdebug->leave_sub;
312 my ($self, $myconfig, $form, $rows) = @_;
314 my $dbh = SL::DB->client->dbh;
316 my ($query, @values);
318 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
320 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
321 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
323 my $q_insert_dunning =
324 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
325 fee, interest, transdate, duedate)
327 (SELECT dunning_level FROM dunning_config WHERE id = ?),
331 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
332 (SELECT (amount - paid) * (current_date - duedate) FROM ar WHERE id = ?)
333 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
336 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
337 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
340 my ($next_dunning_config_id, $customer_id);
343 foreach my $row (@{ $rows }) {
344 push @invoice_ids, $row->{invoice_id};
345 $next_dunning_config_id = $row->{next_dunning_config_id};
346 $customer_id = $row->{customer_id};
348 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
349 do_statement($form, $h_update_ar, $q_update_ar, @values);
351 $send_email |= $row->{email};
353 my $next_config_id = conv_i($row->{next_dunning_config_id});
354 my $invoice_id = conv_i($row->{invoice_id});
356 @values = ($dunning_id, $next_config_id, $next_config_id,
357 $invoice_id, $next_config_id, $invoice_id,
358 $next_config_id, $next_config_id);
359 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
362 $h_update_ar->finish();
363 $h_insert_dunning->finish();
365 $form->{DUNNING_PDFS_EMAIL} = [];
367 $form->{dunning_id} = $dunning_id;
369 $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
371 $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
372 $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
376 $self->send_email($myconfig, $form, $dunning_id, $dbh);
383 $main::lxdebug->enter_sub();
385 my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
389 dcfg.email_body, dcfg.email_subject, dcfg.email_attachment,
390 COALESCE (NULLIF(c.invoice_mail, ''), c.email) AS recipient, c.name,
391 (SELECT login from employee where id = ar.employee_id) as invoice_employee_login
393 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
394 LEFT JOIN ar ON (d.trans_id = ar.id)
395 LEFT JOIN customer c ON (ar.customer_id = c.id)
396 WHERE (d.dunning_id = ?)
398 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
400 # without a recipient, we cannot send a mail
401 if (!$ref || !$ref->{recipient}) {
402 $main::lxdebug->leave_sub();
403 $form->error($main::locale->text("No email recipient for customer #1 defined.", $ref->{name}));
406 # without a sender we cannot send a mail
407 # two cases: check mail from 1. current user OR 2. employee who created the invoice
409 if ($::instance_conf->get_dunning_creator eq 'current_employee') {
410 $from = $myconfig->{email};
411 $form->error($main::locale->text('No email for current user #1 defined.', $myconfig->{name})) unless $from;
414 $from = SL::DB::Manager::AuthUser->find_by(login => $ref->{invoice_employee_login})->get_config_value("email");
416 } or do { $form->error($main::locale->text('No email for user with login #1 defined.', $ref->{invoice_employee_login})) };
419 my $template = SL::Template::create(type => 'PlainText', form => $form, myconfig => $myconfig);
420 my $mail = Mailer->new();
421 $mail->{bcc} = $form->get_bcc_defaults($myconfig, $form->{bcc});
422 $mail->{from} = $from;
423 $mail->{to} = $ref->{recipient};
424 $mail->{subject} = $template->parse_block($ref->{email_subject});
425 $mail->{message} = $template->parse_block($ref->{email_body});
427 $mail->{message} .= $form->create_email_signature();
429 $mail->{message} =~ s/\r\n/\n/g;
431 if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
432 $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
437 $main::lxdebug->leave_sub();
440 sub set_template_options {
441 $main::lxdebug->enter_sub();
443 my ($self, $myconfig, $form) = @_;
445 my $defaults = SL::DB::Default->get;
446 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
447 $form->{templates} = $defaults->templates;
448 $form->{language} = $form->get_template_language($myconfig);
449 $form->{printer_code} = $form->get_printer_code($myconfig);
451 if ($form->{language} ne "") {
452 $form->{language} = "_" . $form->{language};
455 if ($form->{printer_code} ne "") {
456 $form->{printer_code} = "_" . $form->{printer_code};
459 my $extension = 'html';
460 if ($form->{format} eq 'postscript') {
461 $form->{postscript} = 1;
464 } elsif ($form->{"format"} =~ /pdf/) {
466 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
468 } elsif ($form->{"format"} =~ /opendocument/) {
469 $form->{opendocument} = 1;
471 } elsif ($form->{"format"} =~ /excel/) {
477 # search for the template
479 push @template_files, "$form->{formname}_email$form->{language}$form->{printer_code}.$extension" if $form->{media} eq 'email';
480 push @template_files, "$form->{formname}$form->{language}$form->{printer_code}.$extension";
481 push @template_files, "$form->{formname}.$extension";
482 push @template_files, "default.$extension";
485 for my $filename (@template_files) {
486 if (-f ($defaults->templates . "/$filename")) {
487 $form->{IN} = $filename;
492 if (!defined $form->{IN}) {
493 $::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));
496 # prepare meta information for template introspection
497 $form->{template_meta} = {
498 formname => $form->{formname},
499 language => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id} || undef),
500 format => $form->{format},
501 media => $form->{media},
502 extension => $extension,
503 printer => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id} || undef),
504 today => DateTime->today,
507 $main::lxdebug->leave_sub();
512 $main::lxdebug->enter_sub();
514 my ($self, $myconfig, $form) = @_;
516 # connect to database
517 my $dbh = SL::DB->client->dbh;
522 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
524 if ($form->{customer_id}) {
525 $where .= qq| AND (a.customer_id = ?)|;
526 push(@values, $form->{customer_id});
528 } elsif ($form->{customer}) {
529 $where .= qq| AND (ct.name ILIKE ?)|;
530 push(@values, like($form->{customer}));
534 "ordnumber" => "a.ordnumber",
535 "invnumber" => "a.invnumber",
536 "notes" => "a.notes",
537 "country" => "ct.country",
539 foreach my $key (keys(%columns)) {
540 next unless ($form->{$key});
541 $where .= qq| AND $columns{$key} ILIKE ?|;
542 push(@values, like($form->{$key}));
545 if ($form->{dunning_level}) {
546 $where .= qq| AND nextcfg.id = ?|;
547 push(@values, conv_i($form->{dunning_level}));
550 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
551 if ($form->{minamount}) {
552 $where .= qq| AND ((a.amount - a.paid) > ?) |;
553 push(@values, trim($form->{minamount}));
559 WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
560 my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
562 if (!$form->{l_include_direct_debit}) {
563 $where .= qq| AND NOT COALESCE(a.direct_debit, FALSE) |;
568 a.id, a.invoice, a.ordnumber, a.transdate, a.invnumber, a.amount, a.language_id,
569 ct.name AS customername, a.customer_id, a.duedate,
570 a.amount - a.paid AS open_amount,
573 cfg.dunning_description, cfg.dunning_level,
575 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
578 a.duedate + cfg.terms - current_date AS nextlevel,
579 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
580 current_date + cfg.payment_terms AS next_duedate,
582 nextcfg.dunning_description AS next_dunning_description,
583 nextcfg.id AS next_dunning_config_id,
584 nextcfg.terms, nextcfg.active, nextcfg.email
588 LEFT JOIN customer ct ON (a.customer_id = ct.id)
589 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
590 LEFT JOIN dunning_config nextcfg ON
595 WHERE dunning_level >
596 COALESCE((SELECT dunning_level
598 WHERE id = a.dunning_config_id
599 ORDER BY dunning_level DESC
602 ORDER BY dunning_level ASC
605 LEFT JOIN dunning d ON (d.id = (
608 WHERE (d2.trans_id = a.id)
609 AND (d2.dunning_level = cfg.dunning_level)
612 WHERE (a.paid < a.amount)
613 AND (a.duedate < current_date)
617 ORDER BY a.id, transdate, duedate, name|;
618 my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
620 $form->{DUNNINGS} = [];
622 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
623 next if ($ref->{pastdue} < $ref->{terms});
625 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
626 push(@{ $form->{DUNNINGS} }, $ref);
631 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
632 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
634 $main::lxdebug->leave_sub();
639 $main::lxdebug->enter_sub();
641 my ($self, $myconfig, $form) = @_;
643 # connect to database
644 my $dbh = SL::DB->client->dbh;
646 my $where = qq| WHERE (da.trans_id = a.id)|;
650 if ($form->{customer_id}) {
651 $where .= qq| AND (a.customer_id = ?)|;
652 push(@values, $form->{customer_id});
654 } elsif ($form->{customer}) {
655 $where .= qq| AND (ct.name ILIKE ?)|;
656 push(@values, like($form->{customer}));
660 "ordnumber" => "a.ordnumber",
661 "invnumber" => "a.invnumber",
662 "notes" => "a.notes",
664 foreach my $key (keys(%columns)) {
665 next unless ($form->{$key});
666 $where .= qq| AND $columns{$key} ILIKE ?|;
667 push(@values, like($form->{$key}));
670 if ($form->{dunning_level}) {
671 $where .= qq| AND a.dunning_config_id = ?|;
672 push(@values, conv_i($form->{dunning_level}));
675 if ($form->{department_id}) {
676 $where .= qq| AND a.department_id = ?|;
677 push @values, conv_i($form->{department_id});
680 $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
681 if ($form->{minamount}) {
682 $where .= qq| AND ((a.amount - a.paid) > ?) |;
683 push(@values, $form->{minamount});
686 if (!$form->{showold}) {
687 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
690 if ($form->{transdatefrom}) {
691 $where .= qq| AND a.transdate >= ?|;
692 push(@values, $form->{transdatefrom});
694 if ($form->{transdateto}) {
695 $where .= qq| AND a.transdate <= ?|;
696 push(@values, $form->{transdateto});
698 if ($form->{dunningfrom}) {
699 $where .= qq| AND da.transdate >= ?|;
700 push(@values, $form->{dunningfrom});
702 if ($form->{dunningto}) {
703 $where .= qq| AND da.transdate >= ?|;
704 push(@values, $form->{dunningto});
707 if ($form->{salesman_id}) {
708 $where .= qq| AND a.salesman_id = ?|;
709 push(@values, conv_i($form->{salesman_id}));
713 'dunning_description' => [ qw(dn.dunning_description customername invnumber) ],
714 'customername' => [ qw(customername invnumber) ],
715 'invnumber' => [ qw(a.invnumber) ],
716 'transdate' => [ qw(a.transdate a.invnumber) ],
717 'duedate' => [ qw(a.duedate a.invnumber) ],
718 'dunning_date' => [ qw(dunning_date a.invnumber) ],
719 'dunning_duedate' => [ qw(dunning_duedate a.invnumber) ],
720 'salesman' => [ qw(salesman) ],
723 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
724 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername';
725 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
728 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount, a.language_id,
729 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
730 da.interest, dn.dunning_description, da.transdate AS dunning_date,
731 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id,
734 JOIN customer ct ON (a.customer_id = ct.id)
735 LEFT JOIN employee e2 ON (a.salesman_id = e2.id), dunning da
736 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
738 ORDER BY $sortorder|;
740 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
742 foreach my $ref (@{ $form->{DUNNINGS} }) {
743 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
746 $main::lxdebug->leave_sub();
751 $main::lxdebug->enter_sub();
753 my ($self, $myconfig, $form, $copies) = @_;
755 # Don't allow access outside of $spool.
756 map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
759 $copies = 1 unless $copies;
760 my $spool = $::lx_office_conf{paths}->{spool};
761 my $inputfiles = join " ", map { "$spool/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
762 my $dunning_id = $form->{dunning_id};
764 $dunning_id =~ s|[^\d]||g;
766 my $in = IO::File->new($::lx_office_conf{applications}->{ghostscript} . " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
767 $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
769 if ($form->{media} eq 'printer') {
770 $form->get_printer_code($myconfig);
772 if ($form->{printer_command}) {
773 $out = IO::File->new("| $form->{printer_command}");
776 $::locale->with_raw_io($out, sub { $out->print($_) while <$in> });
778 $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
781 my $dunning_filename = $form->get_formname_translation('dunning');
782 print qq|Content-Type: Application/PDF\n| .
783 qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|;
785 $::locale->with_raw_io(\*STDOUT, sub { print while <$in> });
790 map { unlink("$spool/$_") } @{ $form->{DUNNING_PDFS} };
792 $main::lxdebug->leave_sub();
796 $main::lxdebug->enter_sub();
798 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
800 # connect to database
801 my $dbh = $provided_dbh || SL::DB->client->dbh;
803 $dunning_id =~ s|[^\d]||g;
805 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
806 if ($form->{"language_id"}) {
807 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
808 AM->get_language_details($myconfig, $form, $form->{language_id});
810 $output_dateformat = $myconfig->{dateformat};
811 $output_numberformat = $myconfig->{numberformat};
812 $output_longdates = 1;
818 da.transdate AS dunning_date,
819 da.duedate AS dunning_duedate,
821 dcfg.template AS formname,
822 dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
824 ar.transdate, ar.duedate, ar.customer_id,
825 ar.invnumber, ar.ordnumber, ar.cp_id,
826 ar.amount, ar.netamount, ar.paid,
827 ar.employee_id, ar.salesman_id,
828 (SELECT cu.name FROM currencies cu WHERE cu.id = ar.currency_id) AS curr,
829 (SELECT description from department WHERE id = ar.department_id) AS department,
830 ar.amount - ar.paid AS open_amount,
831 ar.amount - ar.paid + da.fee + da.interest AS linetotal
834 LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
835 LEFT JOIN ar ON (ar.id = da.trans_id)
836 WHERE (da.dunning_id = ?)|;
838 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
840 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
842 $form->{TEMPLATE_ARRAYS} = {};
843 map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref}));
846 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
847 map { $form->{$_} = $ref->{$_} } keys %$ref;
848 map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
854 c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
855 c.country, c.department_1, c.department_2, c.email, c.customernumber,
856 c.greeting, c.contact, c.phone, c.fax, c.homepage,
857 c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban,
862 LEFT JOIN ar ON (d.trans_id = ar.id)
863 LEFT JOIN customer c ON (ar.customer_id = c.id)
864 LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
865 LEFT JOIN employee e ON (ar.salesman_id = e.id)
866 WHERE (d.dunning_id = ?)
868 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
869 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
873 cfg.interest_rate, cfg.template AS formname, cfg.dunning_level,
874 cfg.email_subject, cfg.email_body, cfg.email_attachment,
875 d.transdate AS dunning_date,
878 WHERE dunning_id = ?)
880 (SELECT SUM(interest)
882 WHERE dunning_id = ?)
884 (SELECT SUM(amount) - SUM(paid)
889 WHERE dunning_id = ?))
892 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
893 WHERE d.dunning_id = ?
895 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
896 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
898 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
899 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
900 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
901 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
902 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
904 $::form->format_dates($output_dateformat, $output_longdates,
905 qw(dn_dunning_date dn_dunning_duedate dn_transdate dn_duedate
906 dunning_date dunning_duedate transdate duedate)
908 $::form->reformat_numbers($output_numberformat, 2, qw(
909 dn_amount dn_netamount dn_paid dn_open_amount dn_fee dn_interest dn_linetotal
910 amount netamount paid open_amount fee interest linetotal
911 total_interest total_open_interest total_amount total_open_amount
913 $::form->reformat_numbers($output_numberformat, undef, qw(interest_rate));
915 $self->set_customer_cvars($myconfig, $form);
916 $self->set_template_options($myconfig, $form);
918 my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
919 my $spool = $::lx_office_conf{paths}->{spool};
920 $form->{OUT} = "${spool}/$filename";
921 $form->{keep_tmpfile} = 1;
923 delete $form->{tmpfile};
925 push @{ $form->{DUNNING_PDFS} }, $filename;
926 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'path' => "${spool}/$filename",
927 'name' => $form->get_formname_translation('dunning') . "_${dunning_id}.pdf" };
929 my $employee_id = ($::instance_conf->get_dunning_creator eq 'invoice_employee') ?
930 $form->{employee_id} :
931 SL::DB::Manager::Employee->current->id;
933 $form->get_employee_data('prefix' => 'employee', 'id' => $employee_id);
934 $form->get_employee_data('prefix' => 'salesman', 'id' => $form->{salesman_id});
936 $form->{attachment_type} = "dunning";
937 if ( $form->{dunning_level} ) {
938 $form->{attachment_type} .= $form->{dunning_level} if $form->{dunning_level} < 4;
940 $form->{attachment_filename} = $form->get_formname_translation($form->{attachment_type}) . "_${dunning_id}.pdf";
941 $form->{attachment_id} = $form->{invoice_id};
942 $form->parse_template($myconfig);
944 $main::lxdebug->leave_sub();
947 sub print_invoice_for_fees {
948 $main::lxdebug->enter_sub();
950 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
952 my $dbh = $provided_dbh || SL::DB->client->dbh;
954 my ($query, @values, $sth);
958 d.fee_interest_ar_id,
959 d.trans_id AS invoice_id,
963 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
964 WHERE d.dunning_id = ?|;
965 my ($ar_id, $invoice_id, $template, $dunning_level) = selectrow_query($form, $dbh, $query, $dunning_id);
968 $main::lxdebug->leave_sub();
972 my $saved_form = save_form();
974 $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
975 my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
979 ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
980 ar.duedate, ar.notes, ar.notes AS invoicenotes, ar.customer_id,
982 c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country,
983 c.contact, c.customernumber, c.phone, c.fax, c.email,
984 c.taxnumber, c.greeting
987 LEFT JOIN customer c ON (ar.customer_id = c.id)
989 my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
990 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
992 $query = qq|SELECT * FROM employee WHERE login = ?|;
993 $ref = selectfirst_hashref_query($form, $dbh, $query, $::myconfig{login});
994 map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
996 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|;
997 $sth = prepare_execute_query($form, $dbh, $query, $ar_id);
999 my ($row, $fee, $interest) = (0, 0, 0);
1001 while ($ref = $sth->fetchrow_hashref()) {
1002 next if ($ref->{amount} < 0);
1007 $fee = $ref->{amount};
1009 $interest = $ref->{amount};
1013 $form->{fee} = $form->round_amount($fee, 2);
1014 $form->{interest} = $form->round_amount($interest, 2);
1015 $form->{invamount} = $form->round_amount($fee + $interest, 2);
1016 $form->{dunning_id} = $dunning_id;
1017 $form->{formname} = "${template}_invoice";
1019 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
1021 $self->set_customer_cvars($myconfig, $form);
1022 $self->set_template_options($myconfig, $form);
1024 my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
1026 my $spool = $::lx_office_conf{paths}->{spool};
1027 $form->{OUT} = "$spool/$filename";
1028 $form->{keep_tmpfile} = 1;
1029 delete $form->{tmpfile};
1031 map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
1033 $form->{attachment_filename} = $form->get_formname_translation('dunning_invoice') . "_${dunning_id}.pdf";
1034 $form->{attachment_type} = "dunning";
1035 $form->{attachment_id} = $form->{invoice_id};
1036 $form->parse_template($myconfig);
1038 restore_form($saved_form);
1040 push @{ $form->{DUNNING_PDFS} }, $filename;
1041 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
1042 'name' => "dunning_invoice_${dunning_id}.pdf" };
1044 $main::lxdebug->leave_sub();
1047 sub set_customer_cvars {
1048 my ($self, $myconfig, $form) = @_;
1050 my $custom_variables = CVar->get_custom_variables(dbh => $form->get_standard_dbh,
1052 trans_id => $form->{customer_id});
1053 map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
1055 $form->{cp_greeting} = GenericTranslations->get(dbh => $form->get_standard_dbh,
1056 translation_type => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
1057 language_id => $form->{language_id},
1058 allow_fallback => 1);
1059 if ($form->{cp_id}) {
1060 $custom_variables = CVar->get_custom_variables(dbh => $form->get_standard_dbh,
1061 module => 'Contacts',
1062 trans_id => $form->{cp_id});
1063 $form->{"cp_cvar_$_->{name}"} = $_->{value} for @{ $custom_variables };