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