Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision aa36021a

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

  • ID aa36021ace7b4075af6d2d815e0e208b7227d9c4
  • Vorgänger 9af3ce1c
  • Nachfolger 048a4ee5

Auftrags-Controller: Drucken und E-Mail

Unterschiede anzeigen:

SL/Controller/Order.pm
5 5

  
6 6
use SL::Helper::Flash;
7 7
use SL::Presenter;
8
use SL::Locale::String;
9
use SL::SessionFile::Random;
8 10
use SL::PriceSource;
11
use SL::Form;
9 12

  
10 13
use SL::DB::Order;
11 14
use SL::DB::Customer;
......
17 20
use SL::DB::Unit;
18 21

  
19 22
use SL::Helper::DateTime;
23
use SL::Helper::CreatePDF qw(:all);
20 24

  
21 25
use List::Util qw(max first);
22 26
use List::MoreUtils qw(none pairwise);
27
use English qw(-no_match_vars);
28
use File::Spec;
23 29

  
24 30
use Rose::Object::MakeMethods::Generic
25 31
(
......
31 37
__PACKAGE__->run_before('_check_auth');
32 38

  
33 39
__PACKAGE__->run_before('_recalc',
34
                        only => [ qw(edit update save) ]);
40
                        only => [ qw(edit update save create_pdf send_email) ]);
35 41

  
36 42
__PACKAGE__->run_before('_get_unalterable_data',
37 43
                        only => [ qw(save save_and_delivery_order create_pdf send_email) ]);
......
120 126
  $self->redirect_to(@redirect_params);
