+=item customer_tickets CUSTNUM [ LIMIT ] [ PRIORITYVALUE ]
+
+Replacement for the one in RT_External so that we can access custom fields
+properly.
+
+=cut
+
+sub _customer_tickets_search {
+ my ( $self, $custnum, $limit, $priority ) = @_;
+
+ $custnum =~ /^\d+$/ or die "invalid custnum: $custnum";
+ $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);
+
+ my $rtql = "MemberOf = 'freeside://freeside/cust_main/$custnum'";
+
+ 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";
+ }
+ }
+
+ $rtql .= ' AND ( ' .
+ join(' OR ', map { "Status = '$_'" } $self->statuses) .
+ ' )';
+
+ 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 customer_tickets {
+ my $Tickets = _customer_tickets_search(@_);
+
+ 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 ) = @_;
+ my $Tickets = $self->_customer_tickets_search($custnum, 0, $priority);
+ return $Tickets->CountAll;
+}
+
+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.
+ 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;
+ foreach my $CF ( @{ $t->CustomFields->ItemsArrayRef } ) {
+ 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}"};
+ }
+ return \%ticket_info;
+}
+