From cb2abccdef0236f43a1712cb2b39db4ce57a2d40 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Sven=20Sch=C3=B6ling?= <s.schoeling@linet-services.de>
Date: Tue, 17 Jan 2012 11:35:08 +0100
Subject: [PATCH] =?utf8?q?Ansprechpartner=20l=C3=B6schbar=20machen.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Ansprechpartner können jetzt gelöscht werden. Ist der Ansprechpartner noch in
verwendung wird eine Nachricht an den User gegeben und der Ansprechpartner nur
auf ungültig gesetzt. Ungültige Ansprechpartner werden in den Belegmasken nicht
angezeigt, es sei denn der Beleg wird aus der Datenbank geholt und der
ungültige Ansprechpartner ist vorausgewählt. Beim ersten Erneuern bei dem ein
anderer Ansprechpartner ausgewählt ist, der ungültige nicht mehr zur Auswahl
gestellt.

Es gibt noch keinen Aufräummechanismus für unbenutzte Zombieansprechpartner in
der Datenbank.

Beim Testen in Bonn hatte ich massive Probleme mit einem Setup unter Ubuntu
10.04 mit Postgres 8.4.10, Perl 5.10.1, DBI 1.609, RDBO 0.785, RDB 0.758, RO
0.856. Kompilierung ist scheinbar ohne erkennbare Zusammenhänge unter CGI und
FCGI schon zur compile time abgestürzt.

Sollten diese Probleme nochmal auftauchen, bitte melden.
---
 SL/DB/Contact.pm                       | 21 +++++++++++++++++++++
 bin/mozilla/ct.pl                      | 23 +++++++++++++----------
 bin/mozilla/do.pl                      | 12 ++++++++++--
 bin/mozilla/ir.pl                      | 13 ++++++++++---
 bin/mozilla/is.pl                      | 13 ++++++++++---
 bin/mozilla/oe.pl                      | 13 ++++++++++---
 locale/de/all                          |  2 ++
 templates/webpages/ct/_contact.html    |  8 ++++++--
 templates/webpages/ct/form_header.html |  3 ---
 templates/webpages/do/form_header.html |  9 +--------
 templates/webpages/ir/form_header.html |  8 +-------
 templates/webpages/is/form_header.html |  2 +-
 templates/webpages/oe/form_header.html |  2 +-
 13 files changed, 86 insertions(+), 43 deletions(-)

