Auftrags-Controller: E-Mail-Funktion
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Tue, 22 Sep 2015 15:11:23 +0000 (17:11 +0200)
committerG. Richardson <information@kivitendo-premium.de>
Mon, 16 Nov 2015 15:15:57 +0000 (16:15 +0100)
SL/Controller/Order.pm
templates/webpages/order/form.html
templates/webpages/order/tabs/_email_dialog.html [new file with mode: 0644]
templates/webpages/order/tabs/basic_data.html

index b00b089..25ec07c 100644 (file)
@@ -36,7 +36,7 @@ use Rose::Object::MakeMethods::Generic
 __PACKAGE__->run_before('_check_auth');
 
 __PACKAGE__->run_before('_recalc',
-                        only => [ qw(edit update save create_pdf) ]);
+                        only => [ qw(edit update save create_pdf send_email) ]);
 
 
 #
@@ -107,33 +107,8 @@ sub action_save {
 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,
-        variable_content_types => {
-          longdescription => 'html',
-          partnotes       => 'html',
-          notes           => 'html',
-        },
-      );
-      1;
-    } || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->getMessage : $EVAL_ERROR;
-  });
-
+  my @errors = _create_pdf($self->order, \$pdf);
   if (scalar @errors) {
     return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
   }
@@ -160,6 +135,84 @@ sub action_download_pdf {
   );
 }
 
