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