1 <% include('elements/search.html',
2 'title' => 'Time worked',
3 'name_singular' => 'transaction',
5 'count_query' => $count_query,
6 'count_addl' => [ $format_seconds_sub,
7 $applied_time ? $format_seconds_sub : () ],
8 'header' => [ 'Ticket #',
12 $applied_time ? 'Applied' : (),
14 'fields' => [ 'ticketid',
15 sub { encode_entities(shift->get('subject')) },
17 sub { my $seconds = shift->get('transaction_time');
18 &{ $format_seconds_sub }( $seconds );
21 sub { my $seconds = shift->get('applied_time');
22 &{ $format_seconds_sub }( $seconds );
36 my $format_seconds_sub = sub {
38 #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
39 (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
40 $seconds)%3600)/60)."m";
47 unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
49 local $FS::Record::nowarn_classload = 1;
50 #some amount of false laziness w/timeworked.html...
54 'Tickets.Id AS ticketid',
56 'Users.name AS otaker',
58 my @select_total = ( 'COUNT(*)' );
60 my ($transaction_time, $applied_time);
61 my $join = 'JOIN Tickets ON Transactions.ObjectId = Tickets.Id '.
62 'JOIN Users ON Transactions.Creator = Users.Id '; #.
64 my $where = "WHERE Transactions.ObjectType = 'RT::Ticket'";
67 if ( $cgi->param('cfname') =~ /^\w(\w|\s)*$/ ) {
69 # a TimeValue-type custom field
70 $cfname = $cgi->param('cfname');
72 $transaction_time = "(CASE Transactions.Type
73 WHEN 'CustomField' THEN
74 ( coalesce(to_number(ocfv_new.Content,'999999'),0)
75 - coalesce(to_number(ocfv_old.Content,'999999'),0) )
76 ELSE ( to_number(ocfv_main.Content,'999999') )
79 # complicated because we have to deal with the case of editing the
80 # ticket custom field directly (OldReference/NewReference) as well as
81 # entering a transaction with a custom field value (ObjectId)
83 LEFT JOIN ObjectCustomFieldValues ocfv_new
84 ON ( ocfv_new.Id = Transactions.NewReference )
85 LEFT JOIN ObjectCustomFieldValues ocfv_old
86 ON ( ocfv_old.Id = Transactions.OldReference )
87 LEFT JOIN ObjectCustomFieldValues ocfv_main
88 ON ( ocfv_main.ObjectType = 'RT::Transaction'
89 AND ocfv_main.ObjectId = Transactions.Id )
91 ON ( ( CustomFields.LookupType = 'RT::Queue-RT::Ticket-RT::Transaction'
92 AND CustomFields.Id = ocfv_main.CustomField
93 AND ocfv_main.Id IS NOT NULL
96 ( CustomFields.LookupType = 'RT::Queue-RT::Ticket'
97 AND (CustomFields.Id = ocfv_new.CustomField OR ocfv_new.Id IS NULL)
98 AND (CustomFields.Id = ocfv_old.CustomField OR ocfv_old.Id IS NULL)
99 AND ocfv_main.Id IS NULL
103 $where .= " AND CustomFields.Name = '$cfname'
104 AND (ocfv_new.Id IS NOT NULL OR ocfv_old.Id IS NOT NULL OR ocfv_main.Id IS NOT NULL)";
109 # the intrinsic TimeWorked/TimeTaken fields
110 $transaction_time = "CASE Transactions.Type when 'Set'
111 THEN (to_number(NewValue,'999999')-to_number(OldValue, '999999')) * 60
116 if ( $cgi->param('svcnum') =~ /^\s*(\d+)\s*$/ ) {
117 $where .= " AND EXISTS( SELECT 1 FROM acct_rt_transaction WHERE acct_rt_transaction.transaction_id = Transactions.id AND svcnum = $1 )";
118 $applied = "AND svcnum = $1";
121 $applied_time = "( SELECT SUM(support) from acct_rt_transaction where transaction_id = Transactions.id $applied )";
124 AND ( ( Transactions.Type = 'Set'
125 AND Transactions.Field = 'TimeWorked'
126 AND Transactions.NewValue != Transactions.OldValue )
127 OR ( ( Transactions.Type='Create' OR Transactions.Type='Comment' OR Transactions.Type='Correspond' OR Transactions.Type='Touch' )
128 AND Transactions.TimeTaken > 0
134 #AND transaction_time != 0
136 push @select, "($transaction_time) AS transaction_time";
137 push @select_total, "SUM($transaction_time)";
138 if ( $applied_time ) {
139 push @select, "($applied_time) AS applied_time";
140 push @select_total, "SUM($applied_time)";
143 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
144 # TIMESTAMP is Pg-specific... ?
145 if ( $beginning > 0 ) {
146 $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
147 $where .= " AND Transactions.Created >= $beginning ";
149 if ( $ending < 4294967295 ) {
150 $ending = "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending). "'";
151 $where .= " AND Transactions.Created <= $ending ";
154 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
155 $where .= " AND Users.name = '$1' ";
158 if ( $cgi->param('ticketid') =~ /^\s*(\d+)\s*$/ ) {
159 $where .= " AND Tickets.Id = $1";
163 'select' => join(', ', @select),
164 'table' => 'transactions', #Pg-ism
165 #'table' => 'Transactions',
166 'addl_from' => $join,
167 'extra_sql' => $where,
168 'order by' => 'ORDER BY Created',
171 my $count_query = 'SELECT '.join(', ', @select_total). " FROM Transactions $join $where";
173 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];