Part validate
[kivitendo-erp.git] / SL / BackgroundJob / MassRecordCreationAndPrinting.pm
index e14e131..16823dc 100644 (file)
@@ -30,6 +30,7 @@ use constant DONE                        => 3;
 #   conversion_errors  => [ { id => 124, number => 'A981723', message => "Stuff went boom" }, ],
 #   print_errors       => [ { id => 234, number => 'L87123123', message => "Printer is out of coffee" }, ],
 #   pdf_file_name      => 'qweqwe.pdf',
+#   session_id         => $::auth->get_session_id,
 # };
 
 sub create_invoices {
@@ -45,17 +46,12 @@ sub create_invoices {
     my $data   = $job_obj->data_as_hash;
 
     eval {
-      my $invoice;
       my $sales_delivery_order = SL::DB::DeliveryOrder->new(id => $delivery_order_id)->load;
       $number                  = $sales_delivery_order->donumber;
+      my %conversion_params    = $data->{transdate} ? ('attributes' => { transdate => $data->{transdate} }) : ();
+      my $invoice              = $sales_delivery_order->convert_to_invoice(%conversion_params);
 
-      if (!$db->do_transaction(sub {
-        $invoice = $sales_delivery_order->convert_to_invoice(sub { $data->{transdate} ? ('attributes' => { transdate => $data->{transdate} }) :
-                                                                         undef }->() ) || die $db->error;
-        1;
-      })) {
-        die $db->error;
-      }
+      die $db->error if !$invoice;
 
       $data->{num_created}++;
       push @{ $data->{invoice_ids} }, $invoice->id;
@@ -101,8 +97,13 @@ sub convert_invoices_to_pdf {
         template  => $ctrl->find_template(name => 'invoice', printer_id => $printer_id),
         variables => Form->new(''),
         return    => 'file_name',
+        variable_content_types => { longdescription => 'html',
+                                    partnotes       => 'html',
+                                    notes           => 'html',}
       );
 
+
+
       $create_params{variables}->{$_} = $variables{$_} for keys %variables;
 
       $invoice->flatten_to_form($create_params{variables}, format_amounts => 1);
@@ -148,7 +149,7 @@ sub convert_invoices_to_pdf {
 
       if (!$printer_id) {
         my $file_name = 'mass_invoice' . $job_obj->id . '.pdf';
-        my $sfile     = SL::SessionFile->new($file_name, mode => 'w');
+        my $sfile     = SL::SessionFile->new($file_name, mode => 'w', session_id => $data->{session_id});
         $sfile->fh->print($self->{merged_pdf});
         $sfile->fh->close;
 
@@ -179,16 +180,10 @@ sub print_pdfs {
 
   foreach  my $local_printer_id ($printer_id, $copy_printer_id) {
     next unless $local_printer_id;
-    my $printer = SL::DB::Printer->new(id => $local_printer_id)->load;
-    my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command);
-    if (!open $out, '|-', $command) {
-      push @{ $data->{print_errors} }, { message => $::locale->text('Could not execute printer command: #1', $!) };
-      $job_obj->update_attributes(data_as_hash => $data);
-      return;
-    }
-    binmode $out;
-    print $out $self->{merged_pdf};
-    close $out;
+    SL::DB::Printer
+      ->new(id => $local_printer_id)
+      ->load
+      ->print_document(content => $self->{merged_pdf});
   }
 
 }