correct ticket order
[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   $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           "position(tickets.status in 'newopenstalledresolvedrejecteddeleted')".
60           " AS svalue " .
61           ( length($priority) ? ", objectcustomfieldvalues.content" : '' ).
62           " $from_sql ORDER BY priority, svalue, id DESC LIMIT $limit";
63   my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
64   $sth->execute(@param)         or die $sth->errstr. "executing $sql";
65
66   #munge column names???  #httemplate/view/cust_main/tickets.html has column
67   #names that might not make sense now...
68   $sth->fetchall_arrayref({});
69
70 }
71
72 sub _from_customer {
73   my( $self, $custnum, $priority ) = @_;
74
75   my @param = ();
76   my $join = '';
77   my $where = '';
78   if ( defined($priority) ) {
79
80     my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM queues
81                                                        WHERE queues.name = ? )
82                       OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
83
84     my $customfield_sql =
85       "customfield = ( 
86         SELECT CustomFields.Id FROM CustomFields
87                   JOIN ObjectCustomFields
88                     ON ( CustomFields.id = ObjectCustomFields.CustomField )
89          WHERE LookupType = 'RT::Queue-RT::Ticket'
90            AND name = ?
91            AND ( $queue_sql )
92        )";
93
94     push @param, $priority_field,
95                  $priority_field_queue,
96                  $priority_field_queue;
97
98     if ( length($priority) ) {
99       #$where = "    
100       #  and ? = ( select content from TicketCustomFieldValues
101       #             where ticket = tickets.id
102       #               and customfield = ( select id from customfields
103       #                                    where name = ?
104       #                                      and ( $queue_sql )
105       #                                 )
106       #          )
107       #";
108       unshift @param, $priority;
109
110       $join = "JOIN ObjectCustomFieldValues
111                  ON ( tickets.id = ObjectCustomFieldValues.ObjectId )";
112       
113       $where = " AND content = ?
114                  AND ObjectCustomFieldValues.disabled != 1
115                  AND ObjectType = 'RT::Ticket'
116                  AND $customfield_sql";
117
118     } else {
119
120       $where =
121                "AND 0 = ( SELECT count(*) FROM ObjectCustomFieldValues
122                            WHERE ObjectId    = tickets.id
123                              AND ObjectType  = 'RT::Ticket'
124                              AND $customfield_sql
125                         )
126                ";
127     }
128
129   }
130
131   my $sql = "
132                     FROM tickets
133                     JOIN queues ON ( tickets.queue = queues.id )
134                     JOIN links ON ( tickets.id = links.localbase )
135                     $join 
136        WHERE ( ". join(' OR ', map "status = '$_'", $self->statuses ). " )
137          AND target = 'freeside://freeside/cust_main/$custnum'
138          $where
139   ";
140
141   ( $sql, @param );
142
143 }
144
145 sub statuses {
146   #my $self = shift;
147   my @statuses = grep { ! /^\s*$/ } $conf->config('cust_main-ticket_statuses');
148   @statuses = (qw( new open stalled )) unless scalar(@statuses);
149   @statuses;
150 }
151
152 sub href_customer_tickets {
153   my( $self, $custnum, $priority ) = @_;
154
155   #my $href = $self->baseurl;
156
157   #i snarfed this from an RT bookmarked search, then unescaped (some of) it with
158   #perl -npe 's/%([0-9A-F]{2})/pack('C', hex($1))/eg;'
159
160   my $href .= 
161     "Search/Results.html?Order=ASC&".
162     "Query= MemberOf = 'freeside://freeside/cust_main/$custnum' ".
163     #" AND ( Status = 'open'  OR Status = 'new'  OR Status = 'stalled' )"
164     " AND ( ". join(' OR ', map "Status = '$_'", $self->statuses ). " ) "
165   ;
166
167   if ( defined($priority) && $field && $priority_field_queue ) {
168     $href .= " AND Queue = '$priority_field_queue' ";
169   }
170   if ( defined($priority) && $field ) {
171     $href .= " AND 'CF.$field' ";
172     if ( $priority ) {
173       $href .= "= '$priority' ";
174     } else {
175       $href .= "IS 'NULL' "; #this is "RTQL", not SQL
176     }
177   }
178
179   #$href = 
180   uri_escape($href);
181   #eventually should unescape all of it...
182
183   $href .= '&Rows=100'.
184            '&OrderBy=id&Page=1'.
185            '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
186            $self->baseurl.
187            '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'.
188            $self->baseurl.
189            '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';
190
191   if ( defined($priority) && $field ) {
192     $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
193   }
194
195   $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';
196
197   if ( defined($priority) && $field ) {
198     $href .=   '%20%0A%27__-__%27%2C';
199   }
200
201   $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';
202
203   #$href =
204   #uri_escape($href);
205
206   $self->baseurl. $href;
207
208 }
209
210 sub href_new_ticket {
211   my( $self, $custnum_or_cust_main, $requestors ) = @_;
212
213   my( $custnum, $cust_main );
214   if ( ref($custnum_or_cust_main) ) {
215     $cust_main = $custnum_or_cust_main;
216     $custnum = $cust_main->custnum;
217   } else {
218     $custnum = $custnum_or_cust_main;
219     $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
220   }
221   my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
222
223   $self->baseurl.
224   'Ticket/Create.html?'.
225     "Queue=$queueid".
226     "&new-MemberOf=freeside://freeside/cust_main/$custnum".
227     ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
228     ;
229 }
230
231 sub href_ticket {
232   my($self, $ticketnum) = @_;
233   $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
234 }
235
236 sub queues {
237   my($self) = @_;
238
239   my $sql = "SELECT id, name FROM queues WHERE disabled = 0";
240   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
241   $sth->execute()               or die $sth->errstr. " executing $sql";
242
243   map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
244
245 }
246
247 sub queue {
248   my($self, $queueid) = @_;
249
250   return '' unless $queueid;
251
252   my $sql = "SELECT name FROM queues WHERE id = ?";
253   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
254   $sth->execute($queueid)       or die $sth->errstr. " executing $sql";
255
256   $sth->fetchrow_arrayref->[0];
257
258 }
259
260 sub baseurl {
261   #my $self = shift;
262   $external_url;
263 }
264
265 1;
266