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