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' },
15 'dry_run' => { label=>"Test mode - don't actually provision" },
20 'desc' => 'Provision phone numbers to 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 globalpop 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 globalpop 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) ? @$tn : ($tn);
147 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
155 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
157 if ( $npa->{nxx}{name} ) {
158 @nxx = ( $npa->{nxx}{name} );
160 @nxx = keys %{ $npa->{nxx} };
163 push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
166 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
167 #my $ac = $npa->{name};
169 #warn Dumper($r) unless length($ac) == 3;
171 push @return, $npa->{name}
172 unless grep { $_ eq $npa->{name} } @return;
175 warn "WARNING: returning nothing for get_dids without known options"; #?
182 } #foreach my $rate_center
186 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
187 @return = sort { $a cmp $b } @return; #string comparison actually dwiw
188 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
189 @return = sort { lc($a) cmp lc($b) } @return;
190 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
194 local $SIG{HUP} = 'IGNORE';
195 local $SIG{INT} = 'IGNORE';
196 local $SIG{QUIT} = 'IGNORE';
197 local $SIG{TERM} = 'IGNORE';
198 local $SIG{TSTP} = 'IGNORE';
199 local $SIG{PIPE} = 'IGNORE';
201 my $oldAutoCommit = $FS::UID::AutoCommit;
202 local $FS::UID::AutoCommit = 0;
205 my $errmsg = 'WARNING: error populating phone availability cache: ';
207 foreach my $return (@return) {
208 my $phone_avail = new FS::phone_avail {
209 'exportnum' => $self->exportnum,
210 'countrycode' => '1', #don't hardcode me when gp goes int'l
211 'state' => $opt{'state'},
214 $error = $phone_avail->insert();
222 $dbh->rollback if $oldAutoCommit;
224 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
229 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
230 @return = sort { $a <=> $b } @return;
232 warn "WARNING: returning nothing for get_dids without known options"; #?
240 my( $self, $command, @args ) = @_;
242 eval "use Net::GlobalPOPs::MediaServicesAPI;";
245 my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
246 'login' => $self->option('login'),
247 'password' => $self->option('password'),
251 $gp->$command(@args);
256 my( $self, $svc_phone ) = (shift, shift);
258 return '' if $self->option('dry_run');
260 #we want to provision and catch errors now, not queue
262 my $r = $self->gp_command('reserveDID',
263 'did' => $svc_phone->phonenum,
265 'endpointgroup' => $self->option('endpointgroup'),
268 my $rdid = $r->{did};
270 if ( $rdid->{'statuscode'} != 100 ) {
271 return "Error running globalpop reserveDID: ".
272 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
275 my $a = $self->gp_command('assignDID',
276 'did' => $svc_phone->phonenum,
277 'endpointgroup' => $self->option('endpointgroup'),
282 my $adid = $a->{did};
284 if ( $adid->{'statuscode'} != 100 ) {
285 return "Error running globalpop assignDID: ".
286 $adid->{'statuscode'}. ': '. $adid->{'status'};
292 sub _export_replace {
293 my( $self, $new, $old ) = (shift, shift, shift);
295 #hmm, what's to change?
300 my( $self, $svc_phone ) = (shift, shift);
302 return '' if $self->option('dry_run');
304 #probably okay to queue the deletion...?
305 #but hell, let's do it inline anyway, who wants phone numbers hanging around
307 my $r = $self->gp_command('releaseDID',
308 'did' => $svc_phone->phonenum,
311 my $rdid = $r->{did};
313 if ( $rdid->{'statuscode'} != 100 ) {
314 return "Error running globalpop releaseDID: ".
315 $rdid->{'statuscode'}. ': '. $rdid->{'status'};
321 sub _export_suspend {
322 my( $self, $svc_phone ) = (shift, shift);
327 sub _export_unsuspend {
328 my( $self, $svc_phone ) = (shift, shift);
333 #hmm, might forgo queueing entirely for most things, data is too much of a pita
334 #sub globalpops_voip_queue {
335 # my( $self, $svcnum, $method ) = (shift, shift, shift);
336 # my $queue = new FS::queue {
337 # 'svcnum' => $svcnum,
338 # 'job' => 'FS::part_export::globalpops_voip::globalpops_voip_command',
341 # $self->option('login'),
342 # $self->option('password'),
348 sub globalpops_voip_command {
349 my($login, $password, $method, @args) = @_;
351 eval "use Net::GlobalPOPs::MediaServicesAPI;";
354 my $gp = new Net::GlobalPOPs
356 'password' => $password,
360 my $return = $gp->$method( @args );
363 #$return->{'statuscode'}
365 die $return->{'status'} if $return->{'statuscode'};