Brieffunktion: Umstellung Druck auf Template Toolkit
authorMoritz Bunkus <m.bunkus@linet-services.de>
Wed, 20 Apr 2016 13:46:17 +0000 (15:46 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Wed, 20 Apr 2016 13:46:17 +0000 (15:46 +0200)
Damit wird auch alter Backendcode wie IS->get_customer() nicht mehr
benötigt.

SL/Controller/Letter.pm
SL/DB/Contact.pm
templates/print/RB/letter.tex

index bdef557..553fe0a 100644 (file)
@@ -178,37 +178,6 @@ sub action_print_letter {
   my $display_form = $::form->{display_form} || "display_form";
   my $letter       = $self->_update;
 
-  $self->export_letter_to_form($letter);
-  $::form->{formname} = "letter";
-  $::form->{type}     = "letter";
-  $::form->{format}   = "pdf";
-
-  my $language_saved      = $::form->{language_id};
-  my $greeting_saved      = $::form->{greeting};
-  my $cp_id_saved         = $::form->{cp_id};
-
-  $::form->{customer_id} = $self->letter->customer_id;
-  IS->customer_details(\%::myconfig, $::form);
-
-  if (!$cp_id_saved) {
-    # No contact was selected. Delete all contact variables because
-    # IS->customer_details() and IR->vendor_details() get the default
-    # contact anyway.
-    map({ delete($::form->{$_}); } grep(/^cp_/, keys(%{ $::form })));
-  }
-
-  $::form->{greeting} = $greeting_saved;
-  $::form->{language_id} = $language_saved;
-
-  if ($::form->{cp_id}) {
-    CT->get_contact(\%::myconfig, $::form);
-  }
-
-  $::form->{cp_contact_formal} = ($::form->{cp_greeting} ? "$::form->{cp_greeting} " : '') . ($::form->{cp_givenname} ? "$::form->{cp_givenname} " : '') . $::form->{cp_name};
-
-  $::form->get_employee_data('prefix' => 'employee', 'id' => $letter->{employee_id});
-  $::form->get_employee_data('prefix' => 'salesman', 'id' => $letter->{salesman_id});
-
   my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
     name        => 'letter',
     printer_id  => $::form->{printer_id},
@@ -221,20 +190,29 @@ sub action_print_letter {
     $::form->error($::locale->text('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files));
   }
 
-  my %create_params = (
-    template  => $template_file,
-    variables => $::form,
-    return    => 'file_name',
-    variable_content_types => {
-      body                 => 'html',
-    },
-  );
-
-  my $pdf_file_name;
+  my %result;
   eval {
-    $pdf_file_name          = SL::Helper::CreatePDF->create_pdf(%create_params);
+    %result = SL::Template::LaTeX->parse_and_create_pdf(
+      $template_file,
+      SELF          => $self,
+      FORM          => $::form,
+      letter        => $letter,
+      template_meta => {
+        formname  => 'letter',
+        language  => SL::DB::Language->new,
+        extension => 'pdf',
+        format    => $::form->{format},
+        media     => $::form->{media},
+        printer   => SL::DB::Manager::Printer->find_by_or_create(id => $::form->{printer_id} || undef),
+        today     => DateTime->today,
+      },
+    );
+
+    die $result{error} if $result{error};
 
-    $::form->{letternumber} = $self->letter->letternumber;
+    $::form->{type}         = 'letter';
+    $::form->{formname}     = 'letter';
+    $::form->{letternumber} = $letter->letternumber;
     my $attachment_name     = $::form->generate_attachment_filename;
 
     if ($::instance_conf->get_webdav_documents) {
@@ -242,11 +220,11 @@ sub action_print_letter {
         filename => $attachment_name,
         webdav   => SL::Webdav->new(
           type   => 'letter',
-          number => $self->letter->letternumber,
+          number => $letter->letternumber,
         ),
       );
 
-      $webdav_file->store(file => $pdf_file_name);
+      $webdav_file->store(file => $result{file_name});
     }
 
     # set some form defaults for printing webdav copy variables
@@ -255,23 +233,23 @@ sub action_print_letter {
       my $signature        = $::myconfig{signature};
       $mail->{$_}          = $params{email}->{$_} for qw(to cc subject message bcc);
       $mail->{from}        = qq|"$::myconfig{name}" <$::myconfig{email}>|;
-      $mail->{attachments} = [{ filename => $pdf_file_name,
+      $mail->{attachments} = [{ filename => $result{file_name},
                                 name     => $params{email}->{attachment_filename} }];
       $mail->{message}    .=  "\n-- \n$signature";
       $mail->{message}     =~ s/\r//g;
 
       $mail->send;
-      unlink $pdf_file_name;
+      unlink $result{file_name};
 
       flash_later('info', t8('The email has been sent.'));
-      $self->redirect_to(action => 'edit', 'letter.id' => $self->letter->id);
+      $self->redirect_to(action => 'edit', 'letter.id' => $letter->id);
 
       return 1;
     }
 
     if (!$::form->{printer_id} || $::form->{media} eq 'screen') {
-      $self->send_file($pdf_file_name, name => $attachment_name);
-      unlink $pdf_file_name;
+      $self->send_file($result{file_name}, name => $attachment_name);
+      unlink $result{file_name};
 
       return 1;
     }
@@ -279,16 +257,16 @@ sub action_print_letter {
     my $printer = SL::DB::Printer->new(id => $::form->{printer_id})->load;
     $printer->print_document(
       copies    => $::form->{copies},
-      file_name => $pdf_file_name,
+      file_name => $result{file_name},
     );
 
-    unlink $pdf_file_name;
+    unlink $result{file_name};
 
     flash_later('info', t8('The documents have been sent to the printer \'#1\'.', $printer->printer_description));
-    $self->redirect_to(action => 'edit', 'letter.id' => $self->letter->id, media => 'printer', printer_id => $::form->{printer_id});
+    $self->redirect_to(action => 'edit', 'letter.id' => $letter->id, media => 'printer', printer_id => $::form->{printer_id});
     1;
   } or do {
-    unlink $pdf_file_name if $pdf_file_name;
+    unlink $result{file_name} if $result{file_name};
     $::form->error(t8("Creating the PDF failed:") . " " . $@);
   };
 }
index 81b743f..395fe26 100644 (file)
@@ -45,4 +45,10 @@ sub full_name_dep {
     . join '', map { " ($_)" } grep $_, $self->cp_abteilung;
 }
 
+sub formal_greeting {
+  my ($self) = @_;
+  die 'not an accessor' if @_ > 1;
+  join ' ', grep $_, $self->cp_greeting, $self->cp_givenname, $self->cp_name;
+}
+
 1;
index 1092810..bf1eab5 100644 (file)
@@ -1,17 +1,22 @@
+% config: use-template-toolkit=1
+% config: tag-style=$( )$
+$( USE KiviLatex )$
+$( USE P )$
+$( SET customer = letter.customer )$
 \input{inheaders.tex}
-
+$( KiviLatex.required_packages_for_html )$
 
 % Variablen, die in settings verwendet werden
-\newcommand{\lxlangcode} {<%template_meta.language.template_code%>}
-\newcommand{\lxmedia} {<%media%>}
-\newcommand{\lxcurrency} {<%currency%>}
-\newcommand{\lxtitlebar} {<%titlebar%>}
+\newcommand{\lxlangcode}{$(template_meta.language.template_code)$}
+\newcommand{\lxmedia}{$(template_meta.media)$}
+\newcommand{\lxcurrency}{}
+\newcommand{\lxtitlebar}{}
 
 % settings: Einstellungen, Logo, Briefpapier, Kopfzeile, Fusszeile
 \input{insettings.tex}
 
 % laufende Kopfzeile:
-%\ourhead{}{}{<%subject%>}{<%letternumber%>}{<%date%>}
+%\ourhead{}{}{$( KiviLatex.filter(letter.subject) )$}{$( KiviLatex.filter(letter.letternumber) )$}{$( KiviLatex.filter(letter.date.to_kivitendo) )$}
 \ourhead{}{}{}{}{}
 
 \begin{document}
 
 \begin{minipage}{14cm}
 
-  <%name%>
+  $( KiviLatex.filter(customer.name) )$
 
-  <%contact_formal%>
+  $( KiviLatex.filter(letter.contact.formar_greeting) )$
 
-  <%street%>
+  $( KiviLatex.filter(customer.street) )$
 
-  <%zipcode%> <%city%>
+  $( KiviLatex.filter(customer.zipcode) )$ $( KiviLatex.filter(customer.city) )$
 
-  <%country%>
+  $( KiviLatex.filter(customer.country) )$
 
 \end{minipage}
 
 \vspace{2.5cm}
-\hfill <%date%>
+\hfill $( KiviLatex.filter(letter.date.to_kivitendo) )$
 
-<%if reference%>
-\textbf{\ihrzeichen : <%reference%>}
-<%end if%>
+$( IF letter.reference )$
+\textbf{\ihrzeichen : $( KiviLatex.filter(letter.reference) )$}
 
 \vspace{1cm}
+$( END )$
 
-<%if subject%>
-\textbf{\betreff : <%subject%>}
-<%end if%>
+$( IF letter.subject )$
+\textbf{\betreff : $( KiviLatex.filter(letter.subject) )$}
 
 \vspace{1cm}
+$( END )$
 
-  <%greeting%>
+$( KiviLatex.filter(letter.greeting) )$
 
 \vspace{0.5cm}
 
-  <%body%>
+$( KiviLatex.filter_html(letter.body) )$
 
 \vspace{0.5cm}
 
-  <%company_name%>
-
-\vspace*{0.5cm}
-
 \begin{minipage}{6cm}
 
-\textbf{<%employee_name%>}
+\textbf{$( KiviLatex.filter(letter.employee.name) )$}
 
 \end{minipage}
 \begin{minipage}{6cm}
 
-\textbf{<%salesman_name%>}
+\textbf{$( KiviLatex.filter(letter.salesman.name) )$}
 
 \end{minipage}