agent option to select RT queue
[freeside.git] / FS / FS / TicketSystem / RT_External.pm
1 package FS::TicketSystem::RT_External;
2
3 use strict;
4 use vars qw( $conf $default_queueid
5              $priority_field $priority_field_queue $field
6              $dbh $external_url );
7 use URI::Escape;
8 use FS::UID qw(dbh);
9 use FS::Record qw(qsearchs);
10 use FS::cust_main;
11
12 FS::UID->install_callback( sub { 
13   my $conf = new FS::Conf;
14   $default_queueid = $conf->config('ticket_system-default_queueid');
15   $priority_field =
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'
22                   : $priority_field;
23   } else {
24     $priority_field_queue = '';
25     $field = '';
26   }
27
28   $external_url = '';
29   $dbh = dbh;
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";
34
35     $external_url = $conf->config('ticket_system-rt_external_url');
36   }
37
38 } );
39
40 sub num_customer_tickets {
41   my( $self, $custnum, $priority ) = @_;
42
43   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
44
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";
48
49   $sth->fetchrow_arrayref->[0];
50
51 }
52
53 sub customer_tickets {
54   my( $self, $custnum, $limit, $priority ) = @_;
55   $limit ||= 0;
56
57   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
58   my $sql = "select tickets.*, queues.name".
59             ( length($priority) ? ", ticketcustomfieldvalues.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";
63
64   #munge column names???  #httemplate/view/cust_main/tickets.html has column
65   #names that might not make sense now...
66   $sth->fetchall_arrayref({});
67
68 }
69
70 sub _from_customer {
71   my( $self, $custnum, $priority ) = @_;
72
73   my @param = ();
74   my $join = '';
75   my $where = '';
76   if ( defined($priority) ) {
77
78     my $queue_sql = " customfields.queue = ( select id from queues
79                                               where queues.name = ? )
80                       or ( ? = '' and customfields.queue = 0 )";
81
82     if ( length($priority) ) {
83       #$where = "    
84       #  and ? = ( select content from TicketCustomFieldValues
85       #             where ticket = tickets.id
86       #               and customfield = ( select id from customfields
87       #                                    where name = ?
88       #                                      and ( $queue_sql )
89       #                                 )
90       #          )
91       #";
92       push @param, $priority;
93
94       $join = "join TicketCustomFieldValues
95                  on ( tickets.id = TicketCustomFieldValues.ticket )";
96       
97       $where = "and content = ?
98                 and customfield = ( select id from customfields
99                                      where name = ?
100                                        and ( $queue_sql )
101                                   )
102                ";
103     } else {
104       $where =
105                "and 0 = ( select count(*) from TicketCustomFieldValues
106                            where ticket = tickets.id
107                              and customfield = ( select id from customfields
108                                                   where name = ?
109                                                     and ( $queue_sql )
110                                                )
111                         )
112                ";
113     }
114     push @param, $priority_field,
115                  $priority_field_queue,
116                  $priority_field_queue;
117   }
118
119   my $sql = "
120                     from tickets
121                     join queues on ( tickets.queue = queues.id )
122                     join links on ( tickets.id = links.localbase )
123                     $join 
124        where ( status = 'new' or status = 'open' or status = 'stalled' )
125          and target = 'freeside://freeside/cust_main/$custnum'
126          $where
127   ";
128
129   ( $sql, @param );
130
131 }
132
133 sub href_customer_tickets {
134   my( $self, $custnum, $priority ) = @_;
135
136   my $href = $self->baseurl;
137
138   #i snarfed this from an RT bookmarked search, it could be unescaped in the
139   #source for readability and run through uri_escape
140   $href .= 
141     'Search/Results.html?Order=ASC&Query=%20MemberOf%20%3D%20%27freeside%3A%2F%2Ffreeside%2Fcust_main%2F'.
142     $custnum.
143     '%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'
144   ;
145
146   if ( defined($priority) && $field && $priority_field_queue ) {
147     $href .= 'AND%20Queue%20%3D%20%27'. $priority_field_queue. '%27%20';
148   }
149   if ( defined($priority) && $field ) {
150     $href .= '%20AND%20%27CF.'. $field. '%27%20';
151     if ( $priority ) {
152       $href .= '%3D%20%27'. $priority. '%27%20';
153     } else {
154       $href .= 'IS%20%27NULL%27%20';
155     }
156   }
157
158   $href .= '&Rows=100'.
159            '&OrderBy=id&Page=1'.
160            '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
161            $self->baseurl.
162            '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'.
163            $self->baseurl.
164            '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';
165
166   if ( defined($priority) && $field ) {
167     $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
168   }
169
170   $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';
171
172   if ( defined($priority) && $field ) {
173     $href .=   '%20%0A%27__-__%27%2C';
174   }
175
176   $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';
177
178   $href;
179
180 }
181
182 sub href_new_ticket {
183   my( $self, $custnum_or_cust_main, $requestors ) = @_;
184
185   my( $custnum, $cust_main );
186   if ( ref($custnum_or_cust_main) ) {
187     $cust_main = $custnum_or_cust_main;
188     $custnum = $cust_main->custnum;
189   } else {
190     $custnum = $custnum_or_cust_main;
191     $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
192   }
193   my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
194
195   $self->baseurl.
196   'Ticket/Create.html?'.
197     "Queue=$queueid".
198     "&new-MemberOf=freeside://freeside/cust_main/$custnum".
199     ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
200     ;
201 }
202
203 sub href_ticket {
204   my($self, $ticketnum) = @_;
205   $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
206 }
207
208 sub queues {
209   my($self) = @_;
210
211   my $sql = "select id, name from queues where disabled = 0";
212   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
213   $sth->execute()               or die $sth->errstr. " executing $sql";
214
215   map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
216
217 }
218
219 sub queue {
220   my($self, $queueid) = @_;
221
222   my $sql = "select name from queues where id = ?";
223   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
224   $sth->execute($queueid)       or die $sth->errstr. " executing $sql";
225
226   $sth->fetchrow_arrayref->[0];
227
228 }
229
230 sub baseurl {
231   #my $self = shift;
232   $external_url;
233 }
234
235 1;
236