+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;
+ 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}"};
+ }
+ $ticket_info{'is_unreplied'} = $t->IsUnreplied;
+ my $svcnums = [
+ map { $_->Target =~ /cust_svc\/(\d+)/; $1 }
+ @{ $t->Services->ItemsArrayRef }
+ ];
+ $ticket_info{'svcnums'} = $svcnums;
+
+ return \%ticket_info;
+}
+
+=item create_ticket SESSION_HASHREF, OPTION => VALUE ...
+
+Class method. Creates a ticket. If there is an error, returns the scalar
+error, otherwise returns the newly created RT::Ticket object.
+
+Accepts the following options:
+
+=over 4
+
+=item queue
+
+Queue name or Id
+
+=item subject
+
+Ticket subject
+
+=item requestor
+
+Requestor email address or arrayref of addresses
+
+=item cc
+
+Cc: email address or arrayref of addresses
+
+=item message
+
+Ticket message
+
+=item mime_type
+
+MIME type to use for message. Defaults to text/plain. Specifying text/html
+can be useful to use HTML markup in message.
+
+=item custnum
+
+Customer number (see L<FS::cust_main>) to associate with ticket.
+
+=item svcnum
+
+Service number (see L<FS::cust_svc>) to associate with ticket. Will also
+associate the customer who has this service (unless the service is unlinked).
+
+=back
+
+=cut
+
+sub create_ticket {
+ my($self, $session, %param) = @_;
+
+ $session = $self->session($session);
+
+ my $Queue = RT::Queue->new($session->{'CurrentUser'});
+ $Queue->Load( $param{'queue'} );
+
+ my $req = ref($param{'requestor'})
+ ? $param{'requestor'}
+ : ( $param{'requestor'} ? [ $param{'requestor'} ] : [] );
+
+ my $cc = ref($param{'cc'})
+ ? $param{'cc'}
+ : ( $param{'cc'} ? [ $param{'cc'} ] : [] );
+
+ my $mimeobj = MIME::Entity->build(
+ 'Data' => $param{'message'},
+ 'Type' => ( $param{'mime_type'} || 'text/plain' ),
+ );
+
+ my %ticket = (
+ 'Queue' => $Queue->Id,
+ 'Subject' => $param{'subject'},
+ 'Requestor' => $req,
+ 'Cc' => $cc,
+ 'MIMEObj' => $mimeobj,
+ );
+ warn Dumper(\%ticket) if $DEBUG > 1;
+
+ my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
+ my( $id, $Transaction, $ErrStr );
+ {
+ local $SIG{__DIE__};
+ ( $id, $Transaction, $ErrStr ) = $Ticket->Create( %ticket );
+ }
+ return $ErrStr if $id == 0;
+
+ warn "ticket got id $id\n" if $DEBUG;
+
+ #XXX check errors adding custnum/svcnum links (put it in a transaction)...
+ # but we do already know they're good
+
+ if ( $param{'custnum'} ) {
+ my( $val, $msg ) = $Ticket->_AddLink(
+ 'Type' => 'MemberOf',
+ 'Target' => 'freeside://freeside/cust_main/'. $param{'custnum'},
+ );
+ }
+
+ if ( $param{'svcnum'} ) {
+ my( $val, $msg ) = $Ticket->_AddLink(
+ 'Type' => 'MemberOf',
+ 'Target' => 'freeside://freeside/cust_svc/'. $param{'svcnum'},
+ );
+ }
+
+ $Ticket;
+}
+
+=item get_ticket SESSION_HASHREF, OPTION => VALUE ...
+
+Class method. Retrieves a ticket. If there is an error, returns the scalar
+error. Otherwise, currently returns a slightly tricky data structure containing
+the ticket's attributes, a list of the linked customers, each transaction's
+content, description, and create time.
+
+Accepts the following options:
+
+=over 4
+
+=item ticket_id
+
+The ticket id
+
+=back
+
+=cut
+
+sub get_ticket {
+ my($self, $session, %param) = @_;
+
+ $session = $self->session($session);
+
+ my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
+ my $ticketid = $Ticket->Load( $param{'ticket_id'} );
+ return 'Could not load ticket' unless $ticketid;
+
+ my @custs = ();
+ foreach my $link ( @{ $Ticket->Customers->ItemsArrayRef } ) {
+ my $cust = $link->Target;
+ push @custs, $1 if $cust =~ /\/(\d+)$/;
+ }
+
+ my @txns = ();
+ my $transactions = $Ticket->Transactions;
+ while ( my $transaction = $transactions->Next ) {
+ my $t = { created => $transaction->Created,
+ content => $transaction->Content,
+ description => $transaction->Description,
+ type => $transaction->Type,
+ };
+ push @txns, $t;
+ }
+
+ { txns => [ @txns ],
+ custs => [ @custs ],
+ fields => _ticket_info($Ticket),
+ };
+}
+
+=item get_ticket_object SESSION_HASHREF, OPTION => VALUE...
+
+Class method. Retrieve the RT::Ticket object with the specified
+ticket_id. If custnum is supplied, will also check that the object
+is a member of that customer. If there is no ticket or the custnum
+check fails, returns nothing. The meaning of that case is
+"to this customer, the ticket does not exist".
+
+Options:
+
+=over 4
+
+=item ticket_id
+
+=item custnum
+
+=back
+
+=cut
+
+sub get_ticket_object {
+ my $self = shift;
+ my ($session, %opt) = @_;
+ $session = $self->session(shift);
+ # use a small search here so we can check ticket ownership
+ my $query;
+ if ( $opt{'ticket_id'} =~ /^(\d+)$/ ) {
+ $query = "id = $1";