1 <% include('elements/search.html',
2 'title' => 'Time worked summary',
3 'name_singular' => 'ticket',
5 'count_query' => $count_query,
6 'count_addl' => [ $format_seconds_sub,
7 $applied_time ? $format_seconds_sub : () ],
8 'header' => [ 'Ticket #',
11 $applied_time ? 'Applied' : (),
13 'fields' => [ 'ticketid',
14 sub { encode_entities(shift->get('subject')) },
15 sub { my $seconds = shift->get('ticket_time');
16 &{ $format_seconds_sub }( $seconds );
19 sub { my $seconds = shift->get('applied_time');
20 &{ $format_seconds_sub }( $seconds );
23 'sort_fields' => [ 'ticketid',
26 $applied_time ? 'applied_time' : (),
38 my $format_seconds_sub = sub {
40 #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
41 (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
42 $seconds)%3600)/60)."m";
48 #all sorts of false laziness w/rt_transaction.html
51 unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
53 local $FS::Record::nowarn_classload = 1;
55 #some amount of false laziness w/timeworked.html...
58 'Tickets.Id AS ticketid',
61 my @select_total = ( 'COUNT(*)' );
63 my ($transaction_time, $applied_time);
64 my $join = 'JOIN Users ON Transactions.Creator = Users.Id '; #.
67 WHERE Transactions.ObjectType = 'RT::Ticket'
68 AND Transactions.ObjectId = Tickets.Id
72 if ( $cgi->param('cfname') =~ /^\w(\w|\s)*$/ ) {
74 $cfname = $cgi->param('cfname');
76 $transaction_time = "(CASE Transactions.Type
77 WHEN 'CustomField' THEN
78 ( coalesce(to_number(ocfv_new.Content,'999999'),0)
79 - coalesce(to_number(ocfv_old.Content,'999999'),0) )
80 ELSE ( to_number(ocfv_main.Content,'999999') )
84 LEFT JOIN ObjectCustomFieldValues ocfv_new
85 ON ( ocfv_new.Id = Transactions.NewReference )
86 LEFT JOIN ObjectCustomFieldValues ocfv_old
87 ON ( ocfv_old.Id = Transactions.OldReference )
88 LEFT JOIN ObjectCustomFieldValues ocfv_main
89 ON ( ocfv_main.ObjectType = 'RT::Transaction'
90 AND ocfv_main.ObjectId = Transactions.Id )
92 ON ( ( CustomFields.LookupType = 'RT::Queue-RT::Ticket-RT::Transaction'
93 AND CustomFields.Id = ocfv_main.CustomField
94 AND ocfv_main.Id IS NOT NULL
97 ( CustomFields.LookupType = 'RT::Queue-RT::Ticket'
98 AND (CustomFields.Id = ocfv_new.CustomField OR ocfv_new.Id IS NULL)
99 AND (CustomFields.Id = ocfv_old.CustomField OR ocfv_old.Id IS NULL)
100 AND ocfv_main.Id IS NULL
104 $twhere .= " AND CustomFields.Name = '$cfname'
105 AND (ocfv_new.Id IS NOT NULL OR ocfv_old.Id IS NOT NULL OR ocfv_main.Id IS NOT NULL)";
109 $transaction_time = "
110 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 $twhere .= " AND EXISTS( SELECT 1 FROM acct_rt_transaction WHERE acct_rt_transaction.transaction_id = Transactions.id AND svcnum = $1 )";
118 $applied = "AND svcnum = $1";
122 AND ( ( Transactions.Type = 'Set'
123 AND Transactions.Field = 'TimeWorked'
124 AND Transactions.NewValue != Transactions.OldValue )
125 OR ( ( Transactions.Type='Create' OR Transactions.Type='Comment' OR Transactions.Type='Correspond' OR Transactions.Type='Touch' )
126 AND Transactions.TimeTaken > 0
130 $applied_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $applied )";
135 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
136 # TIMESTAMP is Pg-specific... ?
137 if ( $beginning > 0 ) {
138 $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
139 $twhere .= " AND Transactions.Created >= $beginning ";
141 if ( $ending < 4294967295 ) {
142 $ending = "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending). "'";
143 $twhere .= " AND Transactions.Created <= $ending ";
146 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
147 $twhere .= " AND Users.name = '$1' ";
150 my $transactions = "FROM Transactions $join $twhere";
152 my $where = "WHERE EXISTS ( SELECT 1 $transactions )";
154 my $ticket_time = "( SELECT SUM($transaction_time) $transactions )";
155 push @select, "$ticket_time AS ticket_time";
156 push @select_total, "SUM($ticket_time)";
158 if ( $applied_time) {
159 push @select, "$applied_time AS applied_time";
160 push @select_total, "SUM($applied_time)";
164 'select' => join(', ', @select),
165 'table' => 'tickets', #Pg-ism
166 #'table' => 'Tickets',
167 'addl_from' => '', #$join,
168 'extra_sql' => $where,
169 'order by' => 'ORDER BY Created',
172 my $count_query = "SELECT ".join(', ', @select_total)." FROM Tickets $where";
173 #"SELECT COUNT(*), SUM($transactiontime), SUM(acct_rt_transaction.support) FROM Transactions $join $where";
174 #"SELECT COUNT(*), ( SUM($transactiontime) $transactions ) FROM Tickets"; # $join $where";
176 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];