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" },
19 'desc' => 'Provision phone numbers to Vitelity',
20 'options' => \%options,
22 Requires installation of
23 <a href="http://search.cpan.org/dist/Net-Vitelity">Net::Vitelity</a>
28 sub rebless { shift; }
32 my %opt = ref($_[0]) ? %{$_[0]} : @_;
35 # 'orderby' => 'npa', #but it doesn't seem to work :/
39 if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
42 map { join('-', $_->npx, $_->nxx, $_->station ) }
44 'table' => 'phone_avail',
45 'hashref' => { 'exportnum' => $self->exportnum,
47 'state' => $opt{'state'},
48 'npa' => $opt{'areacode'},
49 'nxx' => $opt{'exchange'},
51 'order_by' => 'ORDER BY name', #?
55 } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
58 map { $_->name. ' ('. $_->npa. '-'. $_->nxx. '-XXXX)' }
60 'select' => 'DISTINCT ON ( name, npa, nxx ) *',
61 'table' => 'phone_avail',
62 'hashref' => { 'exportnum' => $self->exportnum,
64 'state' => $opt{'state'},
65 'npa' => $opt{'areacode'},
67 'order_by' => 'ORDER BY name', #?
71 } elsif ( $opt{'state'} ) { #and not other things, then return areacode
73 #XXX need to flush the cache at some point :/
76 'select' => 'DISTINCT npa',
77 'table' => 'phone_avail',
78 'hashref' => { 'exportnum' => $self->exportnum,
79 'countrycode' => '1', #don't hardcode me when gp goes intl
80 'state' => $opt{'state'},
82 'order_by' => 'ORDER BY npa',
85 return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
87 #otherwise, search for em
89 my @ratecenters = $self->vitelity_command( 'listavailratecenters',
90 'state' => $opt{'state'},
93 if ( $ratecenters[0] eq 'unavailable' ) {
95 } elsif ( $ratecenters[0] eq 'missingdata' ) {
96 die "missingdata error running Vitelity API"; #die?
99 local $SIG{HUP} = 'IGNORE';
100 local $SIG{INT} = 'IGNORE';
101 local $SIG{QUIT} = 'IGNORE';
102 local $SIG{TERM} = 'IGNORE';
103 local $SIG{TSTP} = 'IGNORE';
104 local $SIG{PIPE} = 'IGNORE';
106 my $oldAutoCommit = $FS::UID::AutoCommit;
107 local $FS::UID::AutoCommit = 0;
110 my $errmsg = 'WARNING: error populating phone availability cache: ';
113 foreach my $ratecenter (@ratecenters) {
115 my @dids = $self->vitelity_command( 'listlocal',
116 'state' => $opt{'state'},
117 'ratecenter' => $ratecenter,
120 if ( $dids[0] eq 'unavailable' ) {
122 } elsif ( $dids[0] eq 'missingdata' ) {
123 die "missingdata error running Vitelity API"; #die?
126 foreach my $did ( @dids ) {
127 $did =~ /^(\d{3})(\d{3})(\d{4})$/ or die "unparsable did $did\n";
128 my($npa, $nxx, $station) = ($1, $2, $3);
131 my $phone_avail = new FS::phone_avail {
132 'exportnum' => $self->exportnum,
133 'countrycode' => '1', #don't hardcode me when vitelity goes int'l
134 'state' => $opt{'state'},
137 'station' => $station,
138 'name' => $ratecenter,
141 $error = $phone_avail->insert();
143 $dbh->rollback if $oldAutoCommit;
151 $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
153 my @return = sort { $a <=> $b } keys %npa;
154 #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
159 die "get_dids called without state or areacode options";
164 sub vitelity_command {
165 my( $self, $command, @args ) = @_;
167 eval "use Net::Vitelity;";
170 my $vitelity = Net::Vitelity->new(
171 'login' => $self->option('login'),
172 'pass' => $self->option('pass'),
176 $vitelity->$command(@args);
180 my( $self, $svc_phone ) = (shift, shift);
182 return '' if $self->option('dry_run');
184 #we want to provision and catch errors now, not queue
186 my $result = $self->vitelity_command('getlocaldid',
187 'did' => $svc_phone->phonenum,
189 #Options: type=perminute OR type=unlimited OR type=your-pri OR
190 # routesip=route_to_this_subaccount
193 if ( $result ne 'success' ) {
194 return "Error running Vitelity getlocaldid: $result";
200 sub _export_replace {
201 my( $self, $new, $old ) = (shift, shift, shift);
203 #hmm, what's to change?
208 my( $self, $svc_phone ) = (shift, shift);
210 return '' if $self->option('dry_run');
212 #probably okay to queue the deletion...?
213 #but hell, let's do it inline anyway, who wants phone numbers hanging around
215 my $result = $self->vitelity_command('removedid',
216 'did' => $svc_phone->phonenum,
219 if ( $result ne 'success' ) {
220 return "Error running Vitelity getlocaldid: $result";
226 sub _export_suspend {
227 my( $self, $svc_phone ) = (shift, shift);
232 sub _export_unsuspend {
233 my( $self, $svc_phone ) = (shift, shift);