Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision d83928f0

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

  • ID d83928f0d622fb97ef0b00706f5edf055863f801
  • Vorgänger 0f8e74b8
  • Nachfolger 13412f21

Auftrags-Controller: E-Mail-Dialog aus common verwenden.

Unterschiede anzeigen:

SL/Controller/Order.pm
24 24
use SL::Controller::Helper::GetModels;
25 25

  
26 26
use List::Util qw(first);
27
use List::UtilsBy qw(sort_by uniq_by);
27 28
use List::MoreUtils qw(none pairwise first_index);
28 29
use English qw(-no_match_vars);
29 30
use File::Spec;
31
use Cwd;
30 32

  
31 33
use Rose::Object::MakeMethods::Generic
32 34
(
......
254 256
                    ->render($self);
255 257
  }
256 258

  
257
  $self->{email}->{to}   = $self->order->contact->cp_email if $self->order->contact;
258
  $self->{email}->{to} ||= $self->order->$cv_method->email;
259
  $self->{email}->{cc}   = $self->order->$cv_method->cc;
260
  $self->{email}->{bcc}  = join ', ', grep $_, $self->order->$cv_method->bcc, SL::DB::Default->get->global_bcc;
259
  my $email_form;
260
  $email_form->{to}   = $self->order->contact->cp_email if $self->order->contact;
261
  $email_form->{to} ||= $self->order->$cv_method->email;
262
  $email_form->{cc}   = $self->order->$cv_method->cc;
263
  $email_form->{bcc}  = join ', ', grep $_, $self->order->$cv_method->bcc, SL::DB::Default->get->global_bcc;
261 264
  # Todo: get addresses from shipto, if any
262 265

  
263 266
  my $form = Form->new;
......
267 270
  $form->{language} = 'de';
268 271
  $form->{format}   = 'pdf';
269 272

  
270
  $self->{email}->{subject}             = $form->generate_email_subject();
271
  $self->{email}->{attachment_filename} = $form->generate_attachment_filename();
272
  $self->{email}->{message}             = $form->create_email_signature();
273
  $email_form->{subject}             = $form->generate_email_subject();
274
  $email_form->{attachment_filename} = $form->generate_attachment_filename();
275
  $email_form->{message}             = $form->generate_email_body();
276
  $email_form->{js_send_function}    = 'kivi.Order.send_email()';
277

  
278
  my %files = $self->_get_files_for_email_dialog();
279
  my $dialog_html = $self->render('common/_send_email_dialog', { output => 0 },
280
                                  email_form  => $email_form,
281
                                  show_bcc    => $::auth->assert('email_bcc', 'may fail'),
282
                                  FILES       => \%files,
283
                                  is_customer => $self->cv eq 'customer',
284
  );
273 285

  
274
  my $dialog_html = $self->render('order/tabs/_email_dialog', { output => 0 });
275 286
  $self->js
276 287
      ->run('kivi.Order.show_email_dialog', $dialog_html)
277 288
      ->reinit_widgets
