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; }
44 my %opt = ref($_[0]) ? %{$_[0]} : @_;
46 if ( $opt{'tollfree'} ) {
47 my $command = 'listtollfree';
48 $command = 'listdids' if $self->option('fax');
49 my @tollfree = $self->vitelity_command($command);
52 return [] if ( $tollfree[0] eq 'noneavailable' || $tollfree[0] eq 'none');
54 foreach my $did ( @tollfree ) {
55 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
59 my @sorted_ret = sort @ret;
62 } elsif ( $opt{'ratecenter'} && $opt{'state'} ) {
64 my %flushopts = ( 'state' => $opt{'state'},
65 'ratecenter' => $opt{'ratecenter'},
66 'exportnum' => $self->exportnum
68 FS::phone_avail::flush( \%flushopts );
70 local $SIG{HUP} = 'IGNORE';
71 local $SIG{INT} = 'IGNORE';
72 local $SIG{QUIT} = 'IGNORE';
73 local $SIG{TERM} = 'IGNORE';
74 local $SIG{TSTP} = 'IGNORE';
75 local $SIG{PIPE} = 'IGNORE';
77 my $oldAutoCommit = $FS::UID::AutoCommit;
78 local $FS::UID::AutoCommit = 0;
81 my $errmsg = 'WARNING: error populating phone availability cache: ';
83 my $command = 'listlocal';
84 $command = 'listdids' if $self->option('fax');
85 my @dids = $self->vitelity_command( $command,
86 'state' => $opt{'state'},
87 'ratecenter' => $opt{'ratecenter'},
89 # XXX: Options: type=unlimited OR type=pri
91 next if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' );
92 die "missingdata error running Vitelity API" if $dids[0] eq 'missingdata';
94 foreach my $did ( @dids ) {
95 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
96 my($npa, $nxx, $station) = ($1, $2, $3);
98 my $phone_avail = new FS::phone_avail {
99 'exportnum' => $self->exportnum,
100 'countrycode' => '1', # vitelity is US/CA only now
101 'state' => $opt{'state'},
104 'station' => $station,
105 'name' => $opt{'ratecenter'},
108 my $error = $phone_avail->insert();
110 $dbh->rollback if $oldAutoCommit;
115 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
118 map { join('-', $_->npa, $_->nxx, $_->station ) }
120 'table' => 'phone_avail',
121 'hashref' => { 'exportnum' => $self->exportnum,
122 'countrycode' => '1', # vitelity is US/CA only now
123 'name' => $opt{'ratecenter'},
124 'state' => $opt{'state'},
126 'order_by' => 'ORDER BY npa, nxx, station',
130 } elsif ( $opt{'areacode'} ) {
132 my @rc = map { $_->{'Hash'}->{name}.", ".$_->state }
134 'select' => 'DISTINCT name, state',
135 'table' => 'phone_avail',
136 'hashref' => { 'exportnum' => $self->exportnum,
137 'countrycode' => '1', # vitelity is US/CA only now
138 'npa' => $opt{'areacode'},
142 my @sorted_rc = sort @rc;
143 return [ @sorted_rc ];
145 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
147 my @avail = qsearch({
148 'select' => 'DISTINCT npa',
149 'table' => 'phone_avail',
150 'hashref' => { 'exportnum' => $self->exportnum,
151 'countrycode' => '1', # vitelity is US/CA only now
152 'state' => $opt{'state'},
154 'order_by' => 'ORDER BY npa',
157 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
159 #otherwise, search for em
161 my $command = 'listavailratecenters';
162 $command = 'listratecenters' if $self->option('fax');
163 my @ratecenters = $self->vitelity_command( $command,
164 'state' => $opt{'state'},
166 # XXX: Options: type=unlimited OR type=pri
168 if ( $ratecenters[0] eq 'unavailable' || $ratecenters[0] eq 'none' ) {
170 } elsif ( $ratecenters[0] eq 'missingdata' ) {
171 die "missingdata error running Vitelity API"; #die?
174 local $SIG{HUP} = 'IGNORE';
175 local $SIG{INT} = 'IGNORE';
176 local $SIG{QUIT} = 'IGNORE';
177 local $SIG{TERM} = 'IGNORE';
178 local $SIG{TSTP} = 'IGNORE';
179 local $SIG{PIPE} = 'IGNORE';
181 my $oldAutoCommit = $FS::UID::AutoCommit;
182 local $FS::UID::AutoCommit = 0;
185 my $errmsg = 'WARNING: error populating phone availability cache: ';
188 foreach my $ratecenter (@ratecenters) {
190 my $command = 'listlocal';
191 $command = 'listdids' if $self->option('fax');
192 my @dids = $self->vitelity_command( $command,
193 'state' => $opt{'state'},
194 'ratecenter' => $ratecenter,
196 # XXX: Options: type=unlimited OR type=pri
198 if ( $dids[0] eq 'unavailable' || $dids[0] eq 'noneavailable' ) {
200 } elsif ( $dids[0] eq 'missingdata' ) {
201 die "missingdata error running Vitelity API"; #die?
204 foreach my $did ( @dids ) {
205 $did =~ /^(\d{3})(\d{3})(\d{4})/ or die "unparsable did $did\n";
206 my($npa, $nxx, $station) = ($1, $2, $3);
209 my $phone_avail = new FS::phone_avail {
210 'exportnum' => $self->exportnum,
211 'countrycode' => '1', # vitelity is US/CA only now
212 'state' => $opt{'state'},
215 'station' => $station,
216 'name' => $ratecenter,
219 my $error = $phone_avail->insert();
221 $dbh->rollback if $oldAutoCommit;
229 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
231 my @return = sort { $a <=> $b } keys %npa;
235 die "get_dids called without state or areacode options";
240 sub vitelity_command {
241 my( $self, $command, @args ) = @_;
243 eval "use Net::Vitelity;";
246 my $vitelity = Net::Vitelity->new(
247 'login' => $self->option('login'),
248 'pass' => $self->option('pass'),
249 'apitype' => $self->option('fax') ? 'fax' : 'api',
253 $vitelity->$command(@args);
257 my( $self, $svc_phone ) = (shift, shift);
259 return '' if $self->option('dry_run');
261 #we want to provision and catch errors now, not queue
263 my %vparams = ( 'did' => $svc_phone->phonenum );
264 $vparams{'routesip'} = $self->option('routesip')
265 if defined $self->option('routesip');
266 $vparams{'type'} = $self->option('type')
267 if defined $self->option('type');
269 my $command = 'getlocaldid';
270 my $success = 'success';
272 # this is OK as Vitelity for now is US/CA only; it's not a hack
273 $command = 'gettollfree' if $vparams{'did'} =~ /^800|^888|^877|^866|^855/;
275 if($self->option('fax')) {
280 my $result = $self->vitelity_command($command,%vparams);
282 if ( $result ne $success ) {
283 return "Error running Vitelity $command: $result";
289 sub _export_replace {
290 my( $self, $new, $old ) = (shift, shift, shift);
293 if( $old->forwarddst ne $new->forwarddst ) {
294 my $result = $self->vitelity_command('callfw',
295 'did' => $old->phonenum,
296 'forward' => $new->forwarddst ? $new->forwarddst : 'none',
298 if ( $result ne 'ok' ) {
299 return "Error running Vitelity callfw: $result";
303 # vfax forwarding emails
304 if( $old->email ne $new->email && $self->option('fax') ) {
305 my $result = $self->vitelity_command('changeemail',
306 'did' => $old->phonenum,
307 'emails' => $new->email ? $new->email : '',
309 if ( $result ne 'ok' ) {
310 return "Error running Vitelity changeemail: $result";
318 my( $self, $svc_phone ) = (shift, shift);
320 return '' if $self->option('dry_run');
322 #probably okay to queue the deletion...?
323 #but hell, let's do it inline anyway, who wants phone numbers hanging around
325 return 'Deleting vfax DIDs is unsupported by Vitelity API' if $self->option('fax');
327 my $result = $self->vitelity_command('removedid',
328 'did' => $svc_phone->phonenum,
331 if ( $result ne 'success' ) {
332 return "Error running Vitelity removedid: $result";
338 sub _export_suspend {
339 my( $self, $svc_phone ) = (shift, shift);
344 sub _export_unsuspend {
345 my( $self, $svc_phone ) = (shift, shift);