improvements to vitelity.pm, RT4868
[freeside.git] / FS / FS / part_export / vitelity.pm
1 package FS::part_export::vitelity;
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=>'Vitelity API login' },
13   'pass'          => { label=>'Vitelity API password' },
14   'dry_run'       => { label=>"Test mode - don't actually provision" },
15   'routesip'      => { label=>'routesip (optional sub-account)' },
16   'type'          => { label=>'type (optional DID type to order)' },
17 ;
18
19 %info = (
20   'svc'     => 'svc_phone',
21   'desc'    => 'Provision phone numbers to Vitelity',
22   'options' => \%options,
23   'notes'   => <<'END'
24 Requires installation of
25 <a href="http://search.cpan.org/dist/Net-Vitelity">Net::Vitelity</a>
26 from CPAN.
27 <br><br>
28 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
29 <br>type - optional DID type (perminute, unlimited, or your-pri)
30 END
31 );
32
33 sub rebless { shift; }
34
35 sub get_dids {
36   my $self = shift;
37   my %opt = ref($_[0]) ? %{$_[0]} : @_;
38
39 # currently one of three cases: areacode+exchange, areacode, state
40 # name == ratecenter
41
42   my %search = ();
43
44   my $method = '';
45
46   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers in format NPA-NXX-XXXX
47
48     return [
49       map { join('-', $_->npa, $_->nxx, $_->station ) }
50           qsearch({
51             'table'    => 'phone_avail',
52             'hashref'  => { 'exportnum'   => $self->exportnum,
53                             'countrycode' => '1', # vitelity is US/CA only now
54                             'state'       => $opt{'state'},
55                             'npa'         => $opt{'areacode'},
56                             'nxx'         => $opt{'exchange'},
57                           },
58             'order_by' => 'ORDER BY station',
59           })
60     ];
61
62   } elsif ( $opt{'areacode'} ) { #return exchanges in format NPA-NXX- literal 'XXXX'
63
64     return [
65       map { $_->name. ' ('. $_->npa. '-'. $_->nxx. '-XXXX)' } 
66           qsearch({
67             'select'   => 'DISTINCT ON ( name, npa, nxx ) *',
68             'table'    => 'phone_avail',
69             'hashref'  => { 'exportnum'   => $self->exportnum,
70                             'countrycode' => '1', # vitelity is US/CA only now
71                             'state'       => $opt{'state'},
72                             'npa'         => $opt{'areacode'},
73                           },
74             'order_by' => 'ORDER BY nxx',
75           })
76     ];
77
78   } elsif ( $opt{'state'} ) { #and not other things, then return areacode
79
80     #XXX need to flush the cache at some point :/
81
82     my @avail = qsearch({
83       'select'   => 'DISTINCT npa',
84       'table'    => 'phone_avail',
85       'hashref'  => { 'exportnum'   => $self->exportnum,
86                       'countrycode' => '1', # vitelity is US/CA only now
87                       'state'       => $opt{'state'},
88                     },
89       'order_by' => 'ORDER BY npa',
90     });
91
92     return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
93
94     #otherwise, search for em
95
96     my @ratecenters = $self->vitelity_command( 'listavailratecenters',
97                                                  'state' => $opt{'state'}, 
98                                              );
99     # XXX: Options: type=unlimited OR type=pri
100
101     if ( $ratecenters[0] eq 'unavailable' ) {
102       return [];
103     } elsif ( $ratecenters[0] eq 'missingdata' ) {
104       die "missingdata error running Vitelity API"; #die?
105     }
106
107     local $SIG{HUP} = 'IGNORE';
108     local $SIG{INT} = 'IGNORE';
109     local $SIG{QUIT} = 'IGNORE';
110     local $SIG{TERM} = 'IGNORE';
111     local $SIG{TSTP} = 'IGNORE';
112     local $SIG{PIPE} = 'IGNORE';
113
114     my $oldAutoCommit = $FS::UID::AutoCommit;
115     local $FS::UID::AutoCommit = 0;
116     my $dbh = dbh;
117
118     my $errmsg = 'WARNING: error populating phone availability cache: ';
119
120     my %npa = ();
121     foreach my $ratecenter (@ratecenters) {
122
123       my @dids = $self->vitelity_command( 'listlocal',
124                                             'state'      => $opt{'state'},
125                                             'ratecenter' => $ratecenter,
126                                         );
127     # XXX: Options: type=unlimited OR type=pri
128
129       if ( $dids[0] eq 'unavailable' ) {
130         next;
131       } elsif ( $dids[0] eq 'missingdata' ) {
132         die "missingdata error running Vitelity API"; #die?
133       }
134
135       foreach my $did ( @dids ) {
136         $did =~ /^(\d{3})(\d{3})(\d{4})$/ or die "unparsable did $did\n";
137         my($npa, $nxx, $station) = ($1, $2, $3);
138         $npa{$npa}++;
139
140         my $phone_avail = new FS::phone_avail {
141           'exportnum'   => $self->exportnum,
142           'countrycode' => '1', # vitelity is US/CA only now
143           'state'       => $opt{'state'},
144           'npa'         => $npa,
145           'nxx'         => $nxx,
146           'station'     => $station,
147           'name'        => $ratecenter,
148         };
149
150         $error = $phone_avail->insert();
151         if ( $error ) {
152           $dbh->rollback if $oldAutoCommit;
153           die $errmsg.$error;
154         }
155
156       }
157
158     }
159
160     $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
161
162     my @return = sort { $a <=> $b } keys %npa;
163     #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
164
165     return \@return;
166
167   } else {
168     die "get_dids called without state or areacode options";
169   }
170
171 }
172
173 sub vitelity_command {
174   my( $self, $command, @args ) = @_;
175
176   eval "use Net::Vitelity;";
177   die $@ if $@;
178
179   my $vitelity = Net::Vitelity->new(
180     'login' => $self->option('login'),
181     'pass'  => $self->option('pass'),
182     #'debug'    => $debug,
183   );
184
185   $vitelity->$command(@args);
186 }
187
188 sub _export_insert {
189   my( $self, $svc_phone ) = (shift, shift);
190
191   return '' if $self->option('dry_run');
192
193   #we want to provision and catch errors now, not queue
194
195   %vparams = ( 'did' => $svc_phone->phonenum );
196   $vparams{'routesip'} = $self->option('routesip') 
197     if defined $self->option('routesip');
198   $vparams{'type'} = $self->option('type') 
199     if defined $self->option('type');
200
201   my $result = $self->vitelity_command('getlocaldid',%vparams);
202
203   if ( $result ne 'success' ) {
204     return "Error running Vitelity getlocaldid: $result";
205   }
206
207   '';
208 }
209
210 sub _export_replace {
211   my( $self, $new, $old ) = (shift, shift, shift);
212
213   #hmm, what's to change?
214   '';
215 }
216
217 sub _export_delete {
218   my( $self, $svc_phone ) = (shift, shift);
219
220   return '' if $self->option('dry_run');
221
222   #probably okay to queue the deletion...?
223   #but hell, let's do it inline anyway, who wants phone numbers hanging around
224
225   my $result = $self->vitelity_command('removedid',
226     'did'           => $svc_phone->phonenum,
227   );
228
229   if ( $result ne 'success' ) {
230     return "Error running Vitelity getlocaldid: $result";
231   }
232
233   '';
234 }
235
236 sub _export_suspend {
237   my( $self, $svc_phone ) = (shift, shift);
238   #nop for now
239   '';
240 }
241
242 sub _export_unsuspend {
243   my( $self, $svc_phone ) = (shift, shift);
244   #nop for now
245   '';
246 }
247
248 1;
249