prevent deletion of a tower sector with customers, RT#24197
[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 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 =item check
79
80 Checks all fields to make sure this is a valid tower.  If there is
81 an error, returns the error, otherwise returns false.  Called by the insert
82 and replace methods.
83
84 =cut
85
86 sub check {
87   my $self = shift;
88
89   my $error = 
90     $self->ut_numbern('towernum')
91     || $self->ut_text('towername')
92     || $self->ut_enum('disabled', [ '', 'Y' ])
93   ;
94   return $error if $error;
95
96   $self->SUPER::check;
97 }
98
99 =item default_sector
100
101 Returns the default sector.
102
103 =cut
104
105 sub default_sector {
106   my $self = shift;
107   qsearchs('tower_sector', { towernum => $self->towernum,
108                              sectorname => '_default' });
109 }
110
111 =item tower_sector
112
113 Returns the sectors of this tower, as FS::tower_sector objects (see
114 L<FS::tower_sector>), except for the default sector.
115
116 =cut
117
118 sub tower_sector {
119   my $self = shift;
120   qsearch({
121     'table'    => 'tower_sector',
122     'hashref'  => { 'towernum'    => $self->towernum,
123                     'sectorname'  => { op => '!=', value => '_default' },
124                   },
125     'order_by' => 'ORDER BY sectorname',
126   });
127 }
128
129 =item process_o2m
130
131 Wrapper for the default method (see L<FS::o2m_Common>) to manage the 
132 default sector.
133
134 =cut
135
136 sub process_o2m {
137   my $self = shift;
138   my %opt = @_;
139   my $params = $opt{params};
140
141   # Adjust to make sure our default sector is in the list.
142   my $default_sector = $self->default_sector
143     or warn "creating default sector for tower ".$self->towernum."\n";
144   my $idx = max(0, map { $_ =~ /^sectornum(\d+)$/ ? $1 : 0 } keys(%$params));
145   $idx++; # append to the param list
146   my $prefix = "sectornum$idx";
147   # empty sectornum will create the default sector if it doesn't exist yet
148   $params->{$prefix} = $default_sector ? $default_sector->sectornum : '';
149   $params->{$prefix.'_sectorname'} = '_default';
150   $params->{$prefix.'_ip_addr'} = $params->{'default_ip_addr'} || '';
151
152   $self->SUPER::process_o2m(%opt);
153 }
154
155 sub _upgrade_data {
156   # Create default sectors for any tower that doesn't have one.
157   # Shouldn't do any harm if they're missing, but just for completeness.
158   my $class = shift;
159   foreach my $tower (qsearch('tower',{})) {
160     next if $tower->default_sector;
161     my $sector = FS::tower_sector->new({
162         towernum => $tower->towernum,
163         sectorname => '_default',
164         ip_addr => '',
165     });
166     my $error = $sector->insert;
167     die "error creating default sector: $error\n" if $error;
168   }
169   '';
170 }
171
172 =back
173
174 =head1 BUGS
175
176 =head1 SEE ALSO
177
178 L<FS::tower_sector>, L<FS::svc_broadband>, L<FS::Record>
179
180 =cut
181
182 1;
183