f5ac023b5b3a5b2e3cf88afd78c1a3b1ba977c78
[freeside.git] / httemplate / search / rt_ticket.html
1 <& 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 ? $format_seconds_sub : () ],
8              'header'        => [ 'Ticket #',
9                                   'Ticket',
10                                   'Time',
11                                   $applied ? '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 ?
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 ? 'applied_time' : (),
27                                 ],
28              'links'         => [
29                                   $link,
30                                   $link,
31                                   '',
32                                   '',
33                                 ],
34 &>
35 <%once>
36
37 my $format_seconds_sub = sub {
38   my $seconds = shift;
39   #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
40   (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
41 $seconds)%3600)/60)."m";
42 };
43
44 </%once>
45 <%init>
46
47 #all sorts of false laziness w/rt_transaction.html
48
49 die "access denied"
50   unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
51
52 local $FS::Record::nowarn_classload = 1;
53
54 #some amount of false laziness w/timeworked.html...
55
56 my @select = (
57   'Tickets.Id AS ticketid',
58   'Tickets.Subject'
59 );
60 my @select_total = ( 'COUNT(*)' );
61
62 my $join = 'JOIN Users   ON Transactions.Creator = Users.Id '; #.
63
64 my $twhere = "
65   WHERE Transactions.ObjectType = 'RT::Ticket'
66     AND Transactions.ObjectId = Tickets.Id
67 ";
68
69 my $transaction_time;
70 my $applied = '';
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 } else {
108
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   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";
118   }
119
120   $twhere .= "
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
126              )
127         )";
128
129 }
130
131
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 ";
137 }
138 if ( $ending < 4294967295 ) {
139   $ending =    "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending).    "'";
140   $twhere .= " AND Transactions.Created <= $ending    ";
141 }
142
143 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
144   $twhere .= " AND Users.name = '$1' ";
145 }
146
147 my $transactions = "FROM Transactions $join $twhere";
148
149 my $where = "WHERE EXISTS ( SELECT 1 $transactions )";
150
151 my $ticket_time = "( SELECT SUM($transaction_time) $transactions )";
152 push @select, "$ticket_time AS ticket_time";
153 push @select_total, "SUM($ticket_time)";
154
155 if ( $applied ) {
156
157   my $applied_time = "( SELECT SUM(support) FROM acct_rt_transaction LEFT JOIN Transactions ON ( transaction_id = Id ) $twhere $applied )";
158
159   push @select, "$applied_time AS applied_time";
160   push @select_total, "SUM($applied_time)";
161
162 }
163
164 my $query = {
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',
171 };
172
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";
176
177 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];
178
179 </%init>