477 report rewrite, #28020
[freeside.git] / httemplate / search / 477.html
old mode 100755 (executable)
new mode 100644 (file)
index 45c92ff..6849337
-<% include( 'elements/search.html',
-                  'title'       => 'FCC Form 477 Results', 
-                  'html_init'   => $html_init,
-                  'name'        => 'regions',
-                  'query'       => $sql_query,
-                  'count_query' => $count_query,
-                  'header'      => [
-                                     'State code',
-                                     'County code',
-                                     'Census tract code',
-                                     'Quantity',
-                                   ],
-                  'fields'      => [
-                    sub { my $row = shift; substr($row->censustract, 0, 2) },
-                    sub { my $row = shift; substr($row->censustract, 2, 3) },
-                    sub { my $row = shift; substr($row->censustract, 5) },
-                    'quantity',
-                  ],
-                  'links'      => [
-                    [ $link, 'censustract' ],
-                    [ $link, 'censustract' ],
-                    [ $link, 'censustract' ],
-                    [ $link, 'censustract' ],
-                  ],
-              )
-%>
+<& /elements/header.html, $title &>
+<STYLE TYPE="text/css">
+table.fcc477part {
+  border-collapse: collapse;
+  border: 1px #777 solid;
+  margin-bottom: 20px;
+}
+table.fcc477part td {
+  padding: 0px 4px;
+  border-left: 1px #777 solid;
+  border-right: 1px #777 solid;
+}
+table.fcc477part tbody td {
+  text-align: right;
+}
+table.fcc477part thead tr.head {
+  text-align: center;
+  vertical-align: top;
+  font-weight: bold;
+  border-top: 1px #777 solid;
+  border-bottom: 1px #777 solid;
+}
+table.fcc477part thead tr.subhead {
+  text-align: center;
+  font-weight: bold;
+  font-size: small;
+  border-top: 1px #777 solid;
+  border-bottom: 1px #777 solid;
+}
+.parttitle {
+  font-weight: bold;
+  font-size: large;
+  float: left;
+}
+a.download {
+  float: right;
+}
+</STYLE>
+% foreach my $partnum (@partnums) {
+%   $cgi->param('parts', $partnum);
+%   $cgi->param('type', 'csv');
+<table class="fcc477part">
+  <caption>
+    <span class="parttitle">Part <% $partnum %></span>
+    <a class="download" href="<% $cgi->self_url %>">Download</a>
+  </caption>
+%   my $header = ".header$partnum";
+%   my $data = $parts{$partnum};
+  <thead>
+    <& $header &>
+  </thead>
+%   #XXX column headings
+%   foreach my $row (@$data) {
+  <tr>
+%     foreach my $item (@$row) {
+    <td><% $item %></td>
+%     }
+  </tr>
+%   }
+</table>
+% } # foreach $partnum
+<& /elements/footer.html &>
 <%init>
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List packages');
+
+my %parts;
+# load from cache if possible
+my $session;
+if ( $cgi->param('session') =~ /^(\d+)$/ ) {
+  $session = $1;
+  %parts = %{ $m->cache->get($session) };
+} else {
+  $session = sprintf('%010d%06d', time, int(rand(1000000)));
+  $cgi->param('session', $session);
+}
 
-my $curuser = $FS::CurrentUser::CurrentUser;
+my $agentnum;
+if ($cgi->param('agentnum') =~ /^(\d+)$/ ) {
+  $agentnum = $1;
+}
+my $date = parse_datetime($cgi->param('date')) || time;
+my @partnums = grep /^\d+$/, $cgi->param('parts');
+foreach my $partnum (@partnums) {
+  my $method = "part$partnum";
+  $parts{$partnum} ||= FS::Report::FCC_477->$method(
+    date      => $date,
+    agentnum  => $agentnum
+  );
+}
+$m->cache->set($session, \%parts, '1h');
 
