6df21f40624fca43ea3d89cd545bf2806ddfed8d
[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   if ( $dids->{'type'} eq 'Error' ) {
65     my $error =  "Error running VoIP Innovations getDIDs: ".
66         $dids->{'statuscode'}. ': '. $dids->{'status'}. "\n";
67     warn $error;
68     die $error;
69   }
70
71   my $search = $dids->{'search'};
72
73   if ( $search->{'statuscode'} == 302200 ) {
74     return [];
75   } elsif ( $search->{'statuscode'} != 100 ) {
76     my $error = "Error running VoIP Innovations getDIDs: ".
77                  $search->{'statuscode'}. ': '. $search->{'status'}. "\n";
78     warn $error;
79     die $error;
80   }
81
82   my @return = ();
83
84   #my $latas = $search->{state}{lata};
85   my %latas;
86   if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
87     %latas = map $search->{state}{lata}{$_},
88                  qw(name rate_center);
89   } else {
90     %latas = %{ $search->{state}{lata} };
91   } 
92
93   foreach my $lata ( keys %latas ) {
94
95     #warn "LATA $lata";
96     
97     #my $l = $latas{$lata};
98     #$l = $l->{rate_center} if exists $l->{rate_center};
99     
100     my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
101     my $lata_search = $lata_dids->{'search'};
102     unless ( $lata_search->{'statuscode'} == 100 ) {
103       die "Error running VoIP Innovations getDIDs: ". $lata_search->{'status'}; #die??
104     }
105    
106     my $l = $lata_search->{state}{lata}{'rate_center'};
107
108     #use Data::Dumper;
109     #warn Dumper($l);
110
111     my %rate_center;
112     if ( grep $l->{$_}, qw(name friendlyname) ) {
113       %rate_center = map $l->{$_},
114                          qw(name friendlyname);
115     } else {
116       %rate_center = %$l;
117     } 
118
119     foreach my $rate_center ( keys %rate_center ) {
120       
121       #warn "rate center $rate_center";
122
123       my $rc = $rate_center{$rate_center}; 
124       $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
125
126       my @r = ();
127       if ( exists($rc->{npa}) ) {
128         @r = ($rc);
129       } else {
130         @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
131       }
132
133       foreach my $r (@r) {
134
135         my @npa = ();
136         if ( exists($r->{npa}{name}) ) {
137           @npa = ($r->{npa})
138         } else {
139           @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
140         }
141
142         foreach my $npa (@npa) {
143
144           if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
145
146             #warn Dumper($npa);
147
148             my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
149
150             my @tn = ref($tn) eq 'ARRAY' ? @$tn : ($tn);
151             #push @return, @tn;
152             push @return,
153               map {
154                     if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
155                       "$1-$2-$3";
156                     } else {
157                       $_;
158                     }
159                   }
160                map { ref($_) eq 'HASH' ? $_->{'content'} : $_ } #tier always 2?
161                @tn;
162
163           } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
164
165             if ( $npa->{nxx}{name} ) {
166               @nxx = ( $npa->{nxx}{name} );
167             } else {
168               @nxx = keys %{ $npa->{nxx} };
169             }
170
171             push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
172                               @nxx;
173
174           } elsif ( $opt{'state'} ) { #and not other things, then return areacode
175             #my $ac = $npa->{name};
176             #use Data::Dumper;
177             #warn Dumper($r) unless length($ac) == 3;
178
179             push @return, $npa->{name}
180               unless grep { $_ eq $npa->{name} } @return;
181
182           } else {
183             warn "WARNING: returning nothing for get_dids without known options"; #?
184           }
185
186         } #foreach my $npa
187
188       } #foreach my $r
189
190     } #foreach my $rate_center
191
192   } #foreach my $lata
193
194   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
195     @return = sort { $a cmp $b } @return; #string comparison actually dwiw
196   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
197     @return = sort { lc($a) cmp lc($b) } @return;
198   } elsif ( $opt{'state'} ) { #and not other things, then return areacode
199
200     #populate cache
201
202     local $SIG{HUP} = 'IGNORE';
203     local $SIG{INT} = 'IGNORE';
204     local $SIG{QUIT} = 'IGNORE';
205     local $SIG{TERM} = 'IGNORE';
206     local $SIG{TSTP} = 'IGNORE';
207     local $SIG{PIPE} = 'IGNORE';
208
209     my $oldAutoCommit = $FS::UID::AutoCommit;
210     local $FS::UID::AutoCommit = 0;
211     my $dbh = dbh;
212
213     my $errmsg = 'WARNING: error populating phone availability cache: ';
214     my $error = '';
215     foreach my $return (@return) {
216       my $phone_avail = new FS::phone_avail {
217         'exportnum'   => $self->exportnum,
218         'countrycode' => '1', #don't hardcode me when gp goes int'l
219         'state'       => $opt{'state'},
220         'npa'         => $return,
221       };
222       $error = $phone_avail->insert();
223       if ( $error ) {
224         warn $errmsg.$error;
225         last;
226       }
227     }
228
229     if ( $error ) {
230       $dbh->rollback if $oldAutoCommit;
231     } else {
232       $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
233     }
234
235     #end populate cache
236
237     #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
238     @return = sort { $a <=> $b } @return;
239   } else {
240     warn "WARNING: returning nothing for get_dids without known options"; #?
241   }
242
243   \@return;
244
245 }
246
247 sub gp_command {
248   my( $self, $command, @args ) = @_;
249
250   eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
251   if ( $@ ) {
252     warn $@;
253     die $@;
254   }
255
256   my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
257     'login'    => $self->option('login'),
258     'password' => $self->option('password'),
259     #'debug'    => $debug,
260   );
261
262   $gp->$command(@args);
263 }
264
265
266 sub _export_insert {
267   my( $self, $svc_phone ) = (shift, shift);
268
269   return '' if $self->option('dry_run');
270
271   #we want to provision and catch errors now, not queue
272
273   my $r = $self->gp_command('reserveDID',
274     'did'           => $svc_phone->phonenum,
275     'minutes'       => 1,
276     'endpointgroup' => $self->option('endpointgroup'),
277   );
278
279   my $rdid = $r->{did};
280
281   if ( $rdid->{'statuscode'} != 100 ) {
282     return "Error running VoIP Innovations reserveDID: ".
283            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
284   }
285
286   my $a = $self->gp_command('assignDID',
287     'did'           => $svc_phone->phonenum,
288     'endpointgroup' => $self->option('endpointgroup'),
289     #'rewrite'
290     #'cnam'
291   );
292
293   my $adid = $a->{did};
294
295   if ( $adid->{'statuscode'} != 100 ) {
296     return "Error running VoIP Innovations assignDID: ".
297            $adid->{'statuscode'}. ': '. $adid->{'status'};
298   }
299
300   '';
301 }
302
303 sub _export_replace {
304   my( $self, $new, $old ) = (shift, shift, shift);
305
306   #hmm, what's to change?
307   '';
308 }
309
310 sub _export_delete {
311   my( $self, $svc_phone ) = (shift, shift);
312
313   return '' if $self->option('dry_run');
314
315   #probably okay to queue the deletion...?
316   #but hell, let's do it inline anyway, who wants phone numbers hanging around
317
318   my $r = $self->gp_command('releaseDID',
319     'did'           => $svc_phone->phonenum,
320   );
321
322   my $rdid = $r->{did};
323
324   if ( $rdid->{'statuscode'} != 100 ) {
325     return "Error running VoIP Innovations releaseDID: ".
326            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
327   }
328
329   '';
330 }
331
332 sub _export_suspend {
333   my( $self, $svc_phone ) = (shift, shift);
334   #nop for now
335   '';
336 }
337
338 sub _export_unsuspend {
339   my( $self, $svc_phone ) = (shift, shift);
340   #nop for now
341   '';
342 }
343
344 #hmm, might forgo queueing entirely for most things, data is too much of a pita
345 #sub globalpops_voip_queue {
346 #  my( $self, $svcnum, $method ) = (shift, shift, shift);
347 #  my $queue = new FS::queue {
348 #    'svcnum' => $svcnum,
349 #    'job'    => 'FS::part_export::globalpops_voip::globalpops_voip_command',
350 #  };
351 #  $queue->insert(
352 #    $self->option('login'),
353 #    $self->option('password'),
354 #    $method,
355 #    @_,
356 #  );
357 #}
358
359 sub globalpops_voip_command {
360   my($login, $password, $method, @args) = @_;
361
362   eval "use Net::GlobalPOPs::MediaServicesAPI 0.03;";
363   die $@ if $@;
364
365   my $gp = new Net::GlobalPOPs
366                  'login'    => $login,
367                  'password' => $password,
368                  #'debug'    => 1,
369                ;
370
371   my $return = $gp->$method( @args );
372
373   #$return->{'status'} 
374   #$return->{'statuscode'} 
375
376   die $return->{'status'} if $return->{'statuscode'};
377
378 }
379
380 1;
381