1 package FS::part_export::globalpops_voip;
3 use vars qw(@ISA %info);
7 @ISA = qw(FS::part_export);
9 tie my %options, 'Tie::IxHash',
10 'login' => { label=>'GlobalPOPs Media Services API login' },
11 'password' => { label=>'GlobalPOPs Media Services API password' },
16 'desc' => 'Provision phone numbers to GlobalPOPs VoIP',
17 'options' => \%options,
19 Requires installation of
20 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
25 sub rebless { shift; }
29 my %opt = ref($_[0]) ? %{$_[0]} : @_;
32 # 'orderby' => 'npa', #but it doesn't seem to work :/
34 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
35 %getdids = ( 'npa' => $opt{'areacode'},
36 'nxx' => $opt{'exchange'},
38 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
39 %getdids = ( 'npa' => $opt{'areacode'} );
40 } elsif ( $opt{'state'} ) {
41 %getdids = ( 'state' => $opt{'state'} );
44 my $dids = $self->gp_command('getDIDs', %getdids);
49 my $search = $dids->{'search'};
51 #warn Dumper($search);
53 if ( $search->{'statuscode'} == 302200 ) {
55 } elsif ( $search->{'statuscode'} != 100 ) {
56 die "Error running globalpop getDIDs: ".
57 $search->{'statuscode'}. ': '. $search->{'status'}; #die??
62 #my $latas = $search->{state}{lata};
65 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
66 %latas = map $search->{state}{lata}{$_},
69 %latas = %{ $search->{state}{lata} };
72 foreach my $lata ( keys %latas ) {
76 #my $l = $latas{$lata};
77 #$l = $l->{rate_center} if exists $l->{rate_center};
79 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
80 my $lata_search = $lata_dids->{'search'};
81 unless ( $lata_search->{'statuscode'} == 100 ) {
82 die "Error running globalpop getDIDs: ". $lata_search->{'status'}; #die??
85 my $l = $lata_search->{state}{lata}{'rate_center'};
91 if ( grep $l->{$_}, qw(name friendlyname) ) {
92 %rate_center = map $l->{$_},
93 qw(name friendlyname);
98 foreach my $rate_center ( keys %rate_center ) {
100 #warn "rate center $rate_center";
102 my $rc = $rate_center{$rate_center};
103 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
106 if ( exists($rc->{npa}) ) {
109 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
115 if ( exists($r->{npa}{name}) ) {
118 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
121 foreach my $npa (@npa) {
123 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
127 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
129 my @tn = ref($tn) ? @$tn : ($tn);
132 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
140 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
142 if ( $npa->{nxx}{name} ) {
143 @nxx = ( $npa->{nxx}{name} );
145 @nxx = keys %{ $npa->{nxx} };
148 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
151 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
152 #my $ac = $npa->{name};
154 #warn Dumper($r) unless length($ac) == 3;
156 push @return, $npa->{name}
157 unless grep { $_ eq $npa->{name} } @return;
160 warn "WARNING: returning nothing for get_dids without known options"; #?
167 } #foreach my $rate_center
171 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
172 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
173 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
174 @return = sort { lc($a) cmp lc($b) } @return;
175 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
176 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
177 @return = sort { $a <=> $b } @return;
179 warn "WARNING: returning nothing for get_dids without known options"; #?
187 my( $self, $command, @args ) = @_;
189 eval "use Net::GlobalPOPs::MediaServicesAPI;";
192 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
193 'login' => $self->option('login'),
194 'password' => $self->option('password'),
198 $gp->$command(@args);
203 my( $self, $svc_phone ) = (shift, shift);
204 #we want to provision and catch errors now, not queue
207 sub _export_replace {
208 my( $self, $new, $old ) = (shift, shift, shift);
209 #hmm, what's to change?
213 my( $self, $svc_phone ) = (shift, shift);
214 #probably okay to queue the deletion...
217 sub _export_suspend {
218 my( $self, $svc_phone ) = (shift, shift);
222 sub _export_unsuspend {
223 my( $self, $svc_phone ) = (shift, shift);
227 #hmm, might forgo queueing entirely for most things, data is too much of a pita
229 sub globalpops_voip_queue {
230 my( $self, $svcnum, $method ) = (shift, shift, shift);
231 my $queue = new FS::queue {
233 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
236 $self->option('login'),
237 $self->option('password'),
243 sub globalpops_voip_command {
244 my($login, $password, $method, @args) = @_;
246 eval "use Net::GlobalPOPs::MediaServicesAPI;";
249 my $gp = new Net::GlobalPOPs
251 'password' => $password,
255 my $return = $gp->$method( @args );
258 #$return->{'statuscode'}
260 die $return->{'status'} if $return->{'statuscode'};