fix problems with time queue search: ticket subjects need to be HTML-escaped, 'remain...
[freeside.git] / httemplate / search / timeworked.html
1 <% include( 'elements/search.html',
2                  'title'       => 'Time Worked',
3                  'menubar'     => [ 'Main menu' => $p, ],
4                  'name'        => 'time',
5                  'html_form'   => qq!<FORM NAME="timeForm" ACTION="${p}misc/timeworked.html" METHOD="POST">!,
6                  'query'       => $query,
7                  'count_query' => $count_query,
8                  'header' => [ '#',
9                                'Ticket',
10                                'Date',
11                                'Time',
12                                '', # checkbox column
13                              ],
14                  'fields' => [ sub { shift->[0] },
15                                sub { encode_entities(shift->[1]) },
16                                sub { shift->[2] },
17                                sub { my $seconds = shift->[3];
18                                      (($seconds < 0) ? '-' : '') .
19                                      concise(duration($seconds));
20                                    },
21                                sub {
22                                  my $row = shift;
23                                  my $seconds = $row->[3];
24                                  my $id = $row->[4];
25                                  qq!<INPUT NAME="transactionid$id" TYPE="checkbox" VALUE="1">!.
26                                  qq!<INPUT NAME="seconds$id" TYPE="hidden" VALUE="$seconds">!;
27                                },
28                              ],
29                  'links' => [
30                    $link,
31                    $link,
32                    '',
33                    '',
34                    '',
35                  ],
36                  'html_foot' => sub {
37                                   '<BR><INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">'.
38                                   '<INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">'.
39                                   '<BR><INPUT TYPE="submit" NAME="action" VALUE="Assign to accounts"><BR>'.
40                                   '<SCRIPT TYPE="text/javascript">'.
41                                   '  function setAll(setTo) { '.
42                                   '    theForm = document.timeForm;'.
43                                   '    for (i=0,n=theForm.elements.length;i<n;i++)'.
44                                   '      if (theForm.elements[i].name.indexOf("transactionid") != -1)'.
45                                   '        theForm.elements[i].checked = setTo;'.
46                                   '  }'.
47                                   '</SCRIPT>';
48                                 },
49              )
50
51 %>
52 <%init>
53
54 die "access denied"
55   unless $FS::CurrentUser::CurrentUser->access_right('Time queue');
56
57 my @groupby = ();
58
59 my $transactiontime = "
60   CASE transactions.type when 'Set'
61     THEN (to_number(newvalue,'999999')-to_number(oldvalue, '999999')) * 60
62     ELSE timetaken*60
63   END
64 ";
65
66 push @groupby, qw( transactions.type newvalue oldvalue timetaken );
67
68 my $appliedtimeclause = "coalesce (sum(acct_rt_transaction.seconds), 0)";
69
70 my $appliedtimeselect = "
71   coalesce(
72             ( SELECT sum(seconds) FROM acct_rt_transaction
73                 WHERE transaction_id = transactions.id
74             ),
75             0
76           )
77 ";
78
79 push @groupby, "transactions.id";
80
81 my $wheretimeleft = "$transactiontime != $appliedtimeselect";
82
83 push @groupby, "tickets.id";
84 push @groupby, "tickets.subject";
85 push @groupby, "transactions.created";
86
87 my $groupby = join(',', @groupby);
88
89 my $where = "
90   WHERE objecttype='RT::Ticket'
91     AND ( ( transactions.type='Set' AND field='TimeWorked' )
92           OR transactions.type='Comment'
93           OR transactions.type='Correspond'
94         )
95     AND $wheretimeleft
96 ";
97     #AND $wheretimeleft
98
99 my $query = "
100   SELECT tickets.id, tickets.subject,
101          to_char(transactions.created, 'Dy Mon DD HH24:MI:SS YYYY'),
102          $transactiontime-$appliedtimeclause,
103          transactions.id
104     FROM transactions
105       JOIN tickets ON transactions.objectid = tickets.id
106       LEFT JOIN acct_rt_transaction
107         ON transactions.id = acct_rt_transaction.transaction_id
108     $where
109     GROUP BY $groupby
110     ORDER BY transactions.created
111 ";
112
113 my $count_query = "SELECT COUNT(*) FROM transactions $where";
114
115 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->[0]; } ];
116
117 </%init>