delete fees, RT#81713
[freeside.git] / FS / FS / radius_usergroup.pm
1 package FS::radius_usergroup;
2 use base qw(FS::Record);
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs );
6 use FS::svc_acct;
7 use FS::radius_group;
8
9 =head1 NAME
10
11 FS::radius_usergroup - Object methods for radius_usergroup records
12
13 =head1 SYNOPSIS
14
15   use FS::radius_usergroup;
16
17   $record = new FS::radius_usergroup \%hash;
18   $record = new FS::radius_usergroup { '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::radius_usergroup object links an account (see L<FS::svc_acct>) with a
31 RADIUS group (see L<FS::radius_group>).  FS::radius_usergroup inherits from
32 FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item usergroupnum - primary key
37
38 =item svcnum - Account (see L<FS::svc_acct>).
39
40 =item groupnum - RADIUS group (see L<FS::radius_group>).
41
42 =back
43
44 =head1 METHODS
45
46 =over 4
47
48 =item new HASHREF
49
50 Creates a new record.  To add the record to the database, see L<"insert">.
51
52 Note that this stores the hash reference, not a distinct copy of the hash it
53 points to.  You can ask the object for a copy with the I<hash> method.
54
55 =cut
56
57 # the new method can be inherited from FS::Record, if a table method is defined
58
59 sub table { 'radius_usergroup'; }
60
61 =item insert
62
63 Adds this record to the database.  If there is an error, returns the error,
64 otherwise returns false.
65
66 =cut
67
68 #inherited from FS::Record
69
70 =item delete
71
72 Delete this record from the database.
73
74 =cut
75
76 #inherited from FS::Record
77
78 =item replace OLD_RECORD
79
80 Replaces the OLD_RECORD with this one in the database.  If there is an error,
81 returns the error, otherwise returns false.
82
83 =cut
84
85 #inherited from FS::Record
86
87 =item check
88
89 Checks all fields to make sure this is a valid record.  If there is
90 an error, returns the error, otherwise returns false.  Called by the insert
91 and replace methods.
92
93 =cut
94
95 sub check {
96   my $self = shift;
97   my $svcnum = $self->svcnum;
98   die "radius_usergroup.groupname is deprecated" if $self->groupname;
99
100   $self->ut_numbern('usergroupnum')
101     || ( $self->ut_foreign_key('svcnum','svc_acct','svcnum')
102       && $self->ut_foreign_key('svcnum','svc_broadband','svcnum')
103       && "Can't find radius_usergroup.svcnum $svcnum in svc_acct.svcnum or svc_broadband.svcnum" ) 
104     || $self->ut_foreign_key('groupnum','radius_group','groupnum')
105     || $self->SUPER::check
106   ;
107 }
108
109 =item svc_x
110
111 Returns the account associated with this record (see L<FS::svc_acct> and 
112 L<FS::svc_broadband>).
113
114 =cut
115
116 sub svc_acct {
117   my $self = shift;
118   qsearchs('svc_acct', { svcnum => $self->svcnum } ) ||
119   qsearchs('svc_broadband', { svcnum => $self->svcnum } )
120 }
121
122 =item radius_group
123
124 Returns the RADIUS group associated with this record (see L<FS::radius_group>).
125
126 =cut
127
128 sub _upgrade_data {  #class method
129   my ($class, %opts) = @_;
130
131   my %group_cache = map { $_->groupname => $_->groupnum } 
132                                                 qsearch('radius_group', {});
133
134   my @radius_usergroup = qsearch('radius_usergroup', {} );
135   my $error = '';
136   foreach my $rug ( @radius_usergroup ) {
137         my $groupname = $rug->groupname;
138         next unless $groupname;
139         unless(defined($group_cache{$groupname})) {
140             my $g = new FS::radius_group {
141                             'groupname' => $groupname,
142                             'description' => $groupname,
143                             };
144             $error = $g->insert;
145             die $error if $error;
146             $group_cache{$groupname} = $g->groupnum;
147         }
148         $rug->groupnum($group_cache{$groupname});
149         $rug->groupname('');
150         $error = $rug->replace;
151         die $error if $error;
152   }
153 }
154
155 =back
156
157 =head1 SEE ALSO
158
159 L<svc_acct>, L<FS::radius_group>, L<FS::Record>, schema.html from the base documentation.
160
161 =cut
162
163 1;
164