227e3eb6608fa7ff4b3a7bb0d02ca0c022268b03
[freeside.git] / FS / FS / tower.pm
1 package FS::tower;
2
3 use strict;
4 use base qw( FS::o2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::tower_sector;
7 use List::Util qw( max );
8
9 =head1 NAME
10
11 FS::tower - Object methods for tower records
12
13 =head1 SYNOPSIS
14
15   use FS::tower;
16
17   $record = new FS::tower \%hash;
18   $record = new FS::tower { '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::tower object represents a tower.  FS::tower inherits from
31 FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item towernum
36
37 primary key
38
39 =item towername
40
41 Tower name
42
43 =item disabled
44
45 Disabled flag, empty or 'Y'
46
47 =back
48
49 =head1 METHODS
50
51 =over 4
52
53 =item new HASHREF
54
55 Creates a new tower.  To add the tower to the database, see L<"insert">.
56
57 Note that this stores the hash reference, not a distinct copy of the hash it
58 points to.  You can ask the object for a copy with the I<hash> method.
59
60 =cut
61
62 # the new method can be inherited from FS::Record, if a table method is defined
63
64 sub table { 'tower'; }
65
66 =item insert
67
68 Adds this record to the database.  If there is an error, returns the error,
69 otherwise returns false.
70
71 =cut
72
73 =item delete
74
75 Delete this record from the database.
76
77 =cut
78
79 =item replace OLD_RECORD
80
81 Replaces the OLD_RECORD with this one in the database.  If there is an error,
82 returns the error, otherwise returns false.
83
84 =cut
85
86 # the replace method can be inherited from FS::Record
87
88 =item check
89
90 Checks all fields to make sure this is a valid tower.  If there is
91 an error, returns the error, otherwise returns false.  Called by the insert
92 and replace methods.
93
94 =cut
95
96 # the check method should currently be supplied - FS::Record contains some
97 # data checking routines
98
99 sub check {
100   my $self = shift;
101
102   my $error = 
103     $self->ut_numbern('towernum')
104     || $self->ut_text('towername')
105     || $self->ut_enum('disabled', [ '', 'Y' ])
106   ;
107   return $error if $error;
108
109   $self->SUPER::check;
110 }
111
112 =item default_sector
113
114 Returns the default sector.
115
116 =cut
117
118 sub default_sector {
119   my $self = shift;
120   qsearchs('tower_sector', { towernum => $self->towernum,
121                              sectorname => '_default' });
122 }
123
124 =item tower_sector
125
126 Returns the sectors of this tower, as FS::tower_sector objects (see
127 L<FS::tower_sector>), except for the default sector.
128
129 =cut
130
131 sub tower_sector {
132   my $self = shift;
133   qsearch({
134     'table'    => 'tower_sector',
135     'hashref'  => { 'towernum'    => $self->towernum,
136                     'sectorname'  => { op => '!=', value => '_default' },
137                   },
138     'order_by' => 'ORDER BY sectorname',
139   });
140 }
141
142 =item process_o2m
143
144 Wrapper for the default method (see L<FS::o2m_Common>) to manage the 
145 default sector.
146
147 =cut
148
149 sub process_o2m {
150   my $self = shift;
151   my %opt = @_;
152   my $params = $opt{params};
153
154   # Adjust to make sure our default sector is in the list.
155   my $default_sector = $self->default_sector
156     or warn "creating default sector for tower ".$self->towernum."\n";
157   my $idx = max(0, map { $_ =~ /^sectornum(\d+)$/ ? $1 : 0 } keys(%$params));
158   $idx++; # append to the param list
159   my $prefix = "sectornum$idx";
160   # empty sectornum will create the default sector if it doesn't exist yet
161   $params->{$prefix} = $default_sector ? $default_sector->sectornum : '';
162   $params->{$prefix.'_sectorname'} = '_default';
163   $params->{$prefix.'_ip_addr'} = $params->{'default_ip_addr'} || '';
164
165   $self->SUPER::process_o2m(%opt);
166 }
167
168 sub _upgrade_data {
169   # Create default sectors for any tower that doesn't have one.
170   # Shouldn't do any harm if they're missing, but just for completeness.
171   my $class = shift;
172   foreach my $tower (qsearch('tower',{})) {
173     next if $tower->default_sector;
174     my $sector = FS::tower_sector->new({
175         towernum => $tower->towernum,
176         sectorname => '_default',
177         ip_addr => '',
178     });
179     my $error = $sector->insert;
180     die "error creating default sector: $error\n" if $error;
181   }
182   '';
183 }
184
185 =back
186
187 =head1 BUGS
188
189 =head1 SEE ALSO
190
191 L<FS::tower_sector>, L<FS::svc_broadband>, L<FS::Record>
192
193 =cut
194
195 1;
196