Revision b2f213b2
Von Tamino Steinert vor 4 Tagen hinzugefügt
SL/Controller/Reclamation.pm | ||
---|---|---|
4 | 4 |
use parent qw(SL::Controller::Base); |
5 | 5 |
|
6 | 6 |
use SL::Helper::Flash qw(flash_later); |
7 |
use SL::HTML::Util; |
|
7 | 8 |
use SL::Presenter::Tag qw(select_tag hidden_tag div_tag); |
8 | 9 |
use SL::Presenter::ReclamationFilter qw(filter); |
9 | 10 |
use SL::Locale::String qw(t8); |
... | ... | |
394 | 395 |
my ($self) = @_; |
395 | 396 |
|
396 | 397 |
$self->save(); |
398 |
$self->js_reset_reclamation_and_item_ids_after_save; |
|
397 | 399 |
|
398 |
unless ($self->reclamation->customervendor) {
|
|
399 |
return $self->js->flash('error', |
|
400 |
my $cv = $self->reclamation->customervendor
|
|
401 |
or return $self->js->flash('error',
|
|
400 | 402 |
$self->type_data->properties('is_customer') ? |
401 | 403 |
t8('Cannot send E-mail without customer given') |
402 |
: t8('Cannot send E-mail without vendor given')) |
|
403 |
->render($self); |
|
404 |
} |
|
404 |
: t8('Cannot send E-mail without vendor given') |
|
405 |
)->render($self); |
|
405 | 406 |
|
406 | 407 |
my $form = Form->new; |
407 | 408 |
$form->{record_number} = $self->reclamation->record_number; |
... | ... | |
414 | 415 |
$form->{cp_id} = $self->reclamation->contact->cp_id if $self->reclamation->contact; |
415 | 416 |
|
416 | 417 |
my $email_form; |
417 |
$email_form->{to} = $self->reclamation->contact->cp_email if $self->reclamation->contact; |
|
418 |
$email_form->{to} ||= $self->reclamation->customervendor->email; |
|
419 |
$email_form->{cc} = $self->reclamation->customervendor->cc; |
|
420 |
$email_form->{bcc} = join ', ', grep $_, $self->reclamation->customervendor->bcc; |
|
418 |
$email_form->{to} = |
|
419 |
($self->reclamation->contact ? $self->reclamation->contact->cp_email : undef) |
|
420 |
|| $cv->email; |
|
421 |
$email_form->{cc} = $cv->cc; |
|
422 |
$email_form->{bcc} = join ', ', grep $_, $cv->bcc; |
|
421 | 423 |
# TODO: get addresses from shipto, if any |
422 | 424 |
$email_form->{subject} = $form->generate_email_subject(); |
423 | 425 |
$email_form->{attachment_filename} = $form->generate_attachment_filename(); |
... | ... | |
425 | 427 |
$email_form->{js_send_function} = 'kivi.Reclamation.send_email()'; |
426 | 428 |
|
427 | 429 |
my %files = $self->get_files_for_email_dialog(); |
428 |
$self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]); |
|
429 |
my $dialog_html = $self->render('common/_send_email_dialog', { output => 0 }, |
|
430 |
email_form => $email_form, |
|
431 |
show_bcc => $::auth->assert('email_bcc', 'may fail'), |
|
432 |
FILES => \%files, |
|
433 |
is_customer => $self->type_data->properties('is_customer'), |
|
434 |
ALL_EMPLOYEES => $self->{all_employees}, |
|
430 |
|
|
431 |
my @employees_with_email = grep { |
|
432 |
my $user = SL::DB::Manager::AuthUser->find_by(login => $_->login); |
|
433 |
$user && !!trim($user->get_config_value('email')); |
|
434 |
} @{ SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]) }; |
|
435 |
|
|
436 |
my $dialog_html = $self->render( |
|
437 |
'common/_send_email_dialog', { output => 0 }, |
|
438 |
email_form => $email_form, |
|
439 |
show_bcc => $::auth->assert('email_bcc', 'may fail'), |
|
440 |
FILES => \%files, |
|
441 |
is_customer => $self->type_data->properties('is_customer'), |
|
442 |
ALL_EMPLOYEES => \@employees_with_email, |
|
443 |
ALL_PARTNER_EMAIL_ADDRESSES => $cv->get_all_email_addresses(), |
|
435 | 444 |
); |
436 | 445 |
|
437 | 446 |
$self->js |
438 |
->run('kivi.Reclamation.show_email_dialog', $dialog_html)
|
|
439 |
->reinit_widgets
|
|
440 |
->render($self);
|
|
447 |
->run('kivi.Reclamation.show_email_dialog', $dialog_html) |
|
448 |
->reinit_widgets |
|
449 |
->render($self); |
|
441 | 450 |
} |
442 | 451 |
|
443 | 452 |
# send email |
444 |
# |
|
445 |
# TODO: handling error messages: flash is not displayed in dialog, but in the main form |
|
446 | 453 |
sub action_send_email { |
447 | 454 |
my ($self) = @_; |
448 | 455 |
|
... | ... | |
454 | 461 |
die $EVAL_ERROR; |
455 | 462 |
}; |
456 | 463 |
|
457 |
$self->js_reset_reclamation_and_item_ids_after_save; |
|
464 |
my @redirect_params = ( |
|
465 |
action => 'edit', |
|
466 |
type => $self->type, |
|
467 |
id => $self->reclamation->id, |
|
468 |
); |
|
469 |
|
|
470 |
# Set the error handler to reload the document and display errors later, |
|
471 |
# because the document is already saved and saving can have some side effects |
|
472 |
# such as generating a document number, project number or record links, |
|
473 |
# which will be up to date when the document is reloaded. |
|
474 |
# Hint: Do not use "die" here and try to catch exceptions in subroutine |
|
475 |
# calls. You should use "$::form->error" which respects the error handler. |
|
476 |
local $::form->{__ERROR_HANDLER} = sub { |
|
477 |
flash_later('error', $_[0]); |
|
478 |
$self->redirect_to(@redirect_params); |
|
479 |
$::dispatcher->end_request; |
|
480 |
}; |
|
458 | 481 |
|
459 | 482 |
# move $::form->{email_form} to $::form |
460 | 483 |
my $email_form = delete $::form->{email_form}; |
484 |
|
|
485 |
if ($email_form->{additional_to}) { |
|
486 |
$email_form->{to} = join ', ', grep { $_ } $email_form->{to}, @{$email_form->{additional_to}}; |
|
487 |
delete $email_form->{additional_to}; |
|
488 |
} |
|
489 |
|
|
461 | 490 |
my %field_names = (to => 'email'); |
462 | 491 |
$::form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form }; |
463 | 492 |
|
... | ... | |
468 | 497 |
$::form->{$_} = $::form->{print_options}->{$_} for keys %{$::form->{print_options}}; |
469 | 498 |
$::form->{media} = 'email'; |
470 | 499 |
|
471 |
if (($::form->{attachment_policy} // '') !~ m{^(?:old_file|no_file)$}) { |
|
500 |
$::form->{attachment_policy} //= ''; |
|
501 |
|
|
502 |
# Is an old file version available? |
|
503 |
my $attfile; |
|
504 |
if ($::form->{attachment_policy} eq 'old_file') { |
|
505 |
$attfile = SL::File->get_all( |
|
506 |
object_id => $self->reclamaiton->id, |
|
507 |
object_type => $self->type, |
|
508 |
print_variant => $::form->{formname}, |
|
509 |
); |
|
510 |
} |
|
511 |
|
|
512 |
if ( $::form->{attachment_policy} ne 'no_file' |
|
513 |
&& !($::form->{attachment_policy} eq 'old_file' && $attfile) |
|
514 |
) { |
|
472 | 515 |
my $pdf; |
473 |
my @errors = generate_pdf($self->reclamation, \$pdf, { |
|
474 |
media => $::form->{media}, |
|
475 |
format => $::form->{print_options}->{format}, |
|
476 |
formname => $::form->{print_options}->{formname}, |
|
477 |
language => $self->reclamation->language, |
|
478 |
printer_id => $::form->{print_options}->{printer_id}, |
|
479 |
groupitems => $::form->{print_options}->{groupitems} |
|
480 |
}); |
|
516 |
my @errors = generate_pdf( |
|
517 |
$self->reclamation, \$pdf, { |
|
518 |
media => $::form->{media}, |
|
519 |
format => $::form->{print_options}->{format}, |
|
520 |
formname => $::form->{print_options}->{formname}, |
|
521 |
language => $self->reclamation->language, |
|
522 |
printer_id => $::form->{print_options}->{printer_id}, |
|
523 |
groupitems => $::form->{print_options}->{groupitems}, |
|
524 |
}); |
|
481 | 525 |
if (scalar @errors) { |
482 |
return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
|
|
526 |
$::form->error(t8('Generating the document failed: #1', $errors[0]));
|
|
483 | 527 |
} |
484 | 528 |
|
485 |
my @warnings = store_pdf_to_webdav_and_filemanagement($self->reclamation, $pdf, $::form->{attachment_filename}); |
|
529 |
my @warnings = store_pdf_to_webdav_and_filemanagement( |
|
530 |
$self->reclamation, $pdf, $::form->{attachment_filename} |
|
531 |
); |
|
486 | 532 |
if (scalar @warnings) { |
487 | 533 |
flash_later('warning', $_) for @warnings; |
488 | 534 |
} |
... | ... | |
492 | 538 |
$sfile->fh->close; |
493 | 539 |
|
494 | 540 |
$::form->{tmpfile} = $sfile->file_name; |
495 |
$::form->{tmpdir} = $sfile->get_path; # for Form::cleanup which may be called in Form::send_email |
|
541 |
$::form->{tmpdir} = $sfile->get_path; # for Form::cleanup which may be |
|
542 |
# called in Form::send_email |
|
496 | 543 |
} |
497 | 544 |
|
498 |
$::form->{id} = $self->reclamation->id; # this is used in SL::Mailer to create a linked record to the mail |
|
545 |
$::form->{id} = $self->reclamation->id; # this is used in SL::Mailer to |
|
546 |
# create a linked record to the mail |
|
499 | 547 |
$::form->send_email(\%::myconfig, 'pdf'); |
500 | 548 |
|
501 |
# internal notes |
|
502 |
my $intnotes = $self->reclamation->intnotes; |
|
503 |
$intnotes .= "\n\n" if $self->reclamation->intnotes; |
|
504 |
$intnotes .= t8('[email]') . "\n"; |
|
505 |
$intnotes .= t8('Date') . ": " . $::locale->format_date_object( |
|
506 |
DateTime->now_local, |
|
507 |
precision => 'seconds') . "\n"; |
|
508 |
$intnotes .= t8('To (email)') . ": " . $::form->{email} . "\n"; |
|
509 |
$intnotes .= t8('Cc') . ": " . $::form->{cc} . "\n" if $::form->{cc}; |
|
510 |
$intnotes .= t8('Bcc') . ": " . $::form->{bcc} . "\n" if $::form->{bcc}; |
|
511 |
$intnotes .= t8('Subject') . ": " . $::form->{subject} . "\n\n"; |
|
512 |
$intnotes .= t8('Message') . ": " . $::form->{message}; |
|
513 |
|
|
514 |
$self->reclamation->update_attributes(intnotes => $intnotes); |
|
515 |
|
|
516 |
$self->save_history('MAILED'); |
|
517 |
|
|
518 | 549 |
flash_later('info', t8('The email has been sent.')); |
550 |
$self->save_history('MAILED'); |
|
519 | 551 |
|
520 |
my @redirect_params = ( |
|
521 |
action => 'edit', |
|
522 |
type => $self->type, |
|
523 |
id => $self->reclamation->id, |
|
524 |
); |
|
552 |
# internal notes unless no email journal |
|
553 |
unless ($::instance_conf->get_email_journal) { |
|
554 |
my $intnotes = $self->reclamation->intnotes; |
|
555 |
$intnotes .= "\n\n" if $self->reclamation->intnotes; |
|
556 |
$intnotes .= t8('[email]') . "\n"; |
|
557 |
$intnotes .= t8('Date') . ": " . $::locale->format_date_object( |
|
558 |
DateTime->now_local, |
|
559 |
precision => 'seconds') . "\n"; |
|
560 |
$intnotes .= t8('To (email)') . ": " . $::form->{email} . "\n"; |
|
561 |
$intnotes .= t8('Cc') . ": " . $::form->{cc} . "\n" if $::form->{cc}; |
|
562 |
$intnotes .= t8('Bcc') . ": " . $::form->{bcc} . "\n" if $::form->{bcc}; |
|
563 |
$intnotes .= t8('Subject') . ": " . $::form->{subject} . "\n\n"; |
|
564 |
$intnotes .= t8('Message') . ": " . SL::HTML::Util->strip($::form->{message}); |
|
565 |
|
|
566 |
$self->reclamation->update_attributes(intnotes => $intnotes); |
|
567 |
} |
|
525 | 568 |
|
526 | 569 |
$self->redirect_to(@redirect_params); |
527 | 570 |
} |
Auch abrufbar als: Unified diff
E-Mail-Versand für Auftrag, Lieferschein und Reklamation angleichen