From: Bernd Bleßmann Date: Thu, 17 Sep 2015 10:29:44 +0000 (+0200) Subject: Auftrags-Controller: einfaches Drucken (nur PDF als Download). X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=a74845b3f77065c24ab39e7cf0cae01523ed1de7;p=kivitendo-erp.git Auftrags-Controller: einfaches Drucken (nur PDF als Download). --- diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 7728f6766..8b4733223 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -6,6 +6,9 @@ use parent qw(SL::Controller::Base); use SL::Helper::Flash; use SL::ClientJS; use SL::Presenter; +use SL::Locale::String; +use SL::SessionFile::Random; +use SL::Form; use SL::DB::Order; use SL::DB::Customer; @@ -17,9 +20,11 @@ use SL::DB::Default; use SL::DB::Unit; use SL::Helper::DateTime; +use SL::Helper::CreatePDF qw(:all); use List::Util qw(max first); use List::MoreUtils qw(none pairwise); +use English qw(-no_match_vars); use Rose::Object::MakeMethods::Generic ( @@ -31,7 +36,7 @@ use Rose::Object::MakeMethods::Generic __PACKAGE__->run_before('_check_auth'); __PACKAGE__->run_before('_recalc', - only => [ qw(edit update save) ]); + only => [ qw(edit update save create_pdf) ]); # @@ -99,6 +104,57 @@ sub action_save { $self->redirect_to(@redirect_params); } +sub action_create_pdf { + my ($self) = @_; + + my $print_form = Form->new(''); + $print_form->{type} = 'sales_order'; + $print_form->{formname} = 'sales_order', + $print_form->{format} = 'pdf', + $print_form->{media} = 'file'; + + $self->order->flatten_to_form($print_form, format_amounts => 1); + + my $pdf; + my @errors; + $print_form->throw_on_error(sub { + eval { + $print_form->prepare_for_printing; + + $pdf = SL::Helper::CreatePDF->create_pdf( + template => SL::Helper::CreatePDF->find_template(name => $print_form->{formname}), + variables => $print_form, + ); + 1; + } || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->getMessage : $EVAL_ERROR; + }); + + if (scalar @errors) { + return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self); + } + + my $sfile = SL::SessionFile::Random->new(mode => "w"); + $sfile->fh->print($pdf); + $sfile->fh->close; + + my $tmp_filename = $sfile->file_name; + my $pdf_filename = t8('Sales Order') . '_' . $self->order->ordnumber . '.pdf'; + + $self->js + ->run('download_pdf', $tmp_filename, $pdf_filename) + ->flash('info', t8('The PDF has been created'))->render($self); +} + +sub action_download_pdf { + my ($self) = @_; + + return $self->send_file( + $::form->{tmp_filename}, + type => 'application/pdf', + name => $::form->{pdf_filename}, + ); +} + sub action_customer_vendor_changed { my ($self) = @_; diff --git a/templates/webpages/order/form.html b/templates/webpages/order/form.html index 2fcf2bcc4..a5376e3c7 100644 --- a/templates/webpages/order/form.html +++ b/templates/webpages/order/form.html @@ -31,6 +31,7 @@ [% L.hidden_tag('action', 'Order/dispatch') %] [% L.button_tag('save()', LxERP.t8('Save')) %] + [% L.button_tag('create_pdf()', LxERP.t8('Create PDF')) %] @@ -44,4 +45,19 @@ function save() { $.post("controller.pl", data, kivi.eval_json_result); } +function create_pdf() { + var data = $('#order_form').serialize(); + data += '&action=Order/create_pdf'; + + $.post("controller.pl", data, kivi.eval_json_result); +} + +function download_pdf(tmp_filename, pdf_filename) { + var data = 'action=Order/download_pdf'; + data += '&type=' + $('#type').val(); + data += '&tmp_filename=' + tmp_filename; + data += '&pdf_filename=' + pdf_filename; + $.download("controller.pl", data); +} +