1 package FS::part_export::voip_innovations2;
3 use vars qw(@ISA %info);
5 use FS::Record qw(qsearch dbh);
10 @ISA = qw(FS::part_export);
12 tie my %options, 'Tie::IxHash',
13 'login' => { label=>'VoIP Innovations API login' },
14 'password' => { label=>'VoIP Innovations API password' },
15 'endpointgroup' => { label=>'VoIP Innovations endpoint group number' },
16 'e911' => { label=>'Provision E911 data',
19 'dry_run' => { label=>"Test mode - don't actually provision",
26 'desc' => 'Provision phone numbers / E911 to VoIP Innovations (API 2.0)',
27 'options' => \%options,
30 Requires installation of
31 <a href="http://search.cpan.org/dist/Net-VoIP_Innovations">Net::VoIP_Innovations</a>
36 sub rebless { shift; }
40 my %opt = ref($_[0]) ? %{$_[0]} : @_;
43 # 'orderby' => 'npa', #but it doesn't seem to work :/
45 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
46 %getdids = ( 'npa' => $opt{'areacode'},
47 'nxx' => $opt{'exchange'},
49 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
50 %getdids = ( 'npa' => $opt{'areacode'} );
51 } elsif ( $opt{'state'} ) {
54 'table' => 'phone_avail',
55 'hashref' => { 'exportnum' => $self->exportnum,
56 'countrycode' => '1', #don't hardcode me when gp goes int'l
57 'state' => $opt{'state'},
59 'order_by' => 'ORDER BY npa',
62 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
64 #otherwise, search for em
65 %getdids = ( 'state' => $opt{'state'} );
69 my $dids = $self->gp_command('getDIDs', %getdids);
71 if ( $dids->{'type'} eq 'Error' ) {
72 my $error = "Error running VoIP Innovations getDIDs: ".
73 $dids->{'statuscode'}. ': '. $dids->{'status'}. "\n";
78 my $search = $dids->{'search'};
80 if ( $search->{'statuscode'} == 302200 ) {
82 } elsif ( $search->{'statuscode'} != 100 ) {
84 my $error = "Error running VoIP Innovations getDIDs: ";
85 if ( $search->{'statuscode'} || $search->{'status'} ) {
86 $error .= $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
88 $error .= Dumper($search);
96 #my $latas = $search->{state}{lata};
98 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
99 %latas = map $search->{state}{lata}{$_},
100 qw(name rate_center);
102 %latas = %{ $search->{state}{lata} };
105 foreach my $lata ( keys %latas ) {
109 #my $l = $latas{$lata};
110 #$l = $l->{rate_center} if exists $l->{rate_center};
112 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
113 my $lata_search = $lata_dids->{'search'};
114 unless ( $lata_search->{'statuscode'} == 100 ) {
115 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
118 my $l = $lata_search->{state}{lata}{'rate_center'};
124 if ( grep $l->{$_}, qw(name friendlyname) ) {
125 %rate_center = map $l->{$_},
126 qw(name friendlyname);
131 foreach my $rate_center ( keys %rate_center ) {
133 #warn "rate center $rate_center";
135 my $rc = $rate_center{$rate_center};
136 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
139 if ( exists($rc->{npa}) ) {
142 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
148 if ( exists($r->{npa}{name}) ) {
151 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
154 foreach my $npa (@npa) {
156 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
160 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
162 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
166 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
172 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
175 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
177 if ( $npa->{nxx}{name} ) {
178 @nxx = ( $npa->{nxx}{name} );
180 @nxx = keys %{ $npa->{nxx} };
183 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
186 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
187 #my $ac = $npa->{name};
189 #warn Dumper($r) unless length($ac) == 3;
191 push @return, $npa->{name}
192 unless grep { $_ eq $npa->{name} } @return;
195 warn "WARNING: returning nothing for get_dids without known options"; #?
202 } #foreach my $rate_center
206 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
207 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
208 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
209 @return = sort { lc($a) cmp lc($b) } @return;
210 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
214 local $SIG{HUP} = 'IGNORE';
215 local $SIG{INT} = 'IGNORE';
216 local $SIG{QUIT} = 'IGNORE';
217 local $SIG{TERM} = 'IGNORE';
218 local $SIG{TSTP} = 'IGNORE';
219 local $SIG{PIPE} = 'IGNORE';
221 my $oldAutoCommit = $FS::UID::AutoCommit;
222 local $FS::UID::AutoCommit = 0;
225 my $errmsg = 'WARNING: error populating phone availability cache: ';
227 foreach my $return (@return) {
228 my $phone_avail = new FS::phone_avail {
229 'exportnum' => $self->exportnum,
230 'countrycode' => '1', #don't hardcode me when gp goes int'l
231 'state' => $opt{'state'},
234 $error = $phone_avail->insert();
242 $dbh->rollback if $oldAutoCommit;
244 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
249 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
250 @return = sort { $a <=> $b } @return;
252 warn "WARNING: returning nothing for get_dids without known options"; #?
260 my( $self, $command, @args ) = @_;
262 eval "use Net::VoIP_Innovations 2.00;";
268 my $gp = Net::VoIP_Innovations->new(
269 'login' => $self->option('login'),
270 'password' => $self->option('password'),
274 $gp->$command(@args);
279 my( $self, $svc_phone ) = (shift, shift);
281 return '' if $self->option('dry_run');
283 #we want to provision and catch errors now, not queue
289 my $r = $self->gp_command('reserveDID',
290 'did' => $svc_phone->phonenum,
292 'endpointgroup' => $self->option('endpointgroup'),
295 my $rdid = $r->{did};
297 if ( $rdid->{'statuscode'} != 100 ) {
298 return "Error running VoIP Innovations reserveDID: ".
299 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
306 my $a = $self->gp_command('assignDID',
307 'did' => $svc_phone->phonenum,
308 'endpointgroup' => $self->option('endpointgroup'),
313 my $adid = $a->{did};
315 if ( $adid->{'statuscode'} != 100 ) {
316 return "Error running VoIP Innovations assignDID: ".
317 $adid->{'statuscode'}. ': '. $adid->{'status'};
324 if ( $self->option('e911') ) {
326 my %location_hash = $svc_phone->location_hash;
327 my( $zip, $plus4 ) = split('-', $location_hash->{zip});
328 my $e = $self->gp_command('911Insert',
329 'did' => $svc_phone->phonenum,
330 'Address1' => $location_hash{address1},
331 'Address2' => $location_hash{address2},
332 'City' => $location_hash{city},
333 'State' => $location_hash{state},
335 'PlusFour' => $plus4,
337 $svc_phone->phone_name
338 || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
341 my $edid = $e->{did};
343 if ( $edid->{'statuscode'} != 100 ) {
344 return "Error running VoIP Innovations 911Insert: ".
345 $edid->{'statuscode'}. ': '. $edid->{'status'};
353 sub _export_replace {
354 my( $self, $new, $old ) = (shift, shift, shift);
356 #hmm, anything to change besides E911 data?
362 if ( $self->option('e911') ) {
364 my %location_hash = $svc_phone->location_hash;
365 my( $zip, $plus4 ) = split('-', $location_hash->{zip});
366 my $e = $self->gp_command('911Update',
367 'did' => $svc_phone->phonenum,
368 'Address1' => $location_hash{address1},
369 'Address2' => $location_hash{address2},
370 'City' => $location_hash{city},
371 'State' => $location_hash{state},
373 'PlusFour' => $plus4,
375 $svc_phone->phone_name
376 || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
379 my $edid = $e->{did};
381 if ( $edid->{'statuscode'} != 100 ) {
382 return "Error running VoIP Innovations 911Update: ".
383 $edid->{'statuscode'}. ': '. $edid->{'status'};
392 my( $self, $svc_phone ) = (shift, shift);
394 return '' if $self->option('dry_run');
396 #probably okay to queue the deletion...?
397 #but hell, let's do it inline anyway, who wants phone numbers hanging around
399 my $r = $self->gp_command('releaseDID',
400 'did' => $svc_phone->phonenum,
403 my $rdid = $r->{did};
405 if ( $rdid->{'statuscode'} != 100 ) {
406 return "Error running VoIP Innovations releaseDID: ".
407 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
410 #delete e911 information? assuming release clears all that
415 sub _export_suspend {
416 my( $self, $svc_phone ) = (shift, shift);
421 sub _export_unsuspend {
422 my( $self, $svc_phone ) = (shift, shift);
427 #hmm, might forgo queueing entirely for most things, data is too much of a pita
428 #sub globalpops_voip_queue {
429 # my( $self, $svcnum, $method ) = (shift, shift, shift);
430 # my $queue = new FS::queue {
431 # 'svcnum' => $svcnum,
432 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
435 # $self->option('login'),
436 # $self->option('password'),
442 #sub globalpops_voip_command {
443 # my($login, $password, $method, @args) = @_;
445 # eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
448 # my $gp = new Net::GlobalPOPs::MediaServicesAPI
450 # 'password' => $password,
454 # my $return = $gp->$method( @args );
456 # #$return->{'status'}
457 # #$return->{'statuscode'}
459 # die $return->{'status'} if $return->{'statuscode'};