summaryrefslogtreecommitdiff
path: root/httemplate/search/477partIIB.html
blob: 5b9b3076984be5ccae0b9f1eff6417ddba7032d9 (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
% if ( $cgi->param('_type') eq 'xml' ) {
%   my @cols = qw(a b c);
%   for ( my $row = 0; $row < scalar(@rows); $row++ ) {
%     for my $col (0..2) {
%       if ( exists($data[$col][$row]) ) {
<PartII_<% $row + 1 %><% $cols[$col] %>>\
<% $data[$col][$row] %>\
</PartII_<% $row + 1 %><% $cols[$col] %>>
%       }
%     } #for $col
%   } #for $row
% } else { # HTML mode
% # fake up the search-html.html header
<H2>Part IIB</H2>
<TABLE>
  <TR><TD VALIGN="bottom"><BR></TD></TR>
  <TR><TD COLSPAN=2>
  <TABLE CLASS="grid" CELLSPACING=0>
    <TR>
% foreach (@headers) {
      <TH><% $_ %></TH>
% }
    </TR>
% my $row = 0;
% foreach my $rowhead (@rows) {
    <TR CLASS="row<% $row % 2 %>"> 
      <TD STYLE="text-align: left; font-weight: bold"><% $rowhead %></TD>
%     for my $col (0..2) {
      <TD>
%       if ( exists($data[$col][$row]) ) {
      <% $data[$col][$row] %>
%       }
      </TD>
%     } # for $col
    </TR>
%   $row++;
% } #for $rowhead
  </TABLE>
  </TD></TR>
</TABLE>
% } #XML/HTML
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;

die "access denied"
  unless $curuser->access_right('List packages');

my %search_hash = ();

$search_hash{'agentnum'} = $cgi->param('agentnum');
$search_hash{'state'}    = $cgi->param('state');
$search_hash{'classnum'} = [ $cgi->param('classnum') ];
$search_hash{'status'}   = 'active';

my @row_option;
foreach ($cgi->param('part2b_row_option')) {
  push @row_option, (/^\d+$/ ? $_ : undef);
}

my $is_residential = "AND COALESCE(cust_main.company, '') = ''";
my $has_report_option = sub {
  map {
    defined($row_option[$_]) ?
    "AND EXISTS(
      SELECT 1 FROM part_pkg_option 
      WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
      AND optionname = 'report_option_" . $row_option[$_]."'
      AND optionvalue = '1'
    )" : 'AND FALSE'
  } @_
};

# an arrayref for each column
my @data;
# get the skeleton of the query
my $sql_query = FS::cust_pkg->search(\%search_hash);
my $from_where = $sql_query->{'count_query'};
$from_where =~ s/^SELECT COUNT\(\*\) //;
# columns 1 and 2
my $query_ds0 = "SELECT SUM(COALESCE(part_pkg.fcc_ds0s, pkg_class.fcc_ds0s, 0))
  $from_where";
# column 3
my $query_custnum = "SELECT COUNT(DISTINCT cust_pkg.custnum) $from_where";

my @base_queries = ($query_ds0, $query_ds0, $query_custnum);
my @col_conds = (
  # column 1
  [
    '',
    $is_residential,
    $has_report_option->(0), # nomadic
  ],
  # column 2
  [
    '',
    $is_residential,
    $has_report_option->(0..5),
  ],
  # column 3
  [
    ''
  ]
);

my $col = 0;
foreach (@col_conds) {
  my @col_data;
  my $row = 0;
  foreach my $cond (@{ $col_conds[$col] }) {
    # three parts: the select expression, the VoIP class (column selection),
    # and the row selection
    my $query = $base_queries[$col] . 
                " AND part_pkg.fcc_voip_class = '".($col+1)."'
                $cond";
    my $count = FS::Record->scalar_sql($query) || 0;
    if ( $row == 0 ) {
      $col_data[$row] = $count; # the raw count
    } else {
      if ( $col_data[0] == 0 ) {
        $col_data[$row] = ''; # show nothing in this row, then
      } else {
        $col_data[$row] = sprintf('%.2f', 100 * $count / $col_data[0]) . '%';
      }
    } #if $row == 0
    $row++;
  }
  $data[$col] = \@col_data;
  $col++;
}


my @rows = (
  'total number',
  '% residential',
  '% nomadic',
  '% copper',
  '% FTTP',
  '% coax',
  '% wireless',
  '% other broadband',
);

my @headers = (
  '',
  'without broadband',
  'with broadband',
  'wholesale',
);

</%init>