1 package FS::phone_avail;
4 use vars qw( @ISA $DEBUG $me );
5 use FS::Record qw( qsearch qsearchs dbh );
8 @ISA = qw(FS::cust_main_Mixin FS::Record);
10 $me = '[FS::phone_avail]';
15 FS::phone_avail - Phone number availability cache
21 $record = new FS::phone_avail \%hash;
22 $record = new FS::phone_avail { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::phone_avail object represents availability of phone service.
35 FS::phone_avail inherits from FS::Record. The following fields are currently
88 Creates a new record. To add the record to the database, see L<"insert">.
90 Note that this stores the hash reference, not a distinct copy of the hash it
91 points to. You can ask the object for a copy with the I<hash> method.
95 # the new method can be inherited from FS::Record, if a table method is defined
97 sub table { 'phone_avail'; }
101 Adds this record to the database. If there is an error, returns the error,
102 otherwise returns false.
106 # the insert method can be inherited from FS::Record
110 Delete this record from the database.
114 # the delete method can be inherited from FS::Record
116 =item replace OLD_RECORD
118 Replaces the OLD_RECORD with this one in the database. If there is an error,
119 returns the error, otherwise returns false.
123 # the replace method can be inherited from FS::Record
127 Checks all fields to make sure this is a valid record. If there is
128 an error, returns the error, otherwise returns false. Called by the insert
133 # the check method should currently be supplied - FS::Record contains some
134 # data checking routines
140 $self->ut_numbern('availnum')
141 || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum' )
142 || $self->ut_number('countrycode')
143 || $self->ut_alphan('state')
144 || $self->ut_number('npa')
145 || $self->ut_numbern('nxx')
146 || $self->ut_numbern('station')
147 || $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum' )
148 || $self->ut_textn('availbatch')
150 return $error if $error;
161 return '' unless $self->svcnum;
162 qsearchs('cust_svc', { 'svcnum' => $self->svcnum });
171 return '' unless $self->exportnum;
172 qsearchs('part_export', { 'exportnum' => $self->exportnum });
176 sub process_batch_import {
180 my( $phone_avail, $value ) = @_;
182 $value =~ /^(\d{3})(\d{3})(\d+)$/ or die "unparsable number $value\n";
183 #( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
184 $phone_avail->npa($1);
185 $phone_avail->nxx($2);
186 $phone_avail->station($3);
189 my $opt = { 'table' => 'phone_avail',
190 'params' => [ 'availbatch', 'exportnum', 'countrycode' ],
191 'formats' => { 'default' => [ 'state', $numsub ] },
194 FS::Record::process_batch_import( $job, $opt, @_ );
198 sub flush { # evil direct SQL
201 if ( $opt->{'ratecenter'} =~ /^[\w\s]+$/
202 && $opt->{'state'} =~ /^[A-Z][A-Z]$/
203 && $opt->{'exportnum'} =~ /^\d+$/) {
204 my $sth = dbh->prepare('delete from phone_avail where exportnum = ? '.
205 ' and state = ? and name = ?');
206 $sth->execute($opt->{'exportnum'},$opt->{'state'},$opt->{'ratecenter'})
213 # Used by FS::Upgrade to migrate to a new database.
215 my ($class, %opts) = @_;
217 warn "$me upgrading $class\n" if $DEBUG;
219 my $sth = dbh->prepare(
220 'UPDATE phone_avail SET svcnum = NULL
221 WHERE svcnum IS NOT NULL
222 AND 0 = ( SELECT COUNT(*) FROM svc_phone
223 WHERE phone_avail.svcnum = svc_phone.svcnum )'
224 ) or die dbh->errstr;
226 $sth->execute or die $sth->errstr;
234 Sparse documentation.
238 L<FS::Record>, schema.html from the base documentation.