-die "access denied"
-  unless $curuser->access_right('List packages');
+my $title = 'FCC Form 477 Data - ' . time2str('%b %o, %Y', $date);
+
+if ( $cgi->param('type') eq 'csv' ) {
+  my $partnum = $partnums[0]; # ignore any beyond the first
+  my $data = $parts{$partnum};
+  my $csv = Text::CSV_XS->new({ eol => "\r\n" }); # i think
 
-my %search_hash = ();
-  
-$search_hash{'query'} = $cgi->keywords;
-  
-for ( qw(agentnum magic status classnum) ) {
-  $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
+  my $filename = time2str('%Y-%m-%d', $date) . '-part' . $partnum . '.csv';
+  http_header('Content-Type' => 'text/csv');
+  http_header('Content-Disposition' => qq(attachment;filename="$filename"));
+
+  $m->clear_buffer;
+
+  foreach my $row (@$data) {
+    $csv->combine(@$row);
+    $m->print($csv->string);
+  }
+  $m->abort;
 }
 
+</%init>
+<%def .header6>
+  <TR CLASS="head">
+    <TD ROWSPAN=2>Census Tract</TD>
+    <TD ROWSPAN=2>Technology</TD>
+    <TD COLSPAN=2>Speed (Mbps)</TD>
+    <TD COLSPAN=2>Subscriptions</TD>
+  </TR>
+  <TR CLASS="subhead">
+    <TD>Down</TD>
+    <TD>Up</TD>
+    <TD>Total</TD>
+    <TD>Consumer</TD>
+  </TR>
+</%def>
+<%def .header7>
+  <TR CLASS="head">
+    <TD ROWSPAN=2>State</TD>
+    <TD COLSPAN=2>Speed (Mbps)</TD>
+    <TD COLSPAN=2>Subscriptions</TD>
+  </TR>
+  <TR CLASS="subhead">
+    <TD>Down</TD>
+    <TD>Up</TD>
+    <TD>Total</TD>
+    <TD>Consumer</TD>
+  </TR>
+</%def>
+<%def .header8>
+  <TR CLASS="head">
+    <TD ROWSPAN=2>State</TD>
+    <TD COLSPAN=2>Subscriptions</TD>
+  </TR>
+  <TR CLASS="subhead">
+    <TD>Total</TD>
+    <TD>Direct</TD>
+  </TR>
+</%def>
+<%def .header9>
+  <TR CLASS="head">
+    <TD ROWSPAN=3>State</TD>
+    <TD COLSPAN=2>Wholesale</TD>
+    <TD COLSPAN=12>End User Lines</TD>
+  </TR>
+  <TR CLASS="subhead">
+    <TD ROWSPAN=2>VGEs</TD>
+    <TD ROWSPAN=2>UNE-Ls</TD>
 
-my @report_option = $cgi->param('report_option')
-  if $cgi->param('report_option');
-$search_hash{report_option} = join(',', @report_option) if @report_option;
+    <TD ROWSPAN=2>Total</TD>
+    <TD ROWSPAN=2>With Broadband</TD>
+    <TD COLSPAN=2>Consumer</TD>
+    <TD COLSPAN=2>Business</TD>
 
-my $where = join(' OR ', map { "num = $_" } grep { /^\d+$/ } @report_option );
-my @report_option_name = $where ?
-                           ( map { $_->name }
-                             qsearch({ 'table' => 'part_pkg_report_option',
-                                       'hashref' => {},
-                                       'extra_sql' => "WHERE $where",
-                                    })
-                           ) :
-                           ( 'all packages' );
+    <TD COLSPAN=3>Local Loop</TD>
 
-my $html_init = "For <BR>". join(' and <BR>', @report_option_name). "<BR>";
+    <TD COLSPAN=3>Special Media</TD>
+  </TR>
 
-my $sql_query = FS::cust_pkg->search_sql(\%search_hash);
+  <TR CLASS="subhead">
+    <TD> </TD>
+    <TD>+LD</TD>
+    <TD> </TD>
+    <TD>+LD</TD>
 
-$sql_query->{select} = 'count(*) as quantity, censustract';
-$sql_query->{extra_sql} =~ /^(.*)(ORDER BY bill)(.*)$/s or die "couldn't parse extra_sql";
-$sql_query->{extra_sql} = "$1 GROUP BY censustract $3"; 
+    <TD>Owned</TD>
+    <TD>UNE-L</TD>
+    <TD>Resale</TD>
 
-my $count_query = delete($sql_query->{'count_query'});
-$count_query = 'SELECT count(*) FROM (SELECT count(*) FROM cust_pkg '.
-  $sql_query->{addl_from}. ' '. $sql_query->{extra_sql}. ') AS foo';
+    <TD>Fiber</TD>
+    <TD>Coaxial</TD>
+    <TD>Wireless</TD>
+  </TR>
+</%def>
+<%def .header10>
+  <TR CLASS="head">
+    <TD ROWSPAN=2>State</TD>
+    <TD COLSPAN=2>VoIP OTT</TD>
+    <TD COLSPAN=8>VoIP Non-OTT</TD>
+  </TR>
+  <TR CLASS="subhead">
+    <TD ROWSPAN=2>Total</TD>
+    <TD ROWSPAN=2>Consumer</TD>
 
-my $link = 'cust_pkg.cgi?'. $cgi->query_string. ';censustract=';
-</%init>
+    <TD ROWSPAN=2>Total</TD>
+    <TD ROWSPAN=2>Consumer</TD>
+    <TD ROWSPAN=2>Bundled</TD>
+    <TD COLSPAN=5>Media Type</TD>
+  </TR>
+  <TR CLASS="subhead">
+    <TD>Copper</TD>
+    <TD>Fiber</TD>
+    <TD>Coaxial</TD>
+    <TD>Wireless</TD>
+    <TD>Other</TD>
+  </TR>
+</%def>
+<%def .header11>
+  <TR CLASS="head">
+    <TD ROWSPAN=2>Census Tract</TD>
+    <TD ROWSPAN=2>VoIP?</TD>
+    <TD COLSPAN=2>Lines/Subscriptions</TD>
+  </TR>
+  <TR CLASS="subhead">
+    <TD>Total</TD>
+    <TD>Consumer</TD>
+  </TR>
+</%def>