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
206 my @msas = qsearch('msa', { 'description' => { 'op' => 'ILIKE',
207 'value' => "%$msa%", }
209 return 0 unless scalar(@msas);
210 my @msa = grep { $self->msatest($msa,$_->description) } @msas;
211 return 0 unless scalar(@msa) == 1;
217 my ($their,$our) = (shift,shift);
220 return 1 if $a eq $their;
221 return 1 if ($our =~ /^([\w\s]+)-/ && $1 eq $their);
225 sub process_batch_import {
229 my( $phone_avail, $value ) = @_;
231 $value =~ /^(\d{3})(\d{3})(\d+)$/ or die "unparsable number $value\n";
232 #( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
233 $phone_avail->npa($1);
234 $phone_avail->nxx($2);
235 $phone_avail->station($3);
239 my( $phone_avail, $value ) = @_;
240 return '' if !$value;
241 my $msanum = $phone_avail->msa2msanum($value);
242 die "cannot translate MSA ($value) to msanum" unless $msanum;
243 $phone_avail->msanum($msanum);
246 my $opt = { 'table' => 'phone_avail',
247 'params' => [ 'availbatch', 'exportnum', 'countrycode', 'ordernum', 'vendor_order_id', 'confirmed' ],
248 'formats' => { 'default' => [ 'state', $numsub, 'name' ],
249 'bulk' => [ 'state', $numsub, 'name', 'rate_center_abbrev', $msasub, 'latanum' ],
251 'postinsert_callback' => sub {
253 if($record->ordernum) {
254 my $did_order = qsearchs('did_order',
255 { 'ordernum' => $record->ordernum } );
256 if($did_order && !$did_order->received) {
257 $did_order->received(time);
258 $did_order->confirmed(parse_datetime($record->confirmed));
259 $did_order->vendor_order_id($record->vendor_order_id);
266 FS::Record::process_batch_import( $job, $opt, @_ );
269 sub flush { # evil direct SQL
272 if ( $opt->{'ratecenter'} =~ /^[\w\s]+$/
273 && $opt->{'state'} =~ /^[A-Z][A-Z]$/
274 && $opt->{'exportnum'} =~ /^\d+$/) {
275 my $sth = dbh->prepare('delete from phone_avail where exportnum = ? '.
276 ' and state = ? and name = ?');
277 $sth->execute($opt->{'exportnum'},$opt->{'state'},$opt->{'ratecenter'})
284 # Used by FS::Upgrade to migrate to a new database.
286 my ($class, %opts) = @_;
288 warn "$me upgrading $class\n" if $DEBUG;
290 my $sth = dbh->prepare(
291 'UPDATE phone_avail SET svcnum = NULL
292 WHERE svcnum IS NOT NULL
293 AND 0 = ( SELECT COUNT(*) FROM svc_phone
294 WHERE phone_avail.svcnum = svc_phone.svcnum )'
295 ) or die dbh->errstr;
297 $sth->execute or die $sth->errstr;
305 Sparse documentation.
309 L<FS::Record>, schema.html from the base documentation.