stray closing /TABLE in the no-ticket case
[freeside.git] / httemplate / search / rt_ticket.html
1 <& 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,
7                                   $applied ? $format_seconds_sub : () ],
8              'header'        => [ 'Ticket #',
9                                   'Ticket',
10                                   'Time',
11                                   $applied ? 'Applied' : (),
12                                 ],
13              'fields'        => [ 'ticketid',
14                                   sub { encode_entities(shift->get('subject')) },
15                                   sub { my $seconds = shift->get('ticket_time');
16                                         &{ $format_seconds_sub }( $seconds );
17                                       },
18                                   ($applied ?
19                                     sub { my $seconds = shift->get('applied_time');
20                                         &{ $format_seconds_sub }( $seconds );
21                                       } : () ),
22                                 ],
23              'sort_fields'   => [ 'ticketid',
24                                   'subject',
25                                   'transaction_time',
26                                   $applied ? 'applied_time' : (),
27                                 ],
28              'links'         => [
29                                   $link,
30                                   $link,
31                                   '',
32                                   '',
33                                 ],
34 &>
35 <%once>
36
37 my $format_seconds_sub = sub {
38   my $seconds = shift;
39   #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
40   (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
41 $seconds)%3600)/60)."m";
42 };
43
44 </%once>
45 <%init>
46
47 #all sorts of false laziness w/rt_transaction.html
48
49 die "access denied"
50   unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
51
52 local $FS::Record::nowarn_classload = 1;
53
54 #some amount of false laziness w/timeworked.html...
55
56 my @select = (
57   'Tickets.Id AS ticketid',
58   'Tickets.Subject'
59 );
60 my @select_total = ( 'COUNT(*)' );
61
62 my $join = 'JOIN Users   ON Transactions.Creator = Users.Id '; #.
63
64 my $twhere = "
65   WHERE Transactions.ObjectType = 'RT::Ticket'
66     AND Transactions.ObjectId = Tickets.Id
67 ";
68
69 my $applied = '';
70
71 my $transaction_time = "
72 CASE transactions.type when 'Set'
73   THEN (to_number(newvalue,'999999')-to_number(oldvalue, '999999')) * 60
74   ELSE timetaken*60
75 END";
76
77 if ( $cgi->param('svcnum') =~ /^\s*(\d+)\s*$/ ) {
78   $twhere .= " AND EXISTS( SELECT 1 FROM acct_rt_transaction WHERE acct_rt_transaction.transaction_id = Transactions.id AND svcnum = $1 )";
79   $applied = "AND svcnum = $1";
80 }
81
82 $twhere .= "
83   AND (    ( Transactions.Type = 'Set'
84              AND Transactions.Field = 'TimeWorked'
85              AND Transactions.NewValue != Transactions.OldValue )
86         OR ( Transactions.Type IN ( 'Create', 'Comment', 'Correspond', 'Touch' )
87              AND Transactions.TimeTaken > 0
88            )
89       )";
90
91 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
92 # TIMESTAMP is Pg-specific... ?
93 if ( $beginning > 0 ) {
94   $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
95   $twhere .= " AND Transactions.Created >= $beginning ";
96 }
97 if ( $ending < 4294967295 ) {
98   $ending =    "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending).    "'";
99   $twhere .= " AND Transactions.Created <= $ending    ";
100 }
101
102 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
103   $twhere .= " AND Users.name = '$1' ";
104 }
105
106 my $transactions = "FROM Transactions $join $twhere";
107 my $where = "WHERE EXISTS ( SELECT 1 $transactions )";
108
109 if ( $cgi->param('category') =~ /^(\w+)$/ ) {
110   $where .= " AND ocfv_TimeType.Content = '$1'";
111 }
112
113 my $ticket_time = "( SELECT SUM($transaction_time) $transactions )";
114 push @select, "$ticket_time AS ticket_time";
115 push @select_total, "SUM($ticket_time)";
116
117 if ( $applied ) {
118
119   my $applied_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $applied )";
120
121   push @select, "$applied_time AS applied_time";
122   push @select_total, "SUM($applied_time)";
123
124 }
125
126 my $addl_from = " LEFT JOIN (
127     SELECT DISTINCT ON (ObjectId)
128       ObjectId, Content
129     FROM ObjectCustomFieldValues
130       JOIN CustomFields
131         ON (ObjectCustomFieldValues.CustomField = CustomFields.Id)
132     WHERE CustomFields.Name = 'TimeType'
133       AND ObjectCustomFieldValues.ObjectType = 'RT::Ticket'
134       AND ObjectCustomFieldValues.Disabled = 0
135     ORDER BY ObjectId ASC, ObjectCustomFieldValues.LastUpdated DESC
136     ) AS ocfv_TimeType ON (Tickets.Id = ocfv_TimeType.ObjectId)
137 ";
138
139 my $query = {
140   'select'    => join(', ', @select),
141   'table'     => 'tickets', #Pg-ism
142   #'table'     => 'Tickets',
143   'addl_from' => $addl_from,
144   'extra_sql' => $where,
145   'order by'  => 'ORDER BY Created',
146 };
147
148 my $count_query = "SELECT ".join(', ', @select_total).
149   " FROM Tickets $addl_from $where";
150
151 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];
152
153 </%init>