1 package FS::part_export::globalpops_voip;
3 use vars qw(@ISA %info);
5 use FS::Record qw(qsearch dbh);
9 @ISA = qw(FS::part_export);
11 tie my %options, 'Tie::IxHash',
12 'login' => { label=>'VoIP Innovations API login' },
13 'password' => { label=>'VoIP Innovations API password' },
14 'endpointgroup' => { label=>'VoIP Innovations endpoint group number' },
15 'dry_run' => { label=>"Test mode - don't actually provision" },
20 'desc' => 'Provision phone numbers to VoIP Innovations (formerly GlobalPOPs VoIP)',
21 'options' => \%options,
23 Requires installation of
24 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
29 sub rebless { shift; }
33 my %opt = ref($_[0]) ? %{$_[0]} : @_;
36 # 'orderby' => 'npa', #but it doesn't seem to work :/
38 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
39 %getdids = ( 'npa' => $opt{'areacode'},
40 'nxx' => $opt{'exchange'},
42 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
43 %getdids = ( 'npa' => $opt{'areacode'} );
44 } elsif ( $opt{'state'} ) {
47 'table' => 'phone_avail',
48 'hashref' => { 'exportnum' => $self->exportnum,
49 'countrycode' => '1', #don't hardcode me when gp goes int'l
50 'state' => $opt{'state'},
52 'order_by' => 'ORDER BY npa',
55 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
57 #otherwise, search for em
58 %getdids = ( 'state' => $opt{'state'} );
62 my $dids = $self->gp_command('getDIDs', %getdids);
64 if ( $dids->{'type'} eq 'Error' ) {
65 my $error = "Error running VoIP Innovations getDIDs: ".
66 $dids->{'statuscode'}. ': '. $dids->{'status'}. "\n";
71 my $search = $dids->{'search'};
73 if ( $search->{'statuscode'} == 302200 ) {
75 } elsif ( $search->{'statuscode'} != 100 ) {
76 my $error = "Error running VoIP Innovations getDIDs: ".
77 $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
84 #my $latas = $search->{state}{lata};
86 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
87 %latas = map $search->{state}{lata}{$_},
90 %latas = %{ $search->{state}{lata} };
93 foreach my $lata ( keys %latas ) {
97 #my $l = $latas{$lata};
98 #$l = $l->{rate_center} if exists $l->{rate_center};
100 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
101 my $lata_search = $lata_dids->{'search'};
102 unless ( $lata_search->{'statuscode'} == 100 ) {
103 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
106 my $l = $lata_search->{state}{lata}{'rate_center'};
112 if ( grep $l->{$_}, qw(name friendlyname) ) {
113 %rate_center = map $l->{$_},
114 qw(name friendlyname);
119 foreach my $rate_center ( keys %rate_center ) {
121 #warn "rate center $rate_center";
123 my $rc = $rate_center{$rate_center};
124 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
127 if ( exists($rc->{npa}) ) {
130 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
136 if ( exists($r->{npa}{name}) ) {
139 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
142 foreach my $npa (@npa) {
144 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
148 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
150 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
154 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
160 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
163 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
165 if ( $npa->{nxx}{name} ) {
166 @nxx = ( $npa->{nxx}{name} );
168 @nxx = keys %{ $npa->{nxx} };
171 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
174 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
175 #my $ac = $npa->{name};
177 #warn Dumper($r) unless length($ac) == 3;
179 push @return, $npa->{name}
180 unless grep { $_ eq $npa->{name} } @return;
183 warn "WARNING: returning nothing for get_dids without known options"; #?
190 } #foreach my $rate_center
194 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
195 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
196 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
197 @return = sort { lc($a) cmp lc($b) } @return;
198 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
202 local $SIG{HUP} = 'IGNORE';
203 local $SIG{INT} = 'IGNORE';
204 local $SIG{QUIT} = 'IGNORE';
205 local $SIG{TERM} = 'IGNORE';
206 local $SIG{TSTP} = 'IGNORE';
207 local $SIG{PIPE} = 'IGNORE';
209 my $oldAutoCommit = $FS::UID::AutoCommit;
210 local $FS::UID::AutoCommit = 0;
213 my $errmsg = 'WARNING: error populating phone availability cache: ';
215 foreach my $return (@return) {
216 my $phone_avail = new FS::phone_avail {
217 'exportnum' => $self->exportnum,
218 'countrycode' => '1', #don't hardcode me when gp goes int'l
219 'state' => $opt{'state'},
222 $error = $phone_avail->insert();
230 $dbh->rollback if $oldAutoCommit;
232 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
237 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
238 @return = sort { $a <=> $b } @return;
240 warn "WARNING: returning nothing for get_dids without known options"; #?
248 my( $self, $command, @args ) = @_;
250 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
256 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
257 'login' => $self->option('login'),
258 'password' => $self->option('password'),
262 $gp->$command(@args);
267 my( $self, $svc_phone ) = (shift, shift);
269 return '' if $self->option('dry_run');
271 #we want to provision and catch errors now, not queue
273 my $r = $self->gp_command('reserveDID',
274 'did' => $svc_phone->phonenum,
276 'endpointgroup' => $self->option('endpointgroup'),
279 my $rdid = $r->{did};
281 if ( $rdid->{'statuscode'} != 100 ) {
282 return "Error running VoIP Innovations reserveDID: ".
283 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
286 my $a = $self->gp_command('assignDID',
287 'did' => $svc_phone->phonenum,
288 'endpointgroup' => $self->option('endpointgroup'),
293 my $adid = $a->{did};
295 if ( $adid->{'statuscode'} != 100 ) {
296 return "Error running VoIP Innovations assignDID: ".
297 $adid->{'statuscode'}. ': '. $adid->{'status'};
303 sub _export_replace {
304 my( $self, $new, $old ) = (shift, shift, shift);
306 #hmm, what's to change?
311 my( $self, $svc_phone ) = (shift, shift);
313 return '' if $self->option('dry_run');
315 #probably okay to queue the deletion...?
316 #but hell, let's do it inline anyway, who wants phone numbers hanging around
318 my $r = $self->gp_command('releaseDID',
319 'did' => $svc_phone->phonenum,
322 my $rdid = $r->{did};
324 if ( $rdid->{'statuscode'} != 100 ) {
325 return "Error running VoIP Innovations releaseDID: ".
326 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
332 sub _export_suspend {
333 my( $self, $svc_phone ) = (shift, shift);
338 sub _export_unsuspend {
339 my( $self, $svc_phone ) = (shift, shift);
344 #hmm, might forgo queueing entirely for most things, data is too much of a pita
345 #sub globalpops_voip_queue {
346 # my( $self, $svcnum, $method ) = (shift, shift, shift);
347 # my $queue = new FS::queue {
348 # 'svcnum' => $svcnum,
349 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
352 # $self->option('login'),
353 # $self->option('password'),
359 sub globalpops_voip_command {
360 my($login, $password, $method, @args) = @_;
362 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
365 my $gp = new Net::GlobalPOPs
367 'password' => $password,
371 my $return = $gp->$method( @args );
374 #$return->{'statuscode'}
376 die $return->{'status'} if $return->{'statuscode'};