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 );
9 @ISA = qw(FS::cust_main_Mixin FS::Record);
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 return '' unless $self->exportnum;
182 qsearchs('part_export', { 'exportnum' => $self->exportnum });
191 return '' unless $self->latanum;
192 qsearchs('lata', { 'latanum' => $self->latanum });
197 Translate free-form MSA name to a msa.msanum
204 my $res = qsearchs('msa', { 'description' => { 'op' => 'ILIKE',
207 return 0 unless $res;
211 sub process_batch_import {
215 my( $phone_avail, $value ) = @_;
217 $value =~ /^(\d{3})(\d{3})(\d+)$/ or die "unparsable number $value\n";
218 #( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
219 $phone_avail->npa($1);
220 $phone_avail->nxx($2);
221 $phone_avail->station($3);
225 my( $phone_avail, $value ) = @_;
226 my $msanum = $phone_avail->msa2msanum($value);
227 die "cannot translate MSA ($value) to msanum" unless $msanum;
228 $phone_avail->msanum($msanum);
231 my $opt = { 'table' => 'phone_avail',
232 'params' => [ 'availbatch', 'exportnum', 'countrycode', 'ordernum', 'vendor_order_id', 'confirmed' ],
233 'formats' => { 'default' => [ 'state', $numsub, 'name' ],
234 'bulk' => [ 'state', $numsub, 'name', 'rate_center_abbrev', $msasub, 'latanum' ],
236 'postinsert_callback' => sub {
238 if($record->ordernum) {
239 my $did_order = qsearchs('did_order',
240 { 'ordernum' => $record->ordernum } );
241 if($did_order && !$did_order->received) {
242 $did_order->received(time);
243 $did_order->confirmed(parse_datetime($record->confirmed));
244 $did_order->vendor_order_id($record->vendor_order_id);
251 FS::Record::process_batch_import( $job, $opt, @_ );
254 sub flush { # evil direct SQL
257 if ( $opt->{'ratecenter'} =~ /^[\w\s]+$/
258 && $opt->{'state'} =~ /^[A-Z][A-Z]$/
259 && $opt->{'exportnum'} =~ /^\d+$/) {
260 my $sth = dbh->prepare('delete from phone_avail where exportnum = ? '.
261 ' and state = ? and name = ?');
262 $sth->execute($opt->{'exportnum'},$opt->{'state'},$opt->{'ratecenter'})
269 # Used by FS::Upgrade to migrate to a new database.
271 my ($class, %opts) = @_;
273 warn "$me upgrading $class\n" if $DEBUG;
275 my $sth = dbh->prepare(
276 'UPDATE phone_avail SET svcnum = NULL
277 WHERE svcnum IS NOT NULL
278 AND 0 = ( SELECT COUNT(*) FROM svc_phone
279 WHERE phone_avail.svcnum = svc_phone.svcnum )'
280 ) or die dbh->errstr;
282 $sth->execute or die $sth->errstr;
290 Sparse documentation.
294 L<FS::Record>, schema.html from the base documentation.