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