RT# 83450 - fixed rateplan export
[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 =item svcnum - the L<FS::svc_broadband> record that 'owns' this device
54
55 =back
56
57 =head1 METHODS
58
59 =over 4
60
61 =item new HASHREF
62
63 Creates a new NAS.  To add the NAS to the database, see L<"insert">.
64
65 Note that this stores the hash reference, not a distinct copy of the hash it
66 points to.  You can ask the object for a copy with the I<hash> method.
67
68 =cut
69
70 # the new method can be inherited from FS::Record, if a table method is defined
71
72 sub table { 'nas'; }
73
74 =item insert
75
76 Adds this record to the database.  If there is an error, returns the error,
77 otherwise returns false.
78
79 =item delete
80
81 Delete this record from the database and remove all linked exports.
82
83 =cut
84
85 sub delete {
86   my $oldAutoCommit = $FS::UID::AutoCommit;
87   local $FS::UID::AutoCommit = 0;
88   my $dbh = dbh;
89
90   my $self = shift;
91   my $error = $self->process_m2m(
92     link_table    => 'export_nas',
93     target_table  => 'part_export',
94     params        => []
95   ) || $self->SUPER::delete;
96
97   if ( $error ) {
98     $dbh->rollback if $oldAutoCommit;
99     return $error;
100   }
101   
102   $dbh->commit if $oldAutoCommit;
103   '';
104 }
105
106 =item replace OLD_RECORD
107
108 Replaces the OLD_RECORD with this one in the database.  If there is an error,
109 returns the error, otherwise returns false.
110
111 To change the list of linked exports, see the C<export_nas> method.
112
113 =cut
114
115 sub replace {
116   my $oldAutoCommit = $FS::UID::AutoCommit;
117   local $FS::UID::AutoCommit = 0;
118   my $dbh = dbh;
119
120   my ($self, $old) = @_;
121   $old ||= qsearchs('nas', { 'nasnum' => $self->nasnum });
122
123   my $error;
124   foreach my $part_export ( $self->part_export ) {
125     $error ||= $part_export->export_nas_replace($self, $old);
126   }
127
128   $error ||= $self->SUPER::replace($old);
129
130   if ( $error ) {
131     $dbh->rollback;
132     return $error;
133   }
134
135   $dbh->commit if $oldAutoCommit;
136   '';
137 }
138
139 =item check
140
141 Checks all fields to make sure this is a valid NAS.  If there is
142 an error, returns the error, otherwise returns false.  Called by the insert
143 and replace methods.
144
145 =cut
146
147 # the check method should currently be supplied - FS::Record contains some
148 # data checking routines
149
150 sub check {
151   my $self = shift;
152
153   my $error = 
154     $self->ut_numbern('nasnum')
155     || $self->ut_text('nasname')
156     || $self->ut_textn('shortname')
157     || $self->ut_text('type')
158     || $self->ut_numbern('ports')
159     || $self->ut_text('secret')
160     || $self->ut_textn('server')
161     || $self->ut_textn('community')
162     || $self->ut_text('description')
163     || $self->ut_foreign_keyn('svcnum', 'svc_broadband', 'svcnum')
164   ;
165   return $error if $error;
166
167   $self->SUPER::check;
168 }
169
170 =item part_export
171
172 Return all L<FS::part_export> objects to which this NAS is being exported.
173
174 =cut
175
176 sub part_export {
177   my $self = shift;
178   map { qsearchs('part_export', { exportnum => $_->exportnum }) } 
179         qsearch('export_nas', { nasnum => $self->nasnum})
180 }
181
182 =back
183
184 =head1 BUGS
185
186 =head1 SEE ALSO
187
188 L<FS::Record>, schema.html from the base documentation.
189
190 =cut
191
192 1;
193