fxsellprice auf sellprice setzen
[kivitendo-erp.git] / SL / DN.pm
index f1e132f..2cc87df 100644 (file)
--- a/SL/DN.pm
+++ b/SL/DN.pm
@@ -36,6 +36,7 @@ package DN;
 
 use SL::Common;
 use SL::DBUtils;
+use SL::GenericTranslations;
 use SL::IS;
 use SL::Mailer;
 use SL::MoreCommon;
@@ -288,7 +289,7 @@ sub save_dunning {
                (SELECT SUM(fee)
                 FROM dunning_config
                 WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
-               (SELECT (amount - paid) * (current_date - transdate) FROM ar WHERE id = ?)
+               (SELECT (amount - paid) * (current_date - duedate) FROM ar WHERE id = ?)
                  * (SELECT interest_rate FROM dunning_config WHERE id = ?)
                  / 360,
                current_date,
@@ -363,7 +364,7 @@ sub send_email {
     return;
   }
 
-  my $template     = PlainTextTemplate->new(undef, $form, $myconfig);
+  my $template     = SL::Template::create(type => 'PlainText', form => $form, myconfig => $myconfig);
   my $mail         = Mailer->new();
   $mail->{from}    = $myconfig->{email};
   $mail->{to}      = $ref->{recipient};
@@ -503,7 +504,12 @@ sub get_invoices {
               ORDER BY dunning_level ASC
               LIMIT 1)
              , ?))
-       LEFT JOIN dunning d ON ((d.trans_id = a.id) AND (cfg.dunning_level = d.dunning_level))
+       LEFT JOIN dunning d ON (d.id = (
+         SELECT MAX(d2.id)
+         FROM dunning d2
+         WHERE (d2.trans_id      = a.id)
+           AND (d2.dunning_level = cfg.dunning_level)
+       ))
 
        WHERE (a.paid < a.amount)
          AND (a.duedate < current_date)
@@ -782,6 +788,7 @@ sub print_dunning {
   $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);
 
+  $self->set_customer_cvars($myconfig, $form);
   $self->set_template_options($myconfig, $form);
 
   my $filename          = "dunning_${dunning_id}_" . Common::unique_id() . ".pdf";
@@ -832,7 +839,7 @@ sub print_invoice_for_fees {
   $query =
     qq|SELECT
          ar.invnumber, ar.transdate AS invdate, ar.amount, ar.netamount,
-         ar.duedate,   ar.notes,     ar.notes AS invoicenotes,
+         ar.duedate,   ar.notes,     ar.notes AS invoicenotes, ar.customer_id,
 
          c.name,      c.department_1,   c.department_2, c.street, c.zipcode, c.city, c.country,
          c.contact,   c.customernumber, c.phone,        c.fax,    c.email,
@@ -873,6 +880,7 @@ sub print_invoice_for_fees {
 
   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);
 
+  $self->set_customer_cvars($myconfig, $form);
   $self->set_template_options($myconfig, $form);
 
   my $filename = Common::unique_id() . "dunning_invoice_${dunning_id}.pdf";
@@ -896,4 +904,18 @@ sub print_invoice_for_fees {
   $main::lxdebug->leave_sub();
 }
 
+sub set_customer_cvars {
+  my ($self, $myconfig, $form) = @_;
+
+  my $custom_variables = CVar->get_custom_variables(dbh      => $form->get_standard_dbh,
+                                                    module   => 'CT',
+                                                    trans_id => $form->{customer_id});
+  map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
+
+  $form->{cp_greeting} = GenericTranslations->get(dbh              => $form->get_standard_dbh,
+                                                  translation_type => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
+                                                  language_id      => $form->{language_id},
+                                                  allow_fallback   => 1);
+}
+
 1;