fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / FS / nas.pm
1 package FS::nas;
2
3 use strict;
4 use base qw( FS::m2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::export_nas;
7 use FS::part_export;
8
9 =head1 NAME
10
11 FS::nas - Object methods for nas records
12
13 =head1 SYNOPSIS
14
15   use FS::nas;
16
17   $record = new FS::nas \%hash;
18   $record = new FS::nas { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::nas object represents a RADIUS client.  FS::nas inherits from
31 FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item nasnum - primary key
36
37 =item nasname - "NAS name", i.e. IP address
38
39 =item shortname - short descriptive name
40
41 =item type - the equipment vendor
42
43 =item ports
44
45 =item secret - the authentication secret for this client
46
47 =item server - virtual server name (optional)
48
49 =item community
50
51 =item description - a longer descriptive name
52
53
54 =back
55
56 =head1 METHODS
57
58 =over 4
59
60 =item new HASHREF
61
62 Creates a new NAS.  To add the NAS to the database, see L<"insert">.
63
64 Note that this stores the hash reference, not a distinct copy of the hash it
65 points to.  You can ask the object for a copy with the I<hash> method.
66
67 =cut
68
69 # the new method can be inherited from FS::Record, if a table method is defined
70
71 sub table { 'nas'; }
72
73 =item insert
74
75 Adds this record to the database.  If there is an error, returns the error,
76 otherwise returns false.
77
78 =item delete
79
80 Delete this record from the database and remove all linked exports.
81
82 =cut
83
84 sub delete {
85   my $oldAutoCommit = $FS::UID::AutoCommit;
86   local $FS::UID::AutoCommit = 0;
87   my $dbh = dbh;
88
89   my $self = shift;
90   my $error = $self->process_m2m(
91     link_table    => 'export_nas',
92     target_table  => 'part_export',
93     params        => []
94   ) || $self->SUPER::delete;
95
96   if ( $error ) {
97     $dbh->rollback;
98     return $error;
99   }
100   
101   $dbh->commit if $oldAutoCommit;
102   '';
103 }
104
105 =item replace OLD_RECORD
106
107 Replaces the OLD_RECORD with this one in the database.  If there is an error,
108 returns the error, otherwise returns false.
109
110 To change the list of linked exports, see the C<export_nas> method.
111
112 =cut
113
114 sub replace {
115   my $oldAutoCommit = $FS::UID::AutoCommit;
116   local $FS::UID::AutoCommit = 0;
117   my $dbh = dbh;
118
119   my ($self, $old) = @_;
120   $old ||= qsearchs('nas', { 'nasnum' => $self->nasnum });
121
122   my $error;
123   foreach my $part_export ( $self->part_export ) {
124     $error ||= $part_export->export_nas_replace($self, $old);
125   }
126
127   $error ||= $self->SUPER::replace($old);
128
129   if ( $error ) {
130     $dbh->rollback;
131     return $error;
132   }
133
134   $dbh->commit if $oldAutoCommit;
135   '';
136 }
137
138 =item check
139
140 Checks all fields to make sure this is a valid NAS.  If there is
141 an error, returns the error, otherwise returns false.  Called by the insert
142 and replace methods.
143
144 =cut
145
146 # the check method should currently be supplied - FS::Record contains some
147 # data checking routines
148
149 sub check {
150   my $self = shift;
151
152   my $error = 
153     $self->ut_numbern('nasnum')
154     || $self->ut_text('nasname')
155     || $self->ut_textn('shortname')
156     || $self->ut_text('type')
157     || $self->ut_numbern('ports')
158     || $self->ut_text('secret')
159     || $self->ut_textn('server')
160     || $self->ut_textn('community')
161     || $self->ut_text('description')
162   ;
163   return $error if $error;
164
165   $self->SUPER::check;
166 }
167
168 =item part_export
169
170 Return all L<FS::part_export> objects to which this NAS is being exported.
171
172 =cut
173
174 sub part_export {
175   my $self = shift;
176   map { qsearchs('part_export', { exportnum => $_->exportnum }) } 
177         qsearch('export_nas', { nasnum => $self->nasnum})
178 }
179
180 =back
181
182 =head1 BUGS
183
184 =head1 SEE ALSO
185
186 L<FS::Record>, schema.html from the base documentation.
187
188 =cut
189
190 1;
191