f6ebb60fed5f747ad9077f4f94c69391650d6665
[freeside.git] / httemplate / elements / dashboard-toplist.html
1 % if ( $conf->exists('dashboard-toplist') ) {
2
3   <& /elements/table-grid.html &>
4
5 % my $bgcolor1 = '#eeeeee';
6 %     my $bgcolor2 = '#ffffff';
7 %     my $bgcolor = $bgcolor2;
8
9 % foreach my $line ( $conf->config('dashboard-toplist') ) {
10 %
11 %   if ( $bgcolor eq $bgcolor1 ) {
12 %     $bgcolor = $bgcolor2;
13 %   } else {
14 %     $bgcolor = $bgcolor1;
15 %   }
16
17 %   if ( $line =~ /^\s*cust_main:\s*(\d+)\s*$/ ) { #customer line
18 %     my $custnum = $1;
19 %     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
20 %     if ( $cust_main ) {
21      
22         <TR>
23           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
24             <A HREF="view/cust_main.cgi?<% $custnum %>"><% $cust_main->name %></A>
25           </TD>
26           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
27             <& /elements/mcp_lint.html, 'cust_main'=>$cust_main &>
28           </TD>
29           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
30             <FONT SIZE="-1">
31             <A HREF="<% FS::TicketSystem->href_new_ticket($cust_main) %>"><% mt('(new ticket)') |h %></A>
32             </FONT>
33           </TD>
34
35 %         foreach my $priority ( @custom_priorities ) {
36             <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
37 %           my $num = $num_tickets_by_priority{$priority}->{$custnum};
38 %           if ( $num ) {
39               <A HREF="<%
40                    FS::TicketSystem->href_customer_tickets($custnum,$priority)
41                    %>"><% $num %></A>
42 %             if ( $priority &&
43 %                 exists($num_tickets_by_priority{''}{$custnum}) ) {
44 %                   # decrement the customer's total by the number in 
45 %                   # this priority bin
46 %                   $num_tickets_by_priority{''}{$custnum} -= $num;
47 %             }
48 %           }
49           </TD>
50 %         }
51         </TR>
52
53 %     } else { 
54
55         <TR>
56           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
57             <% mt("Unknown customer number [_1]", $custnum) |h %> 
58           </TD>
59         </TR>
60
61 %     }
62 %
63 %   } elsif ( $line =~ /^\-\-+$/ ) { #divider
64 %     
65       <TR>
66         <TH CLASS="grid" COLSPAN="<% scalar(@custom_priorities) + 4 %>"></TH>
67       </TR>
68
69 %     next;
70 %     
71 %   } elsif ( $line =~ /^\s*$/ ) {
72
73       <TR>
74         <TD CLASS="grid" COLSPAN="<% scalar(@custom_priorities) + 4 %>" BGCOLOR="<% $bgcolor %>">&nbsp;</TD>
75       </TR>
76
77 %   } elsif ( $line =~ /^\S/ ) { #label line
78
79       <TR>
80         <TH CLASS="grid" BGCOLOR="#cccccc"><% $line %></TH>
81         <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Lint') |h %></TH>
82         <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
83 %       foreach my $priority ( @custom_priorities ) {
84           <TH CLASS="grid" BGCOLOR="#cccccc">
85             <% $priority || '<i>(none)</i>'%>
86           </TH>
87 %       }
88       </TR>
89
90 %   } else { #regular line
91
92       <TR>
93         <TD CLASS="grid"  COLSPAN="<% scalar(@custom_priorities) + 4 %>" BGCOLOR="<% $bgcolor %>"><% $line %></TD>
94       </TR>
95
96 %   }
97
98 %    
99 % } 
100
101   </TABLE>
102   <BR>
103
104 % }
105 <%init>
106
107 my $conf = new FS::Conf;
108
109 #false laziness w/httemplate/search/cust_main.cgi... care if 
110 # custom_priority_field becomes anything but a local hack...
111
112 my @custom_priorities = ();
113 my $custom_priority_field = $conf->config('ticket_system-custom_priority_field');
114 if ( $custom_priority_field 
115      && @{[ $conf->config('ticket_system-custom_priority_field-values') ]} ) {
116   @custom_priorities =
117     $conf->config('ticket_system-custom_priority_field-values');
118 }
119 push @custom_priorities, '';
120
121 my %num_tickets_by_priority = map { $_ => {} } @custom_priorities;
122 # "optimization" (i.e. "terrible hack") to avoid constructing 
123 # (@custom_priorities) x (cust_main) queries with a bazillion 
124 # joins each just to count tickets
125 if ( $FS::TicketSystem::system eq 'RT_Internal' ) {
126   my $text = (driver_name =~ /^Pg/) ? 'text' : 'char';
127   # The RT API does not play nicely with aggregate queries,
128   # so we're going to go around it.
129   my $sql = 
130   "SELECT cust_main.custnum AS custnum,
131           ObjectCustomFieldValues.Content as priority,
132           COUNT(DISTINCT Tickets.Id) AS num_tickets
133    FROM cust_main 
134       LEFT JOIN cust_pkg USING (custnum)
135       LEFT JOIN cust_svc USING (pkgnum)
136       JOIN Links ON (
137         ( Links.Target = 'freeside://freeside/cust_main/' || CAST(cust_main.custnum AS $text) OR
138           Links.Target = 'freeside://freeside/cust_svc/'  || CAST(cust_svc.svcnum AS $text)
139         ) AND 
140         Links.Base LIKE '%rt://%/ticket/%' AND 
141         Links.Type = 'MemberOf'
142       )
143       JOIN Tickets ON (Links.LocalBase = Tickets.Id)
144       LEFT JOIN ObjectCustomFields ON (
145         ObjectCustomFields.ObjectId = '0' OR 
146         ObjectCustomFields.ObjectId = Tickets.Queue
147       )
148       LEFT JOIN CustomFields ON (
149         ObjectCustomFields.CustomField = CustomFields.Id AND
150         CustomFields.Name = '$custom_priority_field'
151       )
152       LEFT JOIN ObjectCustomFieldValues ON (
153         ObjectCustomFieldValues.CustomField = CustomFields.Id AND
154         ObjectCustomFieldValues.ObjectType = 'RT::Ticket' AND
155         ObjectCustomFieldValues.Disabled = '0' AND
156         ObjectCustomFieldValues.ObjectId = Tickets.Id
157       )
158     GROUP BY cust_main.custnum, ObjectCustomFieldValues.Content";
159     #warn $sql."\n";
160   my $sth = dbh->prepare($sql) or die dbh->errstr;
161   $sth->execute or die $sth->errstr;
162   while ( my $row = $sth->fetchrow_hashref ) {
163   #warn to_json($row)."\n";
164     $num_tickets_by_priority{ $row->{priority} }->{ $row->{custnum} } =
165       $row->{num_tickets};
166   }
167 }
168 </%init>