ticket auto-association in the correct spot. how the hell did it get merged there...
[freeside.git] / rt / lib / RT / Ticket_Overlay.pm
index a8d32ef..f4664fd 100644 (file)
@@ -556,6 +556,68 @@ sub Create {
 
     # }}}
 
+    # {{{ Deal with auto-customer association
+
+    #unless we already have (a) customer(s)...
+    unless ( $self->Customers->Count ) {
+
+      #first find any requestors with emails but *without* customer targets
+      my @NoCust_Requestors =
+        grep { $_->EmailAddress && ! $_->Customers->Count }
+             @{ $self->_Requestors->UserMembersObj->ItemsArrayRef };
+
+      for my $Requestor (@NoCust_Requestors) {
+
+         #perhaps the stuff in here should be in a User method??
+         my @Customers =
+           &RT::URI::freeside::email_search( email=>$Requestor->EmailAddress );
+
+         foreach my $custnum ( map $_->{'custnum'}, @Customers ) {
+
+           ## false laziness w/RT/Interface/Web_Vendor.pm
+           my @link = ( 'Type'   => 'MemberOf',
+                        'Target' => "freeside://freeside/cust_main/$custnum",
+                      );
+
+           my( $val, $msg ) = $Requestor->_AddLink(@link);
+           #XXX should do something with $msg# push @non_fatal_errors, $msg;
+
+         }
+
+      }
+
+      #find any requestors with customer targets
+  
+      my %cust_target = ();
+
+      my @Requestors =
+        grep { $_->Customers->Count }
+             @{ $self->_Requestors->UserMembersObj->ItemsArrayRef };
+  
+      foreach my $Requestor ( @Requestors ) {
+        foreach my $cust_link ( @{ $Requestor->Customers->ItemsArrayRef } ) {
+          $cust_target{ $cust_link->Target } = 1;
+        }
+      }
+  
+      #and then auto-associate this ticket with those customers
+  
+      foreach my $cust_target ( keys %cust_target ) {
+  
+        my @link = ( 'Type'   => 'MemberOf',
+                     #'Target' => "freeside://freeside/cust_main/$custnum",
+                     'Target' => $cust_target,
+                   );
+  
+        my( $val, $msg ) = $self->_AddLink(@link);
+        push @non_fatal_errors, $msg;
+  
+      }
+
+    }
+
+    # }}}
+
     # {{{ Add all the custom fields
 
     foreach my $arg ( keys %args ) {
@@ -642,7 +704,6 @@ sub Create {
         } else {
             $Owner = $DeferOwner;
             $self->__Set(Field => 'Owner', Value => $Owner->id);
-
         }
         $self->OwnerGroup->_AddMember(
             PrincipalId       => $Owner->PrincipalId,
@@ -1416,6 +1477,25 @@ sub Requestors {
 
 # }}}
 
+# {{{ sub _Requestors
+
+=head2 _Requestors
+
+Private non-ACLed variant of Reqeustors so that we can look them up for the
+purposes of customer auto-association during create.
+
+=cut
+
+sub _Requestors {
+    my $self = shift;
+
+    my $group = RT::Group->new($RT::SystemUser);
+    $group->LoadTicketRoleGroup(Type => 'Requestor', Ticket => $self->Id);
+    return ($group);
+}
+
+# }}}
+
 # {{{ sub Cc
 
 =head2 Cc
@@ -2204,7 +2284,13 @@ sub _Links {
 
     unless ( $self->{"$field$type"} ) {
         $self->{"$field$type"} = new RT::Links( $self->CurrentUser );
-        if ( $self->CurrentUserHasRight('ShowTicket') ) {
+
+        #not sure what this ACL was supposed to do... but returning the
+        # bare (unlimited) RT::Links certainly seems wrong, it causes the
+        # $Ticket->Customers method during creation to return results for every
+        # ticket...
+        #if ( $self->CurrentUserHasRight('ShowTicket') ) {
+
             # Maybe this ticket is a merged ticket
             my $Tickets = new RT::Tickets( $self->CurrentUser );
             # at least to myself
@@ -2221,7 +2307,7 @@ sub _Links {
             $self->{"$field$type"}->Limit( FIELD => 'Type',
                                            VALUE => $type )
               if ($type);
-        }
+        #}
     }
     return ( $self->{"$field$type"} );
 }