Auftrags-Controller: Bemerkungen und interne Bemerkungen
[kivitendo-erp.git] / SL / Controller / CustomerVendor.pm
index fa9d000..dbe1827 100644 (file)
@@ -113,6 +113,18 @@ sub action_show {
 sub _save {
   my ($self) = @_;
 
+  my @errors = $self->{cv}->validate;
+  if (@errors) {
+    flash('error', @errors);
+    $self->_pre_render();
+    $self->render(
+      'customer_vendor/form',
+      title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
+      %{$self->{template_args}}
+    );
+    ::end_of_request();
+  }
+
   my $db = $self->{cv}->db;
 
   $db->do_transaction(sub {
@@ -140,7 +152,7 @@ sub _save {
 
     $self->{contact}->cp_cv_id($self->{cv}->id);
     if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
-      $self->{contact}->save();
+      $self->{contact}->save(cascade => 1);
     }
 
     if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
@@ -193,34 +205,23 @@ sub _save {
 sub action_save {
   my ($self) = @_;
 
-  if (!$self->{cv}->name) {
-    flash('error', t8('Customer missing!'));
-    $self->_pre_render();
-    $self->render(
-      'customer_vendor/form',
-      title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
-      %{$self->{template_args}}
-    );
-  } else {
-
-    $self->_save();
-
-    my @redirect_params = (
-      action => 'edit',
-      id     => $self->{cv}->id,
-      db     => ($self->is_vendor() ? 'vendor' : 'customer'),
-    );
+  $self->_save();
 
-    if ( $self->{contact}->cp_id ) {
-      push(@redirect_params, contact_id => $self->{contact}->cp_id);
-    }
+  my @redirect_params = (
+    action => 'edit',
+    id     => $self->{cv}->id,
+    db     => ($self->is_vendor() ? 'vendor' : 'customer'),
+  );
 
-    if ( $self->{shipto}->shipto_id ) {
-      push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
-    }
+  if ( $self->{contact}->cp_id ) {
+    push(@redirect_params, contact_id => $self->{contact}->cp_id);
+  }
 
-    $self->redirect_to(@redirect_params);
+  if ( $self->{shipto}->shipto_id ) {
+    push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
   }
+
+  $self->redirect_to(@redirect_params);
 }
 
 sub action_save_and_close {
@@ -365,7 +366,7 @@ sub action_delete_contact {
       }
     }) || die($db->error);
 
-    $self->{contact} = SL::DB::Contact->new();
+    $self->{contact} = $self->_new_contact_object;
   }
 
   $self->action_edit();
@@ -493,7 +494,7 @@ sub action_get_delivery {
 
      LEFT JOIN oe
        ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
-           AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe_vendor_id IS NOT NULL') ." )
+           AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
 
      ${where}
      ORDER BY ${arap}.transdate DESC LIMIT 15";
@@ -543,19 +544,27 @@ sub action_ajaj_get_contact {
   };
 
   $data->{contact_cvars} = {
-    map(
-      {
-        if ( $_->config->type eq 'number' ) {
-          $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
-        } else {
-          $_->config->name => $_->value;
-        }
+    map {
+      my $cvar   = $_;
+      my $result = { type => $cvar->config->type };
+
+      if ($cvar->config->type eq 'number') {
+        $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
+
+      } elsif ($result->{type} =~ m{customer|vendor|part}) {
+        my $object       = $cvar->value;
+        my $method       = $result->{type} eq 'part' ? 'description' : 'name';
+
+        $result->{id}    = int($cvar->number_value) || undef;
+        $result->{value} = $object ? $object->$method // '' : '';
+
+      } else {
+        $result->{value} = $cvar->value;
       }
-      grep(
-        { $_->is_valid; }
-        @{$self->{contact}->cvars_by_config}
-      )
-    )
+
+      ( $cvar->config->name => $result )
+
+    } grep { $_->is_valid } @{ $self->{contact}->cvars_by_config }
   };
 
   $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
@@ -603,7 +612,7 @@ sub action_ajaj_autocomplete {
 
   my @hashes = map {
    +{
-     value       => $_->name,
+     value       => $_->displayable_name,
      label       => $_->displayable_name,
      id          => $_->id,
      $number     => $_->$number,
@@ -637,7 +646,7 @@ sub is_orphaned {
   }
 
   my $arap      = $self->is_vendor ? 'ap' : 'ar';
-  my $num_args  = 2;
+  my $num_args  = 3;
 
   my $cv = $self->is_vendor ? 'vendor' : 'customer';
 
@@ -652,6 +661,13 @@ sub is_orphaned {
     SELECT a.id
     FROM oe a
     JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
+    WHERE ct.id = ?
+
+    UNION
+
+    SELECT a.id
+    FROM delivery_orders a
+    JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
     WHERE ct.id = ?';
 
 
@@ -679,11 +695,7 @@ sub _instantiate_args {
       $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
     }
   } else {
-    if ( $self->is_vendor() ) {
-      $self->{cv} = SL::DB::Vendor->new();
-    } else {
-      $self->{cv} = SL::DB::Customer->new();
-    }
+    $self->{cv} = $self->_new_customer_vendor_object;
   }
   $self->{cv}->assign_attributes(%{$::form->{cv}});
 
@@ -735,7 +747,7 @@ sub _instantiate_args {
   if ( $::form->{contact}->{cp_id} ) {
     $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
   } else {
-    $self->{contact} = SL::DB::Contact->new();
+    $self->{contact} = $self->_new_contact_object;
   }
   $self->{contact}->assign_attributes(%{$::form->{contact}});
 
@@ -786,18 +798,14 @@ sub _load_customer_vendor {
       die($::locale->text('Error'));
     }
   } else {
-    $self->{contact} = SL::DB::Contact->new();
+    $self->{contact} = $self->_new_contact_object;
   }
 }
 
 sub _create_customer_vendor {
   my ($self) = @_;
 
-  if ( $self->is_vendor() ) {
-    $self->{cv} = SL::DB::Vendor->new();
-  } else {
-    $self->{cv} = SL::DB::Customer->new();
-  }
+  $self->{cv} = $self->_new_customer_vendor_object;
   $self->{cv}->currency_id($::instance_conf->get_currency_id());
 
   $self->{note} = SL::DB::Note->new();
@@ -806,7 +814,7 @@ sub _create_customer_vendor {
 
   $self->{shipto} = SL::DB::Shipto->new();
 
-  $self->{contact} = SL::DB::Contact->new();
+  $self->{contact} = $self->_new_contact_object;
 }
 
 sub _pre_render {
@@ -946,10 +954,10 @@ sub init_customer_models {
     model        => 'Customer',
     sorted => {
       _default  => {
-        by => 'name',
+        by => 'customernumber',
         dir  => 1,
       },
-      name  => t8('name'),
+      customernumber => t8('Customer Number'),
     },
   );
 }
@@ -962,12 +970,29 @@ sub init_vendor_models {
     model      => 'Vendor',
     sorted => {
       _default  => {
-        by => 'name',
+        by => 'vendornumber',
         dir  => 1,
       },
-      name  => t8('name'),
+      vendornumber => t8('Vendor Number'),
     },
   );
 }
 
+sub _new_customer_vendor_object {
+  my ($self) = @_;
+
+  my $class  = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
+  return $class->new(
+    contacts         => [],
+    shipto           => [],
+    custom_variables => [],
+  );
+}
+
+sub _new_contact_object {
+  my ($self) = @_;
+
+  return SL::DB::Contact->new(custom_variables => []);
+}
+
 1;