+ $sth->finish();
+
+ $query =
+ qq|SELECT
+ c.id AS customer_id, c.name, c.street, c.zipcode, c.city,
+ c.country, c.department_1, c.department_2, c.email, c.customernumber,
+ c.greeting, c.contact, c.phone, c.fax, c.homepage,
+ c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban,
+ c.ustid,
+ co.*
+ FROM dunning d
+ LEFT JOIN ar ON (d.trans_id = ar.id)
+ LEFT JOIN customer c ON (ar.customer_id = c.id)
+ LEFT JOIN contacts co ON (ar.cp_id = co.cp_id)
+ LEFT JOIN employee e ON (ar.salesman_id = e.id)
+ WHERE (d.dunning_id = ?)
+ LIMIT 1|;
+ my $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.transdate AS dunning_date,
+ (SELECT SUM(fee)
+ FROM dunning
+ WHERE dunning_id = ?)
+ AS fee,
+ (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, $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->format_dates($output_dateformat, $output_longdates,
+ qw(dn_dunning_date dn_dunning_duedate dn_transdate dn_duedate
+ dunning_date dunning_duedate transdate duedate)
+ );
+ $::form->reformat_numbers($output_numberformat, 2, qw(
+ dn_amount dn_netamount dn_paid dn_open_amount dn_fee dn_interest dn_linetotal
+ amount netamount paid open_amount fee interest linetotal
+ total_interest total_open_interest total_amount total_open_amount
+ ));
+ $::form->reformat_numbers($output_numberformat, undef, qw(interest_rate));
+
+ $self->set_customer_cvars($myconfig, $form);
+ $self->set_template_options($myconfig, $form);
+
+ my $filename = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
+ my $spool = $::lx_office_conf{paths}->{spool};
+ $form->{OUT} = "${spool}/$filename";
+ $form->{keep_tmpfile} = 1;
+
+ delete $form->{tmpfile};
+
+ push @{ $form->{DUNNING_PDFS} }, $filename;
+ push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'filename' => "${spool}/$filename",
+ 'name' => $form->get_formname_translation('dunning') . "_${dunning_id}.pdf" };
+
+ $form->get_employee_data('prefix' => 'employee', 'id' => $form->{employee_id});
+ $form->get_employee_data('prefix' => 'salesman', 'id' => $form->{salesman_id});
+
+ $form->parse_template($myconfig);