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=>'GlobalPOPs Media Services API login' },
13 'password' => { label=>'GlobalPOPs Media Services API password' },
14 'endpointgroup' => { label=>'GlobalPOPs endpoint group number' },
19 'desc' => 'Provision phone numbers to GlobalPOPs VoIP',
20 'options' => \%options,
22 Requires installation of
23 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
28 sub rebless { shift; }
32 my %opt = ref($_[0]) ? %{$_[0]} : @_;
35 # 'orderby' => 'npa', #but it doesn't seem to work :/
37 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
38 %getdids = ( 'npa' => $opt{'areacode'},
39 'nxx' => $opt{'exchange'},
41 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
42 %getdids = ( 'npa' => $opt{'areacode'} );
43 } elsif ( $opt{'state'} ) {
46 'table' => 'phone_avail',
47 'hashref' => { 'exportnum' => $self->exportnum,
48 'countrycode' => '1', #don't hardcode me when gp goes int'l
49 'state' => $opt{'state'},
51 'order_by' => 'ORDER BY npa',
54 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
56 #otherwise, search for em
57 %getdids = ( 'state' => $opt{'state'} );
61 my $dids = $self->gp_command('getDIDs', %getdids);
66 my $search = $dids->{'search'};
68 if ( $search->{'statuscode'} == 302200 ) {
70 } elsif ( $search->{'statuscode'} != 100 ) {
71 die "Error running globalpop getDIDs: ".
72 $search->{'statuscode'}. ': '. $search->{'status'}; #die??
77 #my $latas = $search->{state}{lata};
79 if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
80 %latas = map $search->{state}{lata}{$_},
83 %latas = %{ $search->{state}{lata} };
86 foreach my $lata ( keys %latas ) {
90 #my $l = $latas{$lata};
91 #$l = $l->{rate_center} if exists $l->{rate_center};
93 my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
94 my $lata_search = $lata_dids->{'search'};
95 unless ( $lata_search->{'statuscode'} == 100 ) {
96 die "Error running globalpop getDIDs: ". $lata_search->{'status'}; #die??
99 my $l = $lata_search->{state}{lata}{'rate_center'};
105 if ( grep $l->{$_}, qw(name friendlyname) ) {
106 %rate_center = map $l->{$_},
107 qw(name friendlyname);
112 foreach my $rate_center ( keys %rate_center ) {
114 #warn "rate center $rate_center";
116 my $rc = $rate_center{$rate_center};
117 $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
120 if ( exists($rc->{npa}) ) {
123 @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
129 if ( exists($r->{npa}{name}) ) {
132 @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
135 foreach my $npa (@npa) {
137 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
141 my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
143 my @tn = ref($tn) ? @$tn : ($tn);
146 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
154 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
156 if ( $npa->{nxx}{name} ) {
157 @nxx = ( $npa->{nxx}{name} );
159 @nxx = keys %{ $npa->{nxx} };
162 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
165 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
166 #my $ac = $npa->{name};
168 #warn Dumper($r) unless length($ac) == 3;
170 push @return, $npa->{name}
171 unless grep { $_ eq $npa->{name} } @return;
174 warn "WARNING: returning nothing for get_dids without known options"; #?
181 } #foreach my $rate_center
185 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
186 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
187 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
188 @return = sort { lc($a) cmp lc($b) } @return;
189 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
193 local $SIG{HUP} = 'IGNORE';
194 local $SIG{INT} = 'IGNORE';
195 local $SIG{QUIT} = 'IGNORE';
196 local $SIG{TERM} = 'IGNORE';
197 local $SIG{TSTP} = 'IGNORE';
198 local $SIG{PIPE} = 'IGNORE';
200 my $oldAutoCommit = $FS::UID::AutoCommit;
201 local $FS::UID::AutoCommit = 0;
204 my $errmsg = 'WARNING: error populating phone availability cache: ';
206 foreach my $return (@return) {
207 my $phone_avail = new FS::phone_avail {
208 'exportnum' => $self->exportnum,
209 'countrycode' => '1', #don't hardcode me when gp goes int'l
210 'state' => $opt{'state'},
213 $error = $phone_avail->insert();
221 $dbh->rollback if $oldAutoCommit;
223 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
228 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
229 @return = sort { $a <=> $b } @return;
231 warn "WARNING: returning nothing for get_dids without known options"; #?
239 my( $self, $command, @args ) = @_;
241 eval "use Net::GlobalPOPs::MediaServicesAPI;";
244 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
245 'login' => $self->option('login'),
246 'password' => $self->option('password'),
250 $gp->$command(@args);
255 my( $self, $svc_phone ) = (shift, shift);
256 #we want to provision and catch errors now, not queue
258 my $r = $self->gp_command('reserveDID',
259 'did' => $svc_phone->phonenum,
261 'endpointgroup' => $self->option('endpointgroup'),
264 my $rdid = $r->{did};
266 if ( $rdid->{'statuscode'} != 100 ) {
267 return "Error running globalpop reserveDID: ".
268 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
271 my $a = $self->gp_command('assignDID',
272 'did' => $svc_phone->phonenum,
273 'endpointgroup' => $self->option('endpointgroup'),
278 my $adid = $a->{did};
280 if ( $adid->{'statuscode'} != 100 ) {
281 return "Error running globalpop assignDID: ".
282 $adid->{'statuscode'}. ': '. $adid->{'status'};
288 sub _export_replace {
289 my( $self, $new, $old ) = (shift, shift, shift);
291 #hmm, what's to change?
296 my( $self, $svc_phone ) = (shift, shift);
298 #probably okay to queue the deletion...?
299 #but hell, let's do it inline anyway, who wants phone numbers hanging around
301 my $r = $self->gp_command('releaseDID',
302 'did' => $svc_phone->phonenum,
305 my $rdid = $r->{did};
307 if ( $rdid->{'statuscode'} != 100 ) {
308 return "Error running globalpop releaseDID: ".
309 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
315 sub _export_suspend {
316 my( $self, $svc_phone ) = (shift, shift);
321 sub _export_unsuspend {
322 my( $self, $svc_phone ) = (shift, shift);
327 #hmm, might forgo queueing entirely for most things, data is too much of a pita
329 sub globalpops_voip_queue {
330 my( $self, $svcnum, $method ) = (shift, shift, shift);
331 my $queue = new FS::queue {
333 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
336 $self->option('login'),
337 $self->option('password'),
343 sub globalpops_voip_command {
344 my($login, $password, $method, @args) = @_;
346 eval "use Net::GlobalPOPs::MediaServicesAPI;";
349 my $gp = new Net::GlobalPOPs
351 'password' => $password,
355 my $return = $gp->$method( @args );
358 #$return->{'statuscode'}
360 die $return->{'status'} if $return->{'statuscode'};