......
284 295
sub action_send_email {
285 296
  my ($self) = @_;
286 297

  
287
  my $mail      = Mailer->new;
288
  $mail->{from} = qq|"$::myconfig{name}" <$::myconfig{email}>|;
289
  $mail->{$_}   = $::form->{email}->{$_} for qw(to cc bcc subject message);
298
  my $email_form  = delete $::form->{email_form};
299
  my %field_names = (to => 'email');
290 300

  
291
  my $pdf;
292
  my @errors = _create_pdf($self->order, \$pdf, {media => 'email'});
293
  if (scalar @errors) {
294
    return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
295
  }
301
  $::form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
296 302

  
297
  $mail->{attachments} = [{ "content" => $pdf,
298
                            "name"    => $::form->{email}->{attachment_filename} }];
303
  # for Form::cleanup which may be called in Form::send_email
304
  $::form->{cwd}    = getcwd();
305
  $::form->{tmpdir} = $::lx_office_conf{paths}->{userspath};
299 306

  
300
  if (my $err = $mail->send) {
301
    return $self->js->flash('error', t8('Sending E-mail: ') . $err)
302
                    ->render($self);
307
  $::form->{media}  = 'email';
308

  
309
  if (($::form->{attachment_policy} // '') eq 'normal') {
310
    my $language;
311
    $language = SL::DB::Language->new(id => $::form->{print_options}->{language_id})->load if $::form->{print_options}->{language_id};
312

  
313
    my $pdf;
314
    my @errors = _create_pdf($self->order, \$pdf, {media      => $::form->{media},
315
                                                   format     => $::form->{print_options}->{format},
316
                                                   formname   => $::form->{print_options}->{formname},
317
                                                   language   => $language,
318
                                                   groupitems => $::form->{print_options}->{groupitems}});
319
    if (scalar @errors) {
320
      return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
321
    }
322

  
323
    my $sfile = SL::SessionFile::Random->new(mode => "w");
324
    $sfile->fh->print($pdf);
325
    $sfile->fh->close;
326

  
327
    $::form->{tmpfile} = $sfile->file_name;
328
    $::form->{tmpdir}  = $sfile->get_path; # for Form::cleanup which may be called in Form::send_email
303 329
  }
304 330

  
331
  $::form->send_email(\%::myconfig, 'pdf');
332

  
305 333
  # internal notes
306 334
  my $intnotes = $self->order->intnotes;
307 335
  $intnotes   .= "\n\n" if $self->order->intnotes;
308 336
  $intnotes   .= t8('[email]')                                                                                        . "\n";
309 337
  $intnotes   .= t8('Date')       . ": " . $::locale->format_date_object(DateTime->now_local, precision => 'seconds') . "\n";
310
  $intnotes   .= t8('To (email)') . ": " . $mail->{to}                                                                . "\n";
311
  $intnotes   .= t8('Cc')         . ": " . $mail->{cc}                                                                . "\n"    if $mail->{cc};
312
  $intnotes   .= t8('Bcc')        . ": " . $mail->{bcc}                                                               . "\n"    if $mail->{bcc};
313
  $intnotes   .= t8('Subject')    . ": " . $mail->{subject}                                                           . "\n\n";
314
  $intnotes   .= t8('Message')    . ": " . $mail->{message};
338
  $intnotes   .= t8('To (email)') . ": " . $::form->{email}                                                           . "\n";
339
  $intnotes   .= t8('Cc')         . ": " . $::form->{cc}                                                              . "\n"    if $::form->{cc};
340
  $intnotes   .= t8('Bcc')        . ": " . $::form->{bcc}                                                             . "\n"    if $::form->{bcc};
341
  $intnotes   .= t8('Subject')    . ": " . $::form->{subject}                                                         . "\n\n";
342
  $intnotes   .= t8('Message')    . ": " . $::form->{message};
315 343

  
316 344
  $self->js
317 345
      ->val('#order_intnotes', $intnotes)
318 346
      ->run('kivi.Order.close_email_dialog')
347
      ->flash('info', t8('The email has been sent.'))
319 348
      ->render($self);
320 349
}
321 350

  
......
1237 1266
  return @errors;
1238 1267
}
1239 1268

  
1269
sub _get_files_for_email_dialog {
1270
  my ($self) = @_;
1271

  
1272
  my %files = map { ($_ => []) } qw(versions files vc_files part_files);
1273

  
1274
  return %files if !$::instance_conf->get_doc_storage;
1275

  
1276
  if ($self->order->id) {
1277
    $files{versions} = [ SL::File->get_all_versions(object_id => $self->order->id,              object_type => $self->order->type, file_type => 'document') ];
1278
    $files{files}    = [ SL::File->get_all(         object_id => $self->order->id,              object_type => $self->order->type, file_type => 'attachment') ];
1279
    $files{vc_files} = [ SL::File->get_all(         object_id => $self->order->{$self->cv}->id, object_type => $self->cv,          file_type => 'attachment') ];
1280
  }
1281

  
1282
  my @parts =
1283
    uniq_by { $_->{id} }
1284
    map {
1285
      +{ id         => $_->part->id,
1286
         partnumber => $_->part->partnumber }
1287
    } @{$self->order->items_sorted};
1288

  
1289
  foreach my $part (@parts) {
1290
    my @pfiles = SL::File->get_all(object_id => $part->{id}, object_type => 'part');
1291
    push @{ $files{part_files} }, map { +{ %{ $_ }, partnumber => $part->{partnumber} } } @pfiles;
1292
  }
1293

  
1294
  foreach my $key (keys %files) {
1295
    $files{$key} = [ sort_by { lc $_->{db_file}->{file_name} } @{ $files{$key} } ];
1296
  }
1297

  
1298
  return %files;
1299
}
1300

  
1240 1301
sub _sales_order_type {
1241 1302
  'sales_order';
1242 1303
}
......
1354 1415

  
1355 1416
Dialog for selecting price and discount sources
1356 1417

  
1357
=item * C<template/webpages/order/tabs/_email_dialog.html>
1358

  
1359
Email dialog
1360

  
1361 1418
=back
1362 1419

  
1363 1420
=item * C<js/kivi.Order.js>
......
1440 1497
C<show_multi_items_dialog> does not use the currently inserted string for
1441 1498
filtering.
1442 1499

  
1500
=item *
1501

  
1502
The language selected in print or email dialog is not saved when the order is saved.
1503

  
1443 1504
=back
1444 1505

  
1445 1506
=head1 To discuss / Nice to have

Auch abrufbar als: Unified diff