update custom priorioty field BS for RT 3.4.4
[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) ? ", 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";
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 ObjectCustomFieldValues
95                  on ( tickets.id = ObjectCustomFieldValues.ObjectId )";
96       
97       $where = "and ObjectType = 'RT::Ticket'
98                 and content = ?
99                 and customfield = ( select id from customfields
100                                      where name = ?
101                                        and ( $queue_sql )
102                                   )
103                ";
104     } else {
105       $where =
106                "and 0 = ( select count(*) from ObjectCustomFieldValues
107                            where ObjectId    = tickets.id
108                              and ObjectType  = 'RT::Ticket'
109                              and customfield = ( select id from customfields
110                                                   where name = ?
111                                                     and ( $queue_sql )
112                                                )
113                         )
114                ";
115     }
116     push @param, $priority_field,
117                  $priority_field_queue,
118                  $priority_field_queue;
119   }
120
121   my $sql = "
122                     from tickets
123                     join queues on ( tickets.queue = queues.id )
124                     join links on ( tickets.id = links.localbase )
125                     $join 
126        where ( status = 'new' or status = 'open' or status = 'stalled' )
127          and target = 'freeside://freeside/cust_main/$custnum'
128          $where
129   ";
130
131   ( $sql, @param );
132
133 }
134
135 sub href_customer_tickets {
136   my( $self, $custnum, $priority ) = @_;
137
138   my $href = $self->baseurl;
139
140   #i snarfed this from an RT bookmarked search, it could be unescaped in the
141   #source for readability and run through uri_escape
142   $href .= 
143     'Search/Results.html?Order=ASC&Query=%20MemberOf%20%3D%20%27freeside%3A%2F%2Ffreeside%2Fcust_main%2F'.
144     $custnum.
145     '%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'
146   ;
147
148   if ( defined($priority) && $field && $priority_field_queue ) {
149     $href .= 'AND%20Queue%20%3D%20%27'. $priority_field_queue. '%27%20';
150   }
151   if ( defined($priority) && $field ) {
152     $href .= '%20AND%20%27CF.'. $field. '%27%20';
153     if ( $priority ) {
154       $href .= '%3D%20%27'. $priority. '%27%20';
155     } else {
156       $href .= 'IS%20%27NULL%27%20';
157     }
158   }
159
160   $href .= '&Rows=100'.
161            '&OrderBy=id&Page=1'.
162            '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
163            $self->baseurl.
164            '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'.
165            $self->baseurl.
166            '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';
167
168   if ( defined($priority) && $field ) {
169     $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
170   }
171
172   $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';
173
174   if ( defined($priority) && $field ) {
175     $href .=   '%20%0A%27__-__%27%2C';
176   }
177
178   $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';
179
180   $href;
181
182 }
183
184 sub href_new_ticket {
185   my( $self, $custnum_or_cust_main, $requestors ) = @_;
186
187   my( $custnum, $cust_main );
188   if ( ref($custnum_or_cust_main) ) {
189     $cust_main = $custnum_or_cust_main;
190     $custnum = $cust_main->custnum;
191   } else {
192     $custnum = $custnum_or_cust_main;
193     $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
194   }
195   my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
196
197   $self->baseurl.
198   'Ticket/Create.html?'.
199     "Queue=$queueid".
200     "&new-MemberOf=freeside://freeside/cust_main/$custnum".
201     ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
202     ;
203 }
204
205 sub href_ticket {
206   my($self, $ticketnum) = @_;
207   $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
208 }
209
210 sub queues {
211   my($self) = @_;
212
213   my $sql = "select id, name from queues where disabled = 0";
214   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
215   $sth->execute()               or die $sth->errstr. " executing $sql";
216
217   map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
218
219 }
220
221 sub queue {
222   my($self, $queueid) = @_;
223
224   return '' unless $queueid;
225
226   my $sql = "select name from queues where id = ?";
227   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
228   $sth->execute($queueid)       or die $sth->errstr. " executing $sql";
229
230   $sth->fetchrow_arrayref->[0];
231
232 }
233
234 sub baseurl {
235   #my $self = shift;
236   $external_url;
237 }
238
239 1;
240