multi-select for agent on RADIUS usage report, RT#73050
[freeside.git] / httemplate / search / rt_cust.html
1 <& elements/search.html,
2      'title'         => 'Time worked per-customer summary',
3      'name_singular' => 'customer',
4      'query'       => $sql_query,
5      'count_query' => $count_query,
6      'header'      => [ FS::UI::Web::cust_header(
7                           $cgi->param('cust_fields')
8                         ),
9                         @extra_headers,
10                         'Support time',
11                         #'Development time',
12                         'Unclassified time',
13                       ],
14      'fields'      => [
15        \&FS::UI::Web::cust_fields,
16        @extra_fields,
17        $support_time_sub,
18        $unclass_time_sub,
19      ],
20      'color'       => [ FS::UI::Web::cust_colors(),
21                         map '', @extra_fields
22                       ],
23      'style'       => [ FS::UI::Web::cust_styles(),
24                         map '', @extra_fields
25                       ],
26      'align'       => [ FS::UI::Web::cust_aligns(),
27                         map '', @extra_fields
28                       ],
29      'links'       => [ ( map { $_ ne 'Cust. Status' ? $link : '' }
30                               FS::UI::Web::cust_header(
31                                          $cgi->param('cust_fields')
32                                                       )
33                         ),
34                         map '', @extra_fields
35                       ],
36
37 &>
38 <%init>
39
40 die "access denied"
41   unless $FS::CurrentUser::CurrentUser->access_right('List rating data')
42 ;
43
44 #false laziness w/cust_main.html (we're really only filtering on status for now)
45
46 my %search_hash = ();
47
48 #$search_hash{'query'} = $cgi->keywords;
49
50 #scalars
51 my @scalars = qw (
52   agentnum salesnum status address city county state zip country                
53   invoice_terms                                                                 
54   no_censustract with_geocode with_email tax no_tax POST no_POST                
55   custbatch usernum                                                             
56   cancelled_pkgs                                                                
57   cust_fields flattened_pkgs                                                    
58   all_tags                                                                      
59   all_pkg_classnums                                                             
60   any_pkg_status                                                                
61 );                                                                              
62                                                                                 
63 for my $param ( @scalars ) {                                                    
64   $search_hash{$param} = scalar( $cgi->param($param) )                          
65     if length($cgi->param($param));                                             
66 }                                                                               
67                                                                                 
68 #lists                                                                          
69 for my $param (qw( classnum refnum tagnum pkg_classnum )) {                     
70   $search_hash{$param} = [ $cgi->param($param) ];                               
71 }
72
73 ###
74 # etc
75 ###
76
77 my $sql_query = FS::cust_main::Search->search(\%search_hash);
78 my $count_query   = delete($sql_query->{'count_query'});
79 my @extra_headers = @{ delete($sql_query->{'extra_headers'}) };
80 my @extra_fields  = @{ delete($sql_query->{'extra_fields'})  };
81
82 my $link = [ "${p}view/cust_main.cgi?", 'custnum' ];
83
84 #eofalse (cust_main.html)
85
86 #false laziness / cribbed from search/rt_ticket.html
87
88 my $twhere = "
89   WHERE Transactions.ObjectType = 'RT::Ticket'
90 "; #AND Transactions.ObjectId = Tickets.Id
91
92 my $transaction_time = "
93 CASE transactions.type when 'Set'
94   THEN (to_number(newvalue,'999999')-to_number(oldvalue, '999999')) * 60
95   ELSE timetaken*60
96 END";
97
98 $twhere .= "
99   AND (    ( Transactions.Type = 'Set'
100              AND Transactions.Field = 'TimeWorked'
101              AND Transactions.NewValue != Transactions.OldValue )
102         OR ( Transactions.Type IN ( 'Create', 'Comment', 'Correspond', 'Touch' )
103              AND Transactions.TimeTaken > 0
104            )
105       )";
106
107 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
108 # TIMESTAMP is Pg-specific... ?
109 if ( $beginning > 0 ) {
110   $beginning = "TIMESTAMP '". time2str('%Y-%m-%d %X', $beginning). "'";
111   $twhere .= " AND Transactions.Created >= $beginning ";
112 }
113 if ( $ending < 4294967295 ) {
114   $ending =    "TIMESTAMP '". time2str('%Y-%m-%d %X', $ending).    "'";
115   $twhere .= " AND Transactions.Created <= $ending    ";
116 }
117
118 my $transactions = "FROM Transactions $twhere";
119
120 #eofalse (rt_ticket.html)
121
122 my $support_time_sub = sub {
123   my $cust_main = shift;
124   my $sec = 0;
125   foreach my $ticket ($cust_main->tickets) {
126
127     my $TimeType = FS::Record->scalar_sql(
128       "SELECT Content FROM ObjectCustomFieldValues
129                       JOIN CustomFields
130                       ON (ObjectCustomFieldValues.CustomField = CustomFields.Id)
131          WHERE CustomFields.Name = 'TimeType'
132            AND ObjectCustomFieldValues.ObjectType = 'RT::Ticket'
133            AND ObjectCustomFieldValues.Disabled = 0
134            AND ObjectId = ". $ticket->{id}
135     );
136     next unless $TimeType eq 'support';
137
138     $sec += FS::Record->scalar_sql(
139       "SELECT SUM($transaction_time) $transactions ".
140       " AND Transactions.ObjectId = ". $ticket->{id}
141     );
142   }
143
144   (($sec < 0) ? '-' : '' ). int(abs($sec)/3600)."h".sprintf("%02d",(abs($sec)%3600)/60)."m";
145
146 };
147
148 my $unclass_time_sub = sub {
149   my $cust_main = shift;
150   my $sec = 0;
151   foreach my $ticket ($cust_main->tickets) {
152
153     my $TimeType = FS::Record->scalar_sql(
154       "SELECT Content FROM ObjectCustomFieldValues
155                       JOIN CustomFields
156                       ON (ObjectCustomFieldValues.CustomField = CustomFields.Id)
157          WHERE CustomFields.Name = 'TimeType'
158            AND ObjectCustomFieldValues.ObjectType = 'RT::Ticket'
159            AND ObjectCustomFieldValues.Disabled = 0
160            AND ObjectId = ". $ticket->{id}
161     );
162     next unless $TimeType eq '';
163
164     $sec += FS::Record->scalar_sql(
165       "SELECT SUM($transaction_time) $transactions ".
166       " AND Transactions.ObjectId = ". $ticket->{id}
167     );
168   }
169
170   (($sec < 0) ? '-' : '' ). int(abs($sec)/3600)."h".sprintf("%02d",(abs($sec)%3600)/60)."m";
171
172 };
173
174 </%init>