121 127
}
122 128

  
129
sub action_create_pdf {
130
  my ($self) = @_;
131

  
132
  my $pdf;
133
  my @errors = _create_pdf($self->order, \$pdf);
134
  if (scalar @errors) {
135
    return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
136
  }
137

  
138
  my $sfile = SL::SessionFile::Random->new(mode => "w");
139
  $sfile->fh->print($pdf);
140
  $sfile->fh->close;
141

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

  
145
  my $form = Form->new;
146
  $form->{ordnumber} = $self->order->ordnumber;
147
  $form->{formname}  = $self->type;
148
  $form->{type}      = $self->type;
149
  $form->{language}  = 'de';
150
  $form->{format}    = 'pdf';
151

  
152
  my $pdf_filename = $form->generate_attachment_filename();
153

  
154
  $self->js
155
    ->run('download_pdf', $pdf_filename, $key)
156
    ->flash('info', t8('The PDF has been created'))->render($self);
157
}
158

  
159
sub action_download_pdf {
160
  my ($self) = @_;
161

  
162
  my $key = $::form->{key};
163
  my $tmp_filename = $::auth->get_session_value("Order::create_pdf-${key}");
164
  return $self->send_file(
165
    $tmp_filename,
166
    type => 'application/pdf',
167
    name => $::form->{pdf_filename},
168
  );
169
}
170

  
171
sub action_show_email_dialog {
172
  my ($self) = @_;
173

  
174
  my $cv_method = $self->cv;
175

  
176
  if (!$self->order->$cv_method) {
177
    return $self->js->flash('error', $self->cv eq 'customer' ? t8('Cannot send E-mail without customer given') : t8('Cannot send E-mail without vendor given'))
178
                    ->render($self);
179
  }
180

  
181
  $self->{email}->{to}   = $self->order->contact->cp_email if $self->order->contact;
182
  $self->{email}->{to} ||= $self->order->$cv_method->email;
183
  $self->{email}->{cc}   = $self->order->$cv_method->cc;
184
  $self->{email}->{bcc}  = join ', ', grep $_, $self->order->$cv_method->bcc, SL::DB::Default->get->global_bcc;
185
  # Todo: get addresses from shipto, if any
186

  
187
  my $form = Form->new;
188
  $form->{ordnumber} = $self->order->ordnumber;
189
  $form->{formname}  = $self->type;
190
  $form->{type}      = $self->type;
191
  $form->{language} = 'de';
192
  $form->{format}   = 'pdf';
193

  
194
  $self->{email}->{subject}             = $form->generate_email_subject();
195
  $self->{email}->{attachment_filename} = $form->generate_attachment_filename();
196
  $self->{email}->{message}             = $form->create_email_signature();
197

  
198
  my $dialog_html = $self->render('order/tabs/_email_dialog', { output => 0 });
199
  $self->js
200
      ->run('show_email_dialog', $dialog_html)
201
      ->reinit_widgets
202
      ->render($self);
203
}
204

  
205
# Todo: handling error messages: flash is not displayed in dialog, but in the main form
206
sub action_send_email {
207
  my ($self) = @_;
208

  
209
  my $mail      = Mailer->new;
210
  $mail->{from} = qq|"$::myconfig{name}" <$::myconfig{email}>|;
211
  $mail->{$_}   = $::form->{email}->{$_} for qw(to cc bcc subject message);
212

  
213
  my $pdf;
214
  my @errors = _create_pdf($self->order, \$pdf, {media => 'email'});
215
  if (scalar @errors) {
216
    return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
217
  }
218

  
219
  $mail->{attachments} = [{ "content" => $pdf,
220
                            "name"    => $::form->{email}->{attachment_filename} }];
221

  
222
  if (my $err = $mail->send) {
223
    return $self->js->flash('error', t8('Sending E-mail: ') . $err)
224
                    ->render($self);
225
  }
226

  
227
  # internal notes
228
  my $intnotes = $self->order->intnotes;
229
  $intnotes   .= "\n\n" if $self->order->intnotes;
230
  $intnotes   .= t8('[email]')                                                                                        . "\n";
231
  $intnotes   .= t8('Date')       . ": " . $::locale->format_date_object(DateTime->now_local, precision => 'seconds') . "\n";
232
  $intnotes   .= t8('To (email)') . ": " . $mail->{to}                                                                . "\n";
233
  $intnotes   .= t8('Cc')         . ": " . $mail->{cc}                                                                . "\n"    if $mail->{cc};
234
  $intnotes   .= t8('Bcc')        . ": " . $mail->{bcc}                                                               . "\n"    if $mail->{bcc};
235
  $intnotes   .= t8('Subject')    . ": " . $mail->{subject}                                                           . "\n\n";
236
  $intnotes   .= t8('Message')    . ": " . $mail->{message};
237

  
238
  $self->js
239
      ->val('#order_intnotes', $intnotes)
240
      ->run('close_email_dialog')
241
      ->render($self);
242
}
243

  
244

  
123 245
sub action_customer_vendor_changed {
124 246
  my ($self) = @_;
125 247

  
......
452 574
  $::request->{layout}->use_javascript("${_}.js")  for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
453 575
}
454 576

  
577
sub _create_pdf {
578
  my ($order, $pdf_ref, $params) = @_;
579

  
580
  my $print_form = Form->new('');
581
  $print_form->{type}     = $order->type;
582
  $print_form->{formname} = $order->type;
583
  $print_form->{format}   = $params->{format} || 'pdf',
584
  $print_form->{media}    = $params->{media}  || 'file';
585

  
586
  $order->flatten_to_form($print_form, format_amounts => 1);
587
  # flatten_to_form sets payment_terms from customer/vendor - we do not want that here
588
  delete $print_form->{payment_terms} if !$print_form->{payment_id};
589

  
590
  my @errors = ();
591
  $print_form->throw_on_error(sub {
592
    eval {
593
      $print_form->prepare_for_printing;
594

  
595
      $$pdf_ref = SL::Helper::CreatePDF->create_pdf(
596
        template  => SL::Helper::CreatePDF->find_template(name => $print_form->{formname}),
597
        variables => $print_form,
598
        variable_content_types => {
599
          longdescription => 'html',
600
          partnotes       => 'html',
601
          notes           => 'html',
602
        },
603
      );
604
      1;
605
    } || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->getMessage : $EVAL_ERROR;
606
  });
607

  
608
  return @errors;
609
}
610

  
455 611
sub _sales_order_type {
456 612
  'sales_order';
457 613
}

Auch abrufbar als: Unified diff