Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b1b3cdeb

Von Bernd Bleßmann vor fast 9 Jahren hinzugefügt

  • ID b1b3cdebde020c32eb5d0e39e9e6139e1508b9f7
  • Vorgänger 46d3bb6e
  • Nachfolger b2242cae

Auftrags-Controller: Druck-Möglichkeit mit Options

Todos/Anmerkungen
- im Moment werden nur PDF/Bildschirm und PDF/Drucker unterstützt
- Sprache kann die Vorlage steuern, aber nicht das Hinzufügen von übersetzten
Artikeln
- es kann sicher noch mehr ausgelagert werden, entweder in den
PrintOptions-Helper oder in einen PrintOptions-Controller, der
dann auch noch mehr machen könnte
- E-Mail ist nicht berücksichtigt bzw. wird im Controller getrennt behandelt

Unterschiede anzeigen:

SL/Controller/Order.pm
10 10
use SL::PriceSource;
11 11
use SL::Form;
12 12
use SL::Webdav;
13
use SL::Template;
13 14

  
14 15
use SL::DB::Order;
15 16
use SL::DB::Customer;
......
21 22
use SL::DB::Unit;
22 23
use SL::DB::Price;
23 24
use SL::DB::Part;
25
use SL::DB::Printer;
26
use SL::DB::Language;
24 27

  
25 28
use SL::Helper::DateTime;
26 29
use SL::Helper::CreatePDF qw(:all);
30
use SL::Helper::PrintOptions;
27 31

  
28 32
use SL::Controller::Helper::GetModels;
29 33

  
......
43 47
__PACKAGE__->run_before('_check_auth');
44 48

  
45 49
__PACKAGE__->run_before('_recalc',
46
                        only => [ qw(update save save_and_delivery_order create_pdf send_email) ]);
50
                        only => [ qw(update save save_and_delivery_order print create_pdf send_email) ]);
47 51

  
48 52
__PACKAGE__->run_before('_get_unalterable_data',
49
                        only => [ qw(save save_and_delivery_order create_pdf send_email) ]);
53
                        only => [ qw(save save_and_delivery_order print create_pdf send_email) ]);
50 54

  
51 55
#
52 56
# actions
......
135 139
  $self->redirect_to(@redirect_params);
