sorting ticket results by customer custnum or name, RT#8784
[freeside.git] / rt / lib / RT / Tickets_Overlay.pm
index e8d350d..8faf3f0 100644 (file)
@@ -136,6 +136,7 @@ our %FIELD_METADATA = (
     QueueAdminCc     => [ 'WATCHERFIELD'    => 'AdminCc' => 'Queue', ], #loc_left_pair
     QueueWatcher     => [ 'WATCHERFIELD'    => undef     => 'Queue', ], #loc_left_pair
     CustomFieldValue => [ 'CUSTOMFIELD', ], #loc_left_pair
+    DateCustomFieldValue => [ 'DATECUSTOMFIELD', ],
     CustomField      => [ 'CUSTOMFIELD', ], #loc_left_pair
     CF               => [ 'CUSTOMFIELD', ], #loc_left_pair
     Updated          => [ 'TRANSDATE', ], #loc_left_pair
@@ -160,6 +161,7 @@ our %dispatch = (
     WATCHERFIELD    => \&_WatcherLimit,
     MEMBERSHIPFIELD => \&_WatcherMembershipLimit,
     CUSTOMFIELD     => \&_CustomFieldLimit,
+    DATECUSTOMFIELD => \&_DateCustomFieldLimit,
     HASATTRIBUTE    => \&_HasAttributeLimit,
 );
 our %can_bundle = ();# WATCHERFIELD => "yes", );
@@ -1340,6 +1342,101 @@ sub _CustomFieldJoin {
     return ($TicketCFs, $CFs);
 }
 
+=head2 _DateCustomFieldLimit
+
+Limit based on CustomFields of type Date
+
+Meta Data:
+  none
+
+=cut
+
+sub _DateCustomFieldLimit {
+    my ( $self, $_field, $op, $value, %rest ) = @_;
+
+    my $field = $rest{'SUBKEY'} || die "No field specified";
+
+    # For our sanity, we can only limit on one queue at a time
+
+    my ($queue, $cfid, $column);
+    ($queue, $field, $cfid, $column) = $self->_CustomFieldDecipher( $field );
+
+# If we're trying to find custom fields that don't match something, we
+# want tickets where the custom field has no value at all.  Note that
+# we explicitly don't include the "IS NULL" case, since we would
+# otherwise end up with a redundant clause.
+
+    my $null_columns_ok;
+    if ( ( $op =~ /^NOT LIKE$/i ) or ( $op eq '!=' ) ) {
+        $null_columns_ok = 1;
+    }
+
+    my $cfkey = $cfid ? $cfid : "$queue.$field";
+    my ($TicketCFs, $CFs) = $self->_CustomFieldJoin( $cfkey, $cfid, $field );
+
+    $self->_OpenParen;
+
+    if ( $CFs && !$cfid ) {
+        $self->SUPER::Limit(
+            ALIAS           => $CFs,
+            FIELD           => 'Name',
+            VALUE           => $field,
+            ENTRYAGGREGATOR => 'AND',
+        );
+    }
+
+    $self->_OpenParen if $null_columns_ok;
+
+    my $date = RT::Date->new( $self->CurrentUser );
+    $date->Set( Format => 'unknown', Value => $value );
+
+    if ( $op eq "=" ) {
+
+        # if we're specifying =, that means we want everything on a
+        # particular single day.  in the database, we need to check for >
+        # and < the edges of that day.
+
+        $date->SetToMidnight( Timezone => 'server' );
+        my $daystart = $date->ISO;
+        $date->AddDay;
+        my $dayend = $date->ISO;
+
+        $self->_OpenParen;
+
+        $self->_SQLLimit(
+            ALIAS    => $TicketCFs,
+            FIELD    => 'Content',
+            OPERATOR => ">=",
+            VALUE    => $daystart,
+            %rest,
+        );
+
+        $self->_SQLLimit(
+            ALIAS    => $TicketCFs,
+            FIELD    => 'Content',
+            OPERATOR => "<=",
+            VALUE    => $dayend,
+            %rest,
+            ENTRYAGGREGATOR => 'AND',
+        );
+
+        $self->_CloseParen;
+
+    }
+    else {
+        $self->_SQLLimit(
+            ALIAS    => $TicketCFs,
+            FIELD    => 'Content',
+            OPERATOR => $op,
+            VALUE    => $date->ISO,
+            %rest,
+        );
+    }
+
+    $self->_CloseParen;
+
+}
+
 =head2 _CustomFieldLimit
 
 Limit based on CustomFields
@@ -1627,6 +1724,7 @@ sub OrderByCols {
             next;
         }
         if ( $row->{FIELD} !~ /\./ ) {
+
             my $meta = $self->FIELDS->{ $row->{FIELD} };
             unless ( $meta ) {
                 push @res, $row;
@@ -1733,7 +1831,60 @@ sub OrderByCols {
            }
 
            push @res, { %$row, FIELD => "Priority", ORDER => $order } ;
-       }
+
+       } elsif ( $field eq 'Customer' ) { #Freeside
+
+           my $linkalias = $self->Join(
+               TYPE   => 'LEFT',
+               ALIAS1 => 'main',
+               FIELD1 => 'id',
+               TABLE2 => 'Links',
+               FIELD2 => 'LocalBase'
+           );
+
+           $self->SUPER::Limit(
+               LEFTJOIN => $linkalias,
+               FIELD    => 'Type',
+               OPERATOR => '=',
+               VALUE    => 'MemberOf',
+           );
+           $self->SUPER::Limit(
+               LEFTJOIN => $linkalias,
+               FIELD    => 'Target',
+               OPERATOR => 'STARTSWITH',
+               VALUE    => 'freeside://freeside/cust_main/',
+           );
+
+           if ( $subkey eq 'Number' ) {
+
+               push @res, { %$row,
+                            ALIAS => $linkalias,
+                            FIELD => "CAST(SUBSTR(Target,31) AS INTEGER)",
+                            #ORDER => ($row->{ORDER} || 'ASC')
+                          };
+
+           } elsif ( $subkey eq 'Name' ) {
+
+              my $custalias = $self->Join(
+                  TYPE   => 'LEFT',
+                  #ALIAS1 => $linkalias,
+                  #FIELD1 => 'CAST(SUBSTR(Target,31) AS INTEGER)',
+                  EXPRESSION => "CAST(SUBSTR($linkalias.Target,31) AS INTEGER)",
+                  TABLE2 => 'cust_main',
+                  FIELD2 => 'custnum',
+                  
+              );
+
+              my $field = "COALESCE( $custalias.company,
+                                     $custalias.last || ', ' || $custalias.first
+                                   )";
+
+              push @res, { %$row, ALIAS => '', FIELD => $field };
+
+           }
+
+       } #Freeside
+
        else {
            push @res, $row;
        }
