+=cut
+
+sub replace {
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $self = shift;
+ my $old = shift || $self->replace_old;
+ my $error = $self->SUPER::replace($old, @_);
+ return $error if $error;
+
+ if ( length($self->blocknum) ) {
+ #warn "FS::router::replace: blocknum = ".$self->blocknum."\n";
+ # then release any blocks we're already holding
+ foreach my $block ($self->addr_block) {
+ $block->set('routernum', 0);
+ $block->set('manual_flag', '');
+ $error ||= $block->replace;
+ }
+ if ( !$error and $self->blocknum > 0 ) {
+ # and, if the new blocknum is a real blocknum, assign it
+ my $block = FS::addr_block->by_key($self->blocknum);
+ if ( $block ) {
+ $block->set('routernum', $self->routernum);
+ $block->set('manual_flag', '');
+ $error ||= $block->replace;
+ } else {
+ $error = "blocknum ".$self->blocknum." not found";
+ }
+ }
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+ }
+ $dbh->commit if $oldAutoCommit;
+ return $error;
+}
+