+=item process_o2m
+
+Wrapper for the default method (see L<FS::o2m_Common>) to manage the
+default sector.
+
+=cut
+
+sub process_o2m {
+ my $self = shift;
+ my %opt = @_;
+ my $params = +{ %{$opt{params}} };
+
+ # Adjust to make sure our default sector is in the list.
+ my $default_sector = $self->default_sector
+ or warn "creating default sector for tower ".$self->towernum."\n";
+ my $idx = max(0, map { $_ =~ /^sectornum(\d+)$/ ? $1 : 0 } keys(%$params));
+ $idx++; # append to the param list
+ my $prefix = "sectornum$idx";
+ # empty sectornum will create the default sector if it doesn't exist yet
+ $params->{$prefix} = $default_sector ? $default_sector->sectornum : '';
+ $params->{$prefix.'_sectorname'} = '_default';
+ $params->{$prefix.'_ip_addr'} = $params->{'default_ip_addr'} || '';
+
+ $self->SUPER::process_o2m(%opt);
+}
+
+sub _upgrade_data {
+ # Create default sectors for any tower that doesn't have one.
+ # Shouldn't do any harm if they're missing, but just for completeness.
+ my $class = shift;
+ foreach my $tower (qsearch('tower',{})) {
+ next if $tower->default_sector;
+ my $sector = FS::tower_sector->new({
+ towernum => $tower->towernum,
+ sectorname => '_default',
+ ip_addr => '',
+ });
+ my $error = $sector->insert;
+ die "error creating default sector: $error\n" if $error;
+ }
+ '';
+}
+