get DIDs from globalpops
[freeside.git] / FS / FS / part_export / globalpops_voip.pm
1 package FS::part_export::globalpops_voip;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 tie my %options, 'Tie::IxHash',
10   'login'    => { label=>'GlobalPOPs Media Services API login' },
11   'password' => { label=>'GlobalPOPs Media Services API password' },
12 ;
13
14 %info = (
15   'svc'     => 'svc_phone',
16   'desc'    => 'Provision phone numbers to GlobalPOPs VoIP',
17   'options' => \%options,
18   'notes'   => <<'END'
19 Requires installation of
20 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
21 from CPAN.
22 END
23 );
24
25 sub rebless { shift; }
26
27 sub get_dids {
28   my $self = shift;
29   my %opt = ref($_[0]) ? %{$_[0]} : @_;
30
31   my %search = ();
32   #  'orderby' => 'npa', #but it doesn't seem to work :/
33
34   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
35     %getdids = ( 'npa'   => $opt{'areacode'},
36                  'nxx'   => $opt{'exchange'},
37                );
38   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
39     %getdids = ( 'npa'   => $opt{'areacode'} );
40   } elsif ( $opt{'state'} ) {
41     %getdids = ( 'state' => $opt{'state'} );
42   }
43
44   my $dids = $self->gp_command('getDIDs', %getdids);
45
46   #use Data::Dumper;
47   #warn Dumper($dids);
48
49   my $search = $dids->{'search'};
50
51   #warn Dumper($search);
52
53   if ( $search->{'statuscode'} == 302200 ) {
54     return [];
55   } elsif ( $search->{'statuscode'} != 100 ) {
56     die "Error running globalpop getDIDs: ".
57         $search->{'statuscode'}. ': '. $search->{'status'}; #die??
58   }
59
60   my @return = ();
61
62   #my $latas = $search->{state}{lata};
63
64   my %latas;
65   if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
66     %latas = map $search->{state}{lata}{$_},
67                  qw(name rate_center);
68   } else {
69     %latas = %{ $search->{state}{lata} };
70   } 
71
72   foreach my $lata ( keys %latas ) {
73
74     #warn "LATA $lata";
75     
76     #my $l = $latas{$lata};
77     #$l = $l->{rate_center} if exists $l->{rate_center};
78     
79     my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
80     my $lata_search = $lata_dids->{'search'};
81     unless ( $lata_search->{'statuscode'} == 100 ) {
82       die "Error running globalpop getDIDs: ". $lata_search->{'status'}; #die??
83     }
84    
85     my $l = $lata_search->{state}{lata}{'rate_center'};
86
87     #use Data::Dumper;
88     #warn Dumper($l);
89
90     my %rate_center;
91     if ( grep $l->{$_}, qw(name friendlyname) ) {
92       %rate_center = map $l->{$_},
93                          qw(name friendlyname);
94     } else {
95       %rate_center = %$l;
96     } 
97
98     foreach my $rate_center ( keys %rate_center ) {
99       
100       #warn "rate center $rate_center";
101
102       my $rc = $rate_center{$rate_center}; 
103       $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
104
105       my @r = ();
106       if ( exists($rc->{npa}) ) {
107         @r = ($rc);
108       } else {
109         @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
110       }
111
112       foreach my $r (@r) {
113
114         my @npa = ();
115         if ( exists($r->{npa}{name}) ) {
116           @npa = ($r->{npa})
117         } else {
118           @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
119         }
120
121         foreach my $npa (@npa) {
122
123           if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
124
125             #warn Dumper($npa);
126
127             my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
128
129             my @tn = ref($tn) ? @$tn : ($tn);
130             #push @return, @tn;
131             push @return, map {
132                                 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
133                                   "$1-$2-$3";
134                                 } else {
135                                   $_;
136                                 }
137                               }
138                               @tn;
139
140           } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
141
142             if ( $npa->{nxx}{name} ) {
143               @nxx = ( $npa->{nxx}{name} );
144             } else {
145               @nxx = keys %{ $npa->{nxx} };
146             }
147
148             push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
149                               @nxx;
150
151           } elsif ( $opt{'state'} ) { #and not other things, then return areacode
152             #my $ac = $npa->{name};
153             #use Data::Dumper;
154             #warn Dumper($r) unless length($ac) == 3;
155
156             push @return, $npa->{name}
157               unless grep { $_ eq $npa->{name} } @return;
158
159           } else {
160             warn "WARNING: returning nothing for get_dids without known options"; #?
161           }
162
163         } #foreach my $npa
164
165       } #foreach my $r
166
167     } #foreach my $rate_center
168
169   } #foreach my $lata
170
171   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
172     @return = sort { $a cmp $b } @return; #string comparison actually dwiw
173   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
174     @return = sort { lc($a) cmp lc($b) } @return;
175   } elsif ( $opt{'state'} ) { #and not other things, then return areacode
176     #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
177     @return = sort { $a <=> $b } @return;
178   } else {
179     warn "WARNING: returning nothing for get_dids without known options"; #?
180   }
181
182   \@return;
183
184 }
185
186 sub gp_command {
187   my( $self, $command, @args ) = @_;
188
189   eval "use Net::GlobalPOPs::MediaServicesAPI;";
190   die $@ if $@;
191
192   my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
193     'login'    => $self->option('login'),
194     'password' => $self->option('password'),
195     #'debug'    => $debug,
196   );
197
198   $gp->$command(@args);
199 }
200
201
202 sub _export_insert {
203   my( $self, $svc_phone ) = (shift, shift);
204   #we want to provision and catch errors now, not queue
205 }
206
207 sub _export_replace {
208   my( $self, $new, $old ) = (shift, shift, shift);
209   #hmm, what's to change?
210 }
211
212 sub _export_delete {
213   my( $self, $svc_phone ) = (shift, shift);
214   #probably okay to queue the deletion...
215 }
216
217 sub _export_suspend {
218   my( $self, $svc_phone ) = (shift, shift);
219   #nop for now
220 }
221
222 sub _export_unsuspend {
223   my( $self, $svc_phone ) = (shift, shift);
224   #nop for now
225 }
226
227 #hmm, might forgo queueing entirely for most things, data is too much of a pita
228
229 sub globalpops_voip_queue {
230   my( $self, $svcnum, $method ) = (shift, shift, shift);
231   my $queue = new FS::queue {
232     'svcnum' => $svcnum,
233     'job'    => 'FS::part_export::globalpops_voip::globalpops_voip_command',
234   };
235   $queue->insert(
236     $self->option('login'),
237     $self->option('password'),
238     $method,
239     @_,
240   );
241 }
242
243 sub globalpops_voip_command {
244   my($login, $password, $method, @args) = @_;
245
246   eval "use Net::GlobalPOPs::MediaServicesAPI;";
247   die $@ if $@;
248
249   my $gp = new Net::GlobalPOPs
250                  'login'    => $login,
251                  'password' => $password,
252                  #'debug'    => 1,
253                ;
254
255   my $return = $gp->$method( @args );
256
257   #$return->{'status'} 
258   #$return->{'statuscode'} 
259
260   die $return->{'status'} if $return->{'statuscode'};
261
262 }
263
264 1;
265