time worked summary report, RT#7573
[freeside.git] / httemplate / search / rt_ticket.html
diff --git a/httemplate/search/rt_ticket.html b/httemplate/search/rt_ticket.html
new file mode 100644 (file)
index 0000000..62fcce2
--- /dev/null
@@ -0,0 +1,126 @@
+<% include('elements/search.html',
+             'title'         => 'Time worked summary',
+             'name_singular' => 'ticket',
+             'query'         => $query,
+             'count_query'   => $count_query,
+             'count_addl'    => [ $format_seconds_sub, $format_seconds_sub, ],
+             'header'        => [ 'Ticket #',
+                                  'Ticket',
+                                  'Time',
+                                  'Applied',
+                                ],
+             'fields'        => [ 'ticketid',
+                                  sub { encode_entities(shift->get('subject')) },
+                                  sub { my $seconds = shift->get('transaction_time');
+                                        &{ $format_seconds_sub }( $seconds );
+                                      },
+                                  sub { my $seconds = shift->get('support');
+                                        &{ $format_seconds_sub }( $seconds );
+                                      },
+                                ],
+             'links'         => [
+                                  $link,
+                                  $link,
+                                  '',
+                                  '',
+                                ],
+          )
+%>
+<%once>
+
+my $format_seconds_sub = sub {
+  my $seconds = shift;
+  #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
+  (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
+$seconds)%3600)/60)."m";
+};
+
+</%once>
+<%init>
+
+#all sorts of false laziness w/rt_transaction.html
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
+
+#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 $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' )
+               AND Transactions.TimeTaken > 0
+             )
+        )
+";
+#AND transaction_time != 0
+#AND $wheretimeleft
+
+my $support = '';
+
+my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+# TIMESTAMP is Pg-specific... ?
+if ( $beginning > 0 ) {
+  $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
+  $twhere .= " AND Transactions.Created >= $beginning ";
+}
+if ( $ending < 4294967295 ) {
+  $ending =    "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending).    "'";
+  $twhere .= " AND Transactions.Created <= $ending    ";
+}
+
+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 )";
+
+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 $query = {
+  'select'    => join(', ',
+                   'Tickets.Id AS ticketid',
+                   'Tickets.Subject',
+                   "$transaction_time AS transaction_time",
+                   "$support_time     AS support",
+                 ),
+  'table'     => 'tickets', #Pg-ism
+  #'table'     => 'Tickets',
+  'addl_from' => '', #$join,
+  '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 $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];
+
+</%init>