Mahnungen: Optional auch Original-Rechnung hinzufügen
authorJan Büren <jan@kivitendo.de>
Mon, 13 Apr 2020 09:13:29 +0000 (11:13 +0200)
committerJan Büren <jan@kivitendo.de>
Mon, 13 Apr 2020 09:13:29 +0000 (11:13 +0200)
SL/DN.pm
bin/mozilla/dn.pl
locale/de/all
templates/webpages/dunning/show_invoices.html

index 4c8001a..dd4d61a 100644 (file)
--- a/SL/DN.pm
+++ b/SL/DN.pm
@@ -51,6 +51,8 @@ use SL::TransNumber;
 use SL::Util qw(trim);
 use SL::DB;
 
+use File::Copy;
+
 use strict;
 
 sub get_config {
@@ -338,7 +340,7 @@ sub _save_dunning {
 
   my @invoice_ids;
   my ($next_dunning_config_id, $customer_id);
-  my $send_email = 0;
+  my ($send_email, $print_invoice) = (0, 0);
 
   foreach my $row (@{ $rows }) {
     push @invoice_ids, $row->{invoice_id};
@@ -348,7 +350,8 @@ sub _save_dunning {
     @values = ($row->{next_dunning_config_id}, $row->{invoice_id});
     do_statement($form, $h_update_ar, $q_update_ar, @values);
 
-    $send_email |= $row->{email};
+    $send_email       |= $row->{email};
+    $print_invoice    |= $row->{print_invoice};
 
     my $next_config_id = conv_i($row->{next_dunning_config_id});
     my $invoice_id     = conv_i($row->{invoice_id});
@@ -371,6 +374,9 @@ sub _save_dunning {
   $self->print_invoice_for_fees($myconfig, $form, $dunning_id, $dbh);
   $self->print_dunning($myconfig, $form, $dunning_id, $dbh);
 
+  if ($print_invoice) {
+    $self->print_original_invoices($myconfig, $form, $_, $dbh) for @invoice_ids;
+  }
 
   if ($send_email) {
     $self->send_email($myconfig, $form, $dunning_id, $dbh);
@@ -1076,4 +1082,43 @@ sub set_customer_cvars {
 
 }
 
+sub print_original_invoices {
+  my ($self, $myconfig, $form, $invoice_id) = @_;
+  # get one invoice as object and print to pdf
+  my $invoice = SL::DB::Invoice->new(id => $invoice_id)->load;
+
+  die "Invalid invoice object" unless ref($invoice) eq 'SL::DB::Invoice';
+
+  my $print_form          = Form->new('');
+  $print_form->{type}     = 'invoice';
+  $print_form->{formname} = 'invoice',
+  $print_form->{format}   = 'pdf',
+  $print_form->{media}    = 'file';
+  # no language override, should always be the object's language
+  $invoice->flatten_to_form($print_form, format_amounts => 1);
+  $print_form->prepare_for_printing;
+
+  my $filename = SL::Helper::CreatePDF->create_pdf(
+                   template  => 'invoice.tex',
+                   variables => $print_form,
+                   return    => 'file_name',
+  );
+
+  my $spool       = $::lx_office_conf{paths}->{spool};
+  my ($volume, $directory, $file_name) = File::Spec->splitpath($filename);
+  my $full_file_name                   = File::Spec->catfile($spool, $file_name);
+
+  move($filename, $full_file_name) or die "The move operation failed: $!";
+
+  # form get_formname_translation should use language_id_$i
+  my $saved_reicpient_locale = $form->{recipient_locale};
+  $form->{recipient_locale}  = $invoice->language;
+
+  push @{ $form->{DUNNING_PDFS} }, $file_name;
+  push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'path' => "${spool}/$file_name",
+                                           'name' => $form->get_formname_translation('invoice') . "_" . $invoice->invnumber . ".pdf" };
+
+  $form->{recipient_locale}  = $saved_reicpient_locale;
+}
+
 1;
index 0b2b27c..5cb2300 100644 (file)
@@ -226,6 +226,7 @@ sub save_dunning {
                           "customer_id"            => $form->{"customer_id_$i"},
                           "language_id"            => $form->{"language_id_$i"},
                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
+                          "print_invoice"          => $form->{"include_invoice_$i"},
                           "email"                  => $form->{"email_$i"}, };
     }
 
index b25c483..2950501 100755 (executable)
@@ -1653,6 +1653,7 @@ $self->{texts} = {
   'Include in drop-down menus'  => 'In Aufklappmenü aufnehmen',
   'Include invalid warehouses ' => 'Ungültige Lager berücksichtigen',
   'Include invoices with direct debit' => 'Inklusive Rechnungen mit Lastschrifteinzug',
+  'Include original Invoices?'  => 'Original-Rechnung hinzufügen?',
   'Includeable in reports'      => 'In Berichten anzeigbar',
   'Included in reports by default' => 'In Berichten standardmäßig enthalten',
   'Including'                   => 'Enthaltene',
index 8036ade..b4128ef 100644 (file)
     [% L.checkbox_tag('selectall_email', checkall='INPUT[name*=email_]', checked=all_email) %]
     <label for="selectall_email">[% 'eMail?' | $T8 %]</label>
    </th>
+   <th class="listheading">
+    [% L.checkbox_tag('selectall_include_invoices', checkall='INPUT[name*=include_invoice_]', checked=all_include_invoices) %]
+    <label for="selectall_include_invoices">[% 'Include original Invoices?' | $T8 %]</label>
+   </th>
 
    <th class="listheading">[% 'Customername' | $T8 %]</th>
    <th class="listheading">[% 'Department' | $T8 %]</th>
@@ -63,6 +67,7 @@
 
      <td><input type="checkbox" name="active_[% loop.count %]" value="1" [% IF row.active %]checked[% END %]></td>
      <td><input type="checkbox" name="email_[% loop.count %]" value="1" [% IF row.email %]checked[% END %]></td>
+     <td><input type="checkbox" name="include_invoice_[% loop.count %]" value="1" [% IF row.include_invoices %]checked[% END %]></td>
      <td><input type="hidden" name="customername_[% loop.count %]" size="6" value="[% HTML.escape(row.customername) %]">[% HTML.escape(row.customername) %]</td>
      <td><input type="hidden" name="department_[% loop.count %]" size="6" value="[% HTML.escape(row.departmentname) %]">[% HTML.escape(row.departmentname) %]</td>
      <td><input type="hidden" name="language_id_[% loop.count %]" size="6" value="[% HTML.escape(row.language_id) %]">[% HTML.escape(row.language) %]</td>