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_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' : (),
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 ($transaction_time, $applied_time);
63 my $join = 'JOIN Users ON Transactions.Creator = Users.Id '; #.
66 WHERE Transactions.ObjectType = 'RT::Ticket'
67 AND Transactions.ObjectId = Tickets.Id
71 if ( $cgi->param('cfname') =~ /^\w(\w|\s)*$/ ) {
73 $cfname = $cgi->param('cfname');
75 $transaction_time = "(CASE Transactions.Type
76 WHEN 'CustomField' THEN
77 ( coalesce(to_number(ocfv_new.Content,'999999'),0)
78 - coalesce(to_number(ocfv_old.Content,'999999'),0) )
79 ELSE ( to_number(ocfv_main.Content,'999999') )
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 $twhere .= " 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)";
108 $transaction_time = "
109 CASE transactions.type when 'Set'
110 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='Create' OR Transactions.Type='Comment' OR Transactions.Type='Correspond' OR Transactions.Type='Touch' )
125 AND Transactions.TimeTaken > 0
129 $applied_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $applied )";
134 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
135 # TIMESTAMP is Pg-specific... ?
136 if ( $beginning > 0 ) {
137 $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
138 $twhere .= " AND Transactions.Created >= $beginning ";
140 if ( $ending < 4294967295 ) {
141 $ending = "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending). "'";
142 $twhere .= " AND Transactions.Created <= $ending ";
145 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
146 $twhere .= " AND Users.name = '$1' ";
149 my $transactions = "FROM Transactions $join $twhere";
151 my $where = "WHERE EXISTS ( SELECT 1 $transactions )";
153 my $ticket_time = "( SELECT SUM($transaction_time) $transactions )";
154 push @select, "$ticket_time AS ticket_time";
155 push @select_total, "SUM($ticket_time)";
157 if ( $applied_time) {
158 push @select, "$applied_time AS applied_time";
159 push @select_total, "SUM($applied_time)";
163 'select' => join(', ', @select),
164 'table' => 'tickets', #Pg-ism
165 #'table' => 'Tickets',
166 'addl_from' => '', #$join,
167 'extra_sql' => $where,
168 'order by' => 'ORDER BY Created',
171 my $count_query = "SELECT ".join(', ', @select_total)." FROM Tickets $where";
172 #"SELECT COUNT(*), SUM($transactiontime), SUM(acct_rt_transaction.support) FROM Transactions $join $where";
173 #"SELECT COUNT(*), ( SUM($transactiontime) $transactions ) FROM Tickets"; # $join $where";
175 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];