improve speed in customer search, #13364
[freeside.git] / httemplate / search / rt_transaction.html
1 <% include('elements/search.html',
2              'title'         => 'Time worked',
3              'name_singular' => 'transaction',
4              'query'         => $query,
5              'count_query'   => $count_query,
6              'count_addl'    => [ $format_seconds_sub, $format_seconds_sub, ],
7              'header'        => [ 'Ticket #',
8                                   'Ticket',
9                                   'Date',
10                                   'Time',
11                                   'Applied',
12                                 ],
13              'fields'        => [ 'ticketid',
14                                   sub { encode_entities(shift->get('subject')) },
15                                   'created',
16                                   sub { my $seconds = shift->get('transaction_time');
17                                         &{ $format_seconds_sub }( $seconds );
18                                       },
19                                   sub { my $seconds = shift->get('support');
20                                         &{ $format_seconds_sub }( $seconds );
21                                       },
22                                 ],
23              'links'         => [
24                                   $link,
25                                   $link,
26                                   '',
27                                   '',
28                                   '',
29                                 ],
30           )
31 %>
32 <%once>
33
34 my $format_seconds_sub = sub {
35   my $seconds = shift;
36   #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
37   (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
38 $seconds)%3600)/60)."m";
39 };
40
41 </%once>
42 <%init>
43
44 die "access denied"
45   unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
46
47 #some amount of false laziness w/timeworked.html...
48
49 my $transactiontime = "
50   CASE transactions.type when 'Set'
51     THEN (to_number(newvalue,'999999')-to_number(oldvalue, '999999')) * 60
52     ELSE timetaken*60
53   END
54 ";
55
56 my $join = 'JOIN Tickets ON Transactions.ObjectId = Tickets.Id '.
57            'JOIN Users   ON Transactions.Creator = Users.Id '; #.
58 #           'LEFT JOIN acct_rt_transaction '.
59 #                 '  ON Transactions.Id = acct_rt_transaction.transaction_id';
60 my $where = "
61   WHERE objecttype='RT::Ticket'
62     AND (    ( Transactions.Type = 'Set'
63                AND Transactions.Field = 'TimeWorked'
64                AND Transactions.NewValue != Transactions.OldValue )
65           OR ( ( Transactions.Type='Create' OR Transactions.Type='Comment' OR Transactions.Type='Correspond' )
66                AND Transactions.TimeTaken > 0
67              )
68         )
69 ";
70 #AND transaction_time != 0
71 #AND $wheretimeleft
72
73 my $support = '';
74
75 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
76 # TIMESTAMP is Pg-specific... ?
77 if ( $beginning > 0 ) {
78   $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
79   $where .= " AND Transactions.Created >= $beginning ";
80 }
81 if ( $ending < 4294967295 ) {
82   $ending =    "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending).    "'";
83   $where .= " AND Transactions.Created <= $ending    ";
84 }
85
86 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
87   $where .= " AND Users.name = '$1' ";
88 }
89
90 if ( $cgi->param('ticketid') =~ /^\s*(\d+)\s*$/ ) {
91   $where .= " AND Tickets.Id = $1";
92 }
93
94 if ( $cgi->param('svcnum') =~ /^\s*(\d+)\s*$/ ) {
95   $where .= " AND EXISTS( SELECT 1 FROM acct_rt_transaction WHERE acct_rt_transaction.transaction_id = Transactions.id AND svcnum = $1 )";
96   $support = "AND svcnum = $1";
97 }
98
99 my $support_time = "( SELECT SUM(support) from acct_rt_transaction where transaction_id = Transactions.id $support )";
100
101 my $query = {
102   'select'    => join(', ',
103                    'Transactions.*',
104                    'Tickets.Id AS ticketid',
105                    'Tickets.Subject',
106                    'Users.name AS otaker',
107                    "$transactiontime AS transaction_time",
108                    "$support_time    AS support",
109                  ),
110   'table'     => 'transactions', #Pg-ism
111   #'table'     => 'Transactions',
112   'addl_from' => $join,
113   'extra_sql' => $where,
114   'order by'  => 'ORDER BY Created',
115 };
116
117 my $count_query =
118   #"SELECT COUNT(*), SUM($transactiontime), SUM(acct_rt_transaction.support) FROM Transactions $join $where";
119   "SELECT COUNT(*),
120           SUM($transactiontime),
121           SUM($support_time)
122      FROM Transactions $join $where";
123
124 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];
125
126 </%init>