commit 22bcb6b18c5832dad188a72ed75cd98c38d299e3
Author: Moritz Bunkus <m.bunkus@linet.de>
Date:   Mon Jan 9 16:38:20 2017 +0100

    ActionBar: Angebote/Aufträge: E-Mail-Versand über Dialog implementiert

diff --git a/bin/mozilla/io.pl b/bin/mozilla/io.pl
index a716f16d0..0cd7385bd 100644
--- a/bin/mozilla/io.pl
+++ b/bin/mozilla/io.pl
@@ -53,6 +53,7 @@ use SL::IO;
 use SL::File;
 use SL::PriceSource;
 
+use SL::DB::Contact;
 use SL::DB::Customer;
 use SL::DB::Default;
 use SL::DB::Language;
@@ -60,6 +61,7 @@ use SL::DB::Printer;
 use SL::DB::Vendor;
 use SL::Helper::CreatePDF;
 use SL::Helper::Flash;
+use SL::Helper::PrintOptions;
 
 require "bin/mozilla/common.pl";
 
@@ -2026,3 +2028,56 @@ sub _make_record {
 
   return $obj;
 }
+
+sub setup_sales_purchase_print_options {
+  my $print_form = Form->new('');
+  $print_form->{printers}  = SL::DB::Manager::Printer->get_all_sorted;
+  $print_form->{languages} = SL::DB::Manager::Language->get_all_sorted;
+
+  $print_form->{$_} = $::form->{$_} for qw(type media language_id printer_id);
+
+  return SL::Helper::PrintOptions->get_print_options(
+    form    => $print_form,
+    options => {
+      show_headers => 1,
+    },
+  );
+}
+
+sub show_sales_purchase_email_dialog {
+  my $contact    = $::form->{cp_id} ? SL::DB::Contact->load_cached($::form->{cp_id}) : undef;
+  my $email_form = {
+    to                  => $contact ? $contact->cp_email : '',
+    subject             => $::form->generate_email_subject,
+    attachment_filename => $::form->generate_attachment_filename,
+  };
+
+  my $html = $::form->parse_html_template("common/_send_email_dialog", {
+    email_form => $email_form,
+    show_bcc   => $::auth->assert('email_bcc', 'may fail'),
+  });
+
+  print $::form->ajax_response_header, $html;
+}
+
+sub send_sales_purchase_email {
+  my $type        = $::form->{type};
+  my $id          = $::form->{id};
+  my $script      = $type =~ m{sales_order|purchase_order|quotation} ? 'oe.pl'
+                  : $type =~ m{delivery_}                            ? 'do.pl'
+                  :                                                    'is.pl';
+
+  my $email_form  = delete $::form->{email_form};
+  my %field_names = (to => 'email');
+
+  $::form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
+
+  $::form->{media} = 'email';
+
+  print_form("return");
+  Common->save_email_status(\%::myconfig, $::form);
+
+  flash_later('info', $::locale->text('The email has been sent.'));
+
+  print $::form->redirect_header($script . '?action=edit&id=' . $::form->escape($id) . '&type=' . $::form->escape($type));
+}
diff --git a/bin/mozilla/oe.pl b/bin/mozilla/oe.pl
index bc01dbbc4..4c33c974a 100644
--- a/bin/mozilla/oe.pl
+++ b/bin/mozilla/oe.pl
@@ -399,7 +399,7 @@ sub setup_oe_action_bar {
         ],
         action => [
           t8('E Mail'),
-          submit => [ '#form', { action_print => 1 } ],
+          call   => [ 'kivi.SalesPurchase.show_email_dialog' ],
           checks => [ @req_trans_desc ],
         ],
       ], #end of combobox "Export"
@@ -593,11 +593,11 @@ sub form_header {
   push @custom_hiddens, map { "shiptocvar_" . $_->name } @{ SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'ShipTo' ]) };
 
   $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
-     qw(id action type vc formname media format proforma queued printed emailed
+     qw(id type vc proforma queued printed emailed
         title creditlimit creditremaining tradediscount business
         max_dunning_level dunning_amount
         CFDD_shipto CFDD_shipto_id
-        message email subject cc bcc taxpart taxservice taxaccounts cursor_fokus
+        taxpart taxservice taxaccounts cursor_fokus
         show_details useasnew),
         @custom_hiddens,
         map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts} ];  # deleted: discount
