prevent deletion of a tower sector with customers, RT#24197
[freeside.git] / FS / FS / tower_sector.pm
1 package FS::tower_sector;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::tower;
7 use FS::svc_broadband;
8
9 =head1 NAME
10
11 FS::tower_sector - Object methods for tower_sector records
12
13 =head1 SYNOPSIS
14
15   use FS::tower_sector;
16
17   $record = new FS::tower_sector \%hash;
18   $record = new FS::tower_sector { '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_sector object represents an tower sector.  FS::tower_sector
31 inherits from FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item sectornum
36
37 primary key
38
39 =item towernum
40
41 towernum
42
43 =item sectorname
44
45 sectorname
46
47 =item ip_addr
48
49 ip_addr
50
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new sector.  To add the sector to the database, see L<"insert">.
61
62 Note that this stores the hash reference, not a distinct copy of the hash it
63 points to.  You can ask the object for a copy with the I<hash> method.
64
65 =cut
66
67 sub table { 'tower_sector'; }
68
69 =item insert
70
71 Adds this record to the database.  If there is an error, returns the error,
72 otherwise returns false.
73
74 =item delete
75
76 Delete this record from the database.
77
78 =cut
79
80 sub delete {
81   my $self = shift;
82
83   #not the most efficient, not not awful, and its not like deleting a sector
84   # with customers is a common operation
85   return "Can't delete a sector with customers" if $self->svc_broadband;
86
87   $self->SUPER::delete;
88 }
89
90 =item replace OLD_RECORD
91
92 Replaces the OLD_RECORD with this one in the database.  If there is an error,
93 returns the error, otherwise returns false.
94
95 =item check
96
97 Checks all fields to make sure this is a valid sector.  If there is
98 an error, returns the error, otherwise returns false.  Called by the insert
99 and replace methods.
100
101 =cut
102
103 sub check {
104   my $self = shift;
105
106   my $error = 
107     $self->ut_numbern('sectornum')
108     || $self->ut_number('towernum', 'tower', 'towernum')
109     || $self->ut_text('sectorname')
110     || $self->ut_textn('ip_addr')
111   ;
112   return $error if $error;
113
114   $self->SUPER::check;
115 }
116
117 =item tower
118
119 Returns the tower for this sector, as an FS::tower object (see L<FS::tower>).
120
121 =cut
122
123 sub tower {
124   my $self = shift;
125   qsearchs('tower', { 'towernum'=>$self->towernum } );
126 }
127
128 =item description
129
130 Returns a description for this sector including tower name.
131
132 =cut
133
134 sub description {
135   my $self = shift;
136   if ( $self->sectorname eq '_default' ) {
137     $self->tower->towername
138   }
139   else {
140     $self->tower->towername. ' sector '. $self->sectorname
141   }
142 }
143
144 =item svc_broadband
145
146 Returns the services on this tower sector.
147
148 =cut
149
150 sub svc_broadband {
151   my $self = shift;
152   qsearch('svc_broadband', { 'sectornum' => $self->sectornum });
153 }
154
155 =back
156
157 =head1 BUGS
158
159 =head1 SEE ALSO
160
161 L<FS::tower>, L<FS::Record>, schema.html from the base documentation.
162
163 =cut
164
165 1;
166