import torrus 1.0.9
[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   'endpointgroup' => { label=>'GlobalPOPs endpoint group number' },
15   'dry_run'       => { label=>"Test mode - don't actually provision" },
16 ;
17
18 %info = (
19   'svc'     => 'svc_phone',
20   'desc'    => 'Provision phone numbers to GlobalPOPs VoIP',
21   'options' => \%options,
22   'notes'   => <<'END'
23 Requires installation of
24 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
25 from CPAN.
26 END
27 );
28
29 sub rebless { shift; }
30
31 sub get_dids {
32   my $self = shift;
33   my %opt = ref($_[0]) ? %{$_[0]} : @_;
34
35   my %getdids = ();
36   #  'orderby' => 'npa', #but it doesn't seem to work :/
37
38   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
39     %getdids = ( 'npa'   => $opt{'areacode'},
40                  'nxx'   => $opt{'exchange'},
41                );
42   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
43     %getdids = ( 'npa'   => $opt{'areacode'} );
44   } elsif ( $opt{'state'} ) {
45
46     my @avail = qsearch({
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'},
51                     },
52       'order_by' => 'ORDER BY npa',
53     });
54
55     return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
56
57     #otherwise, search for em
58     %getdids = ( 'state' => $opt{'state'} );
59
60   }
61
62   my $dids = $self->gp_command('getDIDs', %getdids);
63
64   #use Data::Dumper;
65   #warn Dumper($dids);
66
67   my $search = $dids->{'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   my %latas;
80   if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
81     %latas = map $search->{state}{lata}{$_},
82                  qw(name rate_center);
83   } else {
84     %latas = %{ $search->{state}{lata} };
85   } 
86
87   foreach my $lata ( keys %latas ) {
88
89     #warn "LATA $lata";
90     
91     #my $l = $latas{$lata};
92     #$l = $l->{rate_center} if exists $l->{rate_center};
93     
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??
98     }
99    
100     my $l = $lata_search->{state}{lata}{'rate_center'};
101
102     #use Data::Dumper;
103     #warn Dumper($l);
104
105     my %rate_center;
106     if ( grep $l->{$_}, qw(name friendlyname) ) {
107       %rate_center = map $l->{$_},
108                          qw(name friendlyname);
109     } else {
110       %rate_center = %$l;
111     } 
112
113     foreach my $rate_center ( keys %rate_center ) {
114       
115       #warn "rate center $rate_center";
116
117       my $rc = $rate_center{$rate_center}; 
118       $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
119
120       my @r = ();
121       if ( exists($rc->{npa}) ) {
122         @r = ($rc);
123       } else {
124         @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
125       }
126
127       foreach my $r (@r) {
128
129         my @npa = ();
130         if ( exists($r->{npa}{name}) ) {
131           @npa = ($r->{npa})
132         } else {
133           @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
134         }
135
136         foreach my $npa (@npa) {
137
138           if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
139
140             #warn Dumper($npa);
141
142             my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
143
144             my @tn = ref($tn) ? @$tn : ($tn);
145             #push @return, @tn;
146             push @return, map {
147                                 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
148                                   "$1-$2-$3";
149                                 } else {
150                                   $_;
151                                 }
152                               }
153                               @tn;
154
155           } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
156
157             if ( $npa->{nxx}{name} ) {
158               @nxx = ( $npa->{nxx}{name} );
159             } else {
160               @nxx = keys %{ $npa->{nxx} };
161             }
162
163             push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
164                               @nxx;
165
166           } elsif ( $opt{'state'} ) { #and not other things, then return areacode
167             #my $ac = $npa->{name};
168             #use Data::Dumper;
169             #warn Dumper($r) unless length($ac) == 3;
170
171             push @return, $npa->{name}
172               unless grep { $_ eq $npa->{name} } @return;
173
174           } else {
175             warn "WARNING: returning nothing for get_dids without known options"; #?
176           }
177
178         } #foreach my $npa
179
180       } #foreach my $r
181
182     } #foreach my $rate_center
183
184   } #foreach my $lata
185
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
191
192     #populate cache
193
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';
200
201     my $oldAutoCommit = $FS::UID::AutoCommit;
202     local $FS::UID::AutoCommit = 0;
203     my $dbh = dbh;
204
205     my $errmsg = 'WARNING: error populating phone availability cache: ';
206     my $error = '';
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'},
212         'npa'         => $return,
213       };
214       $error = $phone_avail->insert();
215       if ( $error ) {
216         warn $errmsg.$error;
217         last;
218       }
219     }
220
221     if ( $error ) {
222       $dbh->rollback if $oldAutoCommit;
223     } else {
224       $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
225     }
226
227     #end populate cache
228
229     #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
230     @return = sort { $a <=> $b } @return;
231   } else {
232     warn "WARNING: returning nothing for get_dids without known options"; #?
233   }
234
235   \@return;
236
237 }
238
239 sub gp_command {
240   my( $self, $command, @args ) = @_;
241
242   eval "use Net::GlobalPOPs::MediaServicesAPI;";
243   die $@ if $@;
244
245   my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
246     'login'    => $self->option('login'),
247     'password' => $self->option('password'),
248     #'debug'    => $debug,
249   );
250
251   $gp->$command(@args);
252 }
253
254
255 sub _export_insert {
256   my( $self, $svc_phone ) = (shift, shift);
257
258   return '' if $self->option('dry_run');
259
260   #we want to provision and catch errors now, not queue
261
262   my $r = $self->gp_command('reserveDID',
263     'did'           => $svc_phone->phonenum,
264     'minutes'       => 1,
265     'endpointgroup' => $self->option('endpointgroup'),
266   );
267
268   my $rdid = $r->{did};
269
270   if ( $rdid->{'statuscode'} != 100 ) {
271     return "Error running globalpop reserveDID: ".
272            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
273   }
274
275   my $a = $self->gp_command('assignDID',
276     'did'           => $svc_phone->phonenum,
277     'endpointgroup' => $self->option('endpointgroup'),
278     #'rewrite'
279     #'cnam'
280   );
281
282   my $adid = $a->{did};
283
284   if ( $adid->{'statuscode'} != 100 ) {
285     return "Error running globalpop assignDID: ".
286            $adid->{'statuscode'}. ': '. $adid->{'status'};
287   }
288
289   '';
290 }
291
292 sub _export_replace {
293   my( $self, $new, $old ) = (shift, shift, shift);
294
295   #hmm, what's to change?
296   '';
297 }
298
299 sub _export_delete {
300   my( $self, $svc_phone ) = (shift, shift);
301
302   return '' if $self->option('dry_run');
303
304   #probably okay to queue the deletion...?
305   #but hell, let's do it inline anyway, who wants phone numbers hanging around
306
307   my $r = $self->gp_command('releaseDID',
308     'did'           => $svc_phone->phonenum,
309   );
310
311   my $rdid = $r->{did};
312
313   if ( $rdid->{'statuscode'} != 100 ) {
314     return "Error running globalpop releaseDID: ".
315            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
316   }
317
318   '';
319 }
320
321 sub _export_suspend {
322   my( $self, $svc_phone ) = (shift, shift);
323   #nop for now
324   '';
325 }
326
327 sub _export_unsuspend {
328   my( $self, $svc_phone ) = (shift, shift);
329   #nop for now
330   '';
331 }
332
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',
339 #  };
340 #  $queue->insert(
341 #    $self->option('login'),
342 #    $self->option('password'),
343 #    $method,
344 #    @_,
345 #  );
346 #}
347
348 sub globalpops_voip_command {
349   my($login, $password, $method, @args) = @_;
350
351   eval "use Net::GlobalPOPs::MediaServicesAPI;";
352   die $@ if $@;
353
354   my $gp = new Net::GlobalPOPs
355                  'login'    => $login,
356                  'password' => $password,
357                  #'debug'    => 1,
358                ;
359
360   my $return = $gp->$method( @args );
361
362   #$return->{'status'} 
363   #$return->{'statuscode'} 
364
365   die $return->{'status'} if $return->{'statuscode'};
366
367 }
368
369 1;
370