FS RT #30363 - Hide Account usage template output in an HTML comment when $hide_usage...
[freeside.git] / FS / FS / addr_block.pm
index 686bdbd..3e62a68 100755 (executable)
@@ -1,16 +1,14 @@
 package FS::addr_block;
+use base qw(FS::Record);
 
 use strict;
-use vars qw( @ISA );
-use FS::Record qw( qsearchs qsearch dbh );
-use FS::router;
-use FS::svc_broadband;
-use FS::Conf;
-use NetAddr::IP;
 use Carp qw( carp );
 use List::Util qw( first );
-
-@ISA = qw( FS::Record );
+use NetAddr::IP;
+use FS::Conf;
+use FS::Record qw( qsearch dbh ); #qsearchs
+use FS::IP_Mixin;
+use FS::addr_range;
 
 =head1 NAME
 
@@ -182,25 +180,11 @@ sub check {
 Returns the FS::router object corresponding to this object.  If the 
 block is unassigned, returns undef.
 
-=cut
-
-sub router {
-  my $self = shift;
-  return qsearchs('router', { routernum => $self->routernum });
-}
-
 =item svc_broadband
 
 Returns a list of FS::svc_broadband objects associated
 with this object.
 
-=cut
-
-sub svc_broadband {
-  my $self = shift;
-  return qsearch('svc_broadband', { blocknum => $self->blocknum });
-}
-
 =item NetAddr
 
 Returns a NetAddr::IP object for this block's address and netmask.
@@ -238,7 +222,7 @@ sub next_free_addr {
   my $self = shift;
   my $selfaddr = $self->NetAddr;
 
-  return if $self->manual_flag;
+  return () if $self->manual_flag;
 
   my $conf = new FS::Conf;
   my @excludeaddr = $conf->config('exclude_ip_addr');
@@ -249,15 +233,23 @@ sub next_free_addr {
     $selfaddr->addr,
     $selfaddr->network->addr,
     $selfaddr->broadcast->addr,
-    (map { $_->NetAddr->addr }
-       qsearch('svc_broadband', { blocknum => $self->blocknum })
-    ), @excludeaddr
+    FS::IP_Mixin->used_addresses($self)
   );
 
   # just do a linear search of the block
   my $freeaddr = $selfaddr->network + 1;
   while ( $freeaddr < $selfaddr->broadcast ) {
-    return $freeaddr unless $used{ $freeaddr->addr };
+    # also make sure it's not blocked from assignment by an address range
+    if ( !$used{$freeaddr->addr } ) {
+      my ($range) = grep { !$_->allow_use }
+                  FS::addr_range->any_contains($freeaddr->addr);
+      if ( !$range ) {
+        # then we've found a free address
+        return $freeaddr;
+      }
+      # otherwise, skip to the end of the range
+      $freeaddr = NetAddr::IP->new($range->end, $self->ip_netmask);
+    }
     $freeaddr++;
   }
   return;
@@ -383,12 +375,6 @@ To be implemented.
 
 Returns the agent (see L<FS::agent>) for this address block, if one exists.
 
-=cut
-
-sub agent {
-  qsearchs('agent', { 'agentnum' => shift->agentnum } );
-}
-
 =item label
 
 Returns text including the router name, gateway ip, and netmask for this