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 #======================================================================
42 use SL::GenericTranslations;
50 use SL::Util qw(trim);
56 $main::lxdebug->enter_sub();
58 my ($self, $myconfig, $form) = @_;
61 my $dbh = SL::DB->client->dbh;
65 qq|FROM dunning_config | .
66 qq|ORDER BY dunning_level|;
67 $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
69 foreach my $ref (@{ $form->{DUNNING} }) {
70 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
71 $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
75 qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar, dunning_creator
77 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}, $form->{dunning_creator})
78 = selectrow_query($form, $dbh, $query);
80 $main::lxdebug->leave_sub();
84 my ($self, $myconfig, $form) = @_;
85 $main::lxdebug->enter_sub();
87 my $rc = SL::DB->client->with_transaction(\&_save_config, $self, $myconfig, $form);
89 $::lxdebug->leave_sub;
94 my ($self, $myconfig, $form) = @_;
96 my $dbh = SL::DB->client->dbh;
100 for my $i (1 .. $form->{rowcount}) {
101 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
102 $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
104 if (($form->{"dunning_level_$i"} ne "") &&
105 ($form->{"dunning_description_$i"} ne "")) {
106 @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
107 $form->{"email_subject_$i"}, $form->{"email_body_$i"},
108 $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
109 $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
110 $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}),
111 $form->{"create_invoices_for_fees_$i"} ? 't' : 'f');
112 if ($form->{"id_$i"}) {
114 qq|UPDATE dunning_config SET
115 dunning_level = ?, dunning_description = ?,
116 email_subject = ?, email_body = ?,
117 template = ?, fee = ?, interest_rate = ?,
118 active = ?, auto = ?, email = ?,
119 email_attachment = ?, payment_terms = ?, terms = ?,
120 create_invoices_for_fees = ?
122 push(@values, conv_i($form->{"id_$i"}));
125 qq|INSERT INTO dunning_config
126 (dunning_level, dunning_description, email_subject, email_body,
127 template, fee, interest_rate, active, auto, email,
128 email_attachment, payment_terms, terms, create_invoices_for_fees)
129 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
131 do_query($form, $dbh, $query, @values);
134 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
135 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
136 do_query($form, $dbh, $query, $form->{"id_$i"});
140 $query = qq|UPDATE defaults SET dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?,
141 dunning_creator = ?|;
142 @values = (conv_i($form->{AR_amount_fee}), conv_i($form->{AR_amount_interest}), conv_i($form->{AR}),
143 $form->{dunning_creator});
144 do_query($form, $dbh, $query, @values);
149 sub create_invoice_for_fees {
150 $main::lxdebug->enter_sub();
152 my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
154 my ($query, @values, $sth, $ref);
156 $query = qq|SELECT dcfg.create_invoices_for_fees
158 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
159 WHERE d.dunning_id = ?|;
160 my ($create_invoices_for_fees) = selectrow_query($form, $dbh, $query, $dunning_id);
162 if (!$create_invoices_for_fees) {
163 $main::lxdebug->leave_sub();
167 $query = qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar FROM defaults|;
168 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
174 SELECT MAX(d_fee.fee)
176 WHERE (d_fee.trans_id = d.trans_id)
177 AND (d_fee.dunning_id <> ?)
178 AND NOT (d_fee.fee_interest_ar_id ISNULL)
183 SELECT MAX(d_interest.interest)
184 FROM dunning d_interest
185 WHERE (d_interest.trans_id = d.trans_id)
186 AND (d_interest.dunning_id <> ?)
187 AND NOT (d_interest.fee_interest_ar_id ISNULL)
189 AS max_previous_interest
191 WHERE dunning_id = ?|;
192 @values = ($dunning_id, $dunning_id, $dunning_id);
193 $sth = prepare_execute_query($form, $dbh, $query, @values);
195 my ($fee_remaining, $interest_remaining) = (0, 0);
196 my ($fee_total, $interest_total) = (0, 0);
198 while (my $ref = $sth->fetchrow_hashref()) {
199 $fee_remaining += $form->round_amount($ref->{fee}, 2);
200 $fee_remaining -= $form->round_amount($ref->{max_previous_fee}, 2);
201 $fee_total += $form->round_amount($ref->{fee}, 2);
202 $interest_remaining += $form->round_amount($ref->{interest}, 2);
203 $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
204 $interest_total += $form->round_amount($ref->{interest}, 2);
209 my $amount = $fee_remaining + $interest_remaining;
212 $main::lxdebug->leave_sub();
216 my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
217 my $curr = $form->get_default_currency($myconfig);
218 my $trans_number = SL::TransNumber->new(type => 'invoice', dbh => $dbh);
221 qq|INSERT INTO ar (id, invnumber, transdate, gldate, customer_id,
222 taxincluded, amount, netamount, paid, duedate,
223 invoice, currency_id, taxzone_id, notes,
228 current_date, -- transdate
229 current_date, -- gldate
231 (SELECT ar.customer_id
233 LEFT JOIN ar ON (dn.trans_id = ar.id)
234 WHERE dn.dunning_id = ?
241 (SELECT duedate FROM dunning WHERE dunning_id = ? LIMIT 1),
243 (SELECT id FROM currencies WHERE name = ?), -- curr
245 (SELECT taxzone_id FROM customer WHERE id =
246 (SELECT ar.customer_id
248 LEFT JOIN ar ON (dn.trans_id = ar.id)
249 WHERE dn.dunning_id = ?
254 (SELECT id FROM employee WHERE login = ?)
256 @values = ($ar_id, # id
257 $trans_number->create_unique, # invnumber
258 $dunning_id, # customer_id
261 $dunning_id, # duedate
262 $curr, # default currency
263 $dunning_id, # taxzone_id
264 sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
265 $::myconfig{login}); # employee_id
266 do_query($form, $dbh, $query, @values);
269 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey, tax_id, chart_link)
270 VALUES (?, ?, ?, current_date, current_date, 0,
271 (SELECT id FROM tax WHERE (taxkey = 0) AND (rate = 0)),
272 (SELECT link FROM chart WHERE id = ?))|;
273 $sth = prepare_query($form, $dbh, $query);
275 @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining, conv_i($form->{AR_amount_fee}));
276 do_statement($form, $sth, $query, @values);
278 if ($interest_remaining) {
279 @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining, conv_i($form->{AR_amount_interest}));
280 do_statement($form, $sth, $query, @values);
283 @values = ($ar_id, conv_i($form->{AR}), -1 * $amount, conv_i($form->{AR}));
284 do_statement($form, $sth, $query, @values);
288 $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
289 do_query($form, $dbh, $query, $ar_id, $dunning_id);
291 $main::lxdebug->leave_sub();
296 my ($self, $myconfig, $form, $rows) = @_;
297 $main::lxdebug->enter_sub();
299 my $rc = SL::DB->client->with_transaction(\&_save_dunning, $self, $myconfig, $form, $rows);
302 die SL::DB->client->error
304 $::lxdebug->leave_sub;
311 my ($self, $myconfig, $form, $rows) = @_;
313 my $dbh = SL::DB->client->dbh;
315 my ($query, @values);
317 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
319 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
320 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
322 my $q_insert_dunning =
323 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
324 fee, interest, transdate, duedate)
326 (SELECT dunning_level FROM dunning_config WHERE id = ?),
330 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
331 (SELECT (amount - paid) * (current_date - duedate) FROM ar WHERE id = ?)
332 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
335 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
336 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
339 my ($next_dunning_config_id, $customer_id);
342 foreach my $row (@{ $rows }) {
343 push @invoice_ids, $row->{invoice_id};
344 $next_dunning_config_id = $row->{next_dunning_config_id};
345 $customer_id = $row->{customer_id};
347 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
348 do_statement($form, $h_update_ar, $q_update_ar, @values);
350 $send_email |= $row->{email};
352 my $next_config_id = conv_i($row->{next_dunning_config_id});
353 my $invoice_id = conv_i($row->{invoice_id});
355 @values = ($dunning_id, $next_config_id, $next_config_id,
356 $invoice_id, $next_config_id, $invoice_id,
357 $next_config_id, $next_config_id);
358 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
361 $h_update_ar->finish();
362 $h_insert_dunning->finish();
364 $form->{DUNNING_PDFS_EMAIL} = [];
366 $form->{dunning_id} = $dunning_id;
368 $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
370 $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
371 $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
375 $self->send_email($myconfig, $form, $dunning_id, $dbh);
382 $main::lxdebug->enter_sub();
384 my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
388 dcfg.email_body, dcfg.email_subject, dcfg.email_attachment,
392 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
393 LEFT JOIN ar ON (d.trans_id = ar.id)
394 LEFT JOIN customer c ON (ar.customer_id = c.id)
395 WHERE (d.dunning_id = ?)
397 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
399 if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
400 $main::lxdebug->leave_sub();
404 my $template = SL::Template::create(type => 'PlainText', form => $form, myconfig => $myconfig);
405 my $mail = Mailer->new();
406 $mail->{bcc} = $form->get_bcc_defaults($myconfig, $form->{bcc});
407 $mail->{from} = $myconfig->{email};
408 $mail->{to} = $ref->{recipient};
409 $mail->{subject} = $template->parse_block($ref->{email_subject});
410 $mail->{message} = $template->parse_block($ref->{email_body});
412 $mail->{message} .= $form->create_email_signature();
414 $mail->{message} =~ s/\r\n/\n/g;
416 if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
417 $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
422 $main::lxdebug->leave_sub();
425 sub set_template_options {
426 $main::lxdebug->enter_sub();
428 my ($self, $myconfig, $form) = @_;
430 my $defaults = SL::DB::Default->get;
431 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
432 $form->{templates} = $defaults->templates;
433 $form->{language} = $form->get_template_language($myconfig);
434 $form->{printer_code} = $form->get_printer_code($myconfig);
436 if ($form->{language} ne "") {
437 $form->{language} = "_" . $form->{language};
440 if ($form->{printer_code} ne "") {
441 $form->{printer_code} = "_" . $form->{printer_code};
444 my $extension = 'html';
445 if ($form->{format} eq 'postscript') {
446 $form->{postscript} = 1;
449 } elsif ($form->{"format"} =~ /pdf/) {
451 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
453 } elsif ($form->{"format"} =~ /opendocument/) {
454 $form->{opendocument} = 1;
456 } elsif ($form->{"format"} =~ /excel/) {
462 # search for the template
464 push @template_files, "$form->{formname}_email$form->{language}$form->{printer_code}.$extension" if $form->{media} eq 'email';
465 push @template_files, "$form->{formname}$form->{language}$form->{printer_code}.$extension";
466 push @template_files, "$form->{formname}.$extension";
467 push @template_files, "default.$extension";
470 for my $filename (@template_files) {
471 if (-f ($defaults->templates . "/$filename")) {
472 $form->{IN} = $filename;
477 if (!defined $form->{IN}) {
478 $::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));
481 # prepare meta information for template introspection
482 $form->{template_meta} = {
483 formname => $form->{formname},
484 language => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id} || undef),
485 format => $form->{format},
486 media => $form->{media},
487 extension => $extension,
488 printer => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id} || undef),
489 today => DateTime->today,
492 $main::lxdebug->leave_sub();
497 $main::lxdebug->enter_sub();
499 my ($self, $myconfig, $form) = @_;
501 # connect to database
502 my $dbh = SL::DB->client->dbh;
507 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
509 if ($form->{customer_id}) {
510 $where .= qq| AND (a.customer_id = ?)|;
511 push(@values, $form->{customer_id});
513 } elsif ($form->{customer}) {
514 $where .= qq| AND (ct.name ILIKE ?)|;
515 push(@values, like($form->{customer}));
519 "ordnumber" => "a.ordnumber",
520 "invnumber" => "a.invnumber",
521 "notes" => "a.notes",
522 "country" => "ct.country",
524 foreach my $key (keys(%columns)) {
525 next unless ($form->{$key});
526 $where .= qq| AND $columns{$key} ILIKE ?|;
527 push(@values, like($form->{$key}));
530 if ($form->{dunning_level}) {
531 $where .= qq| AND nextcfg.id = ?|;
532 push(@values, conv_i($form->{dunning_level}));
535 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
536 if ($form->{minamount}) {
537 $where .= qq| AND ((a.amount - a.paid) > ?) |;
538 push(@values, trim($form->{minamount}));
544 WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
545 my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
547 if (!$form->{l_include_direct_debit}) {
548 $where .= qq| AND NOT COALESCE(a.direct_debit, FALSE) |;
553 a.id, a.invoice, a.ordnumber, a.transdate, a.invnumber, a.amount, a.language_id,
554 ct.name AS customername, a.customer_id, a.duedate,
555 a.amount - a.paid AS open_amount,
558 cfg.dunning_description, cfg.dunning_level,
560 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
563 a.duedate + cfg.terms - current_date AS nextlevel,
564 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
565 current_date + cfg.payment_terms AS next_duedate,
567 nextcfg.dunning_description AS next_dunning_description,
568 nextcfg.id AS next_dunning_config_id,
569 nextcfg.terms, nextcfg.active, nextcfg.email
573 LEFT JOIN customer ct ON (a.customer_id = ct.id)
574 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
575 LEFT JOIN dunning_config nextcfg ON
580 WHERE dunning_level >
581 COALESCE((SELECT dunning_level
583 WHERE id = a.dunning_config_id
584 ORDER BY dunning_level DESC
587 ORDER BY dunning_level ASC
590 LEFT JOIN dunning d ON (d.id = (
593 WHERE (d2.trans_id = a.id)
594 AND (d2.dunning_level = cfg.dunning_level)
597 WHERE (a.paid < a.amount)
598 AND (a.duedate < current_date)
602 ORDER BY a.id, transdate, duedate, name|;
603 my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
605 $form->{DUNNINGS} = [];
607 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
608 next if ($ref->{pastdue} < $ref->{terms});
610 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
611 push(@{ $form->{DUNNINGS} }, $ref);
616 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
617 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
619 $main::lxdebug->leave_sub();
624 $main::lxdebug->enter_sub();
626 my ($self, $myconfig, $form) = @_;
628 # connect to database
629 my $dbh = SL::DB->client->dbh;
631 my $where = qq| WHERE (da.trans_id = a.id)|;
635 if ($form->{customer_id}) {
636 $where .= qq| AND (a.customer_id = ?)|;
637 push(@values, $form->{customer_id});
639 } elsif ($form->{customer}) {
640 $where .= qq| AND (ct.name ILIKE ?)|;
641 push(@values, like($form->{customer}));
645 "ordnumber" => "a.ordnumber",
646 "invnumber" => "a.invnumber",
647 "notes" => "a.notes",
649 foreach my $key (keys(%columns)) {
650 next unless ($form->{$key});
651 $where .= qq| AND $columns{$key} ILIKE ?|;
652 push(@values, like($form->{$key}));
655 if ($form->{dunning_level}) {
656 $where .= qq| AND a.dunning_config_id = ?|;
657 push(@values, conv_i($form->{dunning_level}));
660 if ($form->{department_id}) {
661 $where .= qq| AND a.department_id = ?|;
662 push @values, conv_i($form->{department_id});
665 $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
666 if ($form->{minamount}) {
667 $where .= qq| AND ((a.amount - a.paid) > ?) |;
668 push(@values, $form->{minamount});
671 if (!$form->{showold}) {
672 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
675 if ($form->{transdatefrom}) {
676 $where .= qq| AND a.transdate >= ?|;
677 push(@values, $form->{transdatefrom});
679 if ($form->{transdateto}) {
680 $where .= qq| AND a.transdate <= ?|;
681 push(@values, $form->{transdateto});
683 if ($form->{dunningfrom}) {
684 $where .= qq| AND da.transdate >= ?|;
685 push(@values, $form->{dunningfrom});
687 if ($form->{dunningto}) {
688 $where .= qq| AND da.transdate >= ?|;
689 push(@values, $form->{dunningto});
692 if ($form->{salesman_id}) {
693 $where .= qq| AND a.salesman_id = ?|;
694 push(@values, conv_i($form->{salesman_id}));
698 'dunning_description' => [ qw(dn.dunning_description customername invnumber) ],
699 'customername' => [ qw(customername invnumber) ],
700 'invnumber' => [ qw(a.invnumber) ],
701 'transdate' => [ qw(a.transdate a.invnumber) ],
702 'duedate' => [ qw(a.duedate a.invnumber) ],
703 'dunning_date' => [ qw(dunning_date a.invnumber) ],
704 'dunning_duedate' => [ qw(dunning_duedate a.invnumber) ],
705 'salesman' => [ qw(salesman) ],
708 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
709 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername';
710 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
713 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount, a.language_id,
714 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
715 da.interest, dn.dunning_description, da.transdate AS dunning_date,
716 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id,
719 JOIN customer ct ON (a.customer_id = ct.id)
720 LEFT JOIN employee e2 ON (a.salesman_id = e2.id), dunning da
721 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
723 ORDER BY $sortorder|;
725 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
727 foreach my $ref (@{ $form->{DUNNINGS} }) {
728 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
731 $main::lxdebug->leave_sub();
736 $main::lxdebug->enter_sub();
738 my ($self, $myconfig, $form, $copies) = @_;
740 # Don't allow access outside of $spool.
741 map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
744 $copies = 1 unless $copies;
745 my $spool = $::lx_office_conf{paths}->{spool};
746 my $inputfiles = join " ", map { "$spool/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
747 my $dunning_id = $form->{dunning_id};
749 $dunning_id =~ s|[^\d]||g;
751 my $in = IO::File->new($::lx_office_conf{applications}->{ghostscript} . " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
752 $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
754 if ($form->{media} eq 'printer') {
755 $form->get_printer_code($myconfig);
757 if ($form->{printer_command}) {
758 $out = IO::File->new("| $form->{printer_command}");
761 $::locale->with_raw_io($out, sub { $out->print($_) while <$in> });
763 $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
766 my $dunning_filename = $form->get_formname_translation('dunning');
767 print qq|Content-Type: Application/PDF\n| .
768 qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|;
770 $::locale->with_raw_io(\*STDOUT, sub { print while <$in> });
775 map { unlink("$spool/$_") } @{ $form->{DUNNING_PDFS} };
777 $main::lxdebug->leave_sub();
781 $main::lxdebug->enter_sub();
783 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
785 # connect to database
786 my $dbh = $provided_dbh || SL::DB->client->dbh;
788 $dunning_id =~ s|[^\d]||g;
790 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
791 if ($form->{"language_id"}) {
792 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
793 AM->get_language_details($myconfig, $form, $form->{language_id});
795 $output_dateformat = $myconfig->{dateformat};
796 $output_numberformat = $myconfig->{numberformat};
797 $output_longdates = 1;
803 da.transdate AS dunning_date,
804 da.duedate AS dunning_duedate,
806 dcfg.template AS formname,
807 dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
809 ar.transdate, ar.duedate, ar.customer_id,
810 ar.invnumber, ar.ordnumber, ar.cp_id,
811 ar.amount, ar.netamount, ar.paid,
812 ar.employee_id, ar.salesman_id,
813 (SELECT cu.name FROM currencies cu WHERE cu.id = ar.currency_id) AS curr,
814 (SELECT description from department WHERE id = ar.department_id) AS department,
815 ar.amount - ar.paid AS open_amount,
816 ar.amount - ar.paid + da.fee + da.interest AS linetotal
819 LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
820 LEFT JOIN ar ON (ar.id = da.trans_id)
821 WHERE (da.dunning_id = ?)|;
823 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
825 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
827 $form->{TEMPLATE_ARRAYS} = {};
828 map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref}));
831 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
832 map { $form->{$_} = $ref->{$_} } keys %$ref;
833 map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
839 c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
840 c.country, c.department_1, c.department_2, c.email, c.customernumber,
841 c.greeting, c.contact, c.phone, c.fax, c.homepage,
842 c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban,
847 LEFT JOIN ar ON (d.trans_id = ar.id)
848 LEFT JOIN customer c ON (ar.customer_id = c.id)
849 LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
850 LEFT JOIN employee e ON (ar.salesman_id = e.id)
851 WHERE (d.dunning_id = ?)
853 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
854 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
858 cfg.interest_rate, cfg.template AS formname, cfg.dunning_level,
859 cfg.email_subject, cfg.email_body, cfg.email_attachment,
860 d.transdate AS dunning_date,
863 WHERE dunning_id = ?)
865 (SELECT SUM(interest)
867 WHERE dunning_id = ?)
869 (SELECT SUM(amount) - SUM(paid)
874 WHERE dunning_id = ?))
877 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
878 WHERE d.dunning_id = ?
880 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
881 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
883 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
884 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
885 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
886 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
887 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
889 $::form->format_dates($output_dateformat, $output_longdates,
890 qw(dn_dunning_date dn_dunning_duedate dn_transdate dn_duedate
891 dunning_date dunning_duedate transdate duedate)
893 $::form->reformat_numbers($output_numberformat, 2, qw(
894 dn_amount dn_netamount dn_paid dn_open_amount dn_fee dn_interest dn_linetotal
895 amount netamount paid open_amount fee interest linetotal
896 total_interest total_open_interest total_amount total_open_amount
898 $::form->reformat_numbers($output_numberformat, undef, qw(interest_rate));
900 $self->set_customer_cvars($myconfig, $form);
901 $self->set_template_options($myconfig, $form);
903 my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
904 my $spool = $::lx_office_conf{paths}->{spool};
905 $form->{OUT} = "${spool}/$filename";
906 $form->{keep_tmpfile} = 1;
908 delete $form->{tmpfile};
910 push @{ $form->{DUNNING_PDFS} }, $filename;
911 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'path' => "${spool}/$filename",
912 'name' => $form->get_formname_translation('dunning') . "_${dunning_id}.pdf" };
914 my $employee_id = ($::instance_conf->get_dunning_creator eq 'invoice_employee') ?
915 $form->{employee_id} :
916 SL::DB::Manager::Employee->current->id;
918 $form->get_employee_data('prefix' => 'employee', 'id' => $employee_id);
919 $form->get_employee_data('prefix' => 'salesman', 'id' => $form->{salesman_id});
921 $form->{attachment_type} = "dunning";
922 if ( $form->{dunning_level} ) {
923 $form->{attachment_type} .= $form->{dunning_level} if $form->{dunning_level} < 4;
925 $form->{attachment_filename} = $form->get_formname_translation($form->{attachment_type}) . "_${dunning_id}.pdf";
926 $form->{attachment_id} = $form->{invoice_id};
927 $form->parse_template($myconfig);
929 $main::lxdebug->leave_sub();
932 sub print_invoice_for_fees {
933 $main::lxdebug->enter_sub();
935 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
937 my $dbh = $provided_dbh || SL::DB->client->dbh;
939 my ($query, @values, $sth);
943 d.fee_interest_ar_id,
944 d.trans_id AS invoice_id,
948 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
949 WHERE d.dunning_id = ?|;
950 my ($ar_id, $invoice_id, $template, $dunning_level) = selectrow_query($form, $dbh, $query, $dunning_id);
953 $main::lxdebug->leave_sub();
957 my $saved_form = save_form();
959 $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
960 my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
964 ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
965 ar.duedate, ar.notes, ar.notes AS invoicenotes, ar.customer_id,
967 c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country,
968 c.contact, c.customernumber, c.phone, c.fax, c.email,
969 c.taxnumber, c.greeting
972 LEFT JOIN customer c ON (ar.customer_id = c.id)
974 my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
975 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
977 $query = qq|SELECT * FROM employee WHERE login = ?|;
978 $ref = selectfirst_hashref_query($form, $dbh, $query, $::myconfig{login});
979 map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
981 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|;
982 $sth = prepare_execute_query($form, $dbh, $query, $ar_id);
984 my ($row, $fee, $interest) = (0, 0, 0);
986 while ($ref = $sth->fetchrow_hashref()) {
987 next if ($ref->{amount} < 0);
992 $fee = $ref->{amount};
994 $interest = $ref->{amount};
998 $form->{fee} = $form->round_amount($fee, 2);
999 $form->{interest} = $form->round_amount($interest, 2);
1000 $form->{invamount} = $form->round_amount($fee + $interest, 2);
1001 $form->{dunning_id} = $dunning_id;
1002 $form->{formname} = "${template}_invoice";
1004 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
1006 $self->set_customer_cvars($myconfig, $form);
1007 $self->set_template_options($myconfig, $form);
1009 my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
1011 my $spool = $::lx_office_conf{paths}->{spool};
1012 $form->{OUT} = "$spool/$filename";
1013 $form->{keep_tmpfile} = 1;
1014 delete $form->{tmpfile};
1016 map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
1018 $form->{attachment_filename} = $form->get_formname_translation('dunning_invoice') . "_${dunning_id}.pdf";
1019 $form->{attachment_type} = "dunning";
1020 $form->{attachment_id} = $form->{invoice_id};
1021 $form->parse_template($myconfig);
1023 restore_form($saved_form);
1025 push @{ $form->{DUNNING_PDFS} }, $filename;
1026 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
1027 'name' => "dunning_invoice_${dunning_id}.pdf" };
1029 $main::lxdebug->leave_sub();
1032 sub set_customer_cvars {
1033 my ($self, $myconfig, $form) = @_;
1035 my $custom_variables = CVar->get_custom_variables(dbh => $form->get_standard_dbh,
1037 trans_id => $form->{customer_id});
1038 map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
1040 $form->{cp_greeting} = GenericTranslations->get(dbh => $form->get_standard_dbh,
1041 translation_type => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
1042 language_id => $form->{language_id},
1043 allow_fallback => 1);
1044 if ($form->{cp_id}) {
1045 $custom_variables = CVar->get_custom_variables(dbh => $form->get_standard_dbh,
1046 module => 'Contacts',
1047 trans_id => $form->{cp_id});
1048 $form->{"cp_cvar_$_->{name}"} = $_->{value} for @{ $custom_variables };