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,
25 Requires installation of
26 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
31 sub rebless { shift; }
33 sub can_get_dids { 1; }
37 my %opt = ref($_[0]) ? %{$_[0]} : @_;
40 # 'orderby' => 'npa', #but it doesn't seem to work :/
42 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
43 %getdids = ( 'npa' => $opt{'areacode'},
44 'nxx' => $opt{'exchange'},
46 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
47 %getdids = ( 'npa' => $opt{'areacode'} );
48 } elsif ( $opt{'state'} ) {
51 'table' => 'phone_avail',
52 'hashref' => { 'exportnum' => $self->exportnum,
53 'countrycode' => '1', #don't hardcode me when gp goes int'l
54 'state' => $opt{'state'},
56 'order_by' => 'ORDER BY npa',
59 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
61 #otherwise, search for em
62 %getdids = ( 'state' => $opt{'state'} );
66 my $dids = $self->gp_command('getDIDs', %getdids);
68 if ( $dids->{'type'} eq 'Error' ) {
69 my $error = "Error running VoIP Innovations getDIDs: ".
70 $dids->{'statuscode'}. ': '. $dids->{'status'}. "\n";
75 my $search = $dids->{'search'};
77 if ( $search->{'statuscode'} == 302200 ) {
79 } elsif ( $search->{'statuscode'} != 100 ) {
81 my $error = "Error running VoIP Innovations getDIDs: ";
82 if ( $search->{'statuscode'} || $search->{'status'} ) {
83 $error .= $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
85 $error .= Dumper($search);
93 #my $latas = $search->{state}{lata};
95 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
96 %latas = map $search->{state}{lata}{$_},
99 %latas = %{ $search->{state}{lata} };
102 foreach my $lata ( keys %latas ) {
106 #my $l = $latas{$lata};
107 #$l = $l->{rate_center} if exists $l->{rate_center};
109 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
110 my $lata_search = $lata_dids->{'search'};
111 unless ( $lata_search->{'statuscode'} == 100 ) {
112 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
115 my $l = $lata_search->{state}{lata}{'rate_center'};
121 if ( grep $l->{$_}, qw(name friendlyname) ) {
122 %rate_center = map $l->{$_},
123 qw(name friendlyname);
128 foreach my $rate_center ( keys %rate_center ) {
130 #warn "rate center $rate_center";
132 my $rc = $rate_center{$rate_center};
133 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
136 if ( exists($rc->{npa}) ) {
139 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
145 if ( exists($r->{npa}{name}) ) {
148 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
151 foreach my $npa (@npa) {
153 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
157 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
159 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
163 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
169 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
172 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
174 if ( $npa->{nxx}{name} ) {
175 @nxx = ( $npa->{nxx}{name} );
177 @nxx = keys %{ $npa->{nxx} };
180 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
183 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
184 #my $ac = $npa->{name};
186 #warn Dumper($r) unless length($ac) == 3;
188 push @return, $npa->{name}
189 unless grep { $_ eq $npa->{name} } @return;
192 warn "WARNING: returning nothing for get_dids without known options"; #?
199 } #foreach my $rate_center
203 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
204 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
205 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
206 @return = sort { lc($a) cmp lc($b) } @return;
207 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
211 local $SIG{HUP} = 'IGNORE';
212 local $SIG{INT} = 'IGNORE';
213 local $SIG{QUIT} = 'IGNORE';
214 local $SIG{TERM} = 'IGNORE';
215 local $SIG{TSTP} = 'IGNORE';
216 local $SIG{PIPE} = 'IGNORE';
218 my $oldAutoCommit = $FS::UID::AutoCommit;
219 local $FS::UID::AutoCommit = 0;
222 my $errmsg = 'WARNING: error populating phone availability cache: ';
224 foreach my $return (@return) {
225 my $phone_avail = new FS::phone_avail {
226 'exportnum' => $self->exportnum,
227 'countrycode' => '1', #don't hardcode me when gp goes int'l
228 'state' => $opt{'state'},
231 $error = $phone_avail->insert();
239 $dbh->rollback if $oldAutoCommit;
241 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
246 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
247 @return = sort { $a <=> $b } @return;
249 warn "WARNING: returning nothing for get_dids without known options"; #?
257 my( $self, $command, @args ) = @_;
259 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
265 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
266 'login' => $self->option('login'),
267 'password' => $self->option('password'),
271 $gp->$command(@args);
276 my( $self, $svc_phone ) = (shift, shift);
278 return '' if $self->option('dry_run');
280 #we want to provision and catch errors now, not queue
282 my $r = $self->gp_command('reserveDID',
283 'did' => $svc_phone->phonenum,
285 'endpointgroup' => $self->option('endpointgroup'),
288 my $rdid = $r->{did};
290 if ( $rdid->{'statuscode'} != 100 ) {
291 return "Error running VoIP Innovations reserveDID: ".
292 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
295 my $a = $self->gp_command('assignDID',
296 'did' => $svc_phone->phonenum,
297 'endpointgroup' => $self->option('endpointgroup'),
302 my $adid = $a->{did};
304 if ( $adid->{'statuscode'} != 100 ) {
305 return "Error running VoIP Innovations assignDID: ".
306 $adid->{'statuscode'}. ': '. $adid->{'status'};
312 sub _export_replace {
313 my( $self, $new, $old ) = (shift, shift, shift);
315 #hmm, what's to change?
320 my( $self, $svc_phone ) = (shift, shift);
322 return '' if $self->option('dry_run');
324 #probably okay to queue the deletion...?
325 #but hell, let's do it inline anyway, who wants phone numbers hanging around
327 my $r = $self->gp_command('releaseDID',
328 'did' => $svc_phone->phonenum,
331 my $rdid = $r->{did};
333 if ( $rdid->{'statuscode'} != 100 ) {
334 return "Error running VoIP Innovations releaseDID: ".
335 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
341 sub _export_suspend {
342 my( $self, $svc_phone ) = (shift, shift);
347 sub _export_unsuspend {
348 my( $self, $svc_phone ) = (shift, shift);
353 #hmm, might forgo queueing entirely for most things, data is too much of a pita
354 #sub globalpops_voip_queue {
355 # my( $self, $svcnum, $method ) = (shift, shift, shift);
356 # my $queue = new FS::queue {
357 # 'svcnum' => $svcnum,
358 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
361 # $self->option('login'),
362 # $self->option('password'),
368 sub globalpops_voip_command {
369 my($login, $password, $method, @args) = @_;
371 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
374 my $gp = new Net::GlobalPOPs
376 'password' => $password,
380 my $return = $gp->$method( @args );
383 #$return->{'statuscode'}
385 die $return->{'status'} if $return->{'statuscode'};