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,
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 ) {
77 my $error = "Error running VoIP Innovations getDIDs: ".
78 $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
85 #my $latas = $search->{state}{lata};
87 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
88 %latas = map $search->{state}{lata}{$_},
91 %latas = %{ $search->{state}{lata} };
94 foreach my $lata ( keys %latas ) {
98 #my $l = $latas{$lata};
99 #$l = $l->{rate_center} if exists $l->{rate_center};
101 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
102 my $lata_search = $lata_dids->{'search'};
103 unless ( $lata_search->{'statuscode'} == 100 ) {
104 die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
107 my $l = $lata_search->{state}{lata}{'rate_center'};
113 if ( grep $l->{$_}, qw(name friendlyname) ) {
114 %rate_center = map $l->{$_},
115 qw(name friendlyname);
120 foreach my $rate_center ( keys %rate_center ) {
122 #warn "rate center $rate_center";
124 my $rc = $rate_center{$rate_center};
125 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
128 if ( exists($rc->{npa}) ) {
131 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
137 if ( exists($r->{npa}{name}) ) {
140 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
143 foreach my $npa (@npa) {
145 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
149 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
151 my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
155 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
161 map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
164 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
166 if ( $npa->{nxx}{name} ) {
167 @nxx = ( $npa->{nxx}{name} );
169 @nxx = keys %{ $npa->{nxx} };
172 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
175 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
176 #my $ac = $npa->{name};
178 #warn Dumper($r) unless length($ac) == 3;
180 push @return, $npa->{name}
181 unless grep { $_ eq $npa->{name} } @return;
184 warn "WARNING: returning nothing for get_dids without known options"; #?
191 } #foreach my $rate_center
195 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
196 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
197 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
198 @return = sort { lc($a) cmp lc($b) } @return;
199 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
203 local $SIG{HUP} = 'IGNORE';
204 local $SIG{INT} = 'IGNORE';
205 local $SIG{QUIT} = 'IGNORE';
206 local $SIG{TERM} = 'IGNORE';
207 local $SIG{TSTP} = 'IGNORE';
208 local $SIG{PIPE} = 'IGNORE';
210 my $oldAutoCommit = $FS::UID::AutoCommit;
211 local $FS::UID::AutoCommit = 0;
214 my $errmsg = 'WARNING: error populating phone availability cache: ';
216 foreach my $return (@return) {
217 my $phone_avail = new FS::phone_avail {
218 'exportnum' => $self->exportnum,
219 'countrycode' => '1', #don't hardcode me when gp goes int'l
220 'state' => $opt{'state'},
223 $error = $phone_avail->insert();
231 $dbh->rollback if $oldAutoCommit;
233 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
238 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
239 @return = sort { $a <=> $b } @return;
241 warn "WARNING: returning nothing for get_dids without known options"; #?
249 my( $self, $command, @args ) = @_;
251 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
257 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
258 'login' => $self->option('login'),
259 'password' => $self->option('password'),
263 $gp->$command(@args);
268 my( $self, $svc_phone ) = (shift, shift);
270 return '' if $self->option('dry_run');
272 #we want to provision and catch errors now, not queue
274 my $r = $self->gp_command('reserveDID',
275 'did' => $svc_phone->phonenum,
277 'endpointgroup' => $self->option('endpointgroup'),
280 my $rdid = $r->{did};
282 if ( $rdid->{'statuscode'} != 100 ) {
283 return "Error running VoIP Innovations reserveDID: ".
284 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
287 my $a = $self->gp_command('assignDID',
288 'did' => $svc_phone->phonenum,
289 'endpointgroup' => $self->option('endpointgroup'),
294 my $adid = $a->{did};
296 if ( $adid->{'statuscode'} != 100 ) {
297 return "Error running VoIP Innovations assignDID: ".
298 $adid->{'statuscode'}. ': '. $adid->{'status'};
304 sub _export_replace {
305 my( $self, $new, $old ) = (shift, shift, shift);
307 #hmm, what's to change?
312 my( $self, $svc_phone ) = (shift, shift);
314 return '' if $self->option('dry_run');
316 #probably okay to queue the deletion...?
317 #but hell, let's do it inline anyway, who wants phone numbers hanging around
319 my $r = $self->gp_command('releaseDID',
320 'did' => $svc_phone->phonenum,
323 my $rdid = $r->{did};
325 if ( $rdid->{'statuscode'} != 100 ) {
326 return "Error running VoIP Innovations releaseDID: ".
327 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
333 sub _export_suspend {
334 my( $self, $svc_phone ) = (shift, shift);
339 sub _export_unsuspend {
340 my( $self, $svc_phone ) = (shift, shift);
345 #hmm, might forgo queueing entirely for most things, data is too much of a pita
346 #sub globalpops_voip_queue {
347 # my( $self, $svcnum, $method ) = (shift, shift, shift);
348 # my $queue = new FS::queue {
349 # 'svcnum' => $svcnum,
350 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
353 # $self->option('login'),
354 # $self->option('password'),
360 sub globalpops_voip_command {
361 my($login, $password, $method, @args) = @_;
363 eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
366 my $gp = new Net::GlobalPOPs
368 'password' => $password,
372 my $return = $gp->$method( @args );
375 #$return->{'statuscode'}
377 die $return->{'status'} if $return->{'statuscode'};