1 package FS::phone_avail;
4 use vars qw( @ISA $DEBUG $me );
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::Misc::DateTime qw( parse_datetime );
11 @ISA = qw(FS::cust_main_Mixin FS::Record);
13 $me = '[FS::phone_avail]';
18 FS::phone_avail - Phone number availability cache
24 $record = new FS::phone_avail \%hash;
25 $record = new FS::phone_avail { 'column' => 'value' };
27 $error = $record->insert;
29 $error = $new_record->replace($old_record);
31 $error = $record->delete;
33 $error = $record->check;
37 An FS::phone_avail object represents availability of phone service.
38 FS::phone_avail inherits from FS::Record. The following fields are currently
75 =item rate_center_abbrev - abbreviated rate center
77 =item latanum - LATA #
81 =item ordernum - bulk DID order #
95 Creates a new record. To add the record to the database, see L<"insert">.
97 Note that this stores the hash reference, not a distinct copy of the hash it
98 points to. You can ask the object for a copy with the I<hash> method.
102 # the new method can be inherited from FS::Record, if a table method is defined
104 sub table { 'phone_avail'; }
108 Adds this record to the database. If there is an error, returns the error,
109 otherwise returns false.
113 # the insert method can be inherited from FS::Record
117 Delete this record from the database.
121 # the delete method can be inherited from FS::Record
123 =item replace OLD_RECORD
125 Replaces the OLD_RECORD with this one in the database. If there is an error,
126 returns the error, otherwise returns false.
130 # the replace method can be inherited from FS::Record
134 Checks all fields to make sure this is a valid record. If there is
135 an error, returns the error, otherwise returns false. Called by the insert
140 # the check method should currently be supplied - FS::Record contains some
141 # data checking routines
147 $self->ut_numbern('availnum')
148 || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum' )
149 || $self->ut_number('countrycode')
150 || $self->ut_alphan('state')
151 || $self->ut_number('npa')
152 || $self->ut_numbern('nxx')
153 || $self->ut_numbern('station')
154 || $self->ut_textn('name')
155 || $self->ut_textn('rate_center_abbrev')
156 || $self->ut_foreign_keyn('latanum', 'lata', 'latanum' )
157 || $self->ut_foreign_keyn('msanum', 'msa', 'msanum' )
158 || $self->ut_foreign_keyn('ordernum', 'did_order', 'ordernum' )
159 || $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum' )
160 || $self->ut_textn('availbatch')
162 return $error if $error;
173 return '' unless $self->svcnum;
174 qsearchs('cust_svc', { 'svcnum' => $self->svcnum });
183 return '' unless $self->exportnum;
184 qsearchs('part_export', { 'exportnum' => $self->exportnum });
193 return '' unless $self->latanum;
194 qsearchs('lata', { 'latanum' => $self->latanum });
199 Translate free-form MSA name to a msa.msanum
207 if ( $msa =~ /(.+[^,])\s+(\w{2}(-\w{2})*)$/ ) {
211 my @msas = qsearch('msa', { 'description' => { 'op' => 'ILIKE',
212 'value' => "%$msa%", }
214 return 0 unless scalar(@msas);
215 my @msa = grep { $self->msatest($msa,$_->description) } @msas;
216 return 0 unless scalar(@msa) == 1;
222 my ($their,$our) = (shift,shift);
227 return 1 if $our eq $their;
231 return 1 if $a eq $their;
232 return 1 if ($our =~ /^([\w\s]+)-/ && $1 eq $their);
236 sub process_batch_import {
240 my( $phone_avail, $value ) = @_;
242 $value =~ /^(\d{3})(\d{3})(\d+)$/ or die "unparsable number $value\n";
243 #( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
244 $phone_avail->npa($1);
245 $phone_avail->nxx($2);
246 $phone_avail->station($3);
250 my( $phone_avail, $value ) = @_;
251 return '' if !$value;
252 my $msanum = $phone_avail->msa2msanum($value);
253 die "cannot translate MSA ($value) to msanum" unless $msanum;
254 $phone_avail->msanum($msanum);
257 my $opt = { 'table' => 'phone_avail',
258 'params' => [ 'availbatch', 'exportnum', 'countrycode', 'ordernum', 'vendor_order_id', 'confirmed' ],
259 'formats' => { 'default' => [ 'state', $numsub, 'name' ],
260 'bulk' => [ 'state', $numsub, 'name', 'rate_center_abbrev', $msasub, 'latanum' ],
262 'postinsert_callback' => sub {
264 if($record->ordernum) {
265 my $did_order = qsearchs('did_order',
266 { 'ordernum' => $record->ordernum } );
267 if($did_order && !$did_order->received) {
268 $did_order->received(time);
269 $did_order->confirmed(parse_datetime($record->confirmed));
270 $did_order->vendor_order_id($record->vendor_order_id);
277 FS::Record::process_batch_import( $job, $opt, @_ );
280 sub flush { # evil direct SQL
283 if ( $opt->{'ratecenter'} =~ /^[\w\s]+$/
284 && $opt->{'state'} =~ /^[A-Z][A-Z]$/
285 && $opt->{'exportnum'} =~ /^\d+$/) {
286 my $sth = dbh->prepare('delete from phone_avail where exportnum = ? '.
287 ' and state = ? and name = ?');
288 $sth->execute($opt->{'exportnum'},$opt->{'state'},$opt->{'ratecenter'})
295 # Used by FS::Upgrade to migrate to a new database.
297 my ($class, %opts) = @_;
299 warn "$me upgrading $class\n" if $DEBUG;
301 my $sth = dbh->prepare(
302 'UPDATE phone_avail SET svcnum = NULL
303 WHERE svcnum IS NOT NULL
304 AND 0 = ( SELECT COUNT(*) FROM svc_phone
305 WHERE phone_avail.svcnum = svc_phone.svcnum )'
306 ) or die dbh->errstr;
308 $sth->execute or die $sth->errstr;
316 Sparse documentation.
320 L<FS::Record>, schema.html from the base documentation.