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