TimeWorked-like custom fields, RT#11168
[freeside.git] / rt / lib / RT / Ticket_Overlay.pm
index 97bcdf5..e1809a3 100644 (file)
@@ -617,11 +617,16 @@ sub Create {
                     next;
                 }
             }
-            
+
+            #don't show transactions for reminders
+            my $silent = ( !$args{'_RecordTransaction'}
+                           || $self->Type eq 'reminder'
+                         );
+
             my ( $wval, $wmsg ) = $self->_AddLink(
                 Type                          => $LINKTYPEMAP{$type}->{'Type'},
                 $LINKTYPEMAP{$type}->{'Mode'} => $link,
-                Silent                        => !$args{'_RecordTransaction'},
+                Silent                        => $silent,
                 'Silent'. ( $LINKTYPEMAP{$type}->{'Mode'} eq 'Base'? 'Target': 'Base' )
                                               => 1,
             );
@@ -631,6 +636,69 @@ 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;
+  
+      }
+
+    }
+
+    # }}}
+
     # Now that we've created the ticket and set up its metadata, we can actually go and check OwnTicket on the ticket itself. 
     # This might be different than before in cases where extensions like RTIR are doing clever things with RT's ACL system
     if (  $DeferOwner ) { 
@@ -646,7 +714,6 @@ sub Create {
         } else {
             $Owner = $DeferOwner;
             $self->__Set(Field => 'Owner', Value => $Owner->id);
-
         }
         $self->OwnerGroup->_AddMember(
             PrincipalId       => $Owner->PrincipalId,
@@ -654,7 +721,8 @@ sub Create {
         );
     }
 
-    if ( $args{'_RecordTransaction'} ) {
+    #don't make a transaction or fire off any scrips for reminders either
+    if ( $args{'_RecordTransaction'} && $self->Type ne 'reminder' ) {
 
         # {{{ Add a transaction for the create
         my ( $Trans, $Msg, $TransObj ) = $self->_NewTransaction(
@@ -666,7 +734,8 @@ sub Create {
 
         if ( $self->Id && $Trans ) {
 
-            $TransObj->UpdateCustomFields(ARGSRef => \%args);
+          #$TransObj->UpdateCustomFields(ARGSRef => \%args);
+            $TransObj->UpdateCustomFields(%args);
 
             $RT::Logger->info( "Ticket " . $self->Id . " created in queue '" . $QueueObj->Name . "' by " . $self->CurrentUser->Name );
             $ErrStr = $self->loc( "Ticket [_1] created in queue '[_2]'", $self->Id, $QueueObj->Name );
@@ -1428,6 +1497,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
@@ -2135,6 +2223,7 @@ sub _RecordNote {
         NoteType     => 'Correspond',
         TimeTaken    => 0,
         CommitScrips => 1,
+        CustomFields => {},
         @_
     );
 
@@ -2191,6 +2280,7 @@ sub _RecordNote {
              TimeTaken => $args{'TimeTaken'},
              MIMEObj   => $args{'MIMEObj'}, 
              CommitScrips => $args{'CommitScrips'},
+             CustomFields => $args{'CustomFields'},
     );
 
     unless ($Trans) {
@@ -2225,6 +2315,16 @@ sub _Links {
         return $links;
     }
 
+    # without this you will also get RT::User(s) instead of tickets!
+    if ($field == 'Base' and $type == 'MemberOf') {
+        my $rtname = RT->Config->Get('rtname');
+        $links->Limit(
+                       FIELD    => 'Base',
+                       OPERATOR => 'STARTSWITH',
+                       VALUE    => "fsck.com-rt://$rtname/ticket/",
+                     );
+    }
+
     # Maybe this ticket is a merge ticket
     my $limit_on = 'Local'. $field;
     # at least to myself