+sub action_show_email_dialog {
+  my ($self) = @_;
+
+  my $cv_method = $self->cv;
+
+  if (!$self->order->$cv_method) {
+    return $self->js->flash('error', t8('Cannot send E-mail without ' . $self->cv))
+                    ->render($self);
+  }
+
+  $self->{email}->{to}   = $self->order->contact->cp_email if $self->order->contact;
+  $self->{email}->{to} ||= $self->order->$cv_method->email;
+  $self->{email}->{cc}   = $self->order->$cv_method->cc;
+  $self->{email}->{bcc}  = join ', ', grep $_, $self->order->$cv_method->bcc, SL::DB::Default->get->global_bcc;
+  # Todo: get addresses from shipto, if any
+
+  my $form = Form->new;
+  $form->{ordnumber} = $self->order->ordnumber;
+  $form->{formname}  = $self->type;
+  $form->{type}      = $self->type;
+  $form->{language} = 'de';
+  $form->{format}   = 'pdf';
+
+  $self->{email}->{subject}             = $form->generate_email_subject();
+  $self->{email}->{attachment_filename} = $form->generate_attachment_filename();
+  $self->{email}->{message}             = $form->create_email_signature();
+
+  my $dialog_html = $self->render('order/tabs/_email_dialog', { output => 0 });
+  $self->js
+      ->run('show_email_dialog', $dialog_html)
+      ->reinit_widgets
+      ->render($self);
+}
+
+# Todo: handling error messages: flash is not displayed in dialog, but in the main form
+sub action_send_email {
+  my ($self) = @_;
+
+  my $mail      = Mailer->new;
+  $mail->{from} = qq|"$::myconfig{name}" <$::myconfig{email}>|;
+  $mail->{$_}   = $::form->{email}->{$_} for qw(to cc bcc subject message);
+
+  my $pdf;
+  my @errors = _create_pdf($self->order, \$pdf, {media => 'email'});
+  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;
+
+  $mail->{attachments} = [{ "filename" => $sfile->file_name,
+                            "name"     => $::form->{email}->{attachment_filename} }];
+
+  if (my $err = $mail->send) {
+    return $self->js->flash('error', t8('Sending E-mail: ') . $err)
+                    ->render($self);
+  }
+
+  # internal notes
+  my $intnotes = $self->order->intnotes;
+  $intnotes   .= "\n\n" if $self->order->intnotes;
+  $intnotes   .= t8('[email]')                                                                                        . "\n";
+  $intnotes   .= t8('Date')       . ": " . $::locale->format_date_object(DateTime->now_local, precision => 'seconds') . "\n";
+  $intnotes   .= t8('To (email)') . ": " . $mail->{to}                                                                . "\n";
+  $intnotes   .= t8('Cc')         . ": " . $mail->{cc}                                                                . "\n"    if $mail->{cc};
+  $intnotes   .= t8('Bcc')        . ": " . $mail->{bcc}                                                               . "\n"    if $mail->{bcc};
+  $intnotes   .= t8('Subject')    . ": " . $mail->{subject}                                                           . "\n\n";
+  $intnotes   .= t8('Message')    . ": " . $mail->{message};
+
+  $self->js
+      ->val('#order_intnotes', $intnotes)
+      ->run('close_email_dialog')
+      ->render($self);
+}
+
+
 sub action_customer_vendor_changed {
   my ($self) = @_;
 
@@ -445,6 +498,38 @@ sub _pre_render {
   $::request->{layout}->use_javascript("${_}.js")  for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
 }
 
+sub _create_pdf {
+  my ($order, $pdf_ref, $params) = @_;
+
+  my $print_form = Form->new('');
+  $print_form->{type}     = 'sales_order';
+  $print_form->{formname} = 'sales_order',
+  $print_form->{format}   = $params->{format} || 'pdf',
+  $print_form->{media}    = $params->{media}  || 'file';
+
+  $order->flatten_to_form($print_form, format_amounts => 1);
+
+  my @errors = ();
+  $print_form->throw_on_error(sub {
+    eval {
+      $print_form->prepare_for_printing;
+
+      $$pdf_ref = SL::Helper::CreatePDF->create_pdf(
+        template  => SL::Helper::CreatePDF->find_template(name => $print_form->{formname}),
+        variables => $print_form,
+        variable_content_types => {
+          longdescription => 'html',
+          partnotes       => 'html',
+          notes           => 'html',
+        },
+      );
+      1;
+    } || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->getMessage : $EVAL_ERROR;
+  });
+
+  return @errors;
+}
+
 sub _sales_order_type {
   'sales_order';
 }
index a5376e3..ae226b0 100644 (file)
@@ -32,6 +32,7 @@
 
   [% L.button_tag('save()', LxERP.t8('Save')) %]
   [% L.button_tag('create_pdf()', LxERP.t8('Create PDF')) %]
+  [% L.button_tag('email()', LxERP.t8('E-mail')) %]
 
 </form>
 
@@ -60,4 +61,11 @@ function download_pdf(tmp_filename, pdf_filename) {
   $.download("controller.pl", data);
 }
 
+function email() {
+  var data = $('#order_form').serialize();
+  data += '&action=Order/show_email_dialog';
+
+  $.post("controller.pl", data, kivi.eval_json_result);
+}
+
 </script>
diff --git a/templates/webpages/order/tabs/_email_dialog.html b/templates/webpages/order/tabs/_email_dialog.html
new file mode 100644 (file)
index 0000000..3c6df7f
--- /dev/null
@@ -0,0 +1,65 @@
+[%- USE T8 %][%- USE HTML %][%- USE L %][%- USE LxERP %]
+
+<form method="post" id="email_form" method="POST">
+<h2>[%- 'E-mail' | $T8 %]&nbsp;[%- SELF.type | $T8 %]</h2>
+
+<table width="100%">
+  <tr>
+    <td>
+      <table>
+        <tr  align="left">
+          <th align="right" nowrap>[% 'To' | $T8 %]</th>
+          <td>[% L.input_tag("email.to", SELF.email.to, size=30, class=(SELF.email.to ? '' : 'initial_focus')) %]</td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>[% 'Cc' | $T8 %]</th>
+          <td>[% L.input_tag("email.cc", SELF.email.cc, size=30) %]</td>
+        </tr>
+        [%- IF AUTH.assert('email_bcc', 1) %]
+        <tr>
+          <th align="right" nowrap>[% 'Bcc' | $T8 %]</th>
+          <td>[% L.input_tag("email.bcc", SELF.email.bcc, size=30) %]</td>
+        </tr>
+        [%- END %]
+        <tr>
+          <th align="right" nowrap>[% 'Subject' | $T8 %]</th>
+          <td>[% L.input_tag('email.subject', SELF.email.subject, size=30, class=(SELF.email.subject ? 'initial_focus' : '')) %]</td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>[% 'Attachment name' | $T8 %]</th>
+          <td>[% L.input_tag("email.attachment_filename", SELF.email.attachment_filename, size=30) %]</td>
+        </tr>
+      </table>
+    </td>
+  </tr>
+
+  <tr>
+    <table>
+      <tr>
+        <th align="left" nowrap>[% 'Message' | $T8 %]</th>
+      </tr>
+      <tr>
+        <td>
+          [% L.textarea_tag("email.message", SELF.email.message, wrap="soft", style="width: 350px; height: 150px") %]
+        </td>
+      </tr>
+  </tr>
+
+</table>
+
+<br>
+[% L.hidden_tag('action', 'Order/dispatch') %]
+[% L.button_tag('send_email()', LxERP.t8('Continue')) %]
+<a href="#" onclick="close_email_dialog();">[%- LxERP.t8("Cancel") %]</a>
+
+<script type='text/javascript'>
+function send_email() {
+  var data = $('#order_form').serialize();
+  data += '&';
+  data += $('#email_form').serialize();
+  data += '&action=Order/send_email';
+  $.post("controller.pl", data, kivi.eval_json_result);
+}
+</script>
+
+</form>
index bf4c5bf..f0928aa 100644 (file)
@@ -317,6 +317,36 @@ function row_set_keyboard_events(rows) {
   });
 }
 
+var email_dialog;
+
+function show_email_dialog(html) {
+  var id            = 'jqueryui_popup_dialog';
+  var dialog_params = {
+    id:     id,
+    width:  800,
+    height: 500,
+    modal:  true,
+    close: function(event, ui) {
+      email_dialog.remove();
+    },
+  };
+
+  $('#' + id).remove();
+
+  email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
+  email_dialog.html(html);
+  email_dialog.dialog(dialog_params);
+
+  $('.cancel').click(close_email_dialog);
+
+  return true;
+}
+
+close_email_dialog = function() {
+  email_dialog.dialog("close");
+}
+
+
 $(function(){
   $('#order_[%- cv_id %]').change(reload_cv_dependend_selections);
   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });