eb250fb275e5ce0f5d2e236dfd41e28801593df7
[freeside.git] / httemplate / search / rt_transaction.html
1 <& elements/search.html,
2              'title'         => 'Time worked',
3              'name_singular' => 'transaction',
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                                   'Date',
11                                   'Time',
12                                   $applied_time ? 'Applied' : (),
13                                 ],
14              'fields'        => [ 'ticketid',
15                                   sub { encode_entities(shift->get('subject')) },
16                                   'created',
17                                   sub { my $seconds = shift->get('transaction_time');
18                                         &{ $format_seconds_sub }( $seconds );
19                                       },
20                                   ($applied_time ? 
21                                     sub { my $seconds = shift->get('applied_time');
22                                         &{ $format_seconds_sub }( $seconds );
23                                       } : () ),
24                                 ],
25              'links'         => [
26                                   $link,
27                                   $link,
28                                   '',
29                                   '',
30                                   '',
31                                 ],
32 &>
33 <%once>
34
35 my $format_seconds_sub = sub {
36   my $seconds = shift;
37   #(($seconds < 0) ? '-' : '') . concise(duration($seconds));
38   (($seconds < 0) ? '-' : '' ). int(abs($seconds)/3600)."h".sprintf("%02d",(abs(
39 $seconds)%3600)/60)."m";
40 };
41
42 </%once>
43 <%init>
44
45 die "access denied"
46   unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
47
48 local $FS::Record::nowarn_classload = 1;
49 #some amount of false laziness w/timeworked.html...
50
51 my @select = (
52   'Transactions.*',
53   'Tickets.Id AS ticketid',
54   'Tickets.Subject',
55   'Users.name AS otaker',
56 );
57 my @select_total = ( 'COUNT(*)' );
58
59 my ($transaction_time, $applied_time);
60 my $join = 'JOIN Tickets ON Transactions.ObjectId = Tickets.Id '.
61            'JOIN Users   ON Transactions.Creator = Users.Id '; #.
62
63 my $where = "WHERE Transactions.ObjectType = 'RT::Ticket'";
64
65 my $cfname = '';
66 if ( $cgi->param('cfname') =~ /^\w(\w|\s)*$/ ) {
67
68   # a TimeValue-type custom field
69   $cfname = $cgi->param('cfname');
70
71   $transaction_time = "(CASE Transactions.Type 
72     WHEN 'CustomField' THEN 
73       ( coalesce(to_number(ocfv_new.Content,'999999'),0) 
74       - coalesce(to_number(ocfv_old.Content,'999999'),0) )
75     ELSE ( to_number(ocfv_main.Content,'999999') )
76     END) * 60";
77
78   # complicated because we have to deal with the case of editing the
79   # ticket custom field directly (OldReference/NewReference) as well as 
80   # entering a transaction with a custom field value (ObjectId)
81   $join .= "
82     LEFT JOIN ObjectCustomFieldValues ocfv_new
83     ON ( ocfv_new.Id = Transactions.NewReference )
84     LEFT JOIN ObjectCustomFieldValues ocfv_old
85     ON ( ocfv_old.Id = Transactions.OldReference )
86     LEFT JOIN ObjectCustomFieldValues ocfv_main
87     ON ( ocfv_main.ObjectType = 'RT::Transaction'
88          AND ocfv_main.ObjectId = Transactions.Id )
89     JOIN CustomFields
90     ON ( ( CustomFields.LookupType = 'RT::Queue-RT::Ticket-RT::Transaction'
91            AND CustomFields.Id = ocfv_main.CustomField
92            AND ocfv_main.Id IS NOT NULL
93          )
94          OR
95          ( CustomFields.LookupType = 'RT::Queue-RT::Ticket'
96            AND (CustomFields.Id = ocfv_new.CustomField OR ocfv_new.Id IS NULL)
97            AND (CustomFields.Id = ocfv_old.CustomField OR ocfv_old.Id IS NULL)
98            AND ocfv_main.Id IS NULL
99          ) )
100   ";
101
102   $where .= " AND CustomFields.Name = '$cfname'
103   AND (ocfv_new.Id IS NOT NULL OR ocfv_old.Id IS NOT NULL OR ocfv_main.Id IS NOT NULL)";
104
105 }
106 else {
107
108   # the intrinsic TimeWorked/TimeTaken fields
109   $transaction_time = "CASE Transactions.Type when 'Set'
110       THEN (to_number(NewValue,'999999')-to_number(OldValue, '999999')) * 60
111       ELSE TimeTaken*60
112     END";
113  
114   my $applied = ''; 
115   if ( $cgi->param('svcnum') =~ /^\s*(\d+)\s*$/ ) {
116     $where .= " 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   $applied_time = "( SELECT SUM(support) from acct_rt_transaction where transaction_id = Transactions.id $applied )";
121
122   $where .= "
123     AND (    ( Transactions.Type = 'Set'
124                AND Transactions.Field = 'TimeWorked'
125                AND Transactions.NewValue != Transactions.OldValue )
126           OR ( ( Transactions.Type='Create' OR Transactions.Type='Comment' OR Transactions.Type='Correspond' OR Transactions.Type='Touch' )
127                AND Transactions.TimeTaken > 0
128              )
129         )
130   ";
131
132 }
133 #AND transaction_time != 0
134 #AND $wheretimeleft
135 push @select, "($transaction_time) AS transaction_time";
136 push @select_total, "SUM($transaction_time)";
137 if ( $applied_time ) {
138   push @select, "($applied_time) AS applied_time";
139   push @select_total, "SUM($applied_time)";
140 }
141
142 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
143 # TIMESTAMP is Pg-specific... ?
144 if ( $beginning > 0 ) {
145   $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
146   $where .= " AND Transactions.Created >= $beginning ";
147 }
148 if ( $ending < 4294967295 ) {
149   $ending =    "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending).    "'";
150   $where .= " AND Transactions.Created <= $ending    ";
151 }
152
153 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
154   $where .= " AND Users.name = '$1' ";
155 }
156
157 if ( $cgi->param('ticketid') =~ /^\s*(\d+)\s*$/ ) {
158   $where .= " AND Tickets.Id = $1";
159 }
160
161 my $query = {
162   'select'    => join(', ', @select),
163   'table'     => 'transactions', #Pg-ism
164   #'table'     => 'Transactions',
165   'addl_from' => $join,
166   'extra_sql' => $where,
167   'order by'  => 'ORDER BY Created',
168 };
169
170 my $count_query = 'SELECT '.join(', ', @select_total). " FROM Transactions $join $where";
171
172 my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->get('ticketid'); } ];
173
174 </%init>