1 package FS::TicketSystem::RT_External;
4 use vars qw( $conf $default_queueid
5 $priority_field $priority_field_queue $field
9 use FS::Record qw(qsearchs);
12 FS::UID->install_callback( sub {
13 my $conf = new FS::Conf;
14 $default_queueid = $conf->config('ticket_system-default_queueid');
16 $conf->config('ticket_system-custom_priority_field');
17 if ( $priority_field ) {
18 $priority_field_queue =
19 $conf->config('ticket_system-custom_priority_field_queue');
20 $field = $priority_field_queue
21 ? $priority_field_queue. '.%7B'. $priority_field. '%7D'
24 $priority_field_queue = '';
30 if ($conf->config('ticket_system') eq 'RT_External') {
31 my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
32 $dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 })
33 or die "RT_External DBI->connect error: $DBI::errstr\n";
35 $external_url = $conf->config('ticket_system-rt_external_url');
40 sub num_customer_tickets {
41 my( $self, $custnum, $priority ) = @_;
43 my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
45 my $sql = "SELECT COUNT(*) $from_sql";
46 my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
47 $sth->execute(@param) or die $sth->errstr. " executing $sql";
49 $sth->fetchrow_arrayref->[0];
53 sub customer_tickets {
54 my( $self, $custnum, $limit, $priority ) = @_;
57 my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
58 my $sql = "SELECT tickets.*, queues.name".
59 ( length($priority) ? ", objectcustomfieldvalues.content" : '' ).
60 " $from_sql ORDER BY priority DESC LIMIT $limit";
61 my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
62 $sth->execute(@param) or die $sth->errstr. "executing $sql";
64 #munge column names??? #httemplate/view/cust_main/tickets.html has column
65 #names that might not make sense now...
66 $sth->fetchall_arrayref({});
71 my( $self, $custnum, $priority ) = @_;
76 if ( defined($priority) ) {
78 my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM queues
79 WHERE queues.name = ? )
80 OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
84 SELECT CustomFields.Id FROM CustomFields
85 JOIN ObjectCustomFields
86 ON ( CustomFields.id = ObjectCustomFields.CustomField )
87 WHERE LookupType = 'RT::Queue-RT::Ticket'
92 push @param, $priority_field,
93 $priority_field_queue,
94 $priority_field_queue;
96 if ( length($priority) ) {
98 # and ? = ( select content from TicketCustomFieldValues
99 # where ticket = tickets.id
100 # and customfield = ( select id from customfields
106 unshift @param, $priority;
108 $join = "JOIN ObjectCustomFieldValues
109 ON ( tickets.id = ObjectCustomFieldValues.ObjectId )";
111 $where = " AND content = ?
112 AND ObjectType = 'RT::Ticket'
113 AND $customfield_sql";
118 "AND 0 = ( SELECT count(*) FROM ObjectCustomFieldValues
119 WHERE ObjectId = tickets.id
120 AND ObjectType = 'RT::Ticket'
130 JOIN queues ON ( tickets.queue = queues.id )
131 JOIN links ON ( tickets.id = links.localbase )
133 WHERE ( status = 'new' OR status = 'open' OR status = 'stalled' )
134 AND target = 'freeside://freeside/cust_main/$custnum'
142 sub href_customer_tickets {
143 my( $self, $custnum, $priority ) = @_;
145 my $href = $self->baseurl;
147 #i snarfed this from an RT bookmarked search, it could be unescaped in the
148 #source for readability and run through uri_escape
150 'Search/Results.html?Order=ASC&Query=%20MemberOf%20%3D%20%27freeside%3A%2F%2Ffreeside%2Fcust_main%2F'.
152 '%27%20%20AND%20%28%20Status%20%3D%20%27open%27%20%20OR%20Status%20%3D%20%27new%27%20%20OR%20Status%20%3D%20%27stalled%27%20%29%20'
155 if ( defined($priority) && $field && $priority_field_queue ) {
156 $href .= 'AND%20Queue%20%3D%20%27'. $priority_field_queue. '%27%20';
158 if ( defined($priority) && $field ) {
159 $href .= '%20AND%20%27CF.'. $field. '%27%20';
161 $href .= '%3D%20%27'. $priority. '%27%20';
163 $href .= 'IS%20%27NULL%27%20';
167 $href .= '&Rows=100'.
168 '&OrderBy=id&Page=1'.
169 '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
171 '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'.
173 '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';
175 if ( defined($priority) && $field ) {
176 $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
179 $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';
181 if ( defined($priority) && $field ) {
182 $href .= '%20%0A%27__-__%27%2C';
185 $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';
191 sub href_new_ticket {
192 my( $self, $custnum_or_cust_main, $requestors ) = @_;
194 my( $custnum, $cust_main );
195 if ( ref($custnum_or_cust_main) ) {
196 $cust_main = $custnum_or_cust_main;
197 $custnum = $cust_main->custnum;
199 $custnum = $custnum_or_cust_main;
200 $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
202 my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
205 'Ticket/Create.html?'.
207 "&new-MemberOf=freeside://freeside/cust_main/$custnum".
208 ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
213 my($self, $ticketnum) = @_;
214 $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
220 my $sql = "SELECT id, name FROM queues WHERE disabled = 0";
221 my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
222 $sth->execute() or die $sth->errstr. " executing $sql";
224 map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
229 my($self, $queueid) = @_;
231 return '' unless $queueid;
233 my $sql = "SELECT name FROM queues WHERE id = ?";
234 my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
235 $sth->execute($queueid) or die $sth->errstr. " executing $sql";
237 $sth->fetchrow_arrayref->[0];