summaryrefslogtreecommitdiff
path: root/httemplate/elements/dashboard-toplist.html
blob: f6ebb60fed5f747ad9077f4f94c69391650d6665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
% if ( $conf->exists('dashboard-toplist') ) {

  <& /elements/table-grid.html &>

% my $bgcolor1 = '#eeeeee';
%     my $bgcolor2 = '#ffffff';
%     my $bgcolor = $bgcolor2;

% foreach my $line ( $conf->config('dashboard-toplist') ) {
%
%   if ( $bgcolor eq $bgcolor1 ) {
%     $bgcolor = $bgcolor2;
%   } else {
%     $bgcolor = $bgcolor1;
%   }

%   if ( $line =~ /^\s*cust_main:\s*(\d+)\s*$/ ) { #customer line
%     my $custnum = $1;
%     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
%     if ( $cust_main ) {
     
        <TR>
	  <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
	    <A HREF="view/cust_main.cgi?<% $custnum %>"><% $cust_main->name %></A>
	  </TD>
          <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
            <& /elements/mcp_lint.html, 'cust_main'=>$cust_main &>
          </TD>
	  <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
	    <FONT SIZE="-1">
            <A HREF="<% FS::TicketSystem->href_new_ticket($cust_main) %>"><% mt('(new ticket)') |h %></A>
            </FONT>
	  </TD>

%         foreach my $priority ( @custom_priorities ) {
            <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
%           my $num = $num_tickets_by_priority{$priority}->{$custnum};
%           if ( $num ) {
              <A HREF="<%
                   FS::TicketSystem->href_customer_tickets($custnum,$priority)
                   %>"><% $num %></A>
%             if ( $priority &&
%                 exists($num_tickets_by_priority{''}{$custnum}) ) {
%                   # decrement the customer's total by the number in 
%                   # this priority bin
%                   $num_tickets_by_priority{''}{$custnum} -= $num;
%             }
%           }
          </TD>
%         }
        </TR>

%     } else { 

        <TR>
          <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
	    <% mt("Unknown customer number [_1]", $custnum) |h %> 
	  </TD>
        </TR>

%     }
%
%   } elsif ( $line =~ /^\-\-+$/ ) { #divider
%     
      <TR>
        <TH CLASS="grid" COLSPAN="<% scalar(@custom_priorities) + 4 %>"></TH>
      </TR>

%     next;
%     
%   } elsif ( $line =~ /^\s*$/ ) {

      <TR>
        <TD CLASS="grid" COLSPAN="<% scalar(@custom_priorities) + 4 %>" BGCOLOR="<% $bgcolor %>">&nbsp;</TD>
      </TR>

%   } elsif ( $line =~ /^\S/ ) { #label line

      <TR>
        <TH CLASS="grid" BGCOLOR="#cccccc"><% $line %></TH>
	<TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Lint') |h %></TH>
	<TH CLASS="grid" BGCOLOR="#cccccc"></TH>
%       foreach my $priority ( @custom_priorities ) {
          <TH CLASS="grid" BGCOLOR="#cccccc">
	    <% $priority || '<i>(none)</i>'%>
	  </TH>
%       }
      </TR>

%   } else { #regular line

      <TR>
        <TD CLASS="grid"  COLSPAN="<% scalar(@custom_priorities) + 4 %>" BGCOLOR="<% $bgcolor %>"><% $line %></TD>
      </TR>

%   }

%    
% } 

  </TABLE>
  <BR>

% }
<%init>

my $conf = new FS::Conf;

#false laziness w/httemplate/search/cust_main.cgi... care if 
# custom_priority_field becomes anything but a local hack...

my @custom_priorities = ();
my $custom_priority_field = $conf->config('ticket_system-custom_priority_field');
if ( $custom_priority_field 
     && @{[ $conf->config('ticket_system-custom_priority_field-values') ]} ) {
  @custom_priorities =
    $conf->config('ticket_system-custom_priority_field-values');
}
push @custom_priorities, '';

my %num_tickets_by_priority = map { $_ => {} } @custom_priorities;
# "optimization" (i.e. "terrible hack") to avoid constructing 
# (@custom_priorities) x (cust_main) queries with a bazillion 
# joins each just to count tickets
if ( $FS::TicketSystem::system eq 'RT_Internal' ) {
  my $text = (driver_name =~ /^Pg/) ? 'text' : 'char';
  # The RT API does not play nicely with aggregate queries,
  # so we're going to go around it.
  my $sql = 
  "SELECT cust_main.custnum AS custnum,
          ObjectCustomFieldValues.Content as priority,
          COUNT(DISTINCT Tickets.Id) AS num_tickets
   FROM cust_main 
      LEFT JOIN cust_pkg USING (custnum)
      LEFT JOIN cust_svc USING (pkgnum)
      JOIN Links ON (
        ( Links.Target = 'freeside://freeside/cust_main/' || CAST(cust_main.custnum AS $text) OR
          Links.Target = 'freeside://freeside/cust_svc/'  || CAST(cust_svc.svcnum AS $text)
        ) AND 
        Links.Base LIKE '%rt://%/ticket/%' AND 
        Links.Type = 'MemberOf'
      )
      JOIN Tickets ON (Links.LocalBase = Tickets.Id)
      LEFT JOIN ObjectCustomFields ON (
        ObjectCustomFields.ObjectId = '0' OR 
        ObjectCustomFields.ObjectId = Tickets.Queue
      )
      LEFT JOIN CustomFields ON (
        ObjectCustomFields.CustomField = CustomFields.Id AND
        CustomFields.Name = '$custom_priority_field'
      )
      LEFT JOIN ObjectCustomFieldValues ON (
        ObjectCustomFieldValues.CustomField = CustomFields.Id AND
        ObjectCustomFieldValues.ObjectType = 'RT::Ticket' AND
        ObjectCustomFieldValues.Disabled = '0' AND
        ObjectCustomFieldValues.ObjectId = Tickets.Id
      )
    GROUP BY cust_main.custnum, ObjectCustomFieldValues.Content";
    #warn $sql."\n";
  my $sth = dbh->prepare($sql) or die dbh->errstr;
  $sth->execute or die $sth->errstr;
  while ( my $row = $sth->fetchrow_hashref ) {
  #warn to_json($row)."\n";
    $num_tickets_by_priority{ $row->{priority} }->{ $row->{custnum} } =
      $row->{num_tickets};
  }
}
</%init>