1 package FS::part_export::globalpops_voip;
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 'dry_run' => { label=>"Test mode - don't actually provision" },
21 'desc' => 'Provision phone numbers to VoIP Innovations (formerly GlobalPOPs VoIP)',
22 'options' => \%options,
25 Requires installation of
26 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
31 sub rebless { shift; }
35 my %opt = ref($_[0]) ? %{$_[0]} : @_;
38 # 'orderby' => 'npa', #but it doesn't seem to work :/
40 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
41 %getdids = ( 'npa' => $opt{'areacode'},
42 'nxx' => $opt{'exchange'},
44 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
45 %getdids = ( 'npa' => $opt{'areacode'} );
46 } elsif ( $opt{'state'} ) {
49 'table' => 'phone_avail',
50 'hashref' => { 'exportnum' => $self->exportnum,
51 'countrycode' => '1', #don't hardcode me when gp goes int'l
52 'state' => $opt{'state'},
54 'order_by' => 'ORDER BY npa',
57 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
59 #otherwise, search for em
60 %getdids = ( 'state' => $opt{'state'} );
64 my $dids = $self->gp_command('getDIDs', %getdids);
66 if ( $dids->{'type'} eq 'Error' ) {
67 my $error = "Error running VoIP Innovations getDIDs: ".
68 $dids->{'statuscode'}. ': '. $dids->{'status'}. "\n";
73 my $search = $dids->{'search'};
75 if ( $search->{'statuscode'} == 302200 ) {
77 } elsif ( $search->{'statuscode'} != 100 ) {
79 my $error = "Error running VoIP Innovations getDIDs: ";
80 if ( $search->{'statuscode'} || $search->{'status'} ) {
81 $error .= $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
83 $error .= Dumper($search);
91 #my $latas = $search->{state}{lata};
93 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
94 %latas = map $search->{state}{lata}{$_},
97 %latas = %{ $search->{state}{lata} };
100 foreach my $lata ( keys %latas ) {
104 #my $l = $latas{$lata};
105 #$l = $l->{rate_center} if exists $l->{rate_center};
107 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
108 my $lata_search = $lata_dids->{'search'};
109 unless ( $lata_search->{'statuscode'} == 100 ) {
110 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
113 my $l = $lata_search->{state}{lata}{'rate_center'};
119 if ( grep $l->{$_}, qw(name friendlyname) ) {
120 %rate_center = map $l->{$_},
121 qw(name friendlyname);
126 foreach my $rate_center ( keys %rate_center ) {
128 #warn "rate center $rate_center";
130 my $rc = $rate_center{$rate_center};
131 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
134 if ( exists($rc->{npa}) ) {
137 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
143 if ( exists($r->{npa}{name}) ) {
146 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
149 foreach my $npa (@npa) {
151 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
155 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
157 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
161 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
167 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
170 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
172 if ( $npa->{nxx}{name} ) {
173 @nxx = ( $npa->{nxx}{name} );
175 @nxx = keys %{ $npa->{nxx} };
178 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
181 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
182 #my $ac = $npa->{name};
184 #warn Dumper($r) unless length($ac) == 3;
186 push @return, $npa->{name}
187 unless grep { $_ eq $npa->{name} } @return;
190 warn "WARNING: returning nothing for get_dids without known options"; #?
197 } #foreach my $rate_center
201 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
202 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
203 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
204 @return = sort { lc($a) cmp lc($b) } @return;
205 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
209 local $SIG{HUP} = 'IGNORE';
210 local $SIG{INT} = 'IGNORE';
211 local $SIG{QUIT} = 'IGNORE';
212 local $SIG{TERM} = 'IGNORE';
213 local $SIG{TSTP} = 'IGNORE';
214 local $SIG{PIPE} = 'IGNORE';
216 my $oldAutoCommit = $FS::UID::AutoCommit;
217 local $FS::UID::AutoCommit = 0;
220 my $errmsg = 'WARNING: error populating phone availability cache: ';
222 foreach my $return (@return) {
223 my $phone_avail = new FS::phone_avail {
224 'exportnum' => $self->exportnum,
225 'countrycode' => '1', #don't hardcode me when gp goes int'l
226 'state' => $opt{'state'},
229 $error = $phone_avail->insert();
237 $dbh->rollback if $oldAutoCommit;
239 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
244 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
245 @return = sort { $a <=> $b } @return;
247 warn "WARNING: returning nothing for get_dids without known options"; #?
255 my( $self, $command, @args ) = @_;
257 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
263 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
264 'login' => $self->option('login'),
265 'password' => $self->option('password'),
269 $gp->$command(@args);
274 my( $self, $svc_phone ) = (shift, shift);
276 return '' if $self->option('dry_run');
278 #we want to provision and catch errors now, not queue
280 my $r = $self->gp_command('reserveDID',
281 'did' => $svc_phone->phonenum,
283 'endpointgroup' => $self->option('endpointgroup'),
286 my $rdid = $r->{did};
288 if ( $rdid->{'statuscode'} != 100 ) {
289 return "Error running VoIP Innovations reserveDID: ".
290 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
293 my $a = $self->gp_command('assignDID',
294 'did' => $svc_phone->phonenum,
295 'endpointgroup' => $self->option('endpointgroup'),
300 my $adid = $a->{did};
302 if ( $adid->{'statuscode'} != 100 ) {
303 return "Error running VoIP Innovations assignDID: ".
304 $adid->{'statuscode'}. ': '. $adid->{'status'};
310 sub _export_replace {
311 my( $self, $new, $old ) = (shift, shift, shift);
313 #hmm, what's to change?
318 my( $self, $svc_phone ) = (shift, shift);
320 return '' if $self->option('dry_run');
322 #probably okay to queue the deletion...?
323 #but hell, let's do it inline anyway, who wants phone numbers hanging around
325 my $r = $self->gp_command('releaseDID',
326 'did' => $svc_phone->phonenum,
329 my $rdid = $r->{did};
331 if ( $rdid->{'statuscode'} != 100 ) {
332 return "Error running VoIP Innovations releaseDID: ".
333 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
339 sub _export_suspend {
340 my( $self, $svc_phone ) = (shift, shift);
345 sub _export_unsuspend {
346 my( $self, $svc_phone ) = (shift, shift);
351 #hmm, might forgo queueing entirely for most things, data is too much of a pita
352 #sub globalpops_voip_queue {
353 # my( $self, $svcnum, $method ) = (shift, shift, shift);
354 # my $queue = new FS::queue {
355 # 'svcnum' => $svcnum,
356 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
359 # $self->option('login'),
360 # $self->option('password'),
366 sub globalpops_voip_command {
367 my($login, $password, $method, @args) = @_;
369 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
372 my $gp = new Net::GlobalPOPs
374 'password' => $password,
378 my $return = $gp->$method( @args );
381 #$return->{'statuscode'}
383 die $return->{'status'} if $return->{'statuscode'};