@@ -1743,6 +1894,53 @@ sub OrderByCols {
 
 # }}}
 
+#this duplicates/ovverrides the DBIx::SearchBuilder version..
+# we need to fix the "handle FUNCTION(FIELD)" stuff and this is much easier
+# than patching SB
+# but does this have other terrible ramifications?  maybe a flag to trigger
+# this specific case?
+sub _OrderClause {
+    my $self = shift;
+
+    return '' unless $self->{'order_by'};
+
+    my $clause = '';
+    foreach my $row ( @{$self->{'order_by'}} ) {
+
+        my %rowhash = ( ALIAS => 'main',
+                        FIELD => undef,
+                        ORDER => 'ASC',
+                        %$row
+                      );
+        if ($rowhash{'ORDER'} && $rowhash{'ORDER'} =~ /^des/i) {
+            $rowhash{'ORDER'} = "DESC";
+        }
+        else {
+            $rowhash{'ORDER'} = "ASC";
+        }
+        $rowhash{'ALIAS'} = 'main' unless defined $rowhash{'ALIAS'};
+
+        if ( defined $rowhash{'ALIAS'} and
+             $rowhash{'FIELD'} and
+             $rowhash{'ORDER'} ) {
+
+            if ( length $rowhash{'ALIAS'} && $rowhash{'FIELD'} =~ /^((\w+\()+)(.*\)+)$/ ) {
+                # handle 'FUNCTION(FIELD)' formatted fields
+                $rowhash{'FIELD'} = $1. $rowhash{'ALIAS'}. '.'. $3;
+                $rowhash{'ALIAS'} = '';
+            }
+
+            $clause .= ($clause ? ", " : " ");
+            $clause .= $rowhash{'ALIAS'} . "." if length $rowhash{'ALIAS'};
+            $clause .= $rowhash{'FIELD'} . " ";
+            $clause .= $rowhash{'ORDER'};
+        }
+    }
+    $clause = " ORDER BY$clause " if $clause;
+
+    return $clause;
+}
+
 # {{{ Limit the result set based on content
 
 # {{{ sub Limit
@@ -2667,6 +2865,11 @@ sub LimitCustomField {
         $args{CUSTOMFIELD} = $CF->Id;
     }
 
+    # Handle special customfields types
+    if ($CF->Type eq 'Date') {
+        $args{FIELD} = 'DateCustomFieldValue';
+    }
+
     #If we are looking to compare with a null value.
     if ( $args{'OPERATOR'} =~ /^is$/i ) {
         $args{'DESCRIPTION'}