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,
24 Requires installation of
25 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
30 sub rebless { shift; }
34 my %opt = ref($_[0]) ? %{$_[0]} : @_;
37 # 'orderby' => 'npa', #but it doesn't seem to work :/
39 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
40 %getdids = ( 'npa' => $opt{'areacode'},
41 'nxx' => $opt{'exchange'},
43 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
44 %getdids = ( 'npa' => $opt{'areacode'} );
45 } elsif ( $opt{'state'} ) {
48 'table' => 'phone_avail',
49 'hashref' => { 'exportnum' => $self->exportnum,
50 'countrycode' => '1', #don't hardcode me when gp goes int'l
51 'state' => $opt{'state'},
53 'order_by' => 'ORDER BY npa',
56 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
58 #otherwise, search for em
59 %getdids = ( 'state' => $opt{'state'} );
63 my $dids = $self->gp_command('getDIDs', %getdids);
65 if ( $dids->{'type'} eq 'Error' ) {
66 my $error = "Error running VoIP Innovations getDIDs: ".
67 $dids->{'statuscode'}. ': '. $dids->{'status'}. "\n";
72 my $search = $dids->{'search'};
74 if ( $search->{'statuscode'} == 302200 ) {
76 } elsif ( $search->{'statuscode'} != 100 ) {
78 my $error = "Error running VoIP Innovations getDIDs: ";
79 if ( $search->{'statuscode'} || $search->{'status'} ) {
80 $error .= $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
82 $error .= Dumper($search);
90 #my $latas = $search->{state}{lata};
92 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
93 %latas = map $search->{state}{lata}{$_},
96 %latas = %{ $search->{state}{lata} };
99 foreach my $lata ( keys %latas ) {
103 #my $l = $latas{$lata};
104 #$l = $l->{rate_center} if exists $l->{rate_center};
106 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
107 my $lata_search = $lata_dids->{'search'};
108 unless ( $lata_search->{'statuscode'} == 100 ) {
109 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
112 my $l = $lata_search->{state}{lata}{'rate_center'};
118 if ( grep $l->{$_}, qw(name friendlyname) ) {
119 %rate_center = map $l->{$_},
120 qw(name friendlyname);
125 foreach my $rate_center ( keys %rate_center ) {
127 #warn "rate center $rate_center";
129 my $rc = $rate_center{$rate_center};
130 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
133 if ( exists($rc->{npa}) ) {
136 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
142 if ( exists($r->{npa}{name}) ) {
145 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
148 foreach my $npa (@npa) {
150 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
154 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
156 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
160 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
166 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
169 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
171 if ( $npa->{nxx}{name} ) {
172 @nxx = ( $npa->{nxx}{name} );
174 @nxx = keys %{ $npa->{nxx} };
177 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
180 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
181 #my $ac = $npa->{name};
183 #warn Dumper($r) unless length($ac) == 3;
185 push @return, $npa->{name}
186 unless grep { $_ eq $npa->{name} } @return;
189 warn "WARNING: returning nothing for get_dids without known options"; #?
196 } #foreach my $rate_center
200 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
201 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
202 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
203 @return = sort { lc($a) cmp lc($b) } @return;
204 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
208 local $SIG{HUP} = 'IGNORE';
209 local $SIG{INT} = 'IGNORE';
210 local $SIG{QUIT} = 'IGNORE';
211 local $SIG{TERM} = 'IGNORE';
212 local $SIG{TSTP} = 'IGNORE';
213 local $SIG{PIPE} = 'IGNORE';
215 my $oldAutoCommit = $FS::UID::AutoCommit;
216 local $FS::UID::AutoCommit = 0;
219 my $errmsg = 'WARNING: error populating phone availability cache: ';
221 foreach my $return (@return) {
222 my $phone_avail = new FS::phone_avail {
223 'exportnum' => $self->exportnum,
224 'countrycode' => '1', #don't hardcode me when gp goes int'l
225 'state' => $opt{'state'},
228 $error = $phone_avail->insert();
236 $dbh->rollback if $oldAutoCommit;
238 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
243 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
244 @return = sort { $a <=> $b } @return;
246 warn "WARNING: returning nothing for get_dids without known options"; #?
254 my( $self, $command, @args ) = @_;
256 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
262 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
263 'login' => $self->option('login'),
264 'password' => $self->option('password'),
268 $gp->$command(@args);
273 my( $self, $svc_phone ) = (shift, shift);
275 return '' if $self->option('dry_run');
277 #we want to provision and catch errors now, not queue
279 my $r = $self->gp_command('reserveDID',
280 'did' => $svc_phone->phonenum,
282 'endpointgroup' => $self->option('endpointgroup'),
285 my $rdid = $r->{did};
287 if ( $rdid->{'statuscode'} != 100 ) {
288 return "Error running VoIP Innovations reserveDID: ".
289 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
292 my $a = $self->gp_command('assignDID',
293 'did' => $svc_phone->phonenum,
294 'endpointgroup' => $self->option('endpointgroup'),
299 my $adid = $a->{did};
301 if ( $adid->{'statuscode'} != 100 ) {
302 return "Error running VoIP Innovations assignDID: ".
303 $adid->{'statuscode'}. ': '. $adid->{'status'};
309 sub _export_replace {
310 my( $self, $new, $old ) = (shift, shift, shift);
312 #hmm, what's to change?
317 my( $self, $svc_phone ) = (shift, shift);
319 return '' if $self->option('dry_run');
321 #probably okay to queue the deletion...?
322 #but hell, let's do it inline anyway, who wants phone numbers hanging around
324 my $r = $self->gp_command('releaseDID',
325 'did' => $svc_phone->phonenum,
328 my $rdid = $r->{did};
330 if ( $rdid->{'statuscode'} != 100 ) {
331 return "Error running VoIP Innovations releaseDID: ".
332 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
338 sub _export_suspend {
339 my( $self, $svc_phone ) = (shift, shift);
344 sub _export_unsuspend {
345 my( $self, $svc_phone ) = (shift, shift);
350 #hmm, might forgo queueing entirely for most things, data is too much of a pita
351 #sub globalpops_voip_queue {
352 # my( $self, $svcnum, $method ) = (shift, shift, shift);
353 # my $queue = new FS::queue {
354 # 'svcnum' => $svcnum,
355 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
358 # $self->option('login'),
359 # $self->option('password'),
365 sub globalpops_voip_command {
366 my($login, $password, $method, @args) = @_;
368 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
371 my $gp = new Net::GlobalPOPs
373 'password' => $password,
377 my $return = $gp->$method( @args );
380 #$return->{'statuscode'}
382 die $return->{'status'} if $return->{'statuscode'};