RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / internal_diddb.pm
index d7b5587..8771ae8 100644 (file)
@@ -1,31 +1,52 @@
 package FS::part_export::internal_diddb;
 
 use vars qw(@ISA %info);
-#use Tie::IxHash;
+use Tie::IxHash;
 use FS::Record qw(qsearch qsearchs);
 use FS::part_export;
 use FS::phone_avail;
 
 @ISA = qw(FS::part_export);
 
+tie my %options, 'Tie::IxHash',
+  'countrycode' => { label => 'Country code', 'default' => '1', },
+;
+
 %info = (
-  'svc'   => 'svc_phone',
-  'desc'  => 'Provision phone numbers from the internal DID database',
-  'notes' => 'After adding the export, DIDs may be imported under Tools -> Importing -> Import phone numbers (DIDs)',
+  'svc'     => 'svc_phone',
+  'desc'    => 'Provision phone numbers from the internal DID database',
+  'notes'   => 'After adding the export, DIDs may be imported under Tools -> Importing -> Import phone numbers (DIDs)',
+  'options' => \%options,
+  'no_machine' => 1,
 );
 
 sub rebless { shift; }
 
+sub can_get_dids { 1; }
+
 sub get_dids {
   my $self = shift;
   my %opt = ref($_[0]) ? %{$_[0]} : @_;
 
-  my %hash = ( 'countrycode' => 1, #XXX make an option or something
+  my %hash = ( 'countrycode' => ( $self->option('countrycode') || '1' ),
                'exportnum'   => $self->exportnum,
                'svcnum'      => '',
              );
 
-  if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
+  if ( $opt{'ratecenter'} && $opt{'state'} ) {
+    my $rc = $opt{ratecenter};
+    $rc =~ s/, [A-Z][A-Z]$//g;
+    $hash{name} = $rc;
+    $hash{state} = $opt{state};
+
+    return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
+                 qsearch({ 'table'    => 'phone_avail',
+                           'hashref'  => \%hash,
+                           'order_by' => 'ORDER BY station',
+                        })
+           ];
+  }
+  elsif ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
 
     $hash{npa} = $opt{areacode};
     $hash{nxx} = $opt{exchange};
@@ -37,11 +58,22 @@ sub get_dids {
                         })
            ];
 
-  } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
+  } elsif ( $opt{'areacode'} ) { 
 
     $hash{npa} = $opt{areacode};
 
-    return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' }
+    my @rc = qsearch({ 'select' => 'DISTINCT name, state',
+                       'table'    => 'phone_avail',
+                       'hashref'  => \%hash,
+                   });
+
+    if(scalar(@rc)) {
+       my $first_phone_avail = $rc[0];
+       return [ map { $_->get('name').", ".$_->state } @rc ]
+           if $first_phone_avail->get('name');
+    }
+
+    return [ map { '('. $_->npa. '-'. $_->nxx. '-XXXX)' } 
                  qsearch({ 'select'   => 'DISTINCT npa, nxx',
                            'table'    => 'phone_avail',
                            'hashref'  => \%hash,
@@ -49,7 +81,7 @@ sub get_dids {
                         })
            ];
 
-  } elsif ( $opt{'state'} ) { #return aracodes
+  } elsif ( $opt{'state'} ) { #return areacodes
 
     $hash{state} = $opt{state};
 
@@ -62,7 +94,15 @@ sub get_dids {
            ];
 
   } else { 
-    die "FS::part_export::internal_diddb::get_dids called without options\n";
+
+    #die "FS::part_export::internal_diddb::get_dids called without options\n";
+    return [ map { $_->npa. '-'. $_->nxx. '-'. $_->station }
+                 qsearch({ 'table'    => 'phone_avail',
+                           'hashref'  => \%hash,
+                           'order_by' => 'ORDER BY station',
+                        })
+           ];
+
   }
 
 }
@@ -75,7 +115,7 @@ sub _export_insert   { #link phone_avail to svcnum
   my( $npa, $nxx, $station ) = ($1, $2, $3);
 
   my $phone_avail = qsearchs('phone_avail', {
-    'countrycode' => 1, #XXX make an option or something
+    'countrycode' => ( $self->option('countrycode') || '1' ),
     'exportnum'   => $self->exportnum,
     'svcnum'      => '',
     'npa'         => $npa,
@@ -95,12 +135,12 @@ sub _export_insert   { #link phone_avail to svcnum
 sub _export_delete   { #unlink phone_avail from svcnum
   my( $self, $svc_phone ) = (shift, shift);
 
-  $svc_phone =~ /^(\d{3})(\d{3})(\d+)$/
+  $svc_phone->phonenum =~ /^(\d{3})(\d{3})(\d+)$/
     or return "unparsable phone number: ". $svc_phone->phonenum;
   my( $npa, $nxx, $station ) = ($1, $2, $3);
 
   my $phone_avail = qsearchs('phone_avail', {
-    'countrycode' => 1, #XXX make an option or something
+    'countrycode' => ( $self->option('countrycode') || '1'),
     'exportnum'   => $self->exportnum,
     'svcnum'      => $svc_phone->svcnum,
     #these too?
@@ -109,8 +149,11 @@ sub _export_delete   { #unlink phone_avail from svcnum
     'station'     => $station,
   });
 
-  return "can't find number to return to availability: ". $svc_phone->phonenum
-    unless $phone_avail;
+  unless ( $phone_avail ) {
+    warn "WARNING: can't find number to return to availability: ".
+         $svc_phone->phonenum;
+    return;
+  }
 
   $phone_avail->svcnum('');