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