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; }
38 sub can_get_dids { 1; }
42 my %opt = ref($_[0]) ? %{$_[0]} : @_;
45 # 'orderby' => 'npa', #but it doesn't seem to work :/
47 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
48 %getdids = ( 'npa' => $opt{'areacode'},
49 'nxx' => $opt{'exchange'},
51 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
52 %getdids = ( 'npa' => $opt{'areacode'} );
53 } elsif ( $opt{'state'} ) {
56 'table' => 'phone_avail',
57 'hashref' => { 'exportnum' => $self->exportnum,
58 'countrycode' => '1', #don't hardcode me when gp goes int'l
59 'state' => $opt{'state'},
61 'order_by' => 'ORDER BY npa',
64 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
66 #otherwise, search for em
67 %getdids = ( 'state' => $opt{'state'} );
71 my $dids = $self->gp_command('getDIDs', %getdids);
73 if ( $dids->{'type'} eq 'Error' ) {
74 my $error = "Error running VoIP Innovations getDIDs: ".
75 $dids->{'statuscode'}. ': '. $dids->{'status'}. "\n";
80 my $search = $dids->{'search'};
82 if ( $search->{'statuscode'} == 302200 ) {
84 } elsif ( $search->{'statuscode'} != 100 ) {
86 my $error = "Error running VoIP Innovations getDIDs: ";
87 if ( $search->{'statuscode'} || $search->{'status'} ) {
88 $error .= $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
90 $error .= Dumper($search);
98 #my $latas = $search->{state}{lata};
100 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
101 %latas = map $search->{state}{lata}{$_},
102 qw(name rate_center);
104 %latas = %{ $search->{state}{lata} };
107 foreach my $lata ( keys %latas ) {
111 #my $l = $latas{$lata};
112 #$l = $l->{rate_center} if exists $l->{rate_center};
114 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
115 my $lata_search = $lata_dids->{'search'};
116 unless ( $lata_search->{'statuscode'} == 100 ) {
117 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
120 my $l = $lata_search->{state}{lata}{'rate_center'};
126 if ( grep $l->{$_}, qw(name friendlyname) ) {
127 %rate_center = map $l->{$_},
128 qw(name friendlyname);
133 foreach my $rate_center ( keys %rate_center ) {
135 #warn "rate center $rate_center";
137 my $rc = $rate_center{$rate_center};
138 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
141 if ( exists($rc->{npa}) ) {
144 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
150 if ( exists($r->{npa}{name}) ) {
153 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
156 foreach my $npa (@npa) {
158 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
162 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
164 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
168 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
174 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
177 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
179 if ( $npa->{nxx}{name} ) {
180 @nxx = ( $npa->{nxx}{name} );
182 @nxx = keys %{ $npa->{nxx} };
185 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
188 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
189 #my $ac = $npa->{name};
191 #warn Dumper($r) unless length($ac) == 3;
193 push @return, $npa->{name}
194 unless grep { $_ eq $npa->{name} } @return;
197 warn "WARNING: returning nothing for get_dids without known options"; #?
204 } #foreach my $rate_center
208 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
209 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
210 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
211 @return = sort { lc($a) cmp lc($b) } @return;
212 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
216 local $SIG{HUP} = 'IGNORE';
217 local $SIG{INT} = 'IGNORE';
218 local $SIG{QUIT} = 'IGNORE';
219 local $SIG{TERM} = 'IGNORE';
220 local $SIG{TSTP} = 'IGNORE';
221 local $SIG{PIPE} = 'IGNORE';
223 my $oldAutoCommit = $FS::UID::AutoCommit;
224 local $FS::UID::AutoCommit = 0;
227 my $errmsg = 'WARNING: error populating phone availability cache: ';
229 foreach my $return (@return) {
230 my $phone_avail = new FS::phone_avail {
231 'exportnum' => $self->exportnum,
232 'countrycode' => '1', #don't hardcode me when gp goes int'l
233 'state' => $opt{'state'},
236 $error = $phone_avail->insert();
244 $dbh->rollback if $oldAutoCommit;
246 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
251 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
252 @return = sort { $a <=> $b } @return;
254 warn "WARNING: returning nothing for get_dids without known options"; #?
262 my( $self, $command, @args ) = @_;
264 eval "use Net::VoIP_Innovations 2.00;";
270 my $gp = Net::VoIP_Innovations->new(
271 'login' => $self->option('login'),
272 'password' => $self->option('password'),
276 $gp->$command(@args);
281 my( $self, $svc_phone ) = (shift, shift);
283 return '' if $self->option('dry_run');
285 #we want to provision and catch errors now, not queue
291 my $r = $self->gp_command('reserveDID',
292 'did' => $svc_phone->phonenum,
294 'endpointgroup' => $self->option('endpointgroup'),
297 my $rdid = $r->{did};
299 if ( $rdid->{'statuscode'} != 100 ) {
300 return "Error running VoIP Innovations reserveDID: ".
301 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
308 my $a = $self->gp_command('assignDID',
309 'did' => $svc_phone->phonenum,
310 'endpointgroup' => $self->option('endpointgroup'),
315 my $adid = $a->{did};
317 if ( $adid->{'statuscode'} != 100 ) {
318 return "Error running VoIP Innovations assignDID: ".
319 $adid->{'statuscode'}. ': '. $adid->{'status'};
326 if ( $self->option('e911') ) {
328 my %location_hash = $svc_phone->location_hash;
329 my( $zip, $plus4 ) = split('-', $location_hash->{zip});
330 my $e = $self->gp_command('911Insert',
331 'did' => $svc_phone->phonenum,
332 'Address1' => $location_hash{address1},
333 'Address2' => $location_hash{address2},
334 'City' => $location_hash{city},
335 'State' => $location_hash{state},
337 'PlusFour' => $plus4,
339 $svc_phone->phone_name
340 || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
343 my $edid = $e->{did};
345 if ( $edid->{'statuscode'} != 100 ) {
346 return "Error running VoIP Innovations 911Insert: ".
347 $edid->{'statuscode'}. ': '. $edid->{'status'};
355 sub _export_replace {
356 my( $self, $new, $old ) = (shift, shift, shift);
358 #hmm, anything to change besides E911 data?
364 if ( $self->option('e911') ) {
366 my %location_hash = $svc_phone->location_hash;
367 my( $zip, $plus4 ) = split('-', $location_hash->{zip});
368 my $e = $self->gp_command('911Update',
369 'did' => $svc_phone->phonenum,
370 'Address1' => $location_hash{address1},
371 'Address2' => $location_hash{address2},
372 'City' => $location_hash{city},
373 'State' => $location_hash{state},
375 'PlusFour' => $plus4,
377 $svc_phone->phone_name
378 || $svc_phone->cust_svc->cust_pkg->cust_main->contact_firstlast,
381 my $edid = $e->{did};
383 if ( $edid->{'statuscode'} != 100 ) {
384 return "Error running VoIP Innovations 911Update: ".
385 $edid->{'statuscode'}. ': '. $edid->{'status'};
394 my( $self, $svc_phone ) = (shift, shift);
396 return '' if $self->option('dry_run');
398 #probably okay to queue the deletion...?
399 #but hell, let's do it inline anyway, who wants phone numbers hanging around
401 my $r = $self->gp_command('releaseDID',
402 'did' => $svc_phone->phonenum,
405 my $rdid = $r->{did};
407 if ( $rdid->{'statuscode'} != 100 ) {
408 return "Error running VoIP Innovations releaseDID: ".
409 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
412 #delete e911 information? assuming release clears all that
417 sub _export_suspend {
418 my( $self, $svc_phone ) = (shift, shift);
423 sub _export_unsuspend {
424 my( $self, $svc_phone ) = (shift, shift);
429 #hmm, might forgo queueing entirely for most things, data is too much of a pita
430 #sub globalpops_voip_queue {
431 # my( $self, $svcnum, $method ) = (shift, shift, shift);
432 # my $queue = new FS::queue {
433 # 'svcnum' => $svcnum,
434 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
437 # $self->option('login'),
438 # $self->option('password'),
444 #sub globalpops_voip_command {
445 # my($login, $password, $method, @args) = @_;
447 # eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
450 # my $gp = new Net::GlobalPOPs::MediaServicesAPI
452 # 'password' => $password,
456 # my $return = $gp->$method( @args );
458 # #$return->{'status'}
459 # #$return->{'statuscode'}
461 # die $return->{'status'} if $return->{'statuscode'};