1 package FS::TicketSystem::RT_External;
4 use vars qw( $DEBUG $me $conf $dbh $default_queueid $external_url
6 $priority_field $priority_field_queue $field
10 use FS::Record qw(qsearchs);
15 $me = '[FS::TicketSystem::RT_External]';
18 FS::UID->install_callback( sub {
20 $default_queueid = $conf->config('ticket_system-default_queueid');
21 $priority_reverse = $conf->exists('ticket_system-priority_reverse');
23 $conf->config('ticket_system-custom_priority_field');
24 if ( $priority_field ) {
25 $priority_field_queue =
26 $conf->config('ticket_system-custom_priority_field_queue');
28 $field = $priority_field_queue
29 ? $priority_field_queue. '.%7B'. $priority_field. '%7D'
32 $priority_field_queue = '';
38 if ($conf->config('ticket_system') eq 'RT_External') {
39 my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
40 $dbh = FS::DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 })
41 or die "RT_External FS::DBI->connect error: $FS::DBI::errstr\n";
43 $external_url = $conf->config('ticket_system-rt_external_url');
46 #kludge... should *use* the id... but good enough for now
47 if ( $priority_field_queue =~ /^(\d+)$/ ) {
49 my $sql = 'SELECT Name FROM Queues WHERE Id = ?';
50 my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
51 $sth->execute($id) or die $sth->errstr. " executing $sql";
53 $priority_field_queue = $sth->fetchrow_arrayref->[0];
59 sub num_customer_tickets {
60 my( $self, $custnum, $priority ) = @_;
62 my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
64 my $sql = "SELECT COUNT(*) $from_sql";
65 warn "$me $sql (@param)" if $DEBUG;
66 my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
67 $sth->execute(@param) or die $sth->errstr. " executing $sql";
69 $sth->fetchrow_arrayref->[0];
73 sub customer_tickets {
74 my( $self, $custnum, $limit, $priority ) = @_;
77 my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
82 position(Tickets.Status in 'newopenstalledresolvedrejecteddeleted')
84 ". ( length($priority) ? ", ObjectCustomFieldValues.Content" : '' )."
87 Priority ". ( $priority_reverse ? 'ASC' : 'DESC' ). ",
91 warn "$me $sql (@param)" if $DEBUG;
92 my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
93 $sth->execute(@param) or die $sth->errstr. "executing $sql";
95 #munge column names??? #httemplate/view/cust_main/tickets.html has column
96 #names that might not make sense now...
97 $sth->fetchall_arrayref({});
101 sub service_tickets {
102 warn "service_tickets not available with RT_External.\n";
106 sub comments_on_tickets {
107 my ($self, $custnum, $limit, $time ) = @_;
110 my( $from_sql, @param) = $self->_from_customer( $custnum );
112 SELECT transactions.*, Attachments.content, Tickets.subject
114 JOIN Attachments ON( Attachments.transactionid = transactions.id )
115 JOIN Tickets ON ( Tickets.id = transactions.objectid )
116 JOIN Links ON ( Tickets.id = Links.LocalBase
117 AND Links.Base LIKE '%/ticket/' || Tickets.id )
120 WHERE ( Status = 'new' OR Status = 'open' OR Status = 'stalled' )
121 AND Target = 'freeside://freeside/cust_main/$custnum'
122 AND transactions.type = 'Comment'
123 AND transactions.created >= (SELECT TIMESTAMP WITH TIME ZONE 'epoch' + $time * INTERVAL '1 second')
126 cluck $sql if $DEBUG > 0;
128 $dbh->selectall_arrayref( $sql, { Slice => {} } ) or die $dbh->errstr . " $sql";
132 my( $self, $custnum, $priority ) = @_;
137 if ( defined($priority) ) {
139 my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM Queues
140 WHERE Queues.Name = ? )
141 OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
143 my $customfield_sql =
145 SELECT CustomFields.Id FROM CustomFields
146 JOIN ObjectCustomFields
147 ON ( CustomFields.id = ObjectCustomFields.CustomField )
148 WHERE LookupType = 'RT::Queue-RT::Ticket'
153 push @param, $priority_field,
154 $priority_field_queue,
155 $priority_field_queue;
157 if ( length($priority) ) {
159 # and ? = ( select content from TicketCustomFieldValues
160 # where ticket = tickets.id
161 # and customfield = ( select id from customfields
167 unshift @param, $priority;
169 $join = "JOIN ObjectCustomFieldValues
170 ON ( Tickets.id = ObjectCustomFieldValues.ObjectId )";
172 $where = " AND Content = ?
173 AND ObjectCustomFieldValues.Disabled != 1
174 AND ObjectType = 'RT::Ticket'
175 AND $customfield_sql";
179 $where = " AND NOT EXISTS ( SELECT 1 FROM ObjectCustomFieldValues
180 WHERE ObjectId = Tickets.id
181 AND ObjectType = 'RT::Ticket'
191 JOIN Queues ON ( Tickets.Queue = Queues.id )
192 JOIN Users ON ( Tickets.Owner = Users.id )
193 JOIN Links ON ( Tickets.id = Links.LocalBase
194 AND Links.Base LIKE '%/ticket/' || Tickets.id )
197 WHERE ( ". join(' OR ', map "Status = '$_'", $self->statuses ). " )
198 AND Target = 'freeside://freeside/cust_main/$custnum'
208 my @statuses = grep { ! /^\s*$/ } $conf->config('cust_main-ticket_statuses');
209 @statuses = (qw( new open stalled )) unless scalar(@statuses);
213 sub href_customer_tickets {
214 my($self, $custnum) = (shift, shift);
215 if ( $custnum =~ /^(\d+)$/ ) {
216 return $self->href_search_tickets("MemberOf = 'freeside://freeside/cust_main/$1'");
218 warn "bad custnum $custnum"; return '';
221 sub href_service_tickets {
222 warn "service_tickets not available with RT_External.\n";
226 sub href_search_tickets {
227 my( $self, $where ) = ( shift, shift );
228 my( $priority, @statuses);
231 $priority = $opt->{'priority'};
232 @statuses = $opt->{'statuses'} ? @{$opt->{'statuses'}} : $self->statuses;
235 @statuses = $self->statuses;
238 #my $href = $self->baseurl;
240 #i snarfed this from an RT bookmarked search, then unescaped (some of) it with
241 #perl -npe 's/%([0-9A-F]{2})/pack('C', hex($1))/eg;'
245 "Search/Results.html?Order=ASC&".
247 #MemberOf = 'freeside://freeside/cust_main/$custnum' ".
248 " AND ( ". join(' OR ', map "Status = '$_'", @statuses ). " ) "
251 if ( defined($priority) && $field && $priority_field_queue ) {
252 $href .= " AND Queue = '$priority_field_queue' ";
254 if ( defined($priority) && $field ) {
255 $href .= " AND 'CF.$field' ";
257 $href .= "= '$priority' ";
259 $href .= "IS 'NULL' "; #this is "RTQL", not SQL
265 #eventually should unescape all of it...
267 $href .= '&RowsPerPage=50'.
268 '&OrderBy=id&Page=1'.
269 '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
271 'Ticket%2FDisplay.html%3Fid%3D__id__%22%3E__id__%3C%2Fa%3E%3C%2Fb%3E%2FTITLE%3A%23%27%2C%20%0A%27%3Cb%3E%3Ca%20href%3D%22'.
273 'Ticket%2FDisplay.html%3Fid%3D__id__%22%3E__Subject__%3C%2Fa%3E%3C%2Fb%3E%2FTITLE%3ASubject%27%2C%20%0A%27__Status__%27%2C%20';
275 if ( defined($priority) && $field ) {
276 $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
279 $href .= '%0A%27__QueueName__%27%2C%20%0A%27__OwnerName__%27%2C%20%0A%27__Priority__%27%2C%20%0A%27__NEWLINE__%27%2C%20%0A%27%27%2C%20%0A%27%3Csmall%3E__Requestors__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__CreatedRelative__%3C%2Fsmall%3E%27%2C';
281 if ( defined($priority) && $field ) {
282 $href .= '%20%0A%27__-__%27%2C';
285 $href .= '%20%0A%27%3Csmall%3E__ToldRelative__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__LastUpdatedRelative__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__TimeLeft__%3C%2Fsmall%3E%27';
290 $self->baseurl. $href;
294 sub href_params_new_ticket {
295 # my( $self, $custnum_or_cust_main, $requestors ) = @_;
296 # no longer takes $custnum--it must be an object
297 my ( $self, $object, $requestors ) = @_;
298 my $cust_main; # for default requestors
299 if ( $object->isa('FS::cust_main') ) {
300 $cust_main = $object;
302 elsif ( $object->isa('FS::svc_Common') ) {
303 $object = $object->cust_svc;
304 $cust_main = $object->cust_pkg->cust_main if ( $object->cust_pkg );
306 elsif ( $object->isa('FS::cust_svc') ) {
307 $cust_main = $object->cust_pkg->cust_main if ( $object->cust_pkg );
310 # explicit $requestors > config option > invoicing_list
311 $requestors = $conf->config('ticket_system-requestor')
313 $requestors = $cust_main->invoicing_list_emailonly_scalar
314 if (!$requestors) and defined($cust_main);
316 my $subtype = $object->table;
317 my $pkey = $object->get($object->primary_key);
320 'Queue' => ($cust_main->agent->ticketing_queueid || $default_queueid),
321 'new-MemberOf'=> "freeside://freeside/$subtype/$pkey",
322 'Requestors' => $requestors,
325 ( $self->baseurl.'Ticket/Create.html', @param );
328 sub href_new_ticket {
331 my( $base, @param ) = $self->href_params_new_ticket(@_);
333 my $uri = new URI $base;
334 $uri->query_form(@param);
340 my($self, $ticketnum) = @_;
341 $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
347 my $sql = "SELECT id, Name FROM Queues WHERE Disabled = 0";
348 my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
349 $sth->execute() or die $sth->errstr. " executing $sql";
351 map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
356 my($self, $queueid) = @_;
358 return '' unless $queueid;
360 my $sql = "SELECT Name FROM Queues WHERE id = ?";
361 my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
362 $sth->execute($queueid) or die $sth->errstr. " executing $sql";
364 my $rows = $sth->fetchrow_arrayref;
365 $rows ? $rows->[0] : '';
374 sub _retrieve_single_value {
375 my( $self, $sql ) = @_;
377 warn "$me $sql" if $DEBUG;
378 my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
379 $sth->execute or die $sth->errstr. "executing $sql";
381 my $arrayref = $sth->fetchrow_arrayref;
382 $arrayref ? $arrayref->[0] : $arrayref;
385 sub transaction_creator {
386 my( $self, $transaction_id ) = @_;
388 my $sql = "SELECT Name FROM Transactions JOIN Users ON ".
389 "Transactions.Creator=Users.id WHERE Transactions.id = ".
392 $self->_retrieve_single_value($sql);
395 sub transaction_ticketid {
396 my( $self, $transaction_id ) = @_;
398 my $sql = "SELECT ObjectId FROM Transactions WHERE Transactions.id = ".
401 $self->_retrieve_single_value($sql);
404 sub transaction_subject {
405 my( $self, $transaction_id ) = @_;
407 my $sql = "SELECT Subject FROM Transactions JOIN Tickets ON ObjectId=".
408 "Tickets.id WHERE Transactions.id = ". $transaction_id;
410 $self->_retrieve_single_value($sql);
413 sub transaction_status {
414 my( $self, $transaction_id ) = @_;
416 my $sql = "SELECT Status FROM Transactions JOIN Tickets ON ObjectId=".
417 "Tickets.id WHERE Transactions.id = ". $transaction_id;
419 $self->_retrieve_single_value($sql);
423 warn "WARNING: no access rights available w/ external RT";
428 return 'create_ticket unimplemented w/external RT (write something w/RT::Client::REST?)';
431 sub init { } #unimplemented
433 sub selfservice_priority { '' } #unimplemented