per-agent disable_previous_balance, #15863
[freeside.git] / FS / FS / router.pm
1 package FS::router;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs qsearch dbh );
6 use FS::addr_block;
7
8 @ISA = qw( FS::Record FS::m2m_Common );
9
10 =head1 NAME
11
12 FS::router - Object methods for router records
13
14 =head1 SYNOPSIS
15
16   use FS::router;
17
18   $record = new FS::router \%hash;
19   $record = new FS::router { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::router record describes a broadband router, such as a DSLAM or a wireless
32  access point.  FS::router inherits from FS::Record.  The following 
33 fields are currently supported:
34
35 =over 4
36
37 =item routernum - primary key
38
39 =item routername - descriptive name for the router
40
41 =item svcnum - svcnum of the owning FS::svc_broadband, if appropriate
42
43 =back
44
45 =head1 METHODS
46
47 =over 4
48
49 =item new HASHREF
50
51 Create a new record.  To add the record to the database, see "insert".
52
53 =cut
54
55 sub table { 'router'; }
56
57 =item insert
58
59 Adds this record to the database.  If there is an error, returns the error,
60 otherwise returns false.
61
62 =item delete
63
64 Deletes this record from the database.  If there is an error, returns the
65 error, otherwise returns false.
66
67 =item replace OLD_RECORD
68
69 Replaces OLD_RECORD with this one in the database.  If there is an error,
70 returns the error, otherwise returns false.
71
72 =item check
73
74 Checks all fields to make sure this is a valid record.  If there is an error,
75 returns the error, otherwise returns false.  Called by the insert and replace
76 methods.
77
78 =cut
79
80 sub check {
81   my $self = shift;
82
83   my $error =
84     $self->ut_numbern('routernum')
85     || $self->ut_text('routername')
86     || $self->ut_agentnum_acl('agentnum', 'Broadband global configuration')
87   ;
88   return $error if $error;
89
90   $self->SUPER::check;
91 }
92
93 =item delete
94
95 Deletes this router if and only if no address blocks (see L<FS::addr_block>)
96 are currently allocated to it.
97
98 =cut
99
100 sub delete {
101     my $self = shift;
102
103     return 'Router has address blocks allocated to it' if $self->addr_block;
104     
105     local $SIG{HUP} = 'IGNORE';
106     local $SIG{INT} = 'IGNORE';
107     local $SIG{QUIT} = 'IGNORE';
108     local $SIG{TERM} = 'IGNORE';
109     local $SIG{TSTP} = 'IGNORE';
110     local $SIG{PIPE} = 'IGNORE';
111
112     my $oldAutoCommit = $FS::UID::AutoCommit;
113     local $FS::UID::AutoCommit = 0;
114     my $dbh = dbh;
115     
116     my $error = $self->SUPER::delete;
117     if ( $error ) {
118        $dbh->rollback if $oldAutoCommit;
119        return $error;
120     }
121   
122     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
123     '';
124 }
125
126 =item addr_block
127
128 Returns a list of FS::addr_block objects (address blocks) associated
129 with this object.
130
131 =cut
132
133 sub addr_block {
134   my $self = shift;
135   return qsearch('addr_block', { routernum => $self->routernum });
136 }
137
138 =item part_svc_router
139
140 Returns a list of FS::part_svc_router objects associated with this 
141 object.  This is unlikely to be useful for any purpose other than retrieving 
142 the associated FS::part_svc objects.  See below.
143
144 =cut
145
146 sub part_svc_router {
147   my $self = shift;
148   return qsearch('part_svc_router', { routernum => $self->routernum });
149 }
150
151 =item part_svc
152
153 Returns a list of FS::part_svc objects associated with this object.
154
155 =cut
156
157 sub part_svc {
158   my $self = shift;
159   return map { qsearchs('part_svc', { svcpart => $_->svcpart }) }
160       $self->part_svc_router;
161 }
162
163 =item agent
164
165 Returns the agent associated with this router, if any.
166
167 =cut
168
169 sub agent {
170   qsearchs('agent', { 'agentnum' => shift->agentnum });
171 }
172
173 =back
174
175 =head1 SEE ALSO
176
177 FS::svc_broadband, FS::router, FS::addr_block, FS::part_svc,
178 schema.html from the base documentation.
179
180 =cut
181
182 1;
183