+=item customer_tickets CUSTNUM [ PARAMS ]
+
+Replacement for the one in RT_External so that we can access custom fields
+properly. Accepts a hashref with the following parameters:
+
+number - custnum/svcnum
+
+limit
+
+priority
+
+status
+
+queueid
+
+resolved - only return tickets resolved after this timestamp
+
+=cut
+
+# create an RT::Tickets object for a specified custnum or svcnum
+
+sub _tickets_search {
+ my $self = shift;
+ my $type = shift;
+
+ my( $number, $limit, $priority, $status, $queueid, $opt );
+ if ( ref($_[0]) eq 'HASH' ) {
+ $opt = shift;
+ $number = $$opt{'number'};
+ $limit = $$opt{'limit'};
+ $priority = $$opt{'priority'};
+ $status = $$opt{'status'};
+ $queueid = $$opt{'queueid'};
+ } else {
+ ( $number, $limit, $priority, $status, $queueid ) = @_;
+ $opt = {};
+ }
+
+ $type =~ /^Customer|Service$/ or die "invalid type: $type";
+ $number =~ /^\d+$/ or die "invalid custnum/svcnum: $number";
+ $limit =~ /^\d+$/ or die "invalid limit: $limit";
+
+ my $session = $self->session();
+ my $CurrentUser = $session->{CurrentUser}
+ or die "unable to create an RT session";
+
+ my $Tickets = RT::Tickets->new($CurrentUser);
+
+ # "Customer.number" searches tickets linked via cust_svc also
+ my $rtql = "$type.number = $number";
+
+ if ( defined( $priority ) ) {
+ my $custom_priority = FS::Conf->new->config('ticket_system-custom_priority_field');
+ if ( length( $priority ) ) {
+ $rtql .= " AND CF.{$custom_priority} = '$priority'";
+ }
+ else {
+ $rtql .= " AND CF.{$custom_priority} IS NULL";
+ }
+ }
+
+ my @statuses;
+ if ( defined($status) && $status ) {
+ if ( ref($status) ) {
+ if ( ref($status) eq 'HASH' ) {
+ @statuses = grep $status->{$_}, keys %$status;
+ } elsif ( ref($status) eq 'ARRAY' ) {
+ @statuses = @$status;
+ } else {
+ #what should be the failure mode here? die? return no tickets?
+ die 'unknown status ref '. ref($status);
+ }
+ } else {
+ @statuses = ( $status );
+ }
+ @statuses = grep /^\w+$/, @statuses; #injection prevention
+ } else {
+ @statuses = $self->statuses;
+ }
+
+ $rtql .= ' AND ( '.
+ join(' OR ', map { "Status = '$_'" } @statuses).
+ ' ) ';
+
+ $rtql .= " AND Queue = $queueid " if $queueid;
+
+ if ($$opt{'resolved'}) {
+ $rtql .= " AND Resolved >= " . dbh->quote(time2str('%Y-%m-%d %H:%M:%S',$$opt{'resolved'}));
+ }
+
+ warn "$me _customer_tickets_search:\n$rtql\n" if $DEBUG;
+ $Tickets->FromSQL($rtql);
+
+ $Tickets->RowsPerPage($limit);
+ warn "\n\n" . $Tickets->BuildSelectQuery . "\n\n" if $DEBUG > 1;
+
+ return $Tickets;
+}
+
+sub href_customer_tickets {
+ my ($self, $custnum) = (shift, shift);
+ if ($custnum =~ /^(\d+)$/) {
+ return $self->href_search_tickets("Customer.number = $custnum", @_);
+ }
+ warn "bad custnum $custnum"; '';
+}
+
+sub href_service_tickets {
+ my ($self, $svcnum) = (shift, shift);
+ if ($svcnum =~ /^(\d+)$/ ) {
+ return $self->href_search_tickets("Service.number = $svcnum", @_);
+ }
+ warn "bad svcnum $svcnum"; '';
+}
+
+sub customer_tickets {
+ my $self = shift;
+ my $Tickets = $self->_tickets_search('Customer', @_);
+
+ my $conf = FS::Conf->new;
+ my $priority_order =
+ $conf->exists('ticket_system-priority_reverse') ? 'ASC' : 'DESC';
+
+ my @order_by = (
+ { FIELD => 'Priority', ORDER => $priority_order },
+ { FIELD => 'Id', ORDER => 'DESC' },
+ );
+
+ $Tickets->OrderByCols(@order_by);
+
+ my @tickets;
+ while ( my $t = $Tickets->Next ) {
+ push @tickets, _ticket_info($t);
+ }
+
+ return \@tickets;
+}
+
+sub num_customer_tickets {
+ my ( $self, $custnum, $priority ) = @_;
+ $self->_tickets_search('Customer', $custnum, 0, $priority)->CountAll;
+}
+
+sub service_tickets {
+ my $self = shift;
+ my $Tickets = $self->_tickets_search('Service', @_);
+
+ my $conf = FS::Conf->new;
+ my $priority_order =
+ $conf->exists('ticket_system-priority_reverse') ? 'ASC' : 'DESC';
+
+ my @order_by = (
+ { FIELD => 'Priority', ORDER => $priority_order },
+ { FIELD => 'Id', ORDER => 'DESC' },
+ );
+
+ $Tickets->OrderByCols(@order_by);
+
+ my @tickets;
+ while ( my $t = $Tickets->Next ) {
+ push @tickets, _ticket_info($t);
+ }
+
+ return \@tickets;
+}
+
+sub _ticket_info {
+ # Takes an RT::Ticket; returns a hashref of the ticket's fields, including
+ # custom fields. Also returns custom and selfservice priority values as
+ # _custom_priority and _selfservice_priority, and the IsUnreplied property
+ # as is_unreplied.
+ my $t = shift;
+
+ my $custom_priority =
+ FS::Conf->new->config('ticket_system-custom_priority_field') || '';
+ my $ss_priority = selfservice_priority();
+
+ my %ticket_info;
+ foreach my $name ( $t->ReadableAttributes ) {
+ # lowercase names, and skip attributes with non-scalar values
+ $ticket_info{lc($name)} = $t->$name if !ref($t->$name);
+ }
+ $ticket_info{'owner'} = $t->OwnerObj->Name;
+ $ticket_info{'queue'} = $t->QueueObj->Name;
+ $ticket_info{'_cf_sort_order'} = {};
+ my $cf_sort = 0;
+ foreach my $CF ( @{ $t->CustomFields->ItemsArrayRef } ) {
+ $ticket_info{'_cf_sort_order'}{$CF->Name} = $cf_sort++;
+ my $name = 'CF.{'.$CF->Name.'}';
+ $ticket_info{$name} = $t->CustomFieldValuesAsString($CF->Id);
+ }
+ # make this easy to find
+ if ( $custom_priority ) {
+ $ticket_info{'content'} = $ticket_info{"CF.{$custom_priority}"};
+ }
+ if ( $ss_priority ) {
+ $ticket_info{'_selfservice_priority'} = $ticket_info{"CF.{$ss_priority}"};
+ }
+ $ticket_info{'is_unreplied'} = $t->IsUnreplied;
+ my $svcnums = [
+ map { $_->Target =~ /cust_svc\/(\d+)/; $1 }
+ @{ $t->Services->ItemsArrayRef }
+ ];
+ $ticket_info{'svcnums'} = $svcnums;
+
+ return \%ticket_info;
+}
+