quiet warnings about CGI::param in list context
[freeside.git] / httemplate / search / rt_ticket.html
index abe1315..2826cd7 100644 (file)
@@ -1,27 +1,29 @@
-<% include('elements/search.html',
+<& elements/search.html,
              'title'         => 'Time worked summary',
              'name_singular' => 'ticket',
              'query'         => $query,
              'count_query'   => $count_query,
-             'count_addl'    => [ $format_seconds_sub, $format_seconds_sub, ],
+             'count_addl'    => [ $format_seconds_sub,
+                                  $applied ? $format_seconds_sub : () ],
              'header'        => [ 'Ticket #',
                                   'Ticket',
                                   'Time',
-                                  'Applied',
+                                  $applied ? 'Applied' : (),
                                 ],
              'fields'        => [ 'ticketid',
                                   sub { encode_entities(shift->get('subject')) },
-                                  sub { my $seconds = shift->get('transaction_time');
+                                  sub { my $seconds = shift->get('ticket_time');
                                         &{ $format_seconds_sub }( $seconds );
                                       },
-                                  sub { my $seconds = shift->get('support');
+                                  ($applied ?
+                                    sub { my $seconds = shift->get('applied_time');
                                         &{ $format_seconds_sub }( $seconds );
-                                      },
+                                      } : () ),
                                 ],
              'sort_fields'   => [ 'ticketid',
                                   'subject',
                                   'transaction_time',
-                                  'support_time',
+                                  $applied ? 'applied_time' : (),
                                 ],
              'links'         => [
                                   $link,
@@ -29,8 +31,7 @@
                                   '',
                                   '',
                                 ],
-          )
-%>
+&>
 <%once>
 
 my $format_seconds_sub = sub {
@@ -48,34 +49,44 @@ $seconds)%3600)/60)."m";
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
 
+local $FS::Record::nowarn_classload = 1;
+
 #some amount of false laziness w/timeworked.html...
 
-my $transactiontime = "
-  CASE transactions.type when 'Set'
-    THEN (to_number(newvalue,'999999')-to_number(oldvalue, '999999')) * 60
-    ELSE timetaken*60
-  END
-";
+my @select = (
+  'Tickets.Id AS ticketid',
+  'Tickets.Subject'
+);
+my @select_total = ( 'COUNT(*)' );
 
 my $join = 'JOIN Users   ON Transactions.Creator = Users.Id '; #.
-#           'LEFT JOIN acct_rt_transaction '.
-#                 '  ON Transactions.Id = acct_rt_transaction.transaction_id';
 
 my $twhere = "
-  WHERE objecttype='RT::Ticket'
-    AND Transactions.ObjectId = Tickets.Id 
-    AND (    ( Transactions.Type = 'Set'
-               AND Transactions.Field = 'TimeWorked'
-               AND Transactions.NewValue != Transactions.OldValue )
-          OR ( ( Transactions.Type='Create' OR Transactions.Type='Comment' OR Transactions.Type='Correspond' OR Transactions.Type='Touch' )
-               AND Transactions.TimeTaken > 0
-             )
-        )
+  WHERE Transactions.ObjectType = 'RT::Ticket'
+    AND Transactions.ObjectId = Tickets.Id
 ";
-#AND transaction_time != 0
-#AND $wheretimeleft
 
-my $support = '';
+my $applied = '';
+
+my $transaction_time = "
+CASE transactions.type when 'Set'
+  THEN (to_number(newvalue,'999999')-to_number(oldvalue, '999999')) * 60
+  ELSE timetaken*60
+END";
+
+if ( $cgi->param('svcnum') =~ /^\s*(\d+)\s*$/ ) {
+  $twhere .= " AND EXISTS( SELECT 1 FROM acct_rt_transaction WHERE acct_rt_transaction.transaction_id = Transactions.id AND svcnum = $1 )";
+  $applied = "AND svcnum = $1";
+}
+
+$twhere .= "
+  AND (    ( Transactions.Type = 'Set'
+             AND Transactions.Field = 'TimeWorked'
+             AND Transactions.NewValue != Transactions.OldValue )
+        OR ( Transactions.Type IN ( 'Create', 'Comment', 'Correspond', 'Touch' )
+             AND Transactions.TimeTaken > 0
+           )
+      )";
 
 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
 # TIMESTAMP is Pg-specific... ?
@@ -92,39 +103,50 @@ if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
   $twhere .= " AND Users.name = '$1' ";
 }
 
-if ( $cgi->param('svcnum') =~ /^\s*(\d+)\s*$/ ) {
-  $twhere .= " AND EXISTS( SELECT 1 FROM acct_rt_transaction WHERE acct_rt_transaction.transaction_id = Transactions.id AND svcnum = $1 )";
-  $support = "AND svcnum = $1";
+my $transactions = "FROM Transactions $join $twhere";
+my $where = "WHERE EXISTS ( SELECT 1 $transactions )";
+
+if ( $cgi->param('category') =~ /^(\w+)$/ ) {
+  $where .= " AND ocfv_TimeType.Content = '$1'";
 }
 
-my $transactions = "FROM Transactions $join $twhere";
+my $ticket_time = "( SELECT SUM($transaction_time) $transactions )";
+push @select, "$ticket_time AS ticket_time";
+push @select_total, "SUM($ticket_time)";
 
-my $where = "WHERE EXISTS ( SELECT 1 $transactions )";
+if ( $applied ) {
 
-my $transaction_time = "( SELECT SUM($transactiontime) $transactions )";
-my $support_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $support )";
+  my $applied_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $applied )";
+
+  push @select, "$applied_time AS applied_time";
+  push @select_total, "SUM($applied_time)";
+
+}
+
+my $addl_from = " LEFT JOIN (
+    SELECT DISTINCT ON (ObjectId)
+      ObjectId, Content
+    FROM ObjectCustomFieldValues
+      JOIN CustomFields
+        ON (ObjectCustomFieldValues.CustomField = CustomFields.Id)
+    WHERE CustomFields.Name = 'TimeType'
+      AND ObjectCustomFieldValues.ObjectType = 'RT::Ticket'
+      AND ObjectCustomFieldValues.Disabled = 0
+    ORDER BY ObjectId ASC, ObjectCustomFieldValues.LastUpdated DESC
+    ) AS ocfv_TimeType ON (Tickets.Id = ocfv_TimeType.ObjectId)
+";
 
 my $query = {
-  'select'    => join(', ',
-                   'Tickets.Id AS ticketid',
-                   'Tickets.Subject',
-                   "$transaction_time AS transaction_time",
-                   "$support_time     AS support",
-                 ),
+  'select'    => join(', ', @select),
   'table'     => 'tickets', #Pg-ism
   #'table'     => 'Tickets',
-  'addl_from' => '', #$join,
+  'addl_from' => $addl_from,
   'extra_sql' => $where,
   'order by'  => 'ORDER BY Created',
 };
 
-my $count_query =
-  #"SELECT COUNT(*), SUM($transactiontime), SUM(acct_rt_transaction.support) FROM Transactions $join $where";
-  #"SELECT COUNT(*), ( SUM($transactiontime) $transactions ) FROM Tickets"; # $join $where";
-  "SELECT COUNT(*),
-          SUM( $transaction_time ),
-          SUM( $support_time )
-     FROM Tickets $where"; # $join $where";
+my $count_query = "SELECT ".join(', ', @select_total).
+  " FROM Tickets $addl_from $where";
 
 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];