1 <& 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 ? $format_seconds_sub : () ],
8 'header' => [ 'Ticket #',
11 $applied ? '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 ? 'applied_time' : (),
37 my $format_seconds_sub = sub {
39 #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
40 (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
41 $seconds)%3600)/60)."m";
47 #all sorts of false laziness w/rt_transaction.html
50 unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
52 local $FS::Record::nowarn_classload = 1;
54 #some amount of false laziness w/timeworked.html...
57 'Tickets.Id AS ticketid',
60 my @select_total = ( 'COUNT(*)' );
62 my $join = 'JOIN Users ON Transactions.Creator = Users.Id '; #.
65 WHERE Transactions.ObjectType = 'RT::Ticket'
66 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
115 if ( $cgi->param('svcnum') =~ /^\s*(\d+)\s*$/ ) {
116 $twhere .= " AND EXISTS( SELECT 1 FROM acct_rt_transaction WHERE acct_rt_transaction.transaction_id = Transactions.id AND svcnum = $1 )";
117 $applied = "AND svcnum = $1";
121 AND ( ( Transactions.Type = 'Set'
122 AND Transactions.Field = 'TimeWorked'
123 AND Transactions.NewValue != Transactions.OldValue )
124 OR ( Transactions.Type IN ( 'Create', 'Comment', 'Correspond', 'Touch' )
125 AND Transactions.TimeTaken > 0
132 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
133 # TIMESTAMP is Pg-specific... ?
134 if ( $beginning > 0 ) {
135 $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
136 $twhere .= " AND Transactions.Created >= $beginning ";
138 if ( $ending < 4294967295 ) {
139 $ending = "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending). "'";
140 $twhere .= " AND Transactions.Created <= $ending ";
143 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
144 $twhere .= " AND Users.name = '$1' ";
147 my $transactions = "FROM Transactions $join $twhere";
149 my $where = "WHERE EXISTS ( SELECT 1 $transactions )";
151 my $ticket_time = "( SELECT SUM($transaction_time) $transactions )";
152 push @select, "$ticket_time AS ticket_time";
153 push @select_total, "SUM($ticket_time)";
157 my $applied_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $applied )";
159 push @select, "$applied_time AS applied_time";
160 push @select_total, "SUM($applied_time)";
165 'select' => join(', ', @select),
166 'table' => 'tickets', #Pg-ism
167 #'table' => 'Tickets',
168 'addl_from' => '', #$join,
169 'extra_sql' => $where,
170 'order by' => 'ORDER BY Created',
173 my $count_query = "SELECT ".join(', ', @select_total)." FROM Tickets $where";
174 #"SELECT COUNT(*), SUM($transactiontime), SUM(acct_rt_transaction.support) FROM Transactions $join $where";
175 #"SELECT COUNT(*), ( SUM($transactiontime) $transactions ) FROM Tickets"; # $join $where";
177 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];