commit 2729421a189ba463f316a9fe706b95f34c6fbe77
Author: Bernd Bleßmann <bernd@kivitendo-premium.de>
Date:   Fri Mar 5 16:23:33 2021 +0100

    Brief: erzeugtes Dokument im DMS speichern

diff --git a/SL/Controller/File.pm b/SL/Controller/File.pm
index 3412ffbbb..a01e661b7 100644
--- a/SL/Controller/File.pm
+++ b/SL/Controller/File.pm
@@ -43,6 +43,12 @@ use Rose::Object::MakeMethods::Generic
 
 __PACKAGE__->run_before('check_object_params', only => [ qw(list ajax_delete ajax_importdialog ajax_import ajax_unimport ajax_upload ajax_files_uploaded) ]);
 
+# gen:    bitmask: bit 1 (value is 1 or 3)    => file created
+#                  bit 2 (value is 2 or 3)    => file from other source
+# gltype: is this used somewhere?
+# dir:    is this used somewhere?
+# model:  base name of the rose model
+# right:  access right used for import
 my %file_types = (
   'sales_quotation'         => { gen => 1, gltype => '',   dir =>'SalesQuotation',       model => 'Order',          right => 'import_ar'  },
   'sales_order'             => { gen => 1, gltype => '',   dir =>'SalesOrder',           model => 'Order',          right => 'import_ar'  },
@@ -61,6 +67,7 @@ my %file_types = (
   'csv_customer'            => { gen => 1, gltype => '',   dir =>'Reports',              model => 'Customer',       right => 'xx'         },
   'csv_vendor'              => { gen => 1, gltype => '',   dir =>'Reports',              model => 'Vendor',         right => 'xx'         },
   'shop_image'              => { gen => 0, gltype => '',   dir =>'ShopImages',           model => 'Part',           right => 'xx'         },
+  'letter'                  => { gen => 3, gltype => '',   dir =>'Letter',               model => 'Letter',         right => 'sales_letter_edit | purchase_letter_edit' },
 );
 
 #--- 4 locale ---#
diff --git a/SL/Controller/Letter.pm b/SL/Controller/Letter.pm
index 32e376810..4da3e1918 100644
--- a/SL/Controller/Letter.pm
+++ b/SL/Controller/Letter.pm
@@ -14,6 +14,7 @@ use SL::DB::Language;
 use SL::DB::Letter;
 use SL::DB::LetterDraft;
 use SL::DB::Printer;
+use SL::File;
 use SL::Helper::Flash qw(flash flash_later);
 use SL::Helper::CreatePDF;
 use SL::Helper::PrintOptions;
@@ -235,6 +236,17 @@ sub action_print_letter {
       $webdav_file->store(file => $result{file_name});
     }
 
+    if ($::instance_conf->get_doc_storage) {
+      my %save_params = (object_id    => $letter->id,
+                         object_type  => 'letter',
+                         mime_type    => 'application/pdf',
+                         source       => 'created',
+                         file_type    => 'document',
+                         file_name    => $attachment_name,
+                         file_path    => $result{file_name});
+      SL::File->save(%save_params);
+    }
+
     # set some form defaults for printing webdav copy variables
     if ( $::form->{media} eq 'email') {
       my $mail             = Mailer->new;
@@ -313,7 +325,7 @@ sub action_send_email {
 sub _display {
   my ($self, %params) = @_;
 
-  $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery kivi.Letter kivi.SalesPurchase);
+  $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery kivi.Letter kivi.SalesPurchase kivi.File);
 
   my $letter = $self->letter;
 
diff --git a/sql/Pg-upgrade2/file_storage_type_letter.sql b/sql/Pg-upgrade2/file_storage_type_letter.sql
new file mode 100644
index 000000000..cf4c832bd
--- /dev/null
+++ b/sql/Pg-upgrade2/file_storage_type_letter.sql
@@ -0,0 +1,16 @@
+-- @tag: file_storage_type_letter
+-- @description: Letter als valid_type für Filemanagement
+-- @depends: shopimages
+
+ALTER TABLE files
+  DROP CONSTRAINT valid_type;
+ALTER TABLE files
+  ADD  CONSTRAINT valid_type CHECK (
+             (object_type = 'credit_note'     ) OR (object_type = 'invoice'                 ) OR (object_type = 'sales_order'       )
+          OR (object_type = 'sales_quotation' ) OR (object_type = 'sales_delivery_order'    ) OR (object_type = 'request_quotation' )
+          OR (object_type = 'purchase_order'  ) OR (object_type = 'purchase_delivery_order' ) OR (object_type = 'purchase_invoice'  )
+          OR (object_type = 'vendor'          ) OR (object_type = 'customer'                ) OR (object_type = 'part'              )
+          OR (object_type = 'gl_transaction'  ) OR (object_type = 'dunning'                 ) OR (object_type = 'dunning1'          )
+          OR (object_type = 'dunning2'        ) OR (object_type = 'dunning3'                ) OR (object_type = 'draft'             )
+          OR (object_type = 'statement'       ) OR (object_type = 'shop_image'              ) OR (object_type = 'letter'            )
+  );
diff --git a/templates/webpages/letter/edit.html b/templates/webpages/letter/edit.html
index e1c05ebb3..0c4e4e415 100644
--- a/templates/webpages/letter/edit.html
+++ b/templates/webpages/letter/edit.html
@@ -9,7 +9,7 @@
 <form action='controller.pl' method='POST' id='form'>
   <input type="hidden" name="letter.id" value="[% letter.id | html %]">
   <input type="hidden" name="draft.id" value="[% draft.id | html %]">
-  <input type="hidden" name="type" value="[% type | html %]">
+  <input type="hidden" name="type" value="[% FORM.type | html %]">
   [% L.hidden_tag('is_sales', SELF.is_sales) %]
 
   [%- INCLUDE 'common/flash.html' %]
@@ -20,6 +20,9 @@
 [%- IF letter.id %]
  [%- IF INSTANCE_CONF.get_webdav %]
      <li><a href="#ui-tabs-webdav">[% LxERP.t8('WebDAV') %]</a></li>
+ [%- END %]
+ [%- IF INSTANCE_CONF.get_doc_storage %]
+      <li><a href="controller.pl?action=File/list&file_type=document&object_type=[% HTML.escape(FORM.type) %]&object_id=[% HTML.url(letter.id) %]">[% 'Documents' | $T8 %]</a></li>
  [%- END %]
     <li><a href="controller.pl?action=RecordLinks/ajax_list&object_model=Letter&object_id=[% HTML.url(letter.id) %]">[% LxERP.t8("Linked Records") %]</a></li>
 [%- END %]
