X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FBackgroundJob%2FMassRecordCreationAndPrinting.pm;h=e134807e3316b69bb27d9d54d2acfbd3822b6c53;hb=0f37ddcd492b271a6b316893eda1544931c2a0ad;hp=bd878384974408b5d779d7bbefe92f96c0eac066;hpb=ee75e5986c75b6cf9886bf315e83d05ae210add0;p=kivitendo-erp.git diff --git a/SL/BackgroundJob/MassRecordCreationAndPrinting.pm b/SL/BackgroundJob/MassRecordCreationAndPrinting.pm index bd8783849..e134807e3 100644 --- a/SL/BackgroundJob/MassRecordCreationAndPrinting.pm +++ b/SL/BackgroundJob/MassRecordCreationAndPrinting.pm @@ -12,12 +12,16 @@ use SL::DB::Printer; use SL::SessionFile; use SL::Template; use SL::Locale::String qw(t8); +use SL::Helper::MassPrintCreatePDF qw(:all); +use SL::Helper::CreatePDF qw(:all); +use SL::Helper::File qw(store_pdf append_general_pdf_attachments); use SL::Webdav; use constant WAITING_FOR_EXECUTION => 0; use constant CONVERTING_DELIVERY_ORDERS => 1; use constant PRINTING_INVOICES => 2; use constant DONE => 3; + # Data format: # my $data = { # record_ids => [ 123, 124, 127, ], @@ -50,7 +54,7 @@ sub create_invoices { my $sales_delivery_order = SL::DB::DeliveryOrder->new(id => $delivery_order_id)->load; $number = $sales_delivery_order->donumber; - if (!$db->do_transaction(sub { + if (!$db->with_transaction(sub { $invoice = $sales_delivery_order->convert_to_invoice(sub { $data->{transdate} ? ('attributes' => { transdate => $data->{transdate} }) : undef }->() ) || die $db->error; 1; @@ -80,42 +84,36 @@ sub convert_invoices_to_pdf { my $db = $job_obj->db; $job_obj->set_data(status => PRINTING_INVOICES())->save; + my $data = $job_obj->data_as_hash; - require SL::Controller::MassInvoiceCreatePrint; - - my $printer_id = $job_obj->data_as_hash->{printer_id}; - my $ctrl = SL::Controller::MassInvoiceCreatePrint->new; + my $printer_id = $data->{printer_id}; + if ( $data->{media} ne 'printer' ) { + undef $printer_id; + $data->{media} = 'file'; + } my %variables = ( type => 'invoice', formname => 'invoice', format => 'pdf', media => $printer_id ? 'printer' : 'file', + printer_id => $printer_id, ); my @pdf_file_names; foreach my $invoice (@{ $self->{invoices} }) { - my $data = $job_obj->data_as_hash; eval { - my %create_params = ( - template => $ctrl->find_template(name => 'invoice', printer_id => $printer_id), - variables => Form->new(''), + my %params = ( + variables => \%variables, return => 'file_name', - variable_content_types => { longdescription => 'html', - partnotes => 'html', - notes => 'html',} + document => $invoice, ); + push @pdf_file_names, $self->create_massprint_pdf(%params); + $data->{num_printed}++; - - $create_params{variables}->{$_} = $variables{$_} for keys %variables; - - $invoice->flatten_to_form($create_params{variables}, format_amounts => 1); - $create_params{variables}->prepare_for_printing; - - push @pdf_file_names, $ctrl->create_pdf(%create_params); - + # OLD WebDAV Code, may be deleted: # copy file to webdav folder if ($::instance_conf->get_webdav_documents) { my $webdav = SL::Webdav->new( @@ -134,8 +132,6 @@ sub convert_invoices_to_pdf { } } - $data->{num_printed}++; - 1; } or do { @@ -145,58 +141,7 @@ sub convert_invoices_to_pdf { $job_obj->update_attributes(data_as_hash => $data); } - if (@pdf_file_names) { - my $data = $job_obj->data_as_hash; - - eval { - $self->{merged_pdf} = $ctrl->merge_pdfs(file_names => \@pdf_file_names); - unlink @pdf_file_names; - - if (!$printer_id) { - my $file_name = 'mass_invoice' . $job_obj->id . '.pdf'; - my $sfile = SL::SessionFile->new($file_name, mode => 'w', session_id => $data->{session_id}); - $sfile->fh->print($self->{merged_pdf}); - $sfile->fh->close; - - $data->{pdf_file_name} = $file_name; - } - - 1; - - } or do { - push @{ $data->{print_errors} }, { message => $@ }; - }; - - $job_obj->update_attributes(data_as_hash => $data); - } -} - -sub print_pdfs { - my ($self) = @_; - - my $job_obj = $self->{job_obj}; - my $data = $job_obj->data_as_hash; - my $printer_id = $data->{printer_id}; - my $copy_printer_id = $data->{copy_printer_id}; - - return if !$printer_id; - - my $out; - - 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; - } - + $self->merge_massprint_pdf(file_names => \@pdf_file_names, type => 'invoice' ) if scalar(@pdf_file_names) > 0; } sub run {