From 47da14db313eaeb6cd2d260dc60e1c56a9437efc Mon Sep 17 00:00:00 2001
From: =?utf8?q?Jan=20B=C3=BCren?= <jan@kivitendo.de>
Date: Wed, 13 Feb 2019 10:41:45 +0100
Subject: [PATCH] =?utf8?q?generische=20E-Mail-Adresse=20f=C3=BCr=20Liefers?=
 =?utf8?q?cheine?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Ähnlich wie bei Verkaufsrechnungen gibt es generische
Empfänger für Lieferscheine beim E-Mail-Versand.
Die jetzige Konfiguration (nicht änderbar) entspricht
dem Wert Stammdaten und Ansprechpartner in CC.
Ist eine Stammdaten-Mail und ein Ansprechpartner definiert,
bzw. ausgewählt wird der Ansprechpartner in CC gesetzt und
die vorbelegte Anrede ist 'generisch'
---
 SL/DB/MetaSetup/Customer.pm                   |  7 +++---
 SL/Form.pm                                    |  2 +-
 bin/mozilla/io.pl                             | 24 ++++++++++++++-----
 .../customer_add_generic_mail_delivery.sql    |  5 ++++
 4 files changed, 28 insertions(+), 10 deletions(-)
 create mode 100644 sql/Pg-upgrade2/customer_add_generic_mail_delivery.sql

diff --git a/SL/DB/MetaSetup/Customer.pm b/SL/DB/MetaSetup/Customer.pm
index ee9995638..775ee8b9a 100644
--- a/SL/DB/MetaSetup/Customer.pm
+++ b/SL/DB/MetaSetup/Customer.pm
@@ -20,17 +20,18 @@ __PACKAGE__->meta->columns(
   city                      => { type => 'text' },
   commercial_court          => { type => 'text' },
   contact                   => { type => 'text' },
+  contact_origin            => { type => 'text' },
   country                   => { type => 'text' },
   creditlimit               => { type => 'numeric', default => '0', precision => 15, scale => 5 },
   currency_id               => { type => 'integer', not_null => 1 },
   customernumber            => { type => 'text' },
-  contact_origin            => { type => 'text' },
+  delivery_order_mail       => { type => 'text' },
   delivery_term_id          => { type => 'integer' },
   department_1              => { type => 'text' },
   department_2              => { type => 'text' },
   depositor                 => { type => 'text' },
   direct_debit              => { type => 'boolean', default => 'false' },
-  discount                  => { type => 'float', scale => 4 },
+  discount                  => { type => 'float', precision => 4, scale => 4 },
   email                     => { type => 'text' },
   fax                       => { type => 'text' },
   gln                       => { type => 'text' },
@@ -39,8 +40,8 @@ __PACKAGE__->meta->columns(
   hourly_rate               => { type => 'numeric', precision => 8, scale => 2 },
   iban                      => { type => 'text' },
   id                        => { type => 'integer', not_null => 1, sequence => 'id' },
-  itime                     => { type => 'timestamp', default => 'now()' },
   invoice_mail              => { type => 'text' },
+  itime                     => { type => 'timestamp', default => 'now()' },
   language                  => { type => 'text' },
   language_id               => { type => 'integer' },
   mandate_date_of_signature => { type => 'date' },
diff --git a/SL/Form.pm b/SL/Form.pm
index d4b39f6c1..55ac63320 100644
--- a/SL/Form.pm
+++ b/SL/Form.pm
@@ -1352,7 +1352,7 @@ sub generate_email_body {
   # Gentile Signora Ferrari,
   my $body = '';
 
-  if ($self->{cp_id} && !$params{inv_email}) {
+  if ($self->{cp_id} && !$params{record_email}) {
     my $givenname = SL::DB::Contact->load_cached($self->{cp_id})->cp_givenname; # for qw(gender givename name);
     my $name      = SL::DB::Contact->load_cached($self->{cp_id})->cp_name; # for qw(gender givename name);
     my $gender    = SL::DB::Contact->load_cached($self->{cp_id})->cp_gender; # for qw(gender givename name);
diff --git a/bin/mozilla/io.pl b/bin/mozilla/io.pl
index a4a416b95..9f5cadf24 100644
--- a/bin/mozilla/io.pl
+++ b/bin/mozilla/io.pl
@@ -2021,17 +2021,29 @@ sub _get_files_for_email_dialog {
 sub show_sales_purchase_email_dialog {
   my $email = '';
   my $email_cc = '';
-  my $inv_email;
+  my $record_email;
   if ($::form->{cp_id}) {
     $email = SL::DB::Contact->load_cached($::form->{cp_id})->cp_email;
   }
+  # write a dispatch table if a third type enters
+  # check record mail for sales_invoice
   if ($::form->{type} eq 'invoice' && (!$email || $::instance_conf->get_invoice_mail_settings ne 'cp')) {
     # check for invoice_mail if defined (vc.invoice_email)
-    $inv_email = SL::DB::Customer->load_cached($::form->{vc_id})->invoice_mail;
-    if ($inv_email) {
+    $record_email = SL::DB::Customer->load_cached($::form->{vc_id})->invoice_mail;
+    if ($record_email) {
       # check if cc for contact is also wanted
       $email_cc = $email if ($::instance_conf->get_invoice_mail_settings eq 'invoice_mail_cc_cp');
-      $email    = $inv_email;
+      $email    = $record_email;
+    }
+  }
+  # check record mail for sales_delivery_order
+  if ($::form->{type} eq 'sales_delivery_order') {
+    # check for deliver_order_mail if defined (vc.delivery_order_mail)
+    $record_email = SL::DB::Customer->load_cached($::form->{vc_id})->delivery_order_mail;
+    if ($record_email) {
+      # check if cc for contact is also wanted
+      $email_cc = $email; # always cc to cp
+      $email    = $record_email;
     }
   }
   # still no email? use general mail (vc.email)
@@ -2046,7 +2058,7 @@ sub show_sales_purchase_email_dialog {
     to                  => $email,
     cc                  => $email_cc,
     subject             => $::form->generate_email_subject,
-    message             => $::form->generate_email_body('inv_email' => $inv_email),
+    message             => $::form->generate_email_body('record_email' => $record_email),
     attachment_filename => $::form->generate_attachment_filename,
     js_send_function    => 'kivi.SalesPurchase.send_email()',
   };
@@ -2057,7 +2069,7 @@ sub show_sales_purchase_email_dialog {
     show_bcc    => $::auth->assert('email_bcc', 'may fail'),
     FILES       => \%files,
     is_customer => $::form->{vc} eq 'customer',
-    is_invoice_mail => $inv_email,
+    is_invoice_mail => ($record_email && $::form->{type} eq 'invoice'),
   });
 
   print $::form->ajax_response_header, $html;
diff --git a/sql/Pg-upgrade2/customer_add_generic_mail_delivery.sql b/sql/Pg-upgrade2/customer_add_generic_mail_delivery.sql
new file mode 100644
index 000000000..b091953f4
--- /dev/null
+++ b/sql/Pg-upgrade2/customer_add_generic_mail_delivery.sql
@@ -0,0 +1,5 @@
+-- @tag: customer_add_generic_mail_delivery
+-- @description: Lieferschein (generischer E-Mail-Empfänger)
+-- @depends: release_3_5_3
+ALTER TABLE customer ADD COLUMN delivery_order_mail text;
+
-- 
2.20.1