+ my $svcpart = $self->svcnum ? $self->cust_svc->svcpart : $self->svcpart;
+ my @r = map { $_->router } qsearch('part_svc_router',
+ { svcpart => $svcpart });
+ if ( $self->cust_main ) {
+ my $agentnum = $self->cust_main->agentnum;
+ return grep { !$_->agentnum or $_->agentnum == $agentnum } @r;
+ }
+ else {
+ return @r;
+ }
+}
+
+=back
+
+
+=item mac_addr_formatted CASE DELIMITER
+
+Format the MAC address (for use by exports). If CASE starts with "l"
+(for "lowercase"), it's returned in lowercase. DELIMITER is inserted
+between octets.
+
+=cut
+
+sub mac_addr_formatted {
+ my $self = shift;
+ my ($case, $delim) = @_;
+ my $addr = $self->mac_addr;
+ $addr = lc($addr) if $case =~ /^l/i;
+ join( $delim || '', $addr =~ /../g );
+}
+
+#class method
+sub _upgrade_data {
+ my $class = shift;
+
+ local($FS::svc_Common::noexport_hack) = 1;
+
+ # set routernum to addr_block.routernum
+ foreach my $self (qsearch('svc_broadband', {
+ blocknum => {op => '!=', value => ''},
+ routernum => ''
+ })) {
+ my $addr_block = $self->addr_block;
+ if ( !$addr_block ) {
+ # super paranoid mode
+ warn "WARNING: svcnum ".$self->svcnum." is assigned to addr_block ".$self->blocknum.", which does not exist; skipped.\n";
+ next;
+ }
+ my $ip_addr = $self->ip_addr;
+ my $routernum = $addr_block->routernum;
+ if ( $routernum ) {
+ $self->set(routernum => $routernum);
+ my $error = $self->check;
+ # sanity check: don't allow this to change IP address or block
+ # (other than setting blocknum to null for a non-auto-assigned router)
+ if ( $self->ip_addr ne $ip_addr
+ or ($self->blocknum and $self->blocknum != $addr_block->blocknum)) {
+ warn "WARNING: Upgrading service ".$self->svcnum." would change its block/address; skipped.\n";
+ next;
+ }
+
+ $error ||= $self->replace;
+ warn "WARNING: error assigning routernum $routernum to service ".$self->svcnum.
+ ":\n$error; skipped\n"
+ if $error;
+ }
+ else {
+ warn "svcnum ".$self->svcnum.
+ ": no routernum in address block ".$addr_block->cidr.", skipped\n";
+ }
+ }
+
+ # assign blocknums to services that should have them
+ my @all_blocks = qsearch('addr_block', { });
+ SVC: foreach my $self (
+ qsearch({
+ 'select' => 'svc_broadband.*',
+ 'table' => 'svc_broadband',
+ 'addl_from' => 'JOIN router USING (routernum)',
+ 'hashref' => {},
+ 'extra_sql' => 'WHERE svc_broadband.blocknum IS NULL '.
+ 'AND router.manual_addr IS NULL',
+ })
+ ) {
+
+ next SVC if $self->ip_addr eq '';
+ my $NetAddr = $self->NetAddr;
+ # inefficient, but should only need to run once
+ foreach my $block (@all_blocks) {
+ if ($block->NetAddr->contains($NetAddr)) {
+ $self->set(blocknum => $block->blocknum);
+ my $error = $self->replace;
+ warn "WARNING: error assigning blocknum ".$block->blocknum.
+ " to service ".$self->svcnum."\n$error; skipped\n"
+ if $error;
+ next SVC;
+ }
+ }
+ warn "WARNING: no block found containing ".$NetAddr->addr." for service ".
+ $self->svcnum;
+ #next SVC;
+ }
+
+ '';