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 'no_provision_did' => { label=>'Disable DID provisioning',
22 'dry_run' => { label=>"Test mode - don't actually provision",
29 'desc' => 'Provision phone numbers / E911 to VoIP Innovations (API 2.0)',
30 'options' => \%options,
33 Requires installation of
34 <a href="http://search.cpan.org/dist/Net-VoIP_Innovations">Net::VoIP_Innovations</a>
39 sub rebless { shift; }
43 ! $self->option('no_provision_did');
48 my %opt = ref($_[0]) ? %{$_[0]} : @_;
51 # 'orderby' => 'npa', #but it doesn't seem to work :/
53 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
54 %getdids = ( 'npa' => $opt{'areacode'},
55 'nxx' => $opt{'exchange'},
57 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
58 %getdids = ( 'npa' => $opt{'areacode'} );
59 } elsif ( $opt{'state'} ) {
62 'table' => 'phone_avail',
63 'hashref' => { 'exportnum' => $self->exportnum,
64 'countrycode' => '1', #don't hardcode me when gp goes int'l
65 'state' => $opt{'state'},
67 'order_by' => 'ORDER BY npa',
70 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
72 #otherwise, search for em
73 %getdids = ( 'state' => $opt{'state'} );
77 my $dids = $self->gp_command('getDIDs', %getdids);
79 if ( $dids->{'type'} eq 'Error' ) {
80 my $error = "Error running VoIP Innovations getDIDs: ".
81 $dids->{'statuscode'}. ': '. $dids->{'status'}. "\n";
86 my $search = $dids->{'search'};
88 if ( $search->{'statuscode'} == 302200 ) {
90 } elsif ( $search->{'statuscode'} != 100 ) {
92 my $error = "Error running VoIP Innovations getDIDs: ";
93 if ( $search->{'statuscode'} || $search->{'status'} ) {
94 $error .= $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
96 $error .= Dumper($search);
104 #my $latas = $search->{state}{lata};
106 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
107 %latas = map $search->{state}{lata}{$_},
108 qw(name rate_center);
110 %latas = %{ $search->{state}{lata} };
113 foreach my $lata ( keys %latas ) {
117 #my $l = $latas{$lata};
118 #$l = $l->{rate_center} if exists $l->{rate_center};
120 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
121 my $lata_search = $lata_dids->{'search'};
122 unless ( $lata_search->{'statuscode'} == 100 ) {
123 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
126 my $l = $lata_search->{state}{lata}{'rate_center'};
132 if ( grep $l->{$_}, qw(name friendlyname) ) {
133 %rate_center = map $l->{$_},
134 qw(name friendlyname);
139 foreach my $rate_center ( keys %rate_center ) {
141 #warn "rate center $rate_center";
143 my $rc = $rate_center{$rate_center};
144 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
147 if ( exists($rc->{npa}) ) {
150 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
156 if ( exists($r->{npa}{name}) ) {
159 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
162 foreach my $npa (@npa) {
164 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
168 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
170 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
174 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
180 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
183 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
185 if ( $npa->{nxx}{name} ) {
186 @nxx = ( $npa->{nxx}{name} );
188 @nxx = keys %{ $npa->{nxx} };
191 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
194 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
195 #my $ac = $npa->{name};
197 #warn Dumper($r) unless length($ac) == 3;
199 push @return, $npa->{name}
200 unless grep { $_ eq $npa->{name} } @return;
203 warn "WARNING: returning nothing for get_dids without known options"; #?
210 } #foreach my $rate_center
214 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
215 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
216 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
217 @return = sort { lc($a) cmp lc($b) } @return;
218 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
222 local $SIG{HUP} = 'IGNORE';
223 local $SIG{INT} = 'IGNORE';
224 local $SIG{QUIT} = 'IGNORE';
225 local $SIG{TERM} = 'IGNORE';
226 local $SIG{TSTP} = 'IGNORE';
227 local $SIG{PIPE} = 'IGNORE';
229 my $oldAutoCommit = $FS::UID::AutoCommit;
230 local $FS::UID::AutoCommit = 0;
233 my $errmsg = 'WARNING: error populating phone availability cache: ';
235 foreach my $return (@return) {
236 my $phone_avail = new FS::phone_avail {
237 'exportnum' => $self->exportnum,
238 'countrycode' => '1', #don't hardcode me when gp goes int'l
239 'state' => $opt{'state'},
242 $error = $phone_avail->insert();
250 $dbh->rollback if $oldAutoCommit;
252 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
257 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
258 @return = sort { $a <=> $b } @return;
260 warn "WARNING: returning nothing for get_dids without known options"; #?
268 my( $self, $command, @args ) = @_;
270 eval "use Net::VoIP_Innovations 2.00;";
276 my $gp = Net::VoIP_Innovations->new(
277 'login' => $self->option('login'),
278 'password' => $self->option('password'),
282 $gp->$command(@args);
287 my( $self, $svc_phone ) = (shift, shift);
289 return '' if $self->option('dry_run');
291 #we want to provision and catch errors now, not queue
293 unless ( $self->option('no_provision_did') ) {
299 my $r = $self->gp_command('reserveDID',
300 'did' => $svc_phone->phonenum,
302 'endpointgroup' => $self->option('endpointgroup'),
305 my $rdid = $r->{did};
307 if ( $rdid->{'statuscode'} != 100 ) {
308 return "Error running VoIP Innovations reserveDID: ".
309 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
316 my $a = $self->gp_command('assignDID',
317 'did' => $svc_phone->phonenum,
318 'endpointgroup' => $self->option('endpointgroup'),
323 my $adid = $a->{did};
325 if ( $adid->{'statuscode'} != 100 ) {
326 return "Error running VoIP Innovations assignDID: ".
327 $adid->{'statuscode'}. ': '. $adid->{'status'};
336 if ( $self->option('e911') ) {
338 my %location_hash = $svc_phone->location_hash;
339 my( $zip, $plus4 ) = split('-', $location_hash->{zip});
340 my $e = $self->gp_command('911Insert',
341 'did' => $svc_phone->phonenum,
342 'Address1' => $location_hash{address1},
343 'Address2' => $location_hash{address2},
344 'City' => $location_hash{city},
345 'State' => $location_hash{state},
347 'PlusFour' => $plus4,
349 $svc_phone->phone_name
350 || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
353 my $edid = $e->{did};
355 if ( $edid->{'statuscode'} != 100 ) {
356 return "Error running VoIP Innovations 911Insert: ".
357 $edid->{'statuscode'}. ': '. $edid->{'status'};
365 sub _export_replace {
366 my( $self, $new, $old ) = (shift, shift, shift);
368 #hmm, anything to change besides E911 data?
374 if ( $self->option('e911') ) {
376 my %location_hash = $svc_phone->location_hash;
377 my( $zip, $plus4 ) = split('-', $location_hash->{zip});
378 my $e = $self->gp_command('911Update',
379 'did' => $svc_phone->phonenum,
380 'Address1' => $location_hash{address1},
381 'Address2' => $location_hash{address2},
382 'City' => $location_hash{city},
383 'State' => $location_hash{state},
385 'PlusFour' => $plus4,
387 $svc_phone->phone_name
388 || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
391 my $edid = $e->{did};
393 if ( $edid->{'statuscode'} != 100 ) {
394 return "Error running VoIP Innovations 911Update: ".
395 $edid->{'statuscode'}. ': '. $edid->{'status'};
404 my( $self, $svc_phone ) = (shift, shift);
406 return '' if $self->option('dry_run');
408 #probably okay to queue the deletion...?
409 #but hell, let's do it inline anyway, who wants phone numbers hanging around
411 unless ( $self->option('no_provision_did') ) {
413 my $r = $self->gp_command('releaseDID',
414 'did' => $svc_phone->phonenum,
417 my $rdid = $r->{did};
419 if ( $rdid->{'statuscode'} != 100 ) {
420 return "Error running VoIP Innovations releaseDID: ".
421 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
426 #delete e911 information? assuming release clears all that
427 #if ( $self->option('e911') ) {
428 # # but need to handle the no_provision_did case
434 sub _export_suspend {
435 my( $self, $svc_phone ) = (shift, shift);
440 sub _export_unsuspend {
441 my( $self, $svc_phone ) = (shift, shift);
446 #hmm, might forgo queueing entirely for most things, data is too much of a pita
447 #sub globalpops_voip_queue {
448 # my( $self, $svcnum, $method ) = (shift, shift, shift);
449 # my $queue = new FS::queue {
450 # 'svcnum' => $svcnum,
451 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
454 # $self->option('login'),
455 # $self->option('password'),
461 #sub globalpops_voip_command {
462 # my($login, $password, $method, @args) = @_;
464 # eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
467 # my $gp = new Net::GlobalPOPs::MediaServicesAPI
469 # 'password' => $password,
473 # my $return = $gp->$method( @args );
475 # #$return->{'status'}
476 # #$return->{'statuscode'}
478 # die $return->{'status'} if $return->{'statuscode'};