+ foreach my $did ( @tollfree ) {
+ $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
+ push @ret, $did;
+ }
+
+ my @sorted_ret = sort @ret;
+ return \@sorted_ret;
+
+ } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
+
+ my %flushopts = ( 'state' => $opt{'state'},
+ 'ratecenter' => $opt{'ratecenter'},
+ 'exportnum' => $self->exportnum
+ );
+ FS::phone_avail::flush( \%flushopts );
+
+ local $SIG{HUP} = 'IGNORE';
+ local $SIG{INT} = 'IGNORE';
+ local $SIG{QUIT} = 'IGNORE';
+ local $SIG{TERM} = 'IGNORE';
+ local $SIG{TSTP} = 'IGNORE';
+ local $SIG{PIPE} = 'IGNORE';
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $errmsg = 'WARNING: error populating phone availability cache: ';
+
+ my $command = 'listlocal';
+ $command = 'listdids' if $self->option('fax');
+ my @dids = $self->vitelity_command( $command,
+ 'state' => $opt{'state'},
+ 'ratecenter' => $opt{'ratecenter'},
+ );
+ # XXX: Options: type=unlimited OR type=pri
+
+ next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
+ die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
+
+ foreach my $did ( @dids ) {
+ $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
+ my($npa, $nxx, $station) = ($1, $2, $3);
+
+ my $phone_avail = new FS::phone_avail {
+ 'exportnum' => $self->exportnum,
+ 'countrycode' => '1', # vitelity is US/CA only now
+ 'state' => $opt{'state'},
+ 'npa' => $npa,
+ 'nxx' => $nxx,
+ 'station' => $station,
+ 'name' => $opt{'ratecenter'},
+ };
+
+ my $error = $phone_avail->insert();
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ die $errmsg.$error;
+ }
+
+ }
+ $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;