@@ -684,9 +684,11 @@ sub form_footer {
 
   $TMPL_VAR{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
 
+  my $print_options_html = setup_sales_purchase_print_options();
+
   print $form->parse_html_template("oe/form_footer", {
      %TMPL_VAR,
-     print_options   => print_options(inline => 1),
+     print_options   => $print_options_html,
      is_sales        => scalar ($form->{type} =~ /^sales_/),              # these vars are exported, so that the template
      is_order        => scalar ($form->{type} =~ /_order$/),              # may determine what to show
      is_sales_quo    => scalar ($form->{type} =~ /sales_quotation$/),
diff --git a/js/kivi.SalesPurchase.js b/js/kivi.SalesPurchase.js
index 69fec6d8b..2659c858e 100644
--- a/js/kivi.SalesPurchase.js
+++ b/js/kivi.SalesPurchase.js
@@ -208,4 +208,83 @@ namespace('kivi.SalesPurchase', function(ns) {
       }
     });
   };
+
+  this.show_print_options_elements = function(elements, show) {
+    $(elements).each(function(idx, elt) {
+      var $elements = $('#print_options_header_' + elt + ',#print_options_input_' + elt);
+      if (show)
+        $elements.show();
+      else
+        $elements.hide();
+    });
+  };
+
+  this.show_all_print_options_elements = function() {
+    kivi.SalesPurchase.show_print_options_elements([ 'formname', 'language_id', 'format', 'sendmode', 'media', 'printer_id', 'copies', 'groupitems', 'remove_draft' ], true);
+  };
+
+  // Sending records via email.
+  this.send_email = function() {
+    var unset = $('#email_form_to,#email_form_subject,#email_form_message').filter(function(idx, elt) {
+      return $(elt).val() === '';
+    });
+
+    if (unset.length > 0) {
+      alert(kivi.t8("The recipient, subject or body is missing."));
+      $(unset[0]).focus();
+
+      return false;
+    }
+
+    $('#send_email_dialog').children().remove().appendTo('#email_inputs');
+    $('#send_email_dialog').dialog('close');
+
+    $('#action').val('send_sales_purchase_email');
+    $('#form').submit();
+
+    return true;
+  };
+
+  this.setup_send_email_dialog = function() {
+    kivi.SalesPurchase.show_all_print_options_elements();
+    kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
+
+    $('#print_options').children().remove().appendTo('#email_form_print_options');
+
+    var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
+    $('#email_form_' + to_focus).focus();
+  };
+
+  this.finish_send_email_dialog = function() {
+    $('#email_form_print_options').children().remove().appendTo('#print_options');
+    return true;
+  };
+
+  this.show_email_dialog = function() {
+    kivi.popup_dialog({
+      id:     'send_email_dialog',
+      url:    'io.pl',
+      load:   kivi.SalesPurchase.setup_send_email_dialog,
+      data:   {
+        action:      'show_sales_purchase_email_dialog',
+        type:        $('#type').val(),
+        formname:    $('#formname').val(),
+        format:      $('#format').val(),
+        media:       'email',
+        ordnumber:   $('#ordnumber').val(),
+        donumber:    $('#donumber').val(),
+        invnumber:   $('#invnumber').val(),
+        quonumber:   $('#quonumber').val(),
+        cp_id:       $('#cp_id').val(),
+        language_id: $('#language_id').val(),
+      },
+      dialog: {
+        height:      600,
+        title:       kivi.t8('Send email'),
+        beforeClose: kivi.SalesPurchase.finish_send_email_dialog
+      }
+    });
+
+    return true;
+  };
 });
