);
use FS::Record qw( dbh );
-$DEBUG = 1;
+use Tie::IxHash;
+use Storable;
+
+$DEBUG = 0;
=head1 NAME
=cut
+# functions for internal use
+
sub join_optionnames {
join(' ', map { join_optionname($_) } @_);
}
" ON (part_pkg.pkgpart = t_$name.pkgpart)";
}
+sub dbaname {
+ # Returns an sql expression for the DBA name
+ "COALESCE( deploy_zone.dbaname,
+ (SELECT value FROM conf WHERE conf.name = 'company_name'
+ AND (conf.agentnum = deploy_zone.agentnum
+ OR conf.agentnum IS NULL)
+ ORDER BY conf.agentnum IS NOT NULL DESC
+ LIMIT 1)
+ ) AS dbaname"
+}
+
sub active_on {
# Returns a condition to limit packages to those that were setup before a
# certain date, and not canceled before that date.
}
sub is_fixed_broadband {
- "is_broadband::int = 1 AND technology::int IN(".join(',',
- 10, 11, 12, 20, 30, 40, 41, 42, 50, 60, 70, 90, 0
- ).")";
+ "is_broadband::int = 1 AND technology::int IN( 10, 11, 12, 20, 30, 40, 41, 42, 50, 60, 70, 90, 0 )"
}
-=item report_fixed_broadband OPTIONS
+sub is_mobile_broadband {
+ "is_broadband::int = 1 AND technology::int IN( 80, 81, 82, 83, 84, 85, 86, 87, 88)"
+}
-Returns the Fixed Broadband Subscription report (section 5.4), as an arrayref
-of an arrayrefs. OPTIONS may contain:
-- date: a timestamp value to count active packages as of that date
-- agentnum: limit to customers of that agent
+=item report SECTION, OPTIONS
-Columns of this report are:
-- census tract
-- technology code
-- downstream speed
-- upstream speed
-(the above columns form a key)
-- number of subscriptions
-- number of consumer-grade subscriptions
+Returns the report section SECTION (see the C<parts> method for section
+name strings) as an arrayref of arrayrefs. OPTIONS may contain "date"
+(a timestamp value to run the report as of this date) and "agentnum"
+(to limit to a single agent).
=cut
-sub report_fixed_broadband {
+sub report {
+ my $class = shift;
+ my $section = shift;
+ my %opt = @_;
+
+ my $method = $section.'_sql';
+ die "Report section '$section' is not implemented\n"
+ unless $class->can($method);
+ my $statement = $class->$method(%opt);
+
+ my $sth = dbh->prepare($statement);
+ $sth->execute or die $sth->errstr;
+ $sth->fetchall_arrayref;
+}
+
+sub fbd_sql {
+ my $class = shift;
+ my %opt = @_;
+ my $date = $opt{date} || time;
+ warn $date;
+ my $agentnum = $opt{agentnum};
+
+ my @select = (
+ 'censusblock',
+ dbaname(),
+ 'technology',
+ 'CASE WHEN is_consumer IS NOT NULL THEN 1 ELSE 0 END',
+ 'adv_speed_down',
+ 'adv_speed_up',
+ 'CASE WHEN is_business IS NOT NULL THEN 1 ELSE 0 END',
+ 'cir_speed_down',
+ 'cir_speed_up',
+ );
+ my $from =
+ 'deploy_zone_block
+ JOIN deploy_zone USING (zonenum)
+ JOIN agent USING (agentnum)';
+ my @where = (
+ "zonetype = 'B'",
+ "active_date < $date",
+ "(expire_date > $date OR expire_date IS NULL)",
+ );
+ push @where, "agentnum = $agentnum" if $agentnum;
+
+ my $order_by = 'censusblock, agentnum, technology, is_consumer, is_business';
+
+ "SELECT ".join(', ', @select) . "
+ FROM $from
+ WHERE ".join(' AND ', @where)."
+ ORDER BY $order_by
+ ";
+}
+
+sub fbs_sql {
my $class = shift;
- my %opt = shift;
+ my %opt = @_;
my $date = $opt{date} || time;
my $agentnum = $opt{agentnum};
'broadband_downstream, broadband_upstream ';
my $order_by = $group_by;
- my $statement = "SELECT ".join(', ', @select) . "
+ "SELECT ".join(', ', @select) . "
FROM $from
WHERE ".join(' AND ', @where)."
GROUP BY $group_by
ORDER BY $order_by
";
- warn $statement if $DEBUG;
- dbh->selectall_arrayref($statement);
}
-=item report_fixed_voice OPTIONS
-
-Returns the Fixed Voice Subscription Detail report (section 5.5). OPTIONS
-are as above. Columns are:
-
-- census tract
-- service type (0 for non-VoIP, 1 for VoIP)
-(the above columns form a key)
-- VGE lines/VoIP subscriptions in service
-- consumer grade VGE lines/VoIP subscriptions
-
-=cut
-
-sub report_fixed_voice {
+sub fvs_sql {
my $class = shift;
- my %opt = shift;
+ my %opt = @_;
my $date = $opt{date} || time;
my $agentnum = $opt{agentnum};
my $group_by = 'cust_location.censustract, COALESCE(is_voip, 0)';
my $order_by = $group_by;
- my $statement = "SELECT ".join(', ', @select) . "
+ "SELECT ".join(', ', @select) . "
FROM $from
WHERE ".join(' AND ', @where)."
GROUP BY $group_by
ORDER BY $order_by
";
- warn $statement if $DEBUG;
- dbh->selectall_arrayref($statement);
}
-=item report_local_phone OPTIONS
-
-Returns the Local Exchange Telephone Subscription report (section 5.6).
-OPTIONS are as above. Each row is data for one state. Columns are:
-
-- state FIPS code (key)
-- wholesale switched voice lines
-- wholesale unswitched local loops
-- end-user total lines
-- end-user lines sold in a package with broadband
-- consumer-grade lines where you are not the long-distance carrier
-- consumer-grade lines where the carrier IS the long-distance carrier
-- business-grade lines where you are not the long-distance carrier
-- business-grade lines where the carrier IS the long-distance carrier
-- end-user lines where you own the local loop facility
-- end-user lines where you lease an unswitched local loop from a LEC
-- end-user lines resold from another carrier
-- end-user lines provided over fiber to the premises
-- end-user lines provided over coaxial
-- end-user lines provided over fixed wireless
-
-=cut
-
-sub report_local_phone {
+sub lts_sql {
my $class = shift;
- my %opt = shift;
+ my %opt = @_;
my $date = $opt{date} || time;
my $agentnum = $opt{agentnum};
my $group_by = 'state.fips';
my $order_by = $group_by;
- my $statement = "SELECT ".join(', ', @select) . "
+ "SELECT ".join(', ', @select) . "
FROM $from
WHERE ".join(' AND ', @where)."
GROUP BY $group_by
ORDER BY $order_by
";
-
- warn $statement if $DEBUG;
- dbh->selectall_arrayref($statement);
}
-=item report_voip OPTIONS
-
-Returns the Interconnected VoIP Subscription report (section 5.7).
-OPTIONS are as above. Columns are:
-
-- state FIPS code (key)
-- OTT subscriptions (non-last-mile)
-- OTT subscriptions sold to consumers
-- last-mile subscriptions
-- last-mile subscriptions sold to consumers
-- last-mile subscriptions bundled with broadband Internet
-- last-mile subscriptions over copper pairs
-- last-mile subscriptions over coaxial
-- last-mile subscriptions over fiber
-- last-mile subscriptions over fixed wireless
-- last-mile subscriptions over other media
-
-=cut
-
-sub report_voip {
+sub voip_sql {
my $class = shift;
- my %opt = shift;
+ my %opt = @_;
my $date = $opt{date} || time;
my $agentnum = $opt{agentnum};
my $group_by = 'state.fips';
my $order_by = $group_by;
- my $statement = "SELECT ".join(', ', @select) . "
+ "SELECT ".join(', ', @select) . "
+ FROM $from
+ WHERE ".join(' AND ', @where)."
+ GROUP BY $group_by
+ ORDER BY $order_by
+ ";
+}
+
+sub mbs_sql {
+ my $class = shift;
+ my %opt = @_;
+ my $date = $opt{date} || time;
+ my $agentnum = $opt{agentnum};
+
+ my @select = (
+ 'state.fips',
+ 'broadband_downstream',
+ 'broadband_upstream',
+ 'COUNT(*)',
+ 'COUNT(is_consumer)',
+ );
+ my $from =
+ 'cust_pkg
+ JOIN cust_location ON (cust_pkg.locationnum = cust_location.locationnum)
+ JOIN state USING (country, state)
+ JOIN cust_main ON (cust_pkg.custnum = cust_main.custnum)
+ JOIN part_pkg USING (pkgpart) '.
+ join_optionnames_int(qw(
+ is_broadband technology
+ is_consumer
+ )).
+ join_optionnames(qw(broadband_downstream broadband_upstream))
+ ;
+ my @where = (
+ active_on($date),
+ is_mobile_broadband()
+ );
+ push @where, "cust_main.agentnum = $agentnum" if $agentnum;
+ my $group_by = 'state.fips, broadband_downstream, broadband_upstream ';
+ my $order_by = $group_by;
+
+ "SELECT ".join(', ', @select) . "
FROM $from
WHERE ".join(' AND ', @where)."
GROUP BY $group_by
ORDER BY $order_by
";
+}
- warn $statement if $DEBUG;
- dbh->selectall_arrayref($statement);
+sub mvs_sql {
+ my $class = shift;
+ my %opt = @_;
+ my $date = $opt{date} || time;
+ my $agentnum = $opt{agentnum};
+
+ my @select = (
+ 'state.fips',
+ 'COUNT(*)',
+ 'COUNT(mobile_direct)',
+ );
+ my $from =
+ 'cust_pkg
+ JOIN cust_location ON (cust_pkg.locationnum = cust_location.locationnum)
+ JOIN state USING (country, state)
+ JOIN cust_main ON (cust_pkg.custnum = cust_main.custnum)
+ JOIN part_pkg USING (pkgpart) '.
+ join_optionnames_int(qw( is_mobile mobile_direct) )
+ ;
+ my @where = (
+ active_on($date),
+ 'is_mobile = 1'
+ );
+ push @where, "cust_main.agentnum = $agentnum" if $agentnum;
+ my $group_by = 'state.fips';
+ my $order_by = $group_by;
+
+ "SELECT ".join(', ', @select) . "
+ FROM $from
+ WHERE ".join(' AND ', @where)."
+ GROUP BY $group_by
+ ORDER BY $order_by
+ ";
}
+=item parts
+
+Returns a Tie::IxHash reference of the internal short names used for the
+report sections ('fbd', 'mbs', etc.) to the full names.
+
+=cut
+
+tie our %parts, 'Tie::IxHash', (
+ fbd => 'Fixed Broadband Deployment',
+ fbs => 'Fixed Broadband Subscription',
+ fvs => 'Fixed Voice Subscription',
+ lts => 'Local Exchange Telephone Subscription',
+ voip => 'Interconnected VoIP Subscription',
+ mbd => 'Mobile Broadband Deployment',
+ mbsa => 'Mobile Broadband Service Availability',
+ mbs => 'Mobile Broadband Subscription',
+ mvd => 'Mobile Voice Deployment',
+ mvs => 'Mobile Voice Subscription',
+);
+
+sub parts {
+ Storable::dclone(\%parts);
+}
1;