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);
63 if ( $dids->{'type'} eq 'Error' ) {
64 die "Error running VoIP Innovations getDIDs: ".
65 $search->{'statuscode'}. ': '. $search->{'status'}; #die??
71 my $search = $dids->{'search'};
73 if ( $search->{'statuscode'} == 302200 ) {
75 } elsif ( $search->{'statuscode'} != 100 ) {
76 die "Error running VoIP Innovations getDIDs: ".
77 $search->{'statuscode'}. ': '. $search->{'status'}; #die??
82 #my $latas = $search->{state}{lata};
84 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
85 %latas = map $search->{state}{lata}{$_},
88 %latas = %{ $search->{state}{lata} };
91 foreach my $lata ( keys %latas ) {
95 #my $l = $latas{$lata};
96 #$l = $l->{rate_center} if exists $l->{rate_center};
98 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
99 my $lata_search = $lata_dids->{'search'};
100 unless ( $lata_search->{'statuscode'} == 100 ) {
101 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
104 my $l = $lata_search->{state}{lata}{'rate_center'};
110 if ( grep $l->{$_}, qw(name friendlyname) ) {
111 %rate_center = map $l->{$_},
112 qw(name friendlyname);
117 foreach my $rate_center ( keys %rate_center ) {
119 #warn "rate center $rate_center";
121 my $rc = $rate_center{$rate_center};
122 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
125 if ( exists($rc->{npa}) ) {
128 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
134 if ( exists($r->{npa}{name}) ) {
137 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
140 foreach my $npa (@npa) {
142 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
146 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
148 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
152 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
158 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
161 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
163 if ( $npa->{nxx}{name} ) {
164 @nxx = ( $npa->{nxx}{name} );
166 @nxx = keys %{ $npa->{nxx} };
169 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
172 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
173 #my $ac = $npa->{name};
175 #warn Dumper($r) unless length($ac) == 3;
177 push @return, $npa->{name}
178 unless grep { $_ eq $npa->{name} } @return;
181 warn "WARNING: returning nothing for get_dids without known options"; #?
188 } #foreach my $rate_center
192 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
193 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
194 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
195 @return = sort { lc($a) cmp lc($b) } @return;
196 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
200 local $SIG{HUP} = 'IGNORE';
201 local $SIG{INT} = 'IGNORE';
202 local $SIG{QUIT} = 'IGNORE';
203 local $SIG{TERM} = 'IGNORE';
204 local $SIG{TSTP} = 'IGNORE';
205 local $SIG{PIPE} = 'IGNORE';
207 my $oldAutoCommit = $FS::UID::AutoCommit;
208 local $FS::UID::AutoCommit = 0;
211 my $errmsg = 'WARNING: error populating phone availability cache: ';
213 foreach my $return (@return) {
214 my $phone_avail = new FS::phone_avail {
215 'exportnum' => $self->exportnum,
216 'countrycode' => '1', #don't hardcode me when gp goes int'l
217 'state' => $opt{'state'},
220 $error = $phone_avail->insert();
228 $dbh->rollback if $oldAutoCommit;
230 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
235 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
236 @return = sort { $a <=> $b } @return;
238 warn "WARNING: returning nothing for get_dids without known options"; #?
246 my( $self, $command, @args ) = @_;
248 eval "use Net::GlobalPOPs::MediaServicesAPI;";
251 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
252 'login' => $self->option('login'),
253 'password' => $self->option('password'),
257 $gp->$command(@args);
262 my( $self, $svc_phone ) = (shift, shift);
264 return '' if $self->option('dry_run');
266 #we want to provision and catch errors now, not queue
268 my $r = $self->gp_command('reserveDID',
269 'did' => $svc_phone->phonenum,
271 'endpointgroup' => $self->option('endpointgroup'),
274 my $rdid = $r->{did};
276 if ( $rdid->{'statuscode'} != 100 ) {
277 return "Error running VoIP Innovations reserveDID: ".
278 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
281 my $a = $self->gp_command('assignDID',
282 'did' => $svc_phone->phonenum,
283 'endpointgroup' => $self->option('endpointgroup'),
288 my $adid = $a->{did};
290 if ( $adid->{'statuscode'} != 100 ) {
291 return "Error running VoIP Innovations assignDID: ".
292 $adid->{'statuscode'}. ': '. $adid->{'status'};
298 sub _export_replace {
299 my( $self, $new, $old ) = (shift, shift, shift);
301 #hmm, what's to change?
306 my( $self, $svc_phone ) = (shift, shift);
308 return '' if $self->option('dry_run');
310 #probably okay to queue the deletion...?
311 #but hell, let's do it inline anyway, who wants phone numbers hanging around
313 my $r = $self->gp_command('releaseDID',
314 'did' => $svc_phone->phonenum,
317 my $rdid = $r->{did};
319 if ( $rdid->{'statuscode'} != 100 ) {
320 return "Error running VoIP Innovations releaseDID: ".
321 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
327 sub _export_suspend {
328 my( $self, $svc_phone ) = (shift, shift);
333 sub _export_unsuspend {
334 my( $self, $svc_phone ) = (shift, shift);
339 #hmm, might forgo queueing entirely for most things, data is too much of a pita
340 #sub globalpops_voip_queue {
341 # my( $self, $svcnum, $method ) = (shift, shift, shift);
342 # my $queue = new FS::queue {
343 # 'svcnum' => $svcnum,
344 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
347 # $self->option('login'),
348 # $self->option('password'),
354 sub globalpops_voip_command {
355 my($login, $password, $method, @args) = @_;
357 eval "use Net::GlobalPOPs::MediaServicesAPI;";
360 my $gp = new Net::GlobalPOPs
362 'password' => $password,
366 my $return = $gp->$method( @args );
369 #$return->{'statuscode'}
371 die $return->{'status'} if $return->{'statuscode'};