fix Pg-ism
[freeside.git] / httemplate / search / timeworked.html
1 <% include( 'elements/search.html',
2                  'title'       => 'Time Worked',
3                  'name'        => 'time',
4                  'html_form'   => qq!<FORM NAME="timeForm" ACTION="${p}misc/timeworked.html" METHOD="POST">!,
5                  'query'       => $query,
6                  'count_query' => $count_query,
7                  'header' => [ '#',
8                                'Ticket',
9                                'Date',
10                                'Time',
11                                '', # checkbox column
12                              ],
13                  'fields' => [ sub { shift->[0] },
14                                sub { encode_entities(shift->[1]) },
15                                sub { shift->[2] },
16                                sub { my $seconds = shift->[3];
17                                      (($seconds < 0) ? '-' : '') .
18                                      concise(duration($seconds));
19                                    },
20                                sub {
21                                  my $row = shift;
22                                  my $seconds = $row->[3];
23                                  my $id = $row->[4];
24                                  qq!<INPUT NAME="transactionid$id" TYPE="checkbox" VALUE="1">!.
25                                  qq!<INPUT NAME="seconds$id" TYPE="hidden" VALUE="$seconds">!;
26                                },
27                              ],
28                  'links' => [
29                    $link,
30                    $link,
31                    '',
32                    '',
33                    '',
34                  ],
35                  'html_foot' => $html_foot,
36              )
37
38 %>
39 <%init>
40
41 die "access denied"
42   unless $FS::CurrentUser::CurrentUser->access_right('Time queue');
43
44 my @groupby = ();
45
46 my $transactiontime = "
47   CASE Transactions.Type WHEN 'Set'
48     THEN (TO_NUMBER(NewValue,'999999')-TO_NUMBER(OldValue, '999999')) * 60
49     ELSE TimeTaken*60
50   END
51 ";
52
53 push @groupby, qw( Transactions.Type NewValue OldValue TimeTaken );
54
55 my $appliedtimeclause = "COALESCE (SUM(acct_rt_transaction.seconds), 0)";
56
57 my $appliedtimeselect = "
58   COALESCE(
59             ( SELECT SUM(seconds) FROM acct_rt_transaction
60                 WHERE transaction_id = Transactions.id
61             ),
62             0
63           )
64 ";
65
66 push @groupby, "Transactions.id";
67
68 my $wheretimeleft = "$transactiontime != $appliedtimeselect";
69
70 push @groupby, "Tickets.id";
71 push @groupby, "Tickets.Subject";
72 push @groupby, "Transactions.Created";
73
74 my $groupby = join(',', @groupby);
75
76 my $where = "
77   WHERE ObjectType='RT::Ticket'
78     AND ( ( Transactions.Type='Set' AND Field='TimeWorked' )
79           OR Transactions.Type='Create'
80           OR Transactions.Type='Comment'
81           OR Transactions.Type='Correspond'
82         )
83     AND $wheretimeleft
84 ";
85     #AND $wheretimeleft
86
87 my $str2time_sql = str2time_sql;
88 my $closing      = str2time_sql_closing;
89
90 my($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
91 $where .= " AND $str2time_sql Transactions.Created $closing >= $begin ".
92           " AND $str2time_sql Transactions.Created $closing <= $end ";
93
94 my $query = "
95   SELECT Tickets.id, Tickets.Subject,
96          TO_CHAR(Transactions.Created, 'Dy Mon DD HH24:MI:SS YYYY'),
97          $transactiontime-$appliedtimeclause,
98          Transactions.id
99     FROM Transactions
100       JOIN Tickets ON Transactions.ObjectId = Tickets.id
101       LEFT JOIN acct_rt_transaction
102         ON Transactions.id = acct_rt_transaction.transaction_id
103     $where
104     GROUP BY $groupby
105     ORDER BY Transactions.Created
106 ";
107
108 my $count_query = "SELECT COUNT(*) FROM Transactions $where";
109
110 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->[0]; } ];
111
112 my $html_foot = qq'
113   <INPUT TYPE="hidden" NAME="begin" VALUE="$begin">
114   <INPUT TYPE="hidden" NAME="end"   VALUE="$end">
115   <BR>
116   <INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">
117   <INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">
118   <BR>
119   <INPUT TYPE="submit" NAME="action" VALUE="Assign to accounts"><BR>
120   <SCRIPT TYPE="text/javascript">
121     function setAll(setTo) {
122       theForm = document.timeForm;
123       for (i=0,n=theForm.elements.length;i<n;i++)
124         if (theForm.elements[i].name.indexOf("transactionid") != -1)
125           theForm.elements[i].checked = setTo;
126     }
127   </SCRIPT>
128 ';
129
130 </%init>