Merge branch 'patch-19' of https://github.com/gjones2/Freeside
[freeside.git] / httemplate / search / timeworked.html
1 <& 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 <%init>
39
40 die "access denied"
41   unless $FS::CurrentUser::CurrentUser->access_right('Time queue');
42
43 my @groupby = ();
44
45 my $transactiontime = "
46   CASE Transactions.Type WHEN 'Set'
47     THEN (  CASE WHEN NewValue = '' THEN 0 ELSE TO_NUMBER(NewValue,'999999') END
48            -TO_NUMBER(OldValue, '999999')
49           ) * 60
50     ELSE TimeTaken*60
51   END
52 ";
53
54 push @groupby, qw( Transactions.Type NewValue OldValue TimeTaken );
55
56 my $appliedtimeclause = "COALESCE (SUM(acct_rt_transaction.seconds), 0)";
57
58 my $appliedtimeselect = "
59   COALESCE(
60             ( SELECT SUM(seconds) FROM acct_rt_transaction
61                 WHERE transaction_id = Transactions.id
62             ),
63             0
64           )
65 ";
66
67 push @groupby, "Transactions.id";
68
69 my $wheretimeleft = "$transactiontime != $appliedtimeselect";
70
71 push @groupby, "Tickets.id";
72 push @groupby, "Tickets.Subject";
73 push @groupby, "Transactions.Created";
74
75 my $groupby = join(',', @groupby);
76
77 my $where = "
78   WHERE ObjectType='RT::Ticket'
79     AND ( ( Transactions.Type='Set' AND Field='TimeWorked' )
80           OR Transactions.Type IN ( 'Create', 'Comment', 'Correspond', 'Touch' )
81         )
82     AND $wheretimeleft
83 ";
84     #AND $wheretimeleft
85
86 my $str2time_sql = str2time_sql;
87 my $closing      = str2time_sql_closing;
88
89 my($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
90 $where .= " AND $str2time_sql Transactions.Created $closing >= $begin ".
91           " AND $str2time_sql Transactions.Created $closing <= $end ";
92
93 my $query = "
94   SELECT Tickets.id, Tickets.Subject,
95          TO_CHAR(Transactions.Created, 'Dy Mon DD HH24:MI:SS YYYY'),
96          $transactiontime-$appliedtimeclause,
97          Transactions.id
98     FROM Transactions
99       JOIN Tickets ON Transactions.ObjectId = Tickets.id
100       LEFT JOIN acct_rt_transaction
101         ON Transactions.id = acct_rt_transaction.transaction_id
102     $where
103     GROUP BY $groupby
104     ORDER BY Transactions.Created
105 ";
106
107 my $count_query = "SELECT COUNT(*) FROM Transactions $where";
108
109 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->[0]; } ];
110
111 my $html_foot = qq'
112   <INPUT TYPE="hidden" NAME="begin" VALUE="$begin">
113   <INPUT TYPE="hidden" NAME="end"   VALUE="$end">
114   <BR>
115   <INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">
116   <INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">
117   <BR>
118   <INPUT TYPE="submit" NAME="action" VALUE="Assign to accounts"><BR>
119   <SCRIPT TYPE="text/javascript">
120     function setAll(setTo) {
121       theForm = document.timeForm;
122       for (i=0,n=theForm.elements.length;i<n;i++)
123         if (theForm.elements[i].name.indexOf("transactionid") != -1)
124           theForm.elements[i].checked = setTo;
125     }
126   </SCRIPT>
127 ';
128
129 </%init>