136 140
}
137 141

  
138
sub action_create_pdf {
142
sub action_print {
139 143
  my ($self) = @_;
140 144

  
141
  my $pdf;
142
  my @errors = _create_pdf($self->order, \$pdf);
143
  if (scalar @errors) {
144
    return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
145
  my $format      = $::form->{print_options}->{format};
146
  my $media       = $::form->{print_options}->{media};
147
  my $formname    = $::form->{print_options}->{formname};
148
  my $copies      = $::form->{print_options}->{copies};
149
  my $groupitems  = $::form->{print_options}->{groupitems};
150

  
151
  # only pdf by now
152
  if (none { $format eq $_ } qw(pdf)) {
153
    return $self->js->flash('error', t8('Format \'#1\' is not supported yet/anymore.', $format))->render;
145 154
  }
146 155

  
147
  my $sfile = SL::SessionFile::Random->new(mode => "w");
148
  $sfile->fh->print($pdf);
149
  $sfile->fh->close;
156
  # only screen or printer by now
157
  if (none { $media eq $_ } qw(screen printer)) {
158
    return $self->js->flash('error', t8('Media \'#1\' is not supported yet/anymore.', $media))->render;
159
  }
150 160

  
151
  my $key = join('_', Time::HiRes::gettimeofday(), int rand 1000000000000);
152
  $::auth->set_session_value("Order::create_pdf-${key}" => $sfile->file_name);
161
  my $language;
162
  $language = SL::DB::Language->new(id => $::form->{print_options}->{language_id})->load if $::form->{print_options}->{language_id};
153 163

  
154 164
  my $form = Form->new;
155 165
  $form->{ordnumber} = $self->order->ordnumber;
156
  $form->{formname}  = $self->type;
157 166
  $form->{type}      = $self->type;
158
  $form->{language}  = 'de';
159
  $form->{format}    = 'pdf';
167
  $form->{format}    = $format;
168
  $form->{formname}  = $formname;
169
  $form->{language}  = '_' . $language->template_code if $language;
170
  my $pdf_filename   = $form->generate_attachment_filename();
171

  
172
  my $pdf;
173
  my @errors = _create_pdf($self->order, \$pdf, { format     => $format,
174
                                                  formname   => $formname,
175
                                                  language   => $language,
176
                                                  groupitems => $groupitems });
177
  if (scalar @errors) {
178
    return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render;
179
  }
180

  
181
  if ($media eq 'screen') {
182
    # screen/download
183
    my $sfile = SL::SessionFile::Random->new(mode => "w");
184
    $sfile->fh->print($pdf);
185
    $sfile->fh->close;
186

  
187
    my $key = join('_', Time::HiRes::gettimeofday(), int rand 1000000000000);
188
    $::auth->set_session_value("Order::create_pdf-${key}" => $sfile->file_name);
189

  
190
    $self->js
191
    ->run('download_pdf', $pdf_filename, $key)
192
    ->flash('info', t8('The PDF has been created'));
193

  
194
  } elsif ($media eq 'printer') {
195
    # printer
196
    my $printer_id = $::form->{print_options}->{printer_id};
197
    my $printer;
198
    $printer = SL::DB::Printer->new(id => $printer_id)->load if $printer_id;
199
    if (!$printer) {
200
      return $self->js->flash('error', t8('Printer not found.'))->render;
201
    }
202

  
203
    my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command);
160 204

  
161
  my $pdf_filename = $form->generate_attachment_filename();
205
    for my $i (1 .. $copies) {
206
      open my $out, '|-', $command or die $!;
207
      binmode $out;
208
      print $out $pdf;
209
      close $out;
210
    }
211

  
212
    $self->js->flash('info', t8('The PDF has been printed'));
213
  }
162 214

  
163 215
  # copy file to webdav folder
164 216
  if ($self->order->ordnumber && $::instance_conf->get_webdav_documents) {
......
178 230
    }
179 231
  }
180 232

  
181
  $self->js
182
    ->run('download_pdf', $pdf_filename, $key)
183
    ->flash('info', t8('The PDF has been created'))->render($self);
233
  $self->js->render;
184 234
}
185 235

  
186 236
sub action_download_pdf {
......
853 903

  
854 904
  $self->{current_employee_id} = SL::DB::Manager::Employee->current->id;
855 905

  
906
  my $print_form = Form->new('');
907
  $print_form->{type}      = $self->type;
908
  $print_form->{printers}  = SL::DB::Manager::Printer->get_all_sorted;
909
  $print_form->{languages} = SL::DB::Manager::Language->get_all_sorted;
910
  $self->{print_options}   = SL::Helper::PrintOptions->get_print_options(
911
    form => $print_form,
912
    options => {dialog_name_prefix => 'print_options.',
913
                show_headers       => 1,
914
                no_queue           => 1,
915
                no_postscript      => 1,
916
                no_opendocument    => 1,
917
                no_html            => 1},
918
  );
919

  
856 920
  foreach my $item (@{$self->order->orderitems}) {
857 921
    my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order);
858 922
    $item->active_price_source(   $price_source->price_from_source(   $item->active_price_source   ));
......
878 942
sub _create_pdf {
879 943
  my ($order, $pdf_ref, $params) = @_;
880 944

  
945
  my @errors = ();
946

  
881 947
  my $print_form = Form->new('');
882
  $print_form->{type}     = $order->type;
883
  $print_form->{formname} = $order->type;
884
  $print_form->{format}   = $params->{format} || 'pdf',
885
  $print_form->{media}    = $params->{media}  || 'file';
948
  $print_form->{type}        = $order->type;
949
  $print_form->{formname}    = $params->{formname} || $order->type;
950
  $print_form->{format}      = $params->{format}   || 'pdf';
951
  $print_form->{media}       = $params->{media}    || 'file';
952
  $print_form->{groupitems}  = $params->{groupitems};
953
  $print_form->{media}       = 'file'                             if $print_form->{media} eq 'screen';
954
  $print_form->{language}    = $params->{language}->template_code if $print_form->{language};
955
  $print_form->{language_id} = $params->{language}->id            if $print_form->{language};
886 956

  
887 957
  $order->flatten_to_form($print_form, format_amounts => 1);
888 958
  # flatten_to_form sets payment_terms from customer/vendor - we do not want that here
889 959
  delete $print_form->{payment_terms} if !$print_form->{payment_id};
890 960

  
891
  my @errors = ();
961
  # search for the template
962
  my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
963
    name        => $print_form->{formname},
964
    email       => $print_form->{media} eq 'email',
965
    language    => $params->{language},
966
    printer_id  => $print_form->{printer_id},  # todo
967
  );
968

  
969
  if (!defined $template_file) {
970
    push @errors, $::locale->text('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files);
971
  }
972

  
973
  return @errors if scalar @errors;
974

  
892 975
  $print_form->throw_on_error(sub {
893 976
    eval {
894 977
      $print_form->prepare_for_printing;
895 978

  
896 979
      $$pdf_ref = SL::Helper::CreatePDF->create_pdf(
897
        template  => SL::Helper::CreatePDF->find_template(name => $print_form->{formname}),
980
        template  => $template_file,
898 981
        variables => $print_form,
899 982
        variable_content_types => {
900 983
          longdescription => 'html',

Auch abrufbar als: Unified diff