last bit of magic for RT ticket customer auto-association: look for requestor email...
authorivan <ivan>
Tue, 1 Apr 2008 08:43:33 +0000 (08:43 +0000)
committerivan <ivan>
Tue, 1 Apr 2008 08:43:33 +0000 (08:43 +0000)
FS/FS/cust_main.pm
FS/FS/cust_main_invoice.pm
rt/lib/RT/Ticket_Overlay.pm
rt/lib/RT/URI/freeside/Internal.pm

index b58a52c..37f98c6 100644 (file)
@@ -5515,6 +5515,72 @@ sub smart_search {
 
 }
 
+=item email_search
+
+Accepts the following options: I<email>, the email address to search for.  The
+email address will be searched for as an email invoice destination and as an
+svc_acct account.
+
+#Any additional options are treated as an additional qualifier on the search
+#(i.e. I<agentnum>).
+
+Returns a (possibly empty) array of FS::cust_main objects (but usually just
+none or one).
+
+=cut
+
+sub email_search {
+  my %options = @_;
+
+  local($DEBUG) = 1;
+
+  my $email = delete $options{'email'};
+
+  #we're only being used by RT at the moment... no agent virtualization yet
+  #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+  my @cust_main = ();
+
+  if ( $email =~ /([^@]+)\@([^@]+)/ ) {
+
+    my ( $user, $domain ) = ( $1, $2 );
+
+    warn "$me smart_search: searching for $user in domain $domain"
+      if $DEBUG;
+
+    push @cust_main,
+      map $_->cust_main,
+          qsearch( {
+                     'table'     => 'cust_main_invoice',
+                     'hashref'   => { 'dest' => $email },
+                   }
+                 );
+
+    push @cust_main,
+      map  $_->cust_main,
+      grep $_,
+      map  $_->cust_svc->cust_pkg,
+          qsearch( {
+                     'table'     => 'svc_acct',
+                     'hashref'   => { 'username' => $user, },
+                     'extra_sql' =>
+                       'AND ( SELECT domain FROM svc_domain
+                                WHERE svc_acct.domsvc = svc_domain.svcnum
+                            ) = '. dbh->quote($domain),
+                   }
+                 );
+  }
+
+  my %saw = ();
+  @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
+
+  warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
+    if $DEBUG;
+
+  @cust_main;
+
+}
+
 =item check_and_rebuild_fuzzyfiles
 
 =cut
index 71029d0..11d5a44 100644 (file)
@@ -159,6 +159,17 @@ sub address {
   }
 }
 
+=item cust_main
+
+Returns the parent customer object (see L<FS::cust_main>).
+
+=cut
+
+sub cust_main {
+  my $self = shift;
+  qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
+}
+
 =back
 
 =head1 BUGS
index 1916bc4..c8b02e7 100644 (file)
@@ -721,6 +721,31 @@ sub Create {
     #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 = ();
index 9ca0630..0bc4c90 100644 (file)
@@ -107,6 +107,12 @@ sub smart_search { #Subroutine
 
 }
 
+sub email_search { #Subroutine
+
+  return map { { $_->hash } } &FS::cust_main::email_search(@_);
+
+}
+
 sub small_custview {
 
   return &FS::CGI::small_custview(@_);