SL::File: get_all_versions mit dbfile als Parameter gefixed
[kivitendo-erp.git] / SL / Helper / CreatePDF.pm
index f6e07aa..06fb819 100644 (file)
@@ -11,15 +11,17 @@ use File::Temp  ();
 use File::Copy qw(move);
 use List::MoreUtils qw(uniq);
 use List::Util qw(first);
+use Scalar::Util qw(blessed);
 use String::ShellQuote ();
 
-use SL::Form;
 use SL::Common;
 use SL::DB::Language;
 use SL::DB::Printer;
 use SL::MoreCommon;
+use SL::System::Process;
 use SL::Template;
 use SL::Template::LaTeX;
+use SL::X;
 
 use Exporter 'import';
 our @EXPORT_OK = qw(create_pdf merge_pdfs find_template);
@@ -40,15 +42,22 @@ sub create_pdf {
 sub create_parsed_file {
   my ($class, %params) = @_;
 
-  my $userspath      = $::lx_office_conf{paths}->{userspath};
+  my $keep_temp_files = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files};
+  my $userspath       = SL::System::Process::exe_dir() . "/" . $::lx_office_conf{paths}->{userspath};
+  my $temp_dir        = File::Temp->newdir(
+    "kivitendo-print-XXXXXX",
+    DIR     => $userspath,
+    CLEANUP => !$keep_temp_files,
+  );
+
   my $vars           = $params{variables} || {};
   my $form           = Form->new('');
   $form->{$_}        = $vars->{$_} for keys %{$vars};
   $form->{format}    = lc($params{format} || 'pdf');
-  $form->{cwd}       = getcwd();
+  $form->{cwd}       = SL::System::Process::exe_dir();
   $form->{templates} = $::instance_conf->get_templates;
   $form->{IN}        = $params{template};
-  $form->{tmpdir}    = $form->{cwd} . '/' . $userspath;
+  $form->{tmpdir}    = $temp_dir->dirname;
   my $tmpdir         = $form->{tmpdir};
   my ($suffix)       = $params{template} =~ m{\.(.+)};
 
@@ -56,20 +65,30 @@ sub create_parsed_file {
     'kivitendo-printXXXXXX',
     SUFFIX => ".${suffix}",
     DIR    => $form->{tmpdir},
-    UNLINK =>
-      ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1,
+    UNLINK => !$keep_temp_files,
   );
 
   $form->{tmpfile} = $tmpfile;
   (undef, undef, $form->{template_meta}{tmpfile}) = File::Spec->splitpath($tmpfile);
 
-  my $parser = SL::Template::create(
-    type => ($params{template_type} || 'LaTeX'),
+  my %driver_options;
+  eval {
+    %driver_options = _maybe_attach_zugferd_data($params{record});
+  };
+
+  if (my $e = SL::X::ZUGFeRDValidation->caught) {
+    $form->cleanup;
+    die $e->message;
+  }
+
+  my $parser               = SL::Template::create(
+    type                   => ($params{template_type} || 'LaTeX'),
     source                 => $form->{IN},
     form                   => $form,
     myconfig               => \%::myconfig,
     userspath              => $tmpdir,
     variable_content_types => $params{variable_content_types},
+    %driver_options,
   );
 
   my $result = $parser->parse($temp_fh);
@@ -89,7 +108,7 @@ sub create_parsed_file {
   my ($volume, $directory, $file_name) = File::Spec->splitpath($form->{tmpfile});
   my $full_file_name                   = File::Spec->catfile($tmpdir, $file_name);
   if (($params{return} || 'content') eq 'file_name') {
-    my $new_name = File::Spec->catfile($tmpdir, 'keep-' . $form->{tmpfile});
+    my $new_name = File::Spec->catfile($userspath, 'keep-' . $form->{tmpfile});
     rename $full_file_name, $new_name;
 
     $form->cleanup;
@@ -128,8 +147,7 @@ sub merge_pdfs {
 
   if ($params{inp_content}) {
     return $params{inp_content} if $filecount == 0 && !$params{out_path};
-  }
-  elsif ($params{out_path}) {
+  } elsif ($params{out_path}) {
     return 0 if $filecount == 0;
     if ($filecount == 1) {
       if (!rename($params{file_names}->[0], $params{out_path})) {
@@ -138,8 +156,7 @@ sub merge_pdfs {
       }
       return 1;
     }
-  }
-  else {
+  } else {
     return '' if $filecount == 0;
     return scalar(File::Slurp::read_file($params{file_names}->[0])) if $filecount == 1;
   }
@@ -167,30 +184,18 @@ sub merge_pdfs {
       'kivitendo-contentXXXXXX',
       SUFFIX => '.pdf',
       DIR    => $::lx_office_conf{paths}->{userspath},
-      UNLINK => (
-        $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files}
-      )
-        ? 0
-        : 1,
+      UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1,
     );
     binmode $temp_fh;
     print $temp_fh $params{inp_content};
     close $temp_fh;
     $input_names = $inp_name . ' ';
-    $hasodd =
-      ($params{bothsided} && __PACKAGE__->has_odd_pages($inp_name)
-       ? 1
-       : 0
-     );
+    $hasodd = $params{bothsided} && __PACKAGE__->has_odd_pages($inp_name);
   }
   foreach (@{ $params{file_names} }) {
     $input_names .= $emptypage . ' ' if $hasodd;
     $input_names .= String::ShellQuote::shell_quote($_) . ' ';
-    $hasodd =
-      ($params{bothsided} && __PACKAGE__->has_odd_pages($_)
-       ? 1
-       : 0
-     );
+    $hasodd = $params{bothsided} && __PACKAGE__->has_odd_pages($_);
   }
   my $exe = $::lx_office_conf{applications}->{ghostscript} || 'gs';
   my $output =
@@ -215,15 +220,14 @@ sub find_template {
 
   $params{name} or croak "Missing parameter 'name'";
 
-  my $path      = $::instance_conf->get_templates;
-  my $extension = $params{extension} || "tex";
+  my $path                 = $::instance_conf->get_templates;
+  my $extension            = $params{extension} || "tex";
   my ($printer, $language) = ('', '');
 
   if ($params{printer} || $params{printer_id}) {
     if ($params{printer} && !ref $params{printer}) {
       $printer = '_' . $params{printer};
-    }
-    else {
+    } else {
       $printer = $params{printer} || SL::DB::Printer->new(id => $params{printer_id})->load;
       $printer = $printer->template_code ? '_' . $printer->template_code : '';
     }
@@ -232,8 +236,7 @@ sub find_template {
   if ($params{language} || $params{language_id}) {
     if ($params{language} && !ref $params{language}) {
       $language = '_' . $params{language};
-    }
-    else {
+    } else {
       $language = $params{language} || SL::DB::Language->new(id => $params{language_id})->load;
       $language = $language->template_code ? '_' . $language->template_code : '';
     }
@@ -247,11 +250,10 @@ sub find_template {
   );
 
   if ($params{email}) {
-    unshift @template_files,
-      (
+    unshift @template_files, (
       $params{name} . "_email${language}${printer}",
       $params{name} . "_email${language}",
-      );
+    );
   }
 
   @template_files = map { "${_}.${extension}" } uniq grep { $_ } @template_files;
@@ -261,6 +263,35 @@ sub find_template {
   return wantarray ? ($template, @template_files) : $template;
 }
 
+sub _maybe_attach_zugferd_data {
+  my ($record) = @_;
+
+  return if !blessed($record)
+    || !$record->can('customer')
+    || !$record->customer
+    || !$record->can('create_pdf_a_print_options')
+    || !$record->can('create_zugferd_data')
+    || !$record->customer->create_zugferd_invoices_for_this_customer;
+
+  my $xmlfile = File::Temp->new;
+  $xmlfile->print($record->create_zugferd_data);
+  $xmlfile->close;
+
+  my %driver_options = (
+    pdf_a           => $record->create_pdf_a_print_options(zugferd_xmp_data => $record->create_zugferd_xmp_data),
+    pdf_attachments => [
+      { source       => $xmlfile,
+        name         => 'factur-x.xml',
+        description  => $::locale->text('Factur-X/ZUGFeRD invoice'),
+        relationship => '/Alternative',
+        mime_type    => 'text/xml',
+      }
+    ],
+  );
+
+  return %driver_options;
+}
+
 1;
 __END__