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 #======================================================================
39 use SL::GenericTranslations;
50 $main::lxdebug->enter_sub();
52 my ($self, $myconfig, $form) = @_;
55 my $dbh = $form->dbconnect($myconfig);
59 qq|FROM dunning_config | .
60 qq|ORDER BY dunning_level|;
61 $form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
63 foreach my $ref (@{ $form->{DUNNING} }) {
64 $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
65 $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
69 qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar
71 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
75 $main::lxdebug->leave_sub();
79 $main::lxdebug->enter_sub();
81 my ($self, $myconfig, $form) = @_;
84 my $dbh = $form->dbconnect_noauto($myconfig);
88 for my $i (1 .. $form->{rowcount}) {
89 $form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
90 $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
92 if (($form->{"dunning_level_$i"} ne "") &&
93 ($form->{"dunning_description_$i"} ne "")) {
94 @values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
95 $form->{"email_subject_$i"}, $form->{"email_body_$i"},
96 $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
97 $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
98 $form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}),
99 $form->{"create_invoices_for_fees_$i"} ? 't' : 'f');
100 if ($form->{"id_$i"}) {
102 qq|UPDATE dunning_config SET
103 dunning_level = ?, dunning_description = ?,
104 email_subject = ?, email_body = ?,
105 template = ?, fee = ?, interest_rate = ?,
106 active = ?, auto = ?, email = ?,
107 email_attachment = ?, payment_terms = ?, terms = ?,
108 create_invoices_for_fees = ?
110 push(@values, conv_i($form->{"id_$i"}));
113 qq|INSERT INTO dunning_config
114 (dunning_level, dunning_description, email_subject, email_body,
115 template, fee, interest_rate, active, auto, email,
116 email_attachment, payment_terms, terms, create_invoices_for_fees)
117 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
119 do_query($form, $dbh, $query, @values);
122 if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
123 $query = qq|DELETE FROM dunning_config WHERE id = ?|;
124 do_query($form, $dbh, $query, $form->{"id_$i"});
128 $query = qq|UPDATE defaults SET dunning_ar_amount_fee = ?, dunning_ar_amount_interest = ?, dunning_ar = ?|;
129 @values = (conv_i($form->{AR_amount_fee}), conv_i($form->{AR_amount_interest}), conv_i($form->{AR}));
130 do_query($form, $dbh, $query, @values);
135 $main::lxdebug->leave_sub();
138 sub create_invoice_for_fees {
139 $main::lxdebug->enter_sub();
141 my ($self, $myconfig, $form, $dbh, $dunning_id) = @_;
143 my ($query, @values, $sth, $ref);
145 $query = qq|SELECT dcfg.create_invoices_for_fees
147 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
148 WHERE d.dunning_id = ?|;
149 my ($create_invoices_for_fees) = selectrow_query($form, $dbh, $query, $dunning_id);
151 if (!$create_invoices_for_fees) {
152 $main::lxdebug->leave_sub();
156 $query = qq|SELECT dunning_ar_amount_fee, dunning_ar_amount_interest, dunning_ar FROM defaults|;
157 ($form->{AR_amount_fee}, $form->{AR_amount_interest}, $form->{AR}) = selectrow_query($form, $dbh, $query);
163 SELECT MAX(d_fee.fee)
165 WHERE (d_fee.trans_id = d.trans_id)
166 AND (d_fee.dunning_id <> ?)
167 AND NOT (d_fee.fee_interest_ar_id ISNULL)
172 SELECT MAX(d_interest.interest)
173 FROM dunning d_interest
174 WHERE (d_interest.trans_id = d.trans_id)
175 AND (d_interest.dunning_id <> ?)
176 AND NOT (d_interest.fee_interest_ar_id ISNULL)
178 AS max_previous_interest
180 WHERE dunning_id = ?|;
181 @values = ($dunning_id, $dunning_id, $dunning_id);
182 $sth = prepare_execute_query($form, $dbh, $query, @values);
184 my ($fee_remaining, $interest_remaining) = (0, 0);
185 my ($fee_total, $interest_total) = (0, 0);
187 while (my $ref = $sth->fetchrow_hashref()) {
188 $fee_remaining += $form->round_amount($ref->{fee}, 2);
189 $fee_remaining -= $form->round_amount($ref->{max_previous_fee}, 2);
190 $fee_total += $form->round_amount($ref->{fee}, 2);
191 $interest_remaining += $form->round_amount($ref->{interest}, 2);
192 $interest_remaining -= $form->round_amount($ref->{max_previous_interest}, 2);
193 $interest_total += $form->round_amount($ref->{interest}, 2);
198 my $amount = $fee_remaining + $interest_remaining;
201 $main::lxdebug->leave_sub();
205 my ($ar_id) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
206 my $curr = $form->get_default_currency($myconfig);
209 qq|INSERT INTO ar (id, invnumber, transdate, gldate, customer_id,
210 taxincluded, amount, netamount, paid, duedate,
211 invoice, curr, notes,
216 current_date, -- transdate
217 current_date, -- gldate
219 (SELECT ar.customer_id
221 LEFT JOIN ar ON (dn.trans_id = ar.id)
222 WHERE dn.dunning_id = ?
229 (SELECT duedate FROM dunning WHERE dunning_id = ? LIMIT 1),
234 (SELECT id FROM employee WHERE login = ?)
236 @values = ($ar_id, # id
237 $form->update_defaults($myconfig, 'invnumber', $dbh), # invnumber
238 $dunning_id, # customer_id
241 $dunning_id, # duedate
242 $curr, # default currency
243 sprintf($main::locale->text('Automatically created invoice for fee and interest for dunning %s'), $dunning_id), # notes
244 $form->{login}); # employee_id
245 do_query($form, $dbh, $query, @values);
248 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, taxkey)
249 VALUES (?, ?, ?, current_date, current_date, 0)|;
250 $sth = prepare_query($form, $dbh, $query);
252 @values = ($ar_id, conv_i($form->{AR_amount_fee}), $fee_remaining);
253 do_statement($form, $sth, $query, @values);
255 if ($interest_remaining) {
256 @values = ($ar_id, conv_i($form->{AR_amount_interest}), $interest_remaining);
257 do_statement($form, $sth, $query, @values);
260 @values = ($ar_id, conv_i($form->{AR}), -1 * $amount);
261 do_statement($form, $sth, $query, @values);
265 $query = qq|UPDATE dunning SET fee_interest_ar_id = ? WHERE dunning_id = ?|;
266 do_query($form, $dbh, $query, $ar_id, $dunning_id);
268 $main::lxdebug->leave_sub();
272 $main::lxdebug->enter_sub();
274 my ($self, $myconfig, $form, $rows) = @_;
275 # connect to database
276 my $dbh = $form->dbconnect_noauto($myconfig);
278 my ($query, @values);
280 my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
282 my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
283 my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
285 my $q_insert_dunning =
286 qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level, trans_id,
287 fee, interest, transdate, duedate)
289 (SELECT dunning_level FROM dunning_config WHERE id = ?),
293 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
294 (SELECT (amount - paid) * (current_date - duedate) FROM ar WHERE id = ?)
295 * (SELECT interest_rate FROM dunning_config WHERE id = ?)
298 current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
299 my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
302 my ($next_dunning_config_id, $customer_id);
305 foreach my $row (@{ $rows }) {
306 push @invoice_ids, $row->{invoice_id};
307 $next_dunning_config_id = $row->{next_dunning_config_id};
308 $customer_id = $row->{customer_id};
310 @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
311 do_statement($form, $h_update_ar, $q_update_ar, @values);
313 $send_email |= $row->{email};
315 my $next_config_id = conv_i($row->{next_dunning_config_id});
316 my $invoice_id = conv_i($row->{invoice_id});
318 @values = ($dunning_id, $next_config_id, $next_config_id,
319 $invoice_id, $next_config_id, $invoice_id,
320 $next_config_id, $next_config_id);
321 do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
324 $h_update_ar->finish();
325 $h_insert_dunning->finish();
327 $form->{DUNNING_PDFS_EMAIL} = [];
329 $self->create_invoice_for_fees($myconfig, $form, $dbh, $dunning_id);
331 $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
332 $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
334 $form->{dunning_id} = $dunning_id;
337 $self->send_email($myconfig, $form, $dunning_id, $dbh);
343 $main::lxdebug->leave_sub();
347 $main::lxdebug->enter_sub();
349 my ($self, $myconfig, $form, $dunning_id, $dbh) = @_;
353 dcfg.email_body, dcfg.email_subject, dcfg.email_attachment,
357 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
358 LEFT JOIN ar ON (d.trans_id = ar.id)
359 LEFT JOIN customer c ON (ar.customer_id = c.id)
360 WHERE (d.dunning_id = ?)
362 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
364 if (!$ref || !$ref->{recipient} || !$myconfig->{email}) {
365 $main::lxdebug->leave_sub();
369 my $template = SL::Template::create(type => 'PlainText', form => $form, myconfig => $myconfig);
370 my $mail = Mailer->new();
371 $mail->{charset} = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
372 $mail->{from} = $myconfig->{email};
373 $mail->{to} = $ref->{recipient};
374 $mail->{subject} = $template->parse_block($ref->{email_subject});
375 $mail->{message} = $template->parse_block($ref->{email_body});
377 if ($myconfig->{signature}) {
378 $mail->{message} .= "\n-- \n$myconfig->{signature}";
381 $mail->{message} =~ s/\r\n/\n/g;
383 if ($ref->{email_attachment} && @{ $form->{DUNNING_PDFS_EMAIL} }) {
384 $mail->{attachments} = $form->{DUNNING_PDFS_EMAIL};
389 $main::lxdebug->leave_sub();
392 sub set_template_options {
393 $main::lxdebug->enter_sub();
395 my ($self, $myconfig, $form) = @_;
397 $form->{templates} = "$myconfig->{templates}";
398 $form->{language} = $form->get_template_language($myconfig);
399 $form->{printer_code} = $form->get_printer_code($myconfig);
401 if ($form->{language} ne "") {
402 $form->{language} = "_" . $form->{language};
405 if ($form->{printer_code} ne "") {
406 $form->{printer_code} = "_" . $form->{printer_code};
409 my $extension = 'html';
410 if ($form->{format} eq 'postscript') {
411 $form->{postscript} = 1;
414 } elsif ($form->{"format"} =~ /pdf/) {
416 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
418 } elsif ($form->{"format"} =~ /opendocument/) {
419 $form->{opendocument} = 1;
421 } elsif ($form->{"format"} =~ /excel/) {
427 # search for the template
429 push @template_files, "$form->{formname}_email$form->{language}$form->{printer_code}.$extension" if $form->{media} eq 'email';
430 push @template_files, "$form->{formname}$form->{language}$form->{printer_code}.$extension";
431 push @template_files, "$form->{formname}.$extension";
432 push @template_files, "default.$extension";
435 for my $filename (@template_files) {
436 if (-f "$form->{templates}/$filename") {
437 $form->{IN} = $filename;
442 if (!defined $form->{IN}) {
443 $::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));
446 # prepare meta information for template introspection
447 $form->{template_meta} = {
448 formname => $form->{formname},
449 language => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id}),
450 format => $form->{format},
451 media => $form->{media},
452 extension => $extension,
453 printer => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id}),
454 today => DateTime->today,
457 $main::lxdebug->leave_sub();
462 $main::lxdebug->enter_sub();
464 my ($self, $myconfig, $form) = @_;
466 # connect to database
467 my $dbh = $form->dbconnect($myconfig);
472 $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
474 if ($form->{customer_id}) {
475 $where .= qq| AND (a.customer_id = ?)|;
476 push(@values, $form->{customer_id});
478 } elsif ($form->{customer}) {
479 $where .= qq| AND (ct.name ILIKE ?)|;
480 push(@values, '%' . $form->{customer} . '%');
484 "ordnumber" => "a.ordnumber",
485 "invnumber" => "a.invnumber",
486 "notes" => "a.notes",
487 "country" => "ct.country",
489 foreach my $key (keys(%columns)) {
490 next unless ($form->{$key});
491 $where .= qq| AND $columns{$key} ILIKE ?|;
492 push(@values, '%' . $form->{$key} . '%');
495 if ($form->{dunning_level}) {
496 $where .= qq| AND nextcfg.id = ?|;
497 push(@values, conv_i($form->{dunning_level}));
500 $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
501 if ($form->{minamount}) {
502 $where .= qq| AND ((a.amount - a.paid) > ?) |;
503 push(@values, $form->{minamount});
509 WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|;
510 my ($id_for_max_dunning_level) = selectrow_query($form, $dbh, $query);
514 a.id, a.ordnumber, a.transdate, a.invnumber, a.amount, a.language_id,
515 ct.name AS customername, a.customer_id, a.duedate,
516 a.amount - a.paid AS open_amount,
518 cfg.dunning_description, cfg.dunning_level,
520 d.transdate AS dunning_date, d.duedate AS dunning_duedate,
523 a.duedate + cfg.terms - current_date AS nextlevel,
524 current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
525 current_date + cfg.payment_terms AS next_duedate,
527 nextcfg.dunning_description AS next_dunning_description,
528 nextcfg.id AS next_dunning_config_id,
529 nextcfg.terms, nextcfg.active, nextcfg.email
533 LEFT JOIN customer ct ON (a.customer_id = ct.id)
534 LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
535 LEFT JOIN dunning_config nextcfg ON
540 WHERE dunning_level >
541 COALESCE((SELECT dunning_level
543 WHERE id = a.dunning_config_id
544 ORDER BY dunning_level DESC
547 ORDER BY dunning_level ASC
550 LEFT JOIN dunning d ON (d.id = (
553 WHERE (d2.trans_id = a.id)
554 AND (d2.dunning_level = cfg.dunning_level)
557 WHERE (a.paid < a.amount)
558 AND (a.duedate < current_date)
562 ORDER BY a.id, transdate, duedate, name|;
563 my $sth = prepare_execute_query($form, $dbh, $query, $id_for_max_dunning_level, @values);
565 $form->{DUNNINGS} = [];
567 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
568 next if ($ref->{pastdue} < $ref->{terms});
570 $ref->{interest} = $form->round_amount($ref->{interest}, 2);
571 push(@{ $form->{DUNNINGS} }, $ref);
576 $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
577 $form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
580 $main::lxdebug->leave_sub();
585 $main::lxdebug->enter_sub();
587 my ($self, $myconfig, $form) = @_;
589 # connect to database
590 my $dbh = $form->dbconnect($myconfig);
592 my $where = qq| WHERE (da.trans_id = a.id)|;
596 if ($form->{customer_id}) {
597 $where .= qq| AND (a.customer_id = ?)|;
598 push(@values, $form->{customer_id});
600 } elsif ($form->{customer}) {
601 $where .= qq| AND (ct.name ILIKE ?)|;
602 push(@values, '%' . $form->{customer} . '%');
606 "ordnumber" => "a.ordnumber",
607 "invnumber" => "a.invnumber",
608 "notes" => "a.notes",
610 foreach my $key (keys(%columns)) {
611 next unless ($form->{$key});
612 $where .= qq| AND $columns{$key} ILIKE ?|;
613 push(@values, '%' . $form->{$key} . '%');
616 if ($form->{dunning_level}) {
617 $where .= qq| AND a.dunning_config_id = ?|;
618 push(@values, conv_i($form->{dunning_level}));
621 if ($form->{department_id}) {
622 $where .= qq| AND a.department_id = ?|;
623 push @values, conv_i($form->{department_id});
626 $form->{minamount} = $form->parse_amount($myconfig, $form->{minamount});
627 if ($form->{minamount}) {
628 $where .= qq| AND ((a.amount - a.paid) > ?) |;
629 push(@values, $form->{minamount});
632 if (!$form->{showold}) {
633 $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
636 if ($form->{transdatefrom}) {
637 $where .= qq| AND a.transdate >= ?|;
638 push(@values, $form->{transdatefrom});
640 if ($form->{transdateto}) {
641 $where .= qq| AND a.transdate <= ?|;
642 push(@values, $form->{transdateto});
644 if ($form->{dunningfrom}) {
645 $where .= qq| AND da.transdate >= ?|;
646 push(@values, $form->{dunningfrom});
648 if ($form->{dunningto}) {
649 $where .= qq| AND da.transdate >= ?|;
650 push(@values, $form->{dunningto});
653 if ($form->{salesman_id}) {
654 $where .= qq| AND a.salesman_id = ?|;
655 push(@values, conv_i($form->{salesman_id}));
659 'dunning_description' => [ qw(dn.dunning_description customername invnumber) ],
660 'customername' => [ qw(customername invnumber) ],
661 'invnumber' => [ qw(a.invnumber) ],
662 'transdate' => [ qw(a.transdate a.invnumber) ],
663 'duedate' => [ qw(a.duedate a.invnumber) ],
664 'dunning_date' => [ qw(dunning_date a.invnumber) ],
665 'dunning_duedate' => [ qw(dunning_duedate a.invnumber) ],
666 'salesman' => [ qw(salesman) ],
669 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
670 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername';
671 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
674 qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount, a.language_id,
675 ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee,
676 da.interest, dn.dunning_description, da.transdate AS dunning_date,
677 da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id,
680 JOIN customer ct ON (a.customer_id = ct.id)
681 LEFT JOIN employee e2 ON (a.salesman_id = e2.id), dunning da
682 LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
684 ORDER BY $sortorder|;
686 $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
688 foreach my $ref (@{ $form->{DUNNINGS} }) {
689 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
693 $main::lxdebug->leave_sub();
698 $main::lxdebug->enter_sub();
700 my ($self, $myconfig, $form, $copies) = @_;
702 # Don't allow access outside of $spool.
703 map { $_ =~ s|.*/||; } @{ $form->{DUNNING_PDFS} };
706 $copies = 1 unless $copies;
707 my $spool = $::lx_office_conf{paths}->{spool};
708 my $inputfiles = join " ", map { "$spool/$_ " x $copies } @{ $form->{DUNNING_PDFS} };
709 my $dunning_id = $form->{dunning_id};
711 $dunning_id =~ s|[^\d]||g;
713 my $in = IO::File->new($::lx_office_conf{applications}->{ghostscript} . " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |");
714 $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in;
716 if ($form->{media} eq 'printer') {
717 $form->get_printer_code($myconfig);
719 if ($form->{printer_command}) {
720 $out = IO::File->new("| $form->{printer_command}");
723 $::locale->with_raw_io($out, sub { $out->print($_) while <$in> });
725 $form->error($main::locale->text('Could not spawn the printer command.')) unless $out;
728 my $dunning_filename = $form->get_formname_translation('dunning');
729 print qq|Content-Type: Application/PDF\n| .
730 qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|;
732 $::locale->with_raw_io(\*STDOUT, sub { print while <$in> });
737 map { unlink("$spool/$_") } @{ $form->{DUNNING_PDFS} };
739 $main::lxdebug->leave_sub();
743 $main::lxdebug->enter_sub();
745 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
747 # connect to database
748 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
750 $dunning_id =~ s|[^\d]||g;
755 da.transdate AS dunning_date,
756 da.duedate AS dunning_duedate,
758 dcfg.template AS formname,
759 dcfg.email_subject, dcfg.email_body, dcfg.email_attachment,
761 ar.transdate, ar.duedate, ar.customer_id,
762 ar.invnumber, ar.ordnumber, ar.cp_id,
763 ar.amount, ar.netamount, ar.paid,
765 ar.amount - ar.paid AS open_amount,
766 ar.amount - ar.paid + da.fee + da.interest AS linetotal
769 LEFT JOIN dunning_config dcfg ON (dcfg.id = da.dunning_config_id)
770 LEFT JOIN ar ON (ar.id = da.trans_id)
771 WHERE (da.dunning_id = ?)|;
773 my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
775 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
777 $form->{TEMPLATE_ARRAYS} = {};
778 map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref}));
781 map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
782 map { $form->{$_} = $ref->{$_} } keys %$ref;
783 map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
789 c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
790 c.country, c.department_1, c.department_2, c.email, c.customernumber,
791 c.greeting, c.contact, c.phone, c.fax, c.homepage,
792 c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban,
793 c,ustid, e.name as salesman_name,
796 LEFT JOIN ar ON (d.trans_id = ar.id)
797 LEFT JOIN customer c ON (ar.customer_id = c.id)
798 LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
799 LEFT JOIN employee e ON (ar.salesman_id = e.id)
800 WHERE (d.dunning_id = ?)
802 my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
803 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
807 cfg.interest_rate, cfg.template AS formname,
808 cfg.email_subject, cfg.email_body, cfg.email_attachment,
809 d.transdate AS dunning_date,
812 WHERE dunning_id = ?)
814 (SELECT SUM(interest)
816 WHERE dunning_id = ?)
818 (SELECT SUM(amount) - SUM(paid)
823 WHERE dunning_id = ?))
826 LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
827 WHERE d.dunning_id = ?
829 $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id, $dunning_id);
830 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
832 $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
833 $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
834 $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
835 $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
836 $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
838 $self->set_customer_cvars($myconfig, $form);
839 $self->set_template_options($myconfig, $form);
841 my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
842 my $spool = $::lx_office_conf{paths}->{spool};
843 $form->{OUT} = "${spool}/$filename";
844 $form->{keep_tmpfile} = 1;
846 delete $form->{tmpfile};
848 push @{ $form->{DUNNING_PDFS} }, $filename;
849 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
850 'name' => "dunning_${dunning_id}.pdf" };
852 $form->parse_template($myconfig);
854 $dbh->disconnect() unless $provided_dbh;
856 $main::lxdebug->leave_sub();
859 sub print_invoice_for_fees {
860 $main::lxdebug->enter_sub();
862 my ($self, $myconfig, $form, $dunning_id, $provided_dbh) = @_;
864 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
866 my ($query, @values, $sth);
870 d.fee_interest_ar_id,
873 LEFT JOIN dunning_config dcfg ON (d.dunning_config_id = dcfg.id)
874 WHERE d.dunning_id = ?|;
875 my ($ar_id, $template) = selectrow_query($form, $dbh, $query, $dunning_id);
878 $main::lxdebug->leave_sub();
882 my $saved_form = save_form();
884 $query = qq|SELECT SUM(fee), SUM(interest) FROM dunning WHERE id = ?|;
885 my ($fee_total, $interest_total) = selectrow_query($form, $dbh, $query, $dunning_id);
889 ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
890 ar.duedate, ar.notes, ar.notes AS invoicenotes, ar.customer_id,
892 c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country,
893 c.contact, c.customernumber, c.phone, c.fax, c.email,
894 c.taxnumber, c.greeting
897 LEFT JOIN customer c ON (ar.customer_id = c.id)
899 my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id);
900 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
902 $query = qq|SELECT * FROM employee WHERE login = ?|;
903 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{login});
904 map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref };
906 $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|;
907 $sth = prepare_execute_query($form, $dbh, $query, $ar_id);
909 my ($row, $fee, $interest) = (0, 0, 0);
911 while ($ref = $sth->fetchrow_hashref()) {
912 next if ($ref->{amount} < 0);
917 $fee = $ref->{amount};
919 $interest = $ref->{amount};
923 $form->{fee} = $form->round_amount($fee, 2);
924 $form->{interest} = $form->round_amount($interest, 2);
925 $form->{invamount} = $form->round_amount($fee + $interest, 2);
926 $form->{dunning_id} = $dunning_id;
927 $form->{formname} = "${template}_invoice";
929 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
931 $self->set_customer_cvars($myconfig, $form);
932 $self->set_template_options($myconfig, $form);
934 my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
936 my $spool = $::lx_office_conf{paths}->{spool};
937 $form->{OUT} = "$spool/$filename";
938 $form->{keep_tmpfile} = 1;
939 delete $form->{tmpfile};
941 map { delete $form->{$_} } grep /^[a-z_]+_\d+$/, keys %{ $form };
943 $form->parse_template($myconfig);
945 restore_form($saved_form);
947 push @{ $form->{DUNNING_PDFS} }, $filename;
948 push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
949 'name' => "dunning_invoice_${dunning_id}.pdf" };
951 $dbh->disconnect() unless $provided_dbh;
953 $main::lxdebug->leave_sub();
956 sub set_customer_cvars {
957 my ($self, $myconfig, $form) = @_;
959 my $custom_variables = CVar->get_custom_variables(dbh => $form->get_standard_dbh,
961 trans_id => $form->{customer_id});
962 map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
964 $form->{cp_greeting} = GenericTranslations->get(dbh => $form->get_standard_dbh,
965 translation_type => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
966 language_id => $form->{language_id},
967 allow_fallback => 1);