Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / search / rt_ticket.html
1 <% include('elements/search.html',
2              'title'         => 'Time worked summary',
3              'name_singular' => 'ticket',
4              'query'         => $query,
5              'count_query'   => $count_query,
6              'count_addl'    => [ $format_seconds_sub,
7                                   $applied_time ? $format_seconds_sub : () ],
8              'header'        => [ 'Ticket #',
9                                   'Ticket',
10                                   'Time',
11                                   $applied_time ? 'Applied' : (),
12                                 ],
13              'fields'        => [ 'ticketid',
14                                   sub { encode_entities(shift->get('subject')) },
15                                   sub { my $seconds = shift->get('ticket_time');
16                                         &{ $format_seconds_sub }( $seconds );
17                                       },
18                                   ($applied_time ?
19                                     sub { my $seconds = shift->get('applied_time');
20                                         &{ $format_seconds_sub }( $seconds );
21                                       } : () ),
22                                 ],
23              'sort_fields'   => [ 'ticketid',
24                                   'subject',
25                                   'transaction_time',
26                                   $applied_time ? 'applied_time' : (),
27                                 ],
28              'links'         => [
29                                   $link,
30                                   $link,
31                                   '',
32                                   '',
33                                 ],
34           )
35 %>
36 <%once>
37
38 my $format_seconds_sub = sub {
39   my $seconds = shift;
40   #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
41   (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
42 $seconds)%3600)/60)."m";
43 };
44
45 </%once>
46 <%init>
47
48 #all sorts of false laziness w/rt_transaction.html
49
50 die "access denied"
51   unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
52
53 local $FS::Record::nowarn_classload = 1;
54
55 #some amount of false laziness w/timeworked.html...
56
57 my @select = (
58   'Tickets.Id AS ticketid',
59   'Tickets.Subject'
60 );
61 my @select_total = ( 'COUNT(*)' );
62
63 my ($transaction_time, $applied_time);
64 my $join = 'JOIN Users   ON Transactions.Creator = Users.Id '; #.
65
66 my $twhere = "
67   WHERE Transactions.ObjectType = 'RT::Ticket'
68     AND Transactions.ObjectId = Tickets.Id
69 ";
70
71 my $cfname = '';
72 if ( $cgi->param('cfname') =~ /^\w(\w|\s)*$/ ) {
73
74   $cfname = $cgi->param('cfname');
75
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') )
81     END) * 60";
82
83   $join .= "
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 )
91     JOIN CustomFields
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
95          )
96          OR
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
101          ) )
102     ";
103
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)";
106
107 }
108 else {
109   $transaction_time = "
110   CASE transactions.type when 'Set'
111     THEN (to_number(newvalue,'999999')-to_number(oldvalue, '999999')) * 60
112     ELSE timetaken*60
113   END";
114  
115   my $applied = '';
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";
119   }
120
121   $twhere .= "
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
127              )
128         )";
129
130   $applied_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $applied )";
131
132 }
133
134
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 ";
140 }
141 if ( $ending < 4294967295 ) {
142   $ending =    "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending).    "'";
143   $twhere .= " AND Transactions.Created <= $ending    ";
144 }
145
146 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
147   $twhere .= " AND Users.name = '$1' ";
148 }
149
150 my $transactions = "FROM Transactions $join $twhere";
151
152 my $where = "WHERE EXISTS ( SELECT 1 $transactions )";
153
154 my $ticket_time = "( SELECT SUM($transaction_time) $transactions )";
155 push @select, "$ticket_time AS ticket_time";
156 push @select_total, "SUM($ticket_time)";
157
158 if ( $applied_time) {
159   push @select, "$applied_time AS applied_time";
160   push @select_total, "SUM($applied_time)";
161 }
162
163 my $query = {
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',
170 };
171
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";
175
176 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];
177
178 </%init>