diff --git a/SL/DB/Contact.pm b/SL/DB/Contact.pm
index aa74bea3d..4b6c35e42 100644
--- a/SL/DB/Contact.pm
+++ b/SL/DB/Contact.pm
@@ -14,6 +14,27 @@ use SL::DB::Helper::CustomVariables (
 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
 __PACKAGE__->meta->make_manager_class;
 
+sub used {
+  my ($self) = @_;
+
+  return unless $self->cp_id;
+
+  require SL::DB::Order;
+  require SL::DB::Invoice;
+  require SL::DB::PurchaseInvoice;
+  require SL::DB::DeliveryOrder;
+
+  return SL::DB::Manager::Order->get_all_count(query => [ cp_id => $self->cp_id ])
+       + SL::DB::Manager::Invoice->get_all_count(query => [ cp_id => $self->cp_id ])
+       + SL::DB::Manager::PurchaseInvoice->get_all_count(query => [ cp_id => $self->cp_id ])
+       + SL::DB::Manager::DeliveryOrder->get_all_count(query => [ cp_id => $self->cp_id ]);
+}
+
+sub detach {
+  $_[0]->cp_cv_id(undef);
+  $_[0]->save;
+}
+
 sub full_name {
   my ($self) = @_;
   die 'not an accessor' if @_ > 1;
diff --git a/bin/mozilla/ct.pl b/bin/mozilla/ct.pl
index de0a43659..b47d8e262 100644
--- a/bin/mozilla/ct.pl
+++ b/bin/mozilla/ct.pl
@@ -51,6 +51,7 @@ use SL::CT;
 use SL::CVar;
 use SL::DB::Business;
 use SL::DB::Default;
+use SL::Helper::Flash;
 use SL::ReportGenerator;
 
 require "bin/mozilla/common.pl";
@@ -730,23 +731,25 @@ sub delete_shipto {
 }
 
 sub delete_contact {
-  $main::lxdebug->enter_sub();
-
-  $main::auth->assert('customer_vendor_edit');
+  $::lxdebug->enter_sub;
+  $::auth->assert('customer_vendor_edit');
 
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
+  CT->get_contact(\%::myconfig, $::form);
 
-  CT->get_contact(\%myconfig, \%$form);
+  my $contact = SL::DB::Manager::Contact->find_by(cp_id => $::form->{cp_id});
 
-  unless ($form->{cp_used}) {
-    CT->delete_contact($form->{cp_id});
-    @$form{ grep /^cp_/, keys %$form } = undef;
+  if ($contact->used) {
+    $contact->detach;
+    flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
+  } else {
+    $contact->delete;
+    flash('info', $::locale->text('Contact deleted.'));
   }
+  delete $::form->{$_} for grep /^cp_/, keys %$::form;
 
   edit();
 
-  $main::lxdebug->leave_sub();
+  $::lxdebug->leave_sub;
 }
 
 sub ajax_autocomplete {
diff --git a/bin/mozilla/do.pl b/bin/mozilla/do.pl
index 9d28bbb0d..39cba4a2b 100644
--- a/bin/mozilla/do.pl
+++ b/bin/mozilla/do.pl
@@ -266,8 +266,7 @@ sub form_header {
           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
 
   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
-  $form->get_lists("contacts"       => "ALL_CONTACTS",
-                   "shipto"         => "ALL_SHIPTO",
+  $form->get_lists("shipto"         => "ALL_SHIPTO",
                    "projects"       => {
                      "key"          => "ALL_PROJECTS",
                      "all"          => 0,
@@ -280,6 +279,15 @@ sub form_header {
                    "departments"    => "ALL_DEPARTMENTS",
                    "business_types" => "ALL_BUSINESS_TYPES",
     );
+  $::form->{ALL_CONTACTS}          = SL::DB::Manager::Contact->get_all(query => [
+    or => [
+      cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
+      and      => [
+        cp_cv_id => undef,
+        cp_id    => $::form->{cp_id} * 1
+      ]
+    ]
+  ]);
 
   map { $_->{value} = "$_->{description}--$_->{id}" } @{ $form->{ALL_DEPARTMENTS} };
   map { $_->{value} = "$_->{name}--$_->{id}"        } @{ $form->{ALL_VC} };
diff --git a/bin/mozilla/ir.pl b/bin/mozilla/ir.pl
index 25831b2ea..810e06a47 100644
--- a/bin/mozilla/ir.pl
+++ b/bin/mozilla/ir.pl
@@ -283,8 +283,7 @@ sub form_header {
   my @old_project_ids = ($form->{"globalproject_id"});
   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
 
-  $form->get_lists("contacts"      => "ALL_CONTACTS",
-                   "shipto"        => "ALL_SHIPTO",
+  $form->get_lists("shipto"        => "ALL_SHIPTO",
                    "projects"      => { "key"    => "ALL_PROJECTS",
                                         "all"    => 0,
                                         "old_id" => \@old_project_ids },
@@ -295,9 +294,17 @@ sub form_header {
                    "departments"   => "all_departments",
                    "price_factors" => "ALL_PRICE_FACTORS");
 
+  $TMPL_VAR{ALL_CONTACTS}          = SL::DB::Manager::Contact->get_all(query => [
+    or => [
+      cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
+      and      => [
+        cp_cv_id => undef,
+        cp_id    => $::form->{cp_id} * 1
+      ]
+    ]
+  ]);
   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
 #  $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
-  $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
   # customer
diff --git a/bin/mozilla/is.pl b/bin/mozilla/is.pl
index f438f1c9b..685e7b182 100644
--- a/bin/mozilla/is.pl
+++ b/bin/mozilla/is.pl
@@ -300,8 +300,7 @@ sub form_header {
   my @old_project_ids = ($form->{"globalproject_id"});
   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
 
-  $form->get_lists("contacts"      => "ALL_CONTACTS",
-                   "shipto"        => "ALL_SHIPTO",
+  $form->get_lists("shipto"        => "ALL_SHIPTO",
                    "projects"      => { "key"    => "ALL_PROJECTS",
                                         "all"    => 0,
                                         "old_id" => \@old_project_ids },
@@ -313,9 +312,17 @@ sub form_header {
                    "departments"   => "all_departments",
                    "price_factors" => "ALL_PRICE_FACTORS");
 
+  $TMPL_VAR{ALL_CONTACTS}          = SL::DB::Manager::Contact->get_all(query => [
+    or => [
+      cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
+      and      => [
+        cp_cv_id => undef,
+        cp_id    => $::form->{cp_id} * 1
+      ]
+    ]
+  ]);
   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
   $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
-  $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
   # customer
diff --git a/bin/mozilla/oe.pl b/bin/mozilla/oe.pl
index aa6df6d61..11231515d 100644
--- a/bin/mozilla/oe.pl
+++ b/bin/mozilla/oe.pl
@@ -327,8 +327,7 @@ sub form_header {
   my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
 
   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
-  $form->get_lists("contacts"      => "ALL_CONTACTS",
-                   "shipto"        => "ALL_SHIPTO",
+  $form->get_lists("shipto"        => "ALL_SHIPTO",
                    "projects"      => { "key"      => "ALL_PROJECTS",
                                         "all"      => 0,
                                         "old_id"   => \@old_project_ids },
@@ -343,9 +342,17 @@ sub form_header {
                    "price_factors" => "ALL_PRICE_FACTORS");
 
   # label subs
+  $TMPL_VAR{ALL_CONTACTS}          = SL::DB::Manager::Contact->get_all(query => [
+    or => [
+      cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
+      and      => [
+        cp_cv_id => undef,
+        cp_id    => $::form->{cp_id} * 1
+      ]
+    ]
+  ]);
   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
   $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
-  $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
   # vendor/customer
diff --git a/locale/de/all b/locale/de/all
index 66b9e8f6d..2b737d41d 100644
--- a/locale/de/all
+++ b/locale/de/all
@@ -417,6 +417,8 @@ $self->{texts} = {
   'Confirmation'                => 'Auftragsbestätigung',
   'Contact'                     => 'Kontakt',
   'Contact Person'              => 'Ansprechpartner',
+  'Contact deleted.'            => 'Ansprechpartner gelöscht.',
+  'Contact is in use and was flagged invalid.' => 'Ansprechpartner ist noch in Verwendung, und wurde als ungültig markiert.',
   'Contact person (surname)'    => 'Ansprechpartner (Nachname)',
   'Contacts'                    => 'Ansprechpartner',
   'Continue'                    => 'Weiter',
diff --git a/templates/webpages/ct/_contact.html b/templates/webpages/ct/_contact.html
index 8da17e9cb..995c260e9 100644
--- a/templates/webpages/ct/_contact.html
+++ b/templates/webpages/ct/_contact.html
@@ -1,11 +1,11 @@
 [% USE L %][% USE HTML %][% USE T8 %][% USE LxERP %]
-    <table id="contact_table">
+    <table>
      <input type="hidden" name="cp_id" value="[% HTML.escape(cp_id) %]">
      <tr>
       <th align="left">[% 'Contacts' | $T8 %]</th>
       <td>
        [%- L.select_tag('cp_id', L.options_for_select(CONTACTS, default => cp_id, with_empty => 1, empty_title => LxERP.t8('New contact'), value => 'cp_id', title_sub => \contacts_label),
-                        onchange => "\$('#contact_table').load('ct.pl?action=get_contact&id=' + \$('#cvid').attr('value') + '&db=' + \$('#db').attr('value') + '&cp_id=' + \$('#cp_id').attr('value'))") %]
+                        onchange => "\$('#contacts').load('ct.pl?action=get_contact&id=' + \$('#cvid').attr('value') + '&db=' + \$('#db').attr('value') + '&cp_id=' + \$('#cp_id').attr('value'))") %]
       </td>
      </tr>
 
@@ -115,3 +115,7 @@
       <td><input name="bcc" size="40" value="[% HTML.escape(bcc) %]"></td>
      </tr>
     </table>
+
+    <input type="submit" id="delete_contact" name="action" value="[% 'Delete Contact' | $T8 %]" [% IF cp_used %]'disabled'[% END %]>
+
+    <br style="clear: left" />
diff --git a/templates/webpages/ct/form_header.html b/templates/webpages/ct/form_header.html
index 9671cab24..4d6ee5a29 100644
--- a/templates/webpages/ct/form_header.html
+++ b/templates/webpages/ct/form_header.html
@@ -314,9 +314,6 @@
 
    <div id="contacts" class="tabcontent">
     [% INCLUDE 'ct/_contact.html' %]
-
-    <input type="submit" id="delete_contact" name="action" value="[% 'Delete Contact' | $T8 %]" disabled>
-    <br style="clear: left" />
    </div>
 
    <div id="deliveries" class="tabcontent">
diff --git a/templates/webpages/do/form_header.html b/templates/webpages/do/form_header.html
index b41ea72d4..3ed1be230 100644
--- a/templates/webpages/do/form_header.html
+++ b/templates/webpages/do/form_header.html
@@ -137,14 +137,7 @@
           [%- HTML.escape(row.cp_name) %][%- IF row.cp_abteilung %] ([% HTML.escape(row.cp_abteilung) %])[% END -%]
           [%- END %]
           [%- ELSE %]
-          <select name="cp_id" class="fixed_width" >
-           <option></option>
-           [%- FOREACH row = ALL_CONTACTS %]
-           <option value="[% HTML.escape(row.cp_id) %]"[% IF cp_id == row.cp_id %] selected[% END %]>
-            [%- HTML.escape(row.cp_name) %][%- IF row.cp_abteilung %] ([% HTML.escape(row.cp_abteilung) %])[% END -%]
-           </option>
-           [%- END %]
-          </select>
+            [% L.select_tag('cp_id', L.options_for_select(ALL_CONTACTS, default=cp_id, value='cp_id', title='full_name_dep', with_empty=1), style='width: 250px') %]
           [%- END %]
          </td>
         </tr>
diff --git a/templates/webpages/ir/form_header.html b/templates/webpages/ir/form_header.html
index d070b4d74..697e3e54f 100644
--- a/templates/webpages/ir/form_header.html
+++ b/templates/webpages/ir/form_header.html
@@ -54,13 +54,7 @@
         <tr>
           <th align="right">[% 'Contact Person' | $T8 %]</th>
           <td>
-            [%- INCLUDE 'generic/multibox.html'
-                 name       = 'cp_id',
-                 style      = 'width: 250px',
-                 DATA       = ALL_CONTACTS,
-                 id_key     = 'cp_id',
-                 label_sub  = 'contact_labels',
-                 show_empty = 1 -%]
+            [% L.select_tag('cp_id', L.options_for_select(ALL_CONTACTS, default=cp_id, value='cp_id', title='full_name_dep', with_empty=1), style='width: 250px') %]
           </td>
         </tr>
 [%- END %]
diff --git a/templates/webpages/is/form_header.html b/templates/webpages/is/form_header.html
index 34b340cc2..ca2b5364d 100644
--- a/templates/webpages/is/form_header.html
+++ b/templates/webpages/is/form_header.html
@@ -55,7 +55,7 @@
         <tr>
           <th align="right">[% 'Contact Person' | $T8 %]</th>
           <td>
-            [% L.select_tag('cp_id', L.options_for_select(ALL_CONTACTS, default=cp_id, value='cp_id', title_sub=\contact_labels, with_empty=1), style='width: 250px') %]
+            [% L.select_tag('cp_id', L.options_for_select(ALL_CONTACTS, default=cp_id, value='cp_id', title='full_name_dep', with_empty=1), style='width: 250px') %]
           </td>
         </tr>
 [%- END %]
diff --git a/templates/webpages/oe/form_header.html b/templates/webpages/oe/form_header.html
index 5ec0db5ca..3c1703380 100644
--- a/templates/webpages/oe/form_header.html
+++ b/templates/webpages/oe/form_header.html
@@ -58,7 +58,7 @@
                   <tr>
                     <th align="right">[% 'Contact Person' | $T8 %]</th>
                     <td>
-                      [% L.select_tag('cp_id', L.options_for_select(ALL_CONTACTS, default=cp_id, value='cp_id', title_sub=\contact_labels, with_empty=1), style='width: 250px') %]
+                      [% L.select_tag('cp_id', L.options_for_select(ALL_CONTACTS, default=cp_id, value='cp_id', title='full_name_dep', with_empty=1), style='width: 250px') %]
                     </td>
                   </tr>
 [%- END %]
-- 
2.20.1