delete fees, RT#81713
[freeside.git] / FS / FS / radius_group.pm
1 package FS::radius_group;
2 use base qw( FS::o2m_Common FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch dbh );
6 use FS::radius_attr;
7
8 =head1 NAME
9
10 FS::radius_group - Object methods for radius_group records
11
12 =head1 SYNOPSIS
13
14   use FS::radius_group;
15
16   $record = new FS::radius_group \%hash;
17   $record = new FS::radius_group { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::radius_group object represents a RADIUS group.  FS::radius_group inherits from
30 FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item groupnum
35
36 primary key
37
38 =item groupname
39
40 groupname
41
42 =item description
43
44 description
45
46 =item priority
47
48 priority - for export
49
50 =item speed_up, speed_down - connection speeds in bits per second.  Some 
51 exports may use this to generate appropriate RADIUS attributes.
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new RADIUS group.  To add the RADIUS group to the database, see L<"insert">.
62
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to.  You can ask the object for a copy with the I<hash> method.
65
66 =cut
67
68 # the new method can be inherited from FS::Record, if a table method is defined
69
70 sub table { 'radius_group'; }
71
72 =item insert
73
74 Adds this record to the database.  If there is an error, returns the error,
75 otherwise returns false.
76
77 =cut
78
79 # the insert method can be inherited from FS::Record
80
81 =item delete
82
83 Delete this record from the database.
84
85 =cut
86
87 sub delete {
88   # okay, I guess we support it now
89   my $self = shift;
90   local $SIG{HUP} = 'IGNORE';
91   local $SIG{INT} = 'IGNORE';
92   local $SIG{QUIT} = 'IGNORE';
93   local $SIG{TERM} = 'IGNORE';
94   local $SIG{TSTP} = 'IGNORE';
95   local $SIG{PIPE} = 'IGNORE';
96
97   my $oldAutoCommit = $FS::UID::AutoCommit;
98   local $FS::UID::AutoCommit = 0;
99   my $dbh = dbh;
100
101   my $groupnum = $self->groupnum;
102   my $error = $self->process_o2m(
103                 'table' => 'radius_usergroup',
104                 'num_col' => 'groupnum',
105                 'fields' => ['groupnum'], # just delete them
106                 'params' => {},
107               ) || $self->SUPER::delete(@_);
108
109   if ( $error ) {
110     $dbh->rollback if $oldAutoCommit;
111     return $error;
112   }
113
114   foreach my $part_svc_column (
115     qsearch('part_svc_column', { columnname => 'usergroup' }) 
116   ) {
117     my $new_values = join(',', 
118       grep { $_ != $groupnum } split(',', $part_svc_column->columnvalue)
119     );
120     next if $new_values eq $part_svc_column->columnvalue;
121     $part_svc_column->set(columnvalue => $new_values);
122     $error = $part_svc_column->replace;
123     if ( $error ) {
124       $dbh->rollback if $oldAutoCommit;
125       return $error;
126     }
127   }
128
129   foreach my $radius_attr ( $self->radius_attr ) {
130     $error = $radius_attr->delete;
131     if ( $error ) {
132       $dbh->rollback if $oldAutoCommit;
133       return $error;
134     }
135   }
136
137   dbh->commit;
138   '';
139 }
140
141 =item replace OLD_RECORD
142
143 Replaces the OLD_RECORD with this one in the database.  If there is an error,
144 returns the error, otherwise returns false.
145
146 =cut
147
148 # To keep these things from proliferating, we will follow the same 
149 # export/noexport switches that radius_attr uses.  If you _don't_ use
150 # Freeside to maintain your RADIUS group attributes, then it probably 
151 # shouldn't try to rename groups either.
152
153 sub replace {
154   my ($self, $old) = @_;
155   $old ||= $self->replace_old;
156
157   my $error = $self->check;
158   return $error if $error;
159
160   if ( !$FS::radius_attr::noexport_hack ) {
161     foreach ( qsearch('part_export', {}) ) {
162       next if !$_->option('export_attrs',1);
163       $error = $_->export_group_replace($self, $old);
164       return $error if $error;
165     }
166   }
167
168   $self->SUPER::replace($old);
169 }
170
171 =item check
172
173 Checks all fields to make sure this is a valid RADIUS group.  If there is
174 an error, returns the error, otherwise returns false.  Called by the insert
175 and replace methods.
176
177 =cut
178
179 # the check method should currently be supplied - FS::Record contains some
180 # data checking routines
181
182 sub check {
183   my $self = shift;
184
185   my $error = 
186     $self->ut_numbern('groupnum')
187     || $self->ut_text('groupname')
188     || $self->ut_textn('description')
189     || $self->ut_numbern('priority')
190     || $self->ut_numbern('speed_up')
191     || $self->ut_numbern('speed_down')
192   ;
193   return $error if $error;
194
195   $self->SUPER::check;
196 }
197
198 =item long_description
199
200 Returns a description for this group consisting of its description field, 
201 if any, and the RADIUS group name.
202
203 =cut
204
205 sub long_description {
206     my $self = shift;
207     $self->description ? $self->description . " (". $self->groupname . ")"
208                        : $self->groupname;
209 }
210
211 =item radius_attr
212
213 Returns all L<FS::radius_attr> objects (check and reply attributes) for 
214 this group.
215
216 =cut
217
218 sub radius_attr {
219   my $self = shift;
220   qsearch({
221       table   => 'radius_attr', 
222       hashref => {'groupnum' => $self->groupnum },
223       order_by  => 'ORDER BY attrtype, attrname',
224   })
225 }
226
227 =back
228
229 =head1 BUGS
230
231 This isn't export-specific (i.e. groups are globally unique, as opposed to being
232 unique per-export).
233
234 =head1 SEE ALSO
235
236 L<FS::radius_usergroup>, L<FS::Record>, schema.html from the base documentation.
237
238 =cut
239
240 1;
241