This commit was generated by cvs2svn to compensate for changes in r11022,
[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=>'VoIP Innovations API login' },
13   'password'      => { label=>'VoIP Innovations API password' },
14   'endpointgroup' => { label=>'VoIP Innovations 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 VoIP Innovations (formerly 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 VoIP Innovations 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 VoIP Innovations 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) eq 'ARRAY' ? @$tn : ($tn);
145             #push @return, @tn;
146             push @return,
147               map {
148                     if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
149                       "$1-$2-$3";
150                     } else {
151                       $_;
152                     }
153                   }
154                map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
155                @tn;
156
157           } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
158
159             if ( $npa->{nxx}{name} ) {
160               @nxx = ( $npa->{nxx}{name} );
161             } else {
162               @nxx = keys %{ $npa->{nxx} };
163             }
164
165             push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
166                               @nxx;
167
168           } elsif ( $opt{'state'} ) { #and not other things, then return areacode
169             #my $ac = $npa->{name};
170             #use Data::Dumper;
171             #warn Dumper($r) unless length($ac) == 3;
172
173             push @return, $npa->{name}
174               unless grep { $_ eq $npa->{name} } @return;
175
176           } else {
177             warn "WARNING: returning nothing for get_dids without known options"; #?
178           }
179
180         } #foreach my $npa
181
182       } #foreach my $r
183
184     } #foreach my $rate_center
185
186   } #foreach my $lata
187
188   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
189     @return = sort { $a cmp $b } @return; #string comparison actually dwiw
190   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
191     @return = sort { lc($a) cmp lc($b) } @return;
192   } elsif ( $opt{'state'} ) { #and not other things, then return areacode
193
194     #populate cache
195
196     local $SIG{HUP} = 'IGNORE';
197     local $SIG{INT} = 'IGNORE';
198     local $SIG{QUIT} = 'IGNORE';
199     local $SIG{TERM} = 'IGNORE';
200     local $SIG{TSTP} = 'IGNORE';
201     local $SIG{PIPE} = 'IGNORE';
202
203     my $oldAutoCommit = $FS::UID::AutoCommit;
204     local $FS::UID::AutoCommit = 0;
205     my $dbh = dbh;
206
207     my $errmsg = 'WARNING: error populating phone availability cache: ';
208     my $error = '';
209     foreach my $return (@return) {
210       my $phone_avail = new FS::phone_avail {
211         'exportnum'   => $self->exportnum,
212         'countrycode' => '1', #don't hardcode me when gp goes int'l
213         'state'       => $opt{'state'},
214         'npa'         => $return,
215       };
216       $error = $phone_avail->insert();
217       if ( $error ) {
218         warn $errmsg.$error;
219         last;
220       }
221     }
222
223     if ( $error ) {
224       $dbh->rollback if $oldAutoCommit;
225     } else {
226       $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
227     }
228
229     #end populate cache
230
231     #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
232     @return = sort { $a <=> $b } @return;
233   } else {
234     warn "WARNING: returning nothing for get_dids without known options"; #?
235   }
236
237   \@return;
238
239 }
240
241 sub gp_command {
242   my( $self, $command, @args ) = @_;
243
244   eval "use Net::GlobalPOPs::MediaServicesAPI;";
245   die $@ if $@;
246
247   my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
248     'login'    => $self->option('login'),
249     'password' => $self->option('password'),
250     #'debug'    => $debug,
251   );
252
253   $gp->$command(@args);
254 }
255
256
257 sub _export_insert {
258   my( $self, $svc_phone ) = (shift, shift);
259
260   return '' if $self->option('dry_run');
261
262   #we want to provision and catch errors now, not queue
263
264   my $r = $self->gp_command('reserveDID',
265     'did'           => $svc_phone->phonenum,
266     'minutes'       => 1,
267     'endpointgroup' => $self->option('endpointgroup'),
268   );
269
270   my $rdid = $r->{did};
271
272   if ( $rdid->{'statuscode'} != 100 ) {
273     return "Error running VoIP Innovations reserveDID: ".
274            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
275   }
276
277   my $a = $self->gp_command('assignDID',
278     'did'           => $svc_phone->phonenum,
279     'endpointgroup' => $self->option('endpointgroup'),
280     #'rewrite'
281     #'cnam'
282   );
283
284   my $adid = $a->{did};
285
286   if ( $adid->{'statuscode'} != 100 ) {
287     return "Error running VoIP Innovations assignDID: ".
288            $adid->{'statuscode'}. ': '. $adid->{'status'};
289   }
290
291   '';
292 }
293
294 sub _export_replace {
295   my( $self, $new, $old ) = (shift, shift, shift);
296
297   #hmm, what's to change?
298   '';
299 }
300
301 sub _export_delete {
302   my( $self, $svc_phone ) = (shift, shift);
303
304   return '' if $self->option('dry_run');
305
306   #probably okay to queue the deletion...?
307   #but hell, let's do it inline anyway, who wants phone numbers hanging around
308
309   my $r = $self->gp_command('releaseDID',
310     'did'           => $svc_phone->phonenum,
311   );
312
313   my $rdid = $r->{did};
314
315   if ( $rdid->{'statuscode'} != 100 ) {
316     return "Error running VoIP Innovations releaseDID: ".
317            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
318   }
319
320   '';
321 }
322
323 sub _export_suspend {
324   my( $self, $svc_phone ) = (shift, shift);
325   #nop for now
326   '';
327 }
328
329 sub _export_unsuspend {
330   my( $self, $svc_phone ) = (shift, shift);
331   #nop for now
332   '';
333 }
334
335 #hmm, might forgo queueing entirely for most things, data is too much of a pita
336 #sub globalpops_voip_queue {
337 #  my( $self, $svcnum, $method ) = (shift, shift, shift);
338 #  my $queue = new FS::queue {
339 #    'svcnum' => $svcnum,
340 #    'job'    => 'FS::part_export::globalpops_voip::globalpops_voip_command',
341 #  };
342 #  $queue->insert(
343 #    $self->option('login'),
344 #    $self->option('password'),
345 #    $method,
346 #    @_,
347 #  );
348 #}
349
350 sub globalpops_voip_command {
351   my($login, $password, $method, @args) = @_;
352
353   eval "use Net::GlobalPOPs::MediaServicesAPI;";
354   die $@ if $@;
355
356   my $gp = new Net::GlobalPOPs
357                  'login'    => $login,
358                  'password' => $password,
359                  #'debug'    => 1,
360                ;
361
362   my $return = $gp->$method( @args );
363
364   #$return->{'status'} 
365   #$return->{'statuscode'} 
366
367   die $return->{'status'} if $return->{'statuscode'};
368
369 }
370
371 1;
372