svc_broadband merge
[freeside.git] / FS / FS / ac_block.pm
1 package FS::ac_block;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs qsearch );
6 use FS::ac_type;
7 use FS::ac;
8 use FS::svc_broadband;
9 use NetAddr::IP;
10
11 @ISA = qw( FS::Record );
12
13 =head1 NAME
14
15 FS::ac - Object methods for ac records
16
17 =head1 SYNOPSIS
18
19   use FS::ac_block;
20
21   $record = new FS::ac_block \%hash;
22   $record = new FS::ac_block { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::ac_block record describes an address block assigned for broadband 
35 access.  FS::ac_block inherits from FS::Record.  The following fields are 
36 currently supported:
37
38 =over 4
39
40 =item acnum - the access concentrator (see L<FS::ac_type>) to which this 
41 block is assigned.
42
43 =item ip_gateway - the gateway address used by customers within this block.  
44 This functions as the primary key.
45
46 =item ip_netmask - the netmask of the block, expressed as an integer.
47
48 =back
49
50 =head1 METHODS
51
52 =over 4
53
54 =item new HASHREF
55
56 Create a new record.  To add the record to the database, see L<"insert">.
57
58 =cut
59
60 sub table { 'ac_block'; }
61
62 =item insert
63
64 Adds this record to the database.  If there is an error, returns the error,
65 otherwise returns false.
66
67 =item delete
68
69 Deletes this record from the database.  If there is an error, returns the
70 error, otherwise returns false.
71
72 =item replace OLD_RECORD
73
74 Replaces OLD_RECORD with this one in the database.  If there is an error,
75 returns the error, otherwise returns false.
76
77 =item check
78
79 Checks all fields to make sure this is a valid record.  If there is an error,
80 returns the error, otherwise returns false.  Called by the insert and replace
81 methods.
82
83 =cut
84
85 sub check {
86   my $self = shift;
87
88   my $error =
89     $self->ut_number('acnum')
90     || $self->ut_ip('ip_gateway')
91     || $self->ut_number('ip_netmask')
92   ;
93   return $error if $error;
94
95   return "Unknown acnum"
96     unless $self->ac;
97
98   my $self_addr = new NetAddr::IP ($self->ip_gateway, $self->ip_netmask);
99   return "Cannot parse address: ". $self->ip_gateway . '/' . $self->ip_netmask
100     unless $self_addr;
101
102   my @block = grep {
103     my $block_addr = new NetAddr::IP ($_->ip_gateway, $_->ip_netmask);
104     if($block_addr->contains($self_addr) 
105     or $self_addr->contains($block_addr)) { $_; };
106   } qsearch( 'ac_block', {});
107
108   foreach(@block) {
109     return "Block intersects existing block ".$_->ip_gateway."/".$_->ip_netmask;
110   }
111
112   '';
113 }
114
115
116 =item ac
117
118 Returns the L<FS::ac> object corresponding to this object.
119
120 =cut
121
122 sub ac {
123   my $self = shift;
124   return qsearchs('ac', { acnum => $self->acnum });
125 }
126
127 =item svc_broadband
128
129 Returns a list of L<FS::svc_broadband> objects associated
130 with this object.
131
132 =cut
133
134 #sub svc_broadband {
135 #  my $self = shift;
136 #  my @svc = qsearch('svc_broadband', { actypenum => $self->ac->ac_type->actypenum });
137 #  return grep { 
138 #    my $svc_addr = new NetAddr::IP($_->ip_addr, $_->ip_netmask);
139 #    $self_addr->contains($svc_addr);
140 #  } @svc;
141 #}
142
143 =back
144
145 =cut
146
147 1;
148