1 package FS::phone_avail;
2 use base qw( FS::cust_main_Mixin FS::Record );
5 use vars qw( $DEBUG $me );
6 use FS::Misc::DateTime qw( parse_datetime );
7 use FS::Record qw( qsearch qsearchs dbh );
11 $me = '[FS::phone_avail]';
16 FS::phone_avail - Phone number availability cache
22 $record = new FS::phone_avail \%hash;
23 $record = new FS::phone_avail { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::phone_avail object represents availability of phone service.
36 FS::phone_avail inherits from FS::Record. The following fields are currently
73 =item rate_center_abbrev - abbreviated rate center
75 =item latanum - LATA #
79 =item ordernum - bulk DID order #
93 Creates a new record. To add the record to the database, see L<"insert">.
95 Note that this stores the hash reference, not a distinct copy of the hash it
96 points to. You can ask the object for a copy with the I<hash> method.
100 # the new method can be inherited from FS::Record, if a table method is defined
102 sub table { 'phone_avail'; }
106 Adds this record to the database. If there is an error, returns the error,
107 otherwise returns false.
111 # the insert method can be inherited from FS::Record
115 Delete this record from the database.
119 # the delete method can be inherited from FS::Record
121 =item replace OLD_RECORD
123 Replaces the OLD_RECORD with this one in the database. If there is an error,
124 returns the error, otherwise returns false.
128 # the replace method can be inherited from FS::Record
132 Checks all fields to make sure this is a valid record. If there is
133 an error, returns the error, otherwise returns false. Called by the insert
138 # the check method should currently be supplied - FS::Record contains some
139 # data checking routines
145 $self->ut_numbern('availnum')
146 || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum' )
147 || $self->ut_number('countrycode')
148 || $self->ut_alphan('state')
149 || $self->ut_number('npa')
150 || $self->ut_numbern('nxx')
151 || $self->ut_numbern('station')
152 || $self->ut_textn('name')
153 || $self->ut_textn('rate_center_abbrev')
154 || $self->ut_foreign_keyn('latanum', 'lata', 'latanum' )
155 || $self->ut_foreign_keyn('msanum', 'msa', 'msanum' )
156 || $self->ut_foreign_keyn('ordernum', 'did_order', 'ordernum' )
157 || $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum' )
158 || $self->ut_textn('availbatch')
160 return $error if $error;
171 return '' unless $self->svcnum;
172 qsearchs('cust_svc', { 'svcnum' => $self->svcnum });
181 Translate free-form MSA name to a msa.msanum
189 if ( $msa =~ /(.+[^,])\s+(\w{2}(-\w{2})*)$/ ) {
193 my @msas = qsearch('msa', { 'description' => { 'op' => 'ILIKE',
194 'value' => "%$msa%", }
196 return 0 unless scalar(@msas);
197 my @msa = grep { $self->msatest($msa,$_->description) } @msas;
198 return 0 unless scalar(@msa) == 1;
204 my ($their,$our) = (shift,shift);
209 return 1 if $our eq $their;
213 return 1 if $a eq $their;
214 return 1 if ($our =~ /^([\w\s]+)-/ && $1 eq $their);
218 sub process_batch_import {
222 my( $phone_avail, $value ) = @_;
224 $value =~ /^(\d{3})(\d{3})(\d+)$/ or die "unparsable number $value\n";
225 #( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
226 $phone_avail->npa($1);
227 $phone_avail->nxx($2);
228 $phone_avail->station($3);
232 my( $phone_avail, $value ) = @_;
233 return '' if !$value;
234 my $msanum = $phone_avail->msa2msanum($value);
235 die "cannot translate MSA ($value) to msanum" unless $msanum;
236 $phone_avail->msanum($msanum);
239 my $opt = { 'table' => 'phone_avail',
240 'params' => [ 'availbatch', 'exportnum', 'countrycode', 'ordernum', 'vendor_order_id', 'confirmed' ],
241 'formats' => { 'default' => [ 'state', $numsub, 'name' ],
242 'bulk' => [ 'state', $numsub, 'name', 'rate_center_abbrev', $msasub, 'latanum' ],
244 'postinsert_callback' => sub {
246 if($record->ordernum) {
247 my $did_order = qsearchs('did_order',
248 { 'ordernum' => $record->ordernum } );
249 if($did_order && !$did_order->received) {
250 $did_order->received(time);
251 $did_order->confirmed(parse_datetime($record->confirmed));
252 $did_order->vendor_order_id($record->vendor_order_id);
259 FS::Record::process_batch_import( $job, $opt, @_ );
262 sub flush { # evil direct SQL
265 if ( $opt->{'ratecenter'} =~ /^[\w\s]+$/
266 && $opt->{'state'} =~ /^[A-Z][A-Z]$/
267 && $opt->{'exportnum'} =~ /^\d+$/) {
268 my $sth = dbh->prepare('delete from phone_avail where exportnum = ? '.
269 ' and state = ? and name = ?');
270 $sth->execute($opt->{'exportnum'},$opt->{'state'},$opt->{'ratecenter'})
277 # Used by FS::Upgrade to migrate to a new database.
279 my ($class, %opts) = @_;
281 warn "$me upgrading $class\n" if $DEBUG;
283 my $sth = dbh->prepare(
284 'UPDATE phone_avail SET svcnum = NULL
285 WHERE svcnum IS NOT NULL
286 AND 0 = ( SELECT COUNT(*) FROM svc_phone
287 WHERE phone_avail.svcnum = svc_phone.svcnum )'
288 ) or die dbh->errstr;
290 $sth->execute or die $sth->errstr;
298 Sparse documentation.
302 L<FS::Record>, schema.html from the base documentation.