1 package FS::part_export::vitelity;
3 use vars qw(@ISA %info);
5 use FS::Record qw(qsearch dbh);
9 @ISA = qw(FS::part_export);
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 'fax' => { label=>'vfax service', type=>'checkbox' },
18 'restrict_selection' => { type=>'select',
19 label=>'Restrict DID Selection',
20 options=>[ '', 'tollfree', 'non-tollfree' ],
27 'desc' => 'Provision phone numbers to Vitelity',
28 'options' => \%options,
31 Requires installation of
32 <a href="http://search.cpan.org/dist/Net-Vitelity">Net::Vitelity</a>
35 routesip - optional Vitelity sub-account to which newly ordered DIDs will be routed
36 <br>type - optional DID type (perminute, unlimited, or your-pri)
40 sub rebless { shift; }
42 sub can_get_dids { 1; }
43 sub get_dids_can_tollfree { 1; };
47 my %opt = ref($_[0]) ? %{$_[0]} : @_;
49 if ( $opt{'tollfree'} ) {
50 my $command = 'listtollfree';
51 $command = 'listdids' if $self->option('fax');
52 my @tollfree = $self->vitelity_command($command);
55 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
57 foreach my $did ( @tollfree ) {
58 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
62 my @sorted_ret = sort @ret;
65 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
67 my %flushopts = ( 'state' => $opt{'state'},
68 'ratecenter' => $opt{'ratecenter'},
69 'exportnum' => $self->exportnum
71 FS::phone_avail::flush( \%flushopts );
73 local $SIG{HUP} = 'IGNORE';
74 local $SIG{INT} = 'IGNORE';
75 local $SIG{QUIT} = 'IGNORE';
76 local $SIG{TERM} = 'IGNORE';
77 local $SIG{TSTP} = 'IGNORE';
78 local $SIG{PIPE} = 'IGNORE';
80 my $oldAutoCommit = $FS::UID::AutoCommit;
81 local $FS::UID::AutoCommit = 0;
84 my $errmsg = 'WARNING: error populating phone availability cache: ';
86 my $command = 'listlocal';
87 $command = 'listdids' if $self->option('fax');
88 my @dids = $self->vitelity_command( $command,
89 'state' => $opt{'state'},
90 'ratecenter' => $opt{'ratecenter'},
92 # XXX: Options: type=unlimited OR type=pri
94 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
95 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
97 foreach my $did ( @dids ) {
98 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
99 my($npa, $nxx, $station) = ($1, $2, $3);
101 my $phone_avail = new FS::phone_avail {
102 'exportnum' => $self->exportnum,
103 'countrycode' => '1', # vitelity is US/CA only now
104 'state' => $opt{'state'},
107 'station' => $station,
108 'name' => $opt{'ratecenter'},
111 my $error = $phone_avail->insert();
113 $dbh->rollback if $oldAutoCommit;
118 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
121 map { join('-', $_->npa, $_->nxx, $_->station ) }
123 'table' => 'phone_avail',
124 'hashref' => { 'exportnum' => $self->exportnum,
125 'countrycode' => '1', # vitelity is US/CA only now
126 'name' => $opt{'ratecenter'},
127 'state' => $opt{'state'},
129 'order_by' => 'ORDER BY npa, nxx, station',
133 } elsif ( $opt{'areacode'} ) {
135 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
137 'select' => 'DISTINCT name, state',
138 'table' => 'phone_avail',
139 'hashref' => { 'exportnum' => $self->exportnum,
140 'countrycode' => '1', # vitelity is US/CA only now
141 'npa' => $opt{'areacode'},
145 my @sorted_rc = sort @rc;
146 return [ @sorted_rc ];
148 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
150 my @avail = qsearch({
151 'select' => 'DISTINCT npa',
152 'table' => 'phone_avail',
153 'hashref' => { 'exportnum' => $self->exportnum,
154 'countrycode' => '1', # vitelity is US/CA only now
155 'state' => $opt{'state'},
157 'order_by' => 'ORDER BY npa',
160 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
162 #otherwise, search for em
164 my $command = 'listavailratecenters';
165 $command = 'listratecenters' if $self->option('fax');
166 my @ratecenters = $self->vitelity_command( $command,
167 'state' => $opt{'state'},
169 # XXX: Options: type=unlimited OR type=pri
171 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
173 } elsif ( $ratecenters[0] eq 'missingdata' ) {
174 die "missingdata error running Vitelity API"; #die?
177 local $SIG{HUP} = 'IGNORE';
178 local $SIG{INT} = 'IGNORE';
179 local $SIG{QUIT} = 'IGNORE';
180 local $SIG{TERM} = 'IGNORE';
181 local $SIG{TSTP} = 'IGNORE';
182 local $SIG{PIPE} = 'IGNORE';
184 my $oldAutoCommit = $FS::UID::AutoCommit;
185 local $FS::UID::AutoCommit = 0;
188 my $errmsg = 'WARNING: error populating phone availability cache: ';
191 foreach my $ratecenter (@ratecenters) {
193 my $command = 'listlocal';
194 $command = 'listdids' if $self->option('fax');
195 my @dids = $self->vitelity_command( $command,
196 'state' => $opt{'state'},
197 'ratecenter' => $ratecenter,
199 # XXX: Options: type=unlimited OR type=pri
201 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
203 } elsif ( $dids[0] eq 'missingdata' ) {
204 die "missingdata error running Vitelity API"; #die?
207 foreach my $did ( @dids ) {
208 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
209 my($npa, $nxx, $station) = ($1, $2, $3);
212 my $phone_avail = new FS::phone_avail {
213 'exportnum' => $self->exportnum,
214 'countrycode' => '1', # vitelity is US/CA only now
215 'state' => $opt{'state'},
218 'station' => $station,
219 'name' => $ratecenter,
222 my $error = $phone_avail->insert();
224 $dbh->rollback if $oldAutoCommit;
232 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
234 my @return = sort { $a <=> $b } keys %npa;
238 die "get_dids called without state or areacode options";
243 sub vitelity_command {
244 my( $self, $command, @args ) = @_;
246 eval "use Net::Vitelity;";
249 my $vitelity = Net::Vitelity->new(
250 'login' => $self->option('login'),
251 'pass' => $self->option('pass'),
252 'apitype' => $self->option('fax') ? 'fax' : 'api',
256 $vitelity->$command(@args);
260 my( $self, $svc_phone ) = (shift, shift);
262 return '' if $self->option('dry_run');
264 #we want to provision and catch errors now, not queue
266 my %vparams = ( 'did' => $svc_phone->phonenum );
267 $vparams{'routesip'} = $self->option('routesip')
268 if defined $self->option('routesip');
269 $vparams{'type'} = $self->option('type')
270 if defined $self->option('type');
272 my $command = 'getlocaldid';
273 my $success = 'success';
275 # this is OK as Vitelity for now is US/CA only; it's not a hack
276 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
278 if($self->option('fax')) {
283 my $result = $self->vitelity_command($command,%vparams);
285 if ( $result ne $success ) {
286 return "Error running Vitelity $command: $result";
292 sub _export_replace {
293 my( $self, $new, $old ) = (shift, shift, shift);
296 if( $old->forwarddst ne $new->forwarddst ) {
297 my $result = $self->vitelity_command('callfw',
298 'did' => $old->phonenum,
299 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
301 if ( $result ne 'ok' ) {
302 return "Error running Vitelity callfw: $result";
306 # vfax forwarding emails
307 if( $old->email ne $new->email && $self->option('fax') ) {
308 my $result = $self->vitelity_command('changeemail',
309 'did' => $old->phonenum,
310 'emails' => $new->email ? $new->email : '',
312 if ( $result ne 'ok' ) {
313 return "Error running Vitelity changeemail: $result";
321 my( $self, $svc_phone ) = (shift, shift);
323 return '' if $self->option('dry_run');
325 #probably okay to queue the deletion...?
326 #but hell, let's do it inline anyway, who wants phone numbers hanging around
328 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
330 my $result = $self->vitelity_command('removedid',
331 'did' => $svc_phone->phonenum,
334 if ( $result ne 'success' ) {
335 return "Error running Vitelity removedid: $result";
341 sub _export_suspend {
342 my( $self, $svc_phone ) = (shift, shift);
347 sub _export_unsuspend {
348 my( $self, $svc_phone ) = (shift, shift);