a105c2752ff40f94ebb99859f7ee2c249df5a0c4
[freeside.git] / FS / FS / TicketSystem / RT_External.pm
1 package FS::TicketSystem::RT_External;
2
3 use strict;
4 use vars qw( $conf $priority_field $priority_field_queue $field );
5 use FS::UID;
6
7 install_callback FS::UID sub { 
8   my $conf = new FS::Conf;
9   $priority_field =
10     $conf->config('ticket_system-custom_priority_field');
11   if ( $priority_field ) {
12     $priority_field_queue =
13       $conf->config('ticket_system-custom_priority_field_queue');
14     $field = $priority_field_queue
15                   ? $priority_field_queue. '.%7B'. $priority_field. '%7D'
16                   : $priority_field;
17   } else {
18     $priority_field_queue = '';
19     $field = '';
20   }
21 };
22
23 sub num_customer_tickets {
24   my( $self, $custnum, $priority, $dbh ) = @_;
25
26   #$dbh ||= create one from some config options
27
28   my @param = ();
29   my $priority_sql = '';
30   if ( defined($priority) ) {
31     if ( length($priority) ) {
32       my $queue_sql = " queue = ( select id from queues where queues.name = ? )
33                         or ( ? = '' and queue = 0 )";
34       $priority_sql = "
35         and ? = ( select content from TicketCustomFieldValues
36                    where ticket = tickets.id
37                      and customfield = ( select id from customfields
38                                           where name = ?
39                                             and ( $queue_sql )
40                                        )
41                 )
42       ";
43       push @param, $priority,
44                    $priority_field,
45                    $priority_field_queue,
46                    $priority_field_queue;
47     } else {
48       return '0nothandledyet0';
49     }
50   }
51
52   my $sql = "
53     select count(*) from tickets 
54        where ( status = 'new' or status = 'open' or status = 'stalled' )
55          and target = 'freeside://freeside/cust_main/$custnum'
56   ";
57
58   my $sth = $dbh->prepare($sql) or die $dbh->errstr;
59   $sth->execute(@param)         or die $sth->errstr;
60
61   $sth->fetchrow_arrayref->[0];
62
63 }
64
65 sub href_customer_tickets {
66   my( $self, $custnum, $priority ) = @_;
67
68   my $href = 
69     'Search/Results.html?Order=ASC&Query=%20MemberOf%20%3D%20%27freeside%3A%2F%2Ffreeside%2Fcust_main%2F'.
70     $custnum.
71     '%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'
72   ;
73
74   if ( $priority && $field && $priority_field_queue ) {
75     $href .= 'AND%20Queue%20%3D%20%27'. $priority_field_queue. '%27%20';
76   }
77   if ( $priority && $field ) {
78     $href .= '%20AND%20%27CF.'. $field. '%27%20%3D%20%27'. $priority. '%27%20';
79   }
80
81   $href .= '&Rows=100'.
82            '&OrderBy=id&Page=1'.
83            '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22%2Ffreeside%2Frt%2FTicket%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%2Ffreeside%2Frt%2FTicket%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';
84
85   if ( $priority && $field ) {
86     $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
87   }
88
89   $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';
90
91   if ( $priority && $field ) {
92     $href .=   '%20%0A%27__-__%27%2C';
93   }
94
95   $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';
96
97   $href;
98 }
99
100 1;
101