stray closing /TABLE in the no-ticket case
[freeside.git] / FS / FS / tower.pm
1 package FS::tower;
2 use base qw( FS::o2m_Common FS::Record );
3
4 use strict;
5 use List::Util qw( max );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::tower_sector;
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 sub table { 'tower'; }
63
64 =item insert
65
66 Adds this record to the database.  If there is an error, returns the error,
67 otherwise returns false.
68
69 =item delete
70
71 Delete this record from the database.
72
73 =item replace OLD_RECORD
74
75 Replaces the OLD_RECORD with this one in the database.  If there is an error,
76 returns the error, otherwise returns false.
77
78 =cut
79
80 sub replace {
81   my $self = shift;
82   my $old = shift || $self->replace_old;
83   # editing the tower location needs to regenerate coverage on its sectors
84   my $regen_coverage = 0;
85   foreach (qw(latitude longitude height)) {
86     $regen_coverage = 1 if $self->get($_) != $old->get($_);
87   }
88
89   my $error = $self->SUPER::replace($old);
90   return $error if $error;
91
92   if ($regen_coverage) {
93     foreach my $sector ($self->tower_sector) {
94       $sector->queue_generate_coverage;
95     }
96   }
97 }
98
99 =item check
100
101 Checks all fields to make sure this is a valid tower.  If there is
102 an error, returns the error, otherwise returns false.  Called by the insert
103 and replace methods.
104
105 =cut
106
107 sub check {
108   my $self = shift;
109
110   my $error = 
111     $self->ut_numbern('towernum')
112     || $self->ut_text('towername')
113     || $self->ut_enum('disabled', [ '', 'Y' ])
114     || $self->ut_coordn('latitude')
115     || $self->ut_coordn('longitude')
116     || $self->ut_enum('coord_auto', [ '', 'Y' ])
117     || $self->ut_floatn('altitude')
118     || $self->ut_floatn('height')
119     || $self->ut_floatn('veg_height')
120     || $self->ut_alphan('color')
121   ;
122   return $error if $error;
123
124   $self->SUPER::check;
125 }
126
127 =item default_sector
128
129 Returns the default sector.
130
131 =cut
132
133 sub default_sector {
134   my $self = shift;
135   qsearchs('tower_sector', { towernum => $self->towernum,
136                              sectorname => '_default' });
137 }
138
139 =item tower_sector
140
141 Returns the sectors of this tower, as FS::tower_sector objects (see
142 L<FS::tower_sector>), except for the default sector.
143
144 =cut
145
146 sub tower_sector {
147   my $self = shift;
148   qsearch({
149     'table'    => 'tower_sector',
150     'hashref'  => { 'towernum'    => $self->towernum,
151                     'sectorname'  => { op => '!=', value => '_default' },
152                   },
153     'order_by' => 'ORDER BY sectorname',
154   });
155 }
156
157 =item process_o2m
158
159 Wrapper for the default method (see L<FS::o2m_Common>) to manage the 
160 default sector.
161
162 =cut
163
164 sub process_o2m {
165   my $self = shift;
166   my %opt = @_;
167   my $params = +{ %{$opt{params}} };
168
169   # Adjust to make sure our default sector is in the list.
170   my $default_sector = $self->default_sector
171     or warn "creating default sector for tower ".$self->towernum."\n";
172   my $idx = max(0, map { $_ =~ /^sectornum(\d+)$/ ? $1 : 0 } keys(%$params));
173   $idx++; # append to the param list
174   my $prefix = "sectornum$idx";
175   # empty sectornum will create the default sector if it doesn't exist yet
176   $params->{$prefix} = $default_sector ? $default_sector->sectornum : '';
177   $params->{$prefix.'_sectorname'} = '_default';
178   $params->{$prefix.'_ip_addr'} = $params->{'default_ip_addr'} || '';
179
180   $self->SUPER::process_o2m(%opt);
181 }
182
183 sub _upgrade_data {
184   # Create default sectors for any tower that doesn't have one.
185   # Shouldn't do any harm if they're missing, but just for completeness.
186   my $class = shift;
187   foreach my $tower (qsearch('tower',{})) {
188     next if $tower->default_sector;
189     my $sector = FS::tower_sector->new({
190         towernum => $tower->towernum,
191         sectorname => '_default',
192         ip_addr => '',
193     });
194     my $error = $sector->insert;
195     die "error creating default sector: $error\n" if $error;
196   }
197   '';
198 }
199
200 =back
201
202 =head1 BUGS
203
204 =head1 SEE ALSO
205
206 L<FS::tower_sector>, L<FS::svc_broadband>, L<FS::Record>
207
208 =cut
209
210 1;
211