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);
67 my $search = $dids->{'search'};
69 if ( $search->{'statuscode'} == 302200 ) {
71 } elsif ( $search->{'statuscode'} != 100 ) {
72 die "Error running VoIP Innovations getDIDs: ".
73 $search->{'statuscode'}. ': '. $search->{'status'}; #die??
78 #my $latas = $search->{state}{lata};
80 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
81 %latas = map $search->{state}{lata}{$_},
84 %latas = %{ $search->{state}{lata} };
87 foreach my $lata ( keys %latas ) {
91 #my $l = $latas{$lata};
92 #$l = $l->{rate_center} if exists $l->{rate_center};
94 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
95 my $lata_search = $lata_dids->{'search'};
96 unless ( $lata_search->{'statuscode'} == 100 ) {
97 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
100 my $l = $lata_search->{state}{lata}{'rate_center'};
106 if ( grep $l->{$_}, qw(name friendlyname) ) {
107 %rate_center = map $l->{$_},
108 qw(name friendlyname);
113 foreach my $rate_center ( keys %rate_center ) {
115 #warn "rate center $rate_center";
117 my $rc = $rate_center{$rate_center};
118 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
121 if ( exists($rc->{npa}) ) {
124 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
130 if ( exists($r->{npa}{name}) ) {
133 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
136 foreach my $npa (@npa) {
138 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
142 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
144 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
148 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
154 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
157 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
159 if ( $npa->{nxx}{name} ) {
160 @nxx = ( $npa->{nxx}{name} );
162 @nxx = keys %{ $npa->{nxx} };
165 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
168 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
169 #my $ac = $npa->{name};
171 #warn Dumper($r) unless length($ac) == 3;
173 push @return, $npa->{name}
174 unless grep { $_ eq $npa->{name} } @return;
177 warn "WARNING: returning nothing for get_dids without known options"; #?
184 } #foreach my $rate_center
188 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
189 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
190 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
191 @return = sort { lc($a) cmp lc($b) } @return;
192 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
196 local $SIG{HUP} = 'IGNORE';
197 local $SIG{INT} = 'IGNORE';
198 local $SIG{QUIT} = 'IGNORE';
199 local $SIG{TERM} = 'IGNORE';
200 local $SIG{TSTP} = 'IGNORE';
201 local $SIG{PIPE} = 'IGNORE';
203 my $oldAutoCommit = $FS::UID::AutoCommit;
204 local $FS::UID::AutoCommit = 0;
207 my $errmsg = 'WARNING: error populating phone availability cache: ';
209 foreach my $return (@return) {
210 my $phone_avail = new FS::phone_avail {
211 'exportnum' => $self->exportnum,
212 'countrycode' => '1', #don't hardcode me when gp goes int'l
213 'state' => $opt{'state'},
216 $error = $phone_avail->insert();
224 $dbh->rollback if $oldAutoCommit;
226 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
231 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
232 @return = sort { $a <=> $b } @return;
234 warn "WARNING: returning nothing for get_dids without known options"; #?
242 my( $self, $command, @args ) = @_;
244 eval "use Net::GlobalPOPs::MediaServicesAPI;";
247 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
248 'login' => $self->option('login'),
249 'password' => $self->option('password'),
253 $gp->$command(@args);
258 my( $self, $svc_phone ) = (shift, shift);
260 return '' if $self->option('dry_run');
262 #we want to provision and catch errors now, not queue
264 my $r = $self->gp_command('reserveDID',
265 'did' => $svc_phone->phonenum,
267 'endpointgroup' => $self->option('endpointgroup'),
270 my $rdid = $r->{did};
272 if ( $rdid->{'statuscode'} != 100 ) {
273 return "Error running VoIP Innovations reserveDID: ".
274 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
277 my $a = $self->gp_command('assignDID',
278 'did' => $svc_phone->phonenum,
279 'endpointgroup' => $self->option('endpointgroup'),
284 my $adid = $a->{did};
286 if ( $adid->{'statuscode'} != 100 ) {
287 return "Error running VoIP Innovations assignDID: ".
288 $adid->{'statuscode'}. ': '. $adid->{'status'};
294 sub _export_replace {
295 my( $self, $new, $old ) = (shift, shift, shift);
297 #hmm, what's to change?
302 my( $self, $svc_phone ) = (shift, shift);
304 return '' if $self->option('dry_run');
306 #probably okay to queue the deletion...?
307 #but hell, let's do it inline anyway, who wants phone numbers hanging around
309 my $r = $self->gp_command('releaseDID',
310 'did' => $svc_phone->phonenum,
313 my $rdid = $r->{did};
315 if ( $rdid->{'statuscode'} != 100 ) {
316 return "Error running VoIP Innovations releaseDID: ".
317 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
323 sub _export_suspend {
324 my( $self, $svc_phone ) = (shift, shift);
329 sub _export_unsuspend {
330 my( $self, $svc_phone ) = (shift, shift);
335 #hmm, might forgo queueing entirely for most things, data is too much of a pita
336 #sub globalpops_voip_queue {
337 # my( $self, $svcnum, $method ) = (shift, shift, shift);
338 # my $queue = new FS::queue {
339 # 'svcnum' => $svcnum,
340 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
343 # $self->option('login'),
344 # $self->option('password'),
350 sub globalpops_voip_command {
351 my($login, $password, $method, @args) = @_;
353 eval "use Net::GlobalPOPs::MediaServicesAPI;";
356 my $gp = new Net::GlobalPOPs
358 'password' => $password,
362 my $return = $gp->$method( @args );
365 #$return->{'statuscode'}
367 die $return->{'status'} if $return->{'statuscode'};