Vitelity DID selection improvements, RT4868
authorlevinse <levinse>
Tue, 11 Jan 2011 15:49:12 +0000 (15:49 +0000)
committerlevinse <levinse>
Tue, 11 Jan 2011 15:49:12 +0000 (15:49 +0000)
FS/FS/part_export/vitelity.pm
FS/FS/phone_avail.pm
httemplate/elements/select-did.html
httemplate/misc/phonenums.cgi

index 5654aff..12c3a7f 100644 (file)
@@ -43,55 +43,75 @@ sub get_dids {
   my %opt = ref($_[0]) ? %{$_[0]} : @_;
 
   if ( $opt{'tollfree'} ) {
-      # XXX: no caching for now
-      # XXX: limit option
-
     my $command = 'listtollfree';
     $command = 'listdids' if $self->option('fax');
     my @tollfree = $self->vitelity_command($command);
     my @ret = ();
 
-    if (scalar(@tollfree)) {
-       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: ';
-       
-       foreach my $did ( @tollfree ) {
-           $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
-           my($npa, $nxx, $station) = ($1, $2, $3);
-           push @ret, $did;
-
-           my $phone_avail = new FS::phone_avail {
-             'exportnum'   => $self->exportnum,
-             'countrycode' => '1', # vitelity is US/CA only now
-             'npa'         => $npa,
-             'nxx'         => $nxx,
-             'station'     => $station,
-           };
-
-           $error = $phone_avail->insert();
-           if ( $error ) {
-             $dbh->rollback if $oldAutoCommit;
-             die $errmsg.$error;
-           }
-        }
-       $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
+    return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
 
+    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{'areacode'} && $opt{'exchange'} ) { #return numbers in format NPA-NXX-XXXX
+  } 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;
 
     return [
       map { join('-', $_->npa, $_->nxx, $_->station ) }
@@ -99,35 +119,30 @@ sub get_dids {
             'table'    => 'phone_avail',
             'hashref'  => { 'exportnum'   => $self->exportnum,
                             'countrycode' => '1', # vitelity is US/CA only now
-                            'npa'         => $opt{'areacode'},
-                            'nxx'         => $opt{'exchange'},
+                            'name'         => $opt{'ratecenter'},
+                           'state'       => $opt{'state'},
                           },
-            'order_by' => 'ORDER BY station',
+            'order_by' => 'ORDER BY npa, nxx, station',
           })
     ];
 
-  } elsif ( $opt{'areacode'} ) { #return exchanges in format NPA-NXX- literal 'XXXX'
+  } elsif ( $opt{'areacode'} ) { 
 
-    # you can't call $->name .... that returns "(unlinked)"
-    # and in any case this is still major abuse of encapsulation, it just happens to work for the other fields
-    @rc = map { $_->{'Hash'}->{name}.' ('. $_->npa. '-'. $_->nxx. '-XXXX)' } 
+    my @rc = map { $_->{'Hash'}->{name}.", ".$_->state } 
           qsearch({
-            'select'   => 'DISTINCT npa,nxx,name',
+            'select'   => 'DISTINCT name, state',
             'table'    => 'phone_avail',
             'hashref'  => { 'exportnum'   => $self->exportnum,
                             'countrycode' => '1', # vitelity is US/CA only now
                             'npa'         => $opt{'areacode'},
                           },
-            'order_by' => 'ORDER BY nxx',
           });
 
-    @sorted_rc = sort @rc;
+    my @sorted_rc = sort @rc;
     return [ @sorted_rc ];
 
   } elsif ( $opt{'state'} ) { #and not other things, then return areacode
 
-    #XXX need to flush the cache at some point :/
-
     my @avail = qsearch({
       'select'   => 'DISTINCT npa',
       'table'    => 'phone_avail',
@@ -200,7 +215,7 @@ sub get_dids {
           'name'        => $ratecenter,
         };
 
-        $error = $phone_avail->insert();
+        my $error = $phone_avail->insert();
         if ( $error ) {
           $dbh->rollback if $oldAutoCommit;
           die $errmsg.$error;
@@ -213,8 +228,6 @@ sub get_dids {
     $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
 
     my @return = sort { $a <=> $b } keys %npa;
-    #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
-
     return \@return;
 
   } else {
@@ -246,14 +259,14 @@ sub _export_insert {
 
   #we want to provision and catch errors now, not queue
 
-  %vparams = ( 'did' => $svc_phone->phonenum );
+  my %vparams = ( 'did' => $svc_phone->phonenum );
   $vparams{'routesip'} = $self->option('routesip') 
     if defined $self->option('routesip');
   $vparams{'type'} = $self->option('type') 
     if defined $self->option('type');
 
-  $command = 'getlocaldid';
-  $success = 'success';
+  my $command = 'getlocaldid';
+  my $success = 'success';
 
   # this is OK as Vitelity for now is US/CA only; it's not a hack
   $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
index 92286ab..677195d 100644 (file)
@@ -195,6 +195,21 @@ sub process_batch_import {
 
 }
 
+sub flush { # evil direct SQL
+    my $opt = shift;
+
+    if ( $opt->{'ratecenter'} =~ /^[\w\s]+$/
+           && $opt->{'state'} =~ /^[A-Z][A-Z]$/ 
+           && $opt->{'exportnum'} =~ /^\d+$/) {
+       my $sth = dbh->prepare('delete from phone_avail where exportnum = ? '.
+                   ' and state = ? and name = ?');
+       $sth->execute($opt->{'exportnum'},$opt->{'state'},$opt->{'ratecenter'})
+           or die $sth->errstr;
+    }
+
+    '';
+}
+
 # Used by FS::Upgrade to migrate to a new database.
 sub _upgrade_data {
   my ($class, %opts) = @_;
index 5256ae4..6cc63b9 100644 (file)
@@ -13,9 +13,8 @@ Example:
 </%doc>
 % if ( $use_selector ) {
 
-%   if ( $export->exporttype eq 'vitelity' && 
-%              ( $export->option('restrict_selection') eq 'non-tollfree'
-%                  || !$export->option('restrict_selection')) ) {
+%   if ( $export->option('restrict_selection') eq 'non-tollfree'
+%                  || !$export->option('restrict_selection') ) {
     <TABLE>
 
       <TR>
@@ -63,9 +62,8 @@ Example:
     </TABLE>
 
 % } 
-%      if ( $export->exporttype eq 'vitelity' && 
-%              ( $export->option('restrict_selection') eq 'tollfree'
-%                  || !$export->option('restrict_selection')) ) {
+%   if ( $export->option('restrict_selection') eq 'tollfree'
+%                  || !$export->option('restrict_selection') ) {
            <font size="-1">Toll-free</font>
            <% include('/elements/select-phonenum.html',
                        'svcpart' => $svcpart,
index b86d03f..108a5f7 100644 (file)
@@ -13,11 +13,15 @@ if ( scalar(@exports) > 1 ) {
   die "no DID providing export attached to svcpart $svcpart";
 }
 my $export = $exports[0];
-
+    
 my %opts = ();
 if ( $exchangestring eq 'tollfree' ) {
     $opts{'tollfree'} = 1;
 }
+elsif ( $exchangestring =~ /^([\w\s]+), ([A-Z][A-Z])$/ ) {
+    $opts{'ratecenter'} = $1;
+    $opts{'state'} = $2;
+}
 else {
     $exchangestring =~ /\((\d{3})-(\d{3})-XXXX\)\s*$/i
       or die "unparsable exchange: $exchangestring";