Mahnungen: alle erzeugten Dokumente zu jeder gemahnten Rechnung ins WebDAV
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Wed, 17 Mar 2021 12:15:54 +0000 (13:15 +0100)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Wed, 17 Mar 2021 16:09:37 +0000 (17:09 +0100)
SL/DN.pm

index 32e3bfa..0d4146c 100644 (file)
--- a/SL/DN.pm
+++ b/SL/DN.pm
@@ -51,6 +51,7 @@ use SL::DB::Language;
 use SL::TransNumber;
 use SL::Util qw(trim);
 use SL::DB;
+use SL::Webdav;
 
 use File::Copy;
 
@@ -859,7 +860,7 @@ sub print_dunning {
 
   $dunning_id =~ s|[^\d]||g;
 
-  my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates, @dunned_invoice_ids);
+  my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates, @dunned_invoices);
   if ($form->{"language_id"}) {
     ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
       AM->get_language_details($myconfig, $form, $form->{language_id});
@@ -903,7 +904,7 @@ sub print_dunning {
     map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal);
     map { $form->{$_} = $ref->{$_} } keys %$ref;
     map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref;
-    push @dunned_invoice_ids, $ref->{dunned_invoice_id};
+    push @dunned_invoices, {id => $ref->{dunned_invoice_id}, invnumber => $ref->{invnumber}};
   }
   $sth->finish();
 
@@ -1017,11 +1018,11 @@ sub print_dunning {
   # this generates the file in the spool directory
   $form->parse_template($myconfig);
 
-  # save dunning pdf in filemanagement for each invoice
-  if ($::instance_conf->get_doc_storage) {
-    foreach my $dunned_invoice_id (@dunned_invoice_ids) {
+  # save dunning pdf in filemanagement/webdav for each invoice
+  foreach my $dunned_invoice (@dunned_invoices) {
+    if ($::instance_conf->get_doc_storage) {
       SL::File->save(
-        object_id   => $dunned_invoice_id,
+        object_id   => $dunned_invoice->{id},
         object_type => $form->{attachment_type},
         mime_type   => 'application/pdf',
         source      => 'created',
@@ -1030,6 +1031,18 @@ sub print_dunning {
         file_path   => "${spool}/$filename",
       );
     }
+
+    if ($::instance_conf->get_webdav_documents) {
+      my $webdav = SL::Webdav->new(
+        type     => 'invoice',
+        number   => $dunned_invoice->{invnumber},
+      );
+      my $webdav_file = SL::Webdav::File->new(
+        webdav   => $webdav,
+        filename => $form->{attachment_filename},
+      );
+      $webdav_file->store(file => "${spool}/$filename");
+    }
   }
 
   $main::lxdebug->leave_sub();
@@ -1133,20 +1146,35 @@ sub print_invoice_for_fees {
   push @{ $form->{DUNNING_PDFS_EMAIL} }, { 'path' => "${spool}/$filename",
                                            'name' => $attachment_filename };
 
-  # save dunning fee pdf in filemanagement for each dunned invoice
-  if ($::instance_conf->get_doc_storage) {
-    $query                 = qq|SELECT trans_id FROM dunning WHERE dunning_id = ?|;
-    my @dunned_invoice_ids = selectall_array_query($form, $dbh, $query, $dunning_id);
-    foreach my $dunned_invoice_id (@dunned_invoice_ids) {
-      SL::File->save(
-        object_id   => $dunned_invoice_id,
-        object_type => 'dunning',
-        mime_type   => 'application/pdf',
-        source      => 'created',
-        file_type   => 'document',
-        file_name   => $attachment_filename,
-        file_path   => "${spool}/$filename",
-      );
+  # save dunning fee pdf in filemanagement/webdav for each dunned invoice
+  if ($::instance_conf->get_doc_storage || $::instance_conf->get_webdav_documents) {
+    $query              = qq|SELECT trans_id, invnumber FROM dunning LEFT JOIN ar ON (ar.id = trans_id) WHERE dunning_id = ?|;
+    my $dunned_invoices = selectall_hashref_query($form, $dbh, $query, $dunning_id);
+
+    foreach my $dunned_invoice (@$dunned_invoices) {
+      if ($::instance_conf->get_doc_storage) {
+        SL::File->save(
+          object_id   => $dunned_invoice->{trans_id},
+          object_type => 'dunning',
+          mime_type   => 'application/pdf',
+          source      => 'created',
+          file_type   => 'document',
+          file_name   => $attachment_filename,
+          file_path   => "${spool}/$filename",
+        );
+      }
+
+      if ($::instance_conf->get_webdav_documents) {
+        my $webdav = SL::Webdav->new(
+          type     => 'invoice',
+          number   => $dunned_invoice->{invnumber},
+        );
+        my $webdav_file = SL::Webdav::File->new(
+          webdav   => $webdav,
+          filename => $attachment_filename,
+        );
+        $webdav_file->store(file => "${spool}/$filename");
+      }
     }
   }
 
@@ -1222,7 +1250,7 @@ sub print_original_invoices {
 
   $form->{recipient_locale}  = $saved_reicpient_locale;
 
-  # save original invoice pdf in filemanagement for dunned invoice
+  # save original invoice pdf in filemanagement/webdav for dunned invoice
   if ($::instance_conf->get_doc_storage) {
     SL::File->save(
       object_id   => $invoice_id,
@@ -1234,6 +1262,17 @@ sub print_original_invoices {
       file_path   => "${spool}/$file_name",
     );
   }
+  if ($::instance_conf->get_webdav_documents) {
+    my $webdav = SL::Webdav->new(
+      type     => 'invoice',
+      number   => $invoice->invnumber,
+    );
+    my $webdav_file = SL::Webdav::File->new(
+      webdav   => $webdav,
+      filename => $attachment_filename,
+    );
+    $webdav_file->store(file => "${spool}/$file_name");
+  }
 
 }