+sub print_dunning {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form, $dunning_id, $userspath, $spool, $sendmail) = @_;
+ # connect to database
+ my $dbh = $form->dbconnect_noauto($myconfig);
+
+ my $query =
+ qq|SELECT invnumber, ordnumber, customer_id, amount, netamount,
+ ar.transdate, ar.duedate, paid, amount - paid AS open_amount,
+ template AS formname, email_subject, email_body, email_attachment,
+ da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate
+ FROM dunning da
+ LEFT JOIN dunning_config ON (dunning_config.id = da.dunning_config_id)
+ LEFT JOIN ar ON (ar.id = da.trans_id)
+ WHERE (da.dunning_id = ?)|;
+
+ my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
+ my $first = 1;
+ while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+ if ($first) {
+ map({ $form->{"dn_$_"} = []; } keys(%{$ref}));
+ $first = 0;
+ }
+ map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest);
+ map { $form->{$_} = $ref->{$_} } keys %$ref;
+ map { push @{ $form->{"dn_$_"} }, $ref->{$_}} keys %$ref;
+ }
+ $sth->finish;
+
+ $query =
+ qq|SELECT id AS customer_id, name, street, zipcode, city, country, department_1, department_2, email
+ FROM customer
+ WHERE id =
+ (SELECT customer_id
+ FROM dunning d
+ LEFT JOIN ar ON (d.trans_id = ar.id)
+ WHERE d.id = ?)|;
+ $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
+ map { $form->{$_} = $ref->{$_} } keys %{ $ref };
+
+ $query =
+ qq|SELECT
+ cfg.interest_rate, cfg.template AS formname,
+ cfg.email_subject, cfg.email_body, cfg.email_attachment,
+ d.fee, d.transdate AS dunning_date,
+ (SELECT SUM(interest)
+ FROM dunning
+ WHERE dunning_id = ?)
+ AS total_interest,
+ (SELECT SUM(amount) - SUM(paid)
+ FROM ar
+ WHERE id IN
+ (SELECT trans_id
+ FROM dunning
+ WHERE dunning_id = ?))
+ AS total_open_amount
+ FROM dunning d
+ LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
+ WHERE d.dunning_id = ?
+ LIMIT 1|;
+ $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id);
+ map { $form->{$_} = $ref->{$_} } keys %{ $ref };
+
+ $form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
+ $form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
+ $form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
+ $form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
+ $form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
+
+
+ $form->{templates} = "$myconfig->{templates}";
+
+ $form->{language} = $form->get_template_language(\%myconfig);
+ $form->{printer_code} = $form->get_printer_code(\%myconfig);
+
+ if ($form->{language} ne "") {
+ $form->{language} = "_" . $form->{language};
+ }
+
+ if ($form->{printer_code} ne "") {
+ $form->{printer_code} = "_" . $form->{printer_code};
+ }
+
+ $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
+ if ($form->{format} eq 'postscript') {
+ $form->{postscript} = 1;
+ $form->{IN} =~ s/html$/tex/;
+ } elsif ($form->{"format"} =~ /pdf/) {
+ $form->{pdf} = 1;
+ if ($form->{"format"} =~ /opendocument/) {
+ $form->{IN} =~ s/html$/odt/;
+ } else {
+ $form->{IN} =~ s/html$/tex/;
+ }
+ } elsif ($form->{"format"} =~ /opendocument/) {
+ $form->{"opendocument"} = 1;
+ $form->{"IN"} =~ s/html$/odt/;
+ }
+
+ if ($form->{"send_email"} && ($form->{email} ne "")) {
+ $form->{media} = 'email';
+ }
+
+ $form->{keep_tmpfile} = 0;
+ if ($form->{media} eq 'email') {
+ $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
+ unless $form->{subject};
+ if (!$form->{email_attachment}) {
+ $form->{do_not_attach} = 1;
+ } else {
+ $form->{do_not_attach} = 0;
+ }
+ $form->{subject} = parse_strings($myconfig, $form, $userspath, $form->{email_subject});
+ $form->{message} = parse_strings($myconfig, $form, $userspath, $form->{email_body});
+
+ $form->{OUT} = "$sendmail";
+
+ } else {
+
+ my $filename = Common::unique_id() . $form->{login} . ".pdf";
+
+ push(@{ $form->{DUNNING_PDFS} }, $filename);
+ $form->{keep_tmpfile} = 1;
+ }
+
+ $form->parse_template($myconfig, $userspath);
+
+ $dbh->commit;
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+