agent-virtualize credit card surcharge percentage, RT#72961
[freeside.git] / FS / FS / radius_group.pm
1 package FS::radius_group;
2
3 use strict;
4 use base qw( FS::o2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs 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   dbh->commit;
129   '';
130 }
131
132 =item replace OLD_RECORD
133
134 Replaces the OLD_RECORD with this one in the database.  If there is an error,
135 returns the error, otherwise returns false.
136
137 =cut
138
139 # To keep these things from proliferating, we will follow the same 
140 # export/noexport switches that radius_attr uses.  If you _don't_ use
141 # Freeside to maintain your RADIUS group attributes, then it probably 
142 # shouldn't try to rename groups either.
143
144 sub replace {
145   my ($self, $old) = @_;
146   $old ||= $self->replace_old;
147
148   my $error = $self->check;
149   return $error if $error;
150
151   if ( !$FS::radius_attr::noexport_hack ) {
152     foreach ( qsearch('part_export', {}) ) {
153       next if !$_->option('export_attrs',1);
154       $error = $_->export_group_replace($self, $old);
155       return $error if $error;
156     }
157   }
158
159   $self->SUPER::replace($old);
160 }
161
162 =item check
163
164 Checks all fields to make sure this is a valid RADIUS group.  If there is
165 an error, returns the error, otherwise returns false.  Called by the insert
166 and replace methods.
167
168 =cut
169
170 # the check method should currently be supplied - FS::Record contains some
171 # data checking routines
172
173 sub check {
174   my $self = shift;
175
176   my $error = 
177     $self->ut_numbern('groupnum')
178     || $self->ut_text('groupname')
179     || $self->ut_textn('description')
180     || $self->ut_numbern('priority')
181     || $self->ut_numbern('speed_up')
182     || $self->ut_numbern('speed_down')
183   ;
184   return $error if $error;
185
186   $self->SUPER::check;
187 }
188
189 =item long_description
190
191 Returns a description for this group consisting of its description field, 
192 if any, and the RADIUS group name.
193
194 =cut
195
196 sub long_description {
197     my $self = shift;
198     $self->description ? $self->description . " (". $self->groupname . ")"
199                        : $self->groupname;
200 }
201
202 =item radius_attr
203
204 Returns all L<FS::radius_attr> objects (check and reply attributes) for 
205 this group.
206
207 =cut
208
209 sub radius_attr {
210   my $self = shift;
211   qsearch({
212       table   => 'radius_attr', 
213       hashref => {'groupnum' => $self->groupnum },
214       order_by  => 'ORDER BY attrtype, attrname',
215   })
216 }
217
218 =back
219
220 =head1 BUGS
221
222 This isn't export-specific (i.e. groups are globally unique, as opposed to being
223 unique per-export).
224
225 =head1 SEE ALSO
226
227 L<FS::radius_usergroup>, L<FS::Record>, schema.html from the base documentation.
228
229 =cut
230
231 1;
232