diff --git a/js/locale/de.js b/js/locale/de.js
index d7db4951f..fec17ed1e 100644
--- a/js/locale/de.js
+++ b/js/locale/de.js
@@ -90,6 +90,7 @@ namespace("kivi").setupLocale({
 "Save and keep open":"Speichern und geöffnet lassen",
 "Section/Function block actions":"Abschnitts-/Funktionsblockaktionen",
 "Select template to paste":"Einzufügende Vorlage auswählen",
+"Send email":"E-Mail verschicken",
 "Set all source and memo fields":"Alle Beleg-/Memo-Felder setzen",
 "Show all details":"Alle Details anzeigen",
 "Show details":"Details anzeigen",
diff --git a/locale/de/all b/locale/de/all
index 49c08b959..5876d9b8c 100755
--- a/locale/de/all
+++ b/locale/de/all
@@ -2586,6 +2586,7 @@ $self->{texts} = {
   'Semicolon'                   => 'Semikolon',
   'Send a BCC to logged in user?' => 'BCC an eingeloggten Benutzer?',
   'Send a blind copy of all outgoing emails to current user\'s email address?' => 'Eine blinde Kopie aller ausgehenden E-Mails wird an den angemeldeten Nutzer geschickt',
+  'Send email'                  => 'E-Mail verschicken',
   'Send invoice via email'      => 'Rechnung via E-Mail verschicken',
   'Send letter via e-mail'      => 'Brief via E-Mail verschicken',
   'Sender'                      => 'AbsenderIn',
diff --git a/templates/webpages/common/_send_email_dialog.html b/templates/webpages/common/_send_email_dialog.html
new file mode 100644
index 000000000..20d8b2d77
--- /dev/null
+++ b/templates/webpages/common/_send_email_dialog.html
@@ -0,0 +1,44 @@
+[%- USE HTML %][%- USE LxERP -%][%- USE L -%]
+
+<table>
+ <tbody>
+  <tr>
+   <th align="right" nowrap>[% LxERP.t8("To") %]</th>
+   <td>[% L.input_tag("email_form.to", email_form.to, size="80") %]</td>
+  </tr>
+
+  <tr>
+   <th align="right" nowrap>[% LxERP.t8("Cc") %]</th>
+   <td>[% L.input_tag("email_form.cc", email_form.cc, size="80") %]</td>
+  </tr>
+
+ [%- IF show_bcc %]
+  <tr>
+   <th align="right" nowrap>[% LxERP.t8("Bcc") %]</th>
+   <td>[% L.input_tag("email_form.bcc", email_form.bcc, size="80") %]</td>
+  </tr>
+ [%- END %]
+
+  <tr>
+   <th align="right" nowrap>[% LxERP.t8("Subject") %]</th>
+   <td>[% L.input_tag("email_form.subject", email_form.subject, size="80") %]</td>
+  </tr>
+
+  <tr>
+   <th align="right" nowrap>[% LxERP.t8("Attachment name") %]</th>
+   <td>[% L.input_tag("email_form.attachment_filename", email_form.attachment_filename, size="80") %]</td>
+  </tr>
+
+  <tr valign="top">
+   <th align="right" nowrap>[% LxERP.t8("Message") %]</th>
+   <td>[% L.textarea_tag("email_form.message", email_form.message, rows="15" cols="80" wrap="soft") %]</td>
+  </tr>
+ </tbody>
+</table>
+
+<div id="email_form_print_options"></div>
+
+<p>
+ [% L.button_tag("kivi.SalesPurchase.send_email()", LxERP.t8("Send email")) %]
+ [% L.button_tag("\$('#send_email_dialog').dialog('close');", LxERP.t8("Abort")) %]
+</p>
diff --git a/templates/webpages/generic/print_options.html b/templates/webpages/generic/print_options.html
index b5757e1cb..30b87df3c 100644
--- a/templates/webpages/generic/print_options.html
+++ b/templates/webpages/generic/print_options.html
@@ -9,25 +9,25 @@
     <tr>
      [%- FOREACH row = SELECTS %]
      [%- IF row.show %]
-     <th align="left">[%- row.hname %]</th>
+     <th align="left" id="print_options_header_[% row.sname %]">[%- row.hname %]</th>
      [%- END %]
      [%- END %]
      [%- IF display_copies %]
-     <th align="left">[% 'Copies' | $T8 %]</th>
+     <th align="left" id="print_options_header_copies">[% 'Copies' | $T8 %]</th>
      [%- END %]
      [%- IF display_groupitems %]
-     <th align="left">[% 'Group Items' | $T8 %]</th>
+     <th align="left" id="print_options_header_groupitems">[% 'Group Items' | $T8 %]</th>
      [%- END %]
      [%- IF display_remove_draft %]
-     <th align="left">[% 'Remove Draft' | $T8 %]</th>
+     <th align="left" id="print_options_header_remove_draft">[% 'Remove Draft' | $T8 %]</th>
      [%- END %]
     </tr>
     [%- END %]
     <tr>
      [%- FOREACH row = SELECTS %]
      [%- IF row.show %]
-     <td>
-      <select name="[%- name_prefix %][%- row.sname %]">
+     <td id="print_options_input_[% row.sname %]">
+      <select name="[%- name_prefix %][%- row.sname %]" id="[%- id_prefix %][%- row.sname %]">
        [%- FOREACH data = row.DATA %]
         <option value="[% data.value %]" [% data.selected %]>[% data.oname %]</option>
        [%- END %]
@@ -36,16 +36,16 @@
      [%- END %]
      [%- END %]
      [%- IF display_copies %]
-     <td>[%- IF !show_headers %][%- 'Copies' | $T8 %][%- END %]<input name="[%- name_prefix %]copies" size="2" value="[% HTML.escape(copies) %]"></td>
+     <td id="print_options_input_copies">[%- IF !show_headers %][%- 'Copies' | $T8 %][%- END %]<input name="[%- name_prefix %]copies" id="[% id_prefix %]copies" size="2" value="[% HTML.escape(copies) %]"></td>
      [%- END %]
      [%- IF display_groupitems %]
-     <td>[%- IF !show_headers %][% 'Group Items' | $T8 %][%- END %]
-      <input name="[%- name_prefix %]groupitems" type="checkbox" class="checkbox" [% groupitems_checked %]>
+     <td id="print_options_input_groupitems">[%- IF !show_headers %][% 'Group Items' | $T8 %][%- END %]
+      <input name="[%- name_prefix %]groupitems" id="[% id_prefix %]groupitems" type="checkbox" class="checkbox" [% groupitems_checked %]>
      </td>
      [%- END %]
      [%- IF display_remove_draft %]
-     <td>[%- IF !show_headers %][% 'Remove Draft' | $T8 %][%- END %]
-      <input name="[%- name_prefix %]remove_draft" type="checkbox" class="checkbox" [% remove_draft_checked %]>
+     <td id="print_options_input_remove_draft">[%- IF !show_headers %][% 'Remove Draft' | $T8 %][%- END %]
+      <input name="[%- name_prefix %]remove_draft" id="[% id_prefix %]remove_draft" type="checkbox" class="checkbox" [% remove_draft_checked %]>
      </td>
      [%- END %]
     </tr>
diff --git a/templates/webpages/oe/form_footer.html b/templates/webpages/oe/form_footer.html
index aad73eb13..80892da5b 100644
--- a/templates/webpages/oe/form_footer.html
+++ b/templates/webpages/oe/form_footer.html
@@ -129,11 +129,7 @@
 </div>
 </div>
 
-<hr size="3" noshade>
-
-<p>[% print_options %]</p>
-
-<input type="hidden" name="action" value="dispatcher">
+<input type="hidden" name="action" id="action" value="dispatcher">
 <input type="hidden" name="saved_xyznumber" value="[% HTML.escape(saved_xyznumber) %]">
 [% L.hidden_tag("rowcount", rowcount) %]
 <input type="hidden" name="callback" value="[% callback | html %]">
@@ -146,6 +142,12 @@
 <div id="shipto_inputs" class="hidden">
  [%- PROCESS 'common/_ship_to_dialog.html' %]
 </div>
+
+<div id="email_inputs" class="hidden"></div>
+
+<div id="print_options" class="hidden">
+ [% print_options %]
+</div>
 </form>
 
 <div id="shipto_dialog" class="hidden"></div>
diff --git a/templates/webpages/oe/form_header.html b/templates/webpages/oe/form_header.html
index 6482185b2..7899ab324 100644
--- a/templates/webpages/oe/form_header.html
+++ b/templates/webpages/oe/form_header.html
@@ -16,7 +16,7 @@
     [%- END %]
 
 [%- FOREACH row = HIDDENS %]
-   <input type="hidden" name="[% HTML.escape(row.name) %]" value="[% HTML.escape(row.value) %]" >
+   <input type="hidden" name="[% HTML.escape(row.name) %]" id="[% HTML.escape(row.name) %]" value="[% HTML.escape(row.value) %]" >
 [%- END %]
 
     <input type="hidden" name="convert_from_oe_ids" value="[% HTML.escape(convert_from_oe_ids) %]">
@@ -190,17 +190,17 @@
 [%- IF is_order %]
                   <tr>
                     <th width="70%" align="right" nowrap>[% 'Order Number' | $T8 %]</th>
-                    <td><input name="ordnumber" size="11" value="[% HTML.escape(ordnumber) %]"></td>
+                    <td><input name="ordnumber" id="ordnumber" size="11" value="[% HTML.escape(ordnumber) %]"></td>
                   </tr>
 [%- END %]
                   <tr>
                     <th width="70%" align="right" nowrap>[% IF is_req_quo %][% 'RFQ Number' | $T8 %][% ELSE %][% 'Quotation Number' | $T8 %][% END %]</th>
-                    <td><input name="quonumber" size="11" value="[% HTML.escape(quonumber) %]"></td>
+                    <td><input name="quonumber" id="quonumber" size="11" value="[% HTML.escape(quonumber) %]"></td>
                   </tr>
 [%- IF is_order %]
                   <tr>
                     <th width="70%" align="right" nowrap>[% 'Customer Order Number' | $T8 %]</th>
-                    <td><input name="cusordnumber" size="11" value="[% HTML.escape(cusordnumber) %]"></td>
+                    <td><input name="cusordnumber" id="cusordnumber" size="11" value="[% HTML.escape(cusordnumber) %]"></td>
                   </tr>
 [%- END %]
                   <tr>
