f05f05be2e23e65b59beedcf09e28bbf66bca405
[freeside.git] / FS / FS / phone_avail.pm
1 package FS::phone_avail;
2
3 use strict;
4 use vars qw( @ISA $DEBUG $me );
5 use FS::Record qw( qsearch qsearchs );
6
7 @ISA = qw(FS::Record);
8
9 $me = '[FS::phone_avail]';
10 $DEBUG = 0;
11
12 =head1 NAME
13
14 FS::phone_avail - Phone number availability cache
15
16 =head1 SYNOPSIS
17
18   use FS::phone_avail;
19
20   $record = new FS::phone_avail \%hash;
21   $record = new FS::phone_avail { 'column' => 'value' };
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31 =head1 DESCRIPTION
32
33 An FS::phone_avail object represents availability of phone service.
34 FS::phone_avail inherits from FS::Record.  The following fields are currently
35 supported:
36
37 =over 4
38
39 =item availnum
40
41 primary key
42
43 =item exportnum
44
45 exportnum
46
47 =item countrycode
48
49 countrycode
50
51 =item state
52
53 state
54
55 =item npa
56
57 npa
58
59 =item nxx
60
61 nxx
62
63 =item station
64
65 station
66
67 =item name
68
69 Optional name
70
71 =item svcnum
72
73 svcnum
74
75 =item availbatch
76
77 availbatch
78
79 =back
80
81 =head1 METHODS
82
83 =over 4
84
85 =item new HASHREF
86
87 Creates a new record.  To add the record to the database, see L<"insert">.
88
89 Note that this stores the hash reference, not a distinct copy of the hash it
90 points to.  You can ask the object for a copy with the I<hash> method.
91
92 =cut
93
94 # the new method can be inherited from FS::Record, if a table method is defined
95
96 sub table { 'phone_avail'; }
97
98 =item insert
99
100 Adds this record to the database.  If there is an error, returns the error,
101 otherwise returns false.
102
103 =cut
104
105 # the insert method can be inherited from FS::Record
106
107 =item delete
108
109 Delete this record from the database.
110
111 =cut
112
113 # the delete method can be inherited from FS::Record
114
115 =item replace OLD_RECORD
116
117 Replaces the OLD_RECORD with this one in the database.  If there is an error,
118 returns the error, otherwise returns false.
119
120 =cut
121
122 # the replace method can be inherited from FS::Record
123
124 =item check
125
126 Checks all fields to make sure this is a valid record.  If there is
127 an error, returns the error, otherwise returns false.  Called by the insert
128 and replace methods.
129
130 =cut
131
132 # the check method should currently be supplied - FS::Record contains some
133 # data checking routines
134
135 sub check {
136   my $self = shift;
137
138   my $error = 
139     $self->ut_numbern('availnum')
140     || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum' )
141     || $self->ut_number('countrycode')
142     || $self->ut_alphan('state')
143     || $self->ut_number('npa')
144     || $self->ut_numbern('nxx')
145     || $self->ut_numbern('station')
146     || $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum' )
147     || $self->ut_textn('availbatch')
148   ;
149   return $error if $error;
150
151   $self->SUPER::check;
152 }
153
154 sub process_batch_import {
155   my $job = shift;
156
157   my $numsub = sub {
158     my( $phone_avail, $value ) = @_;
159     $value =~ s/\D//g;
160     $value =~ /^(\d{3})(\d{3})(\d+)$/ or die "unparsable number $value\n";
161     #( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
162     $phone_avail->npa($1);
163     $phone_avail->nxx($2);
164     $phone_avail->station($3);
165   };
166
167   my $opt = { 'table'   => 'phone_avail',
168               'params'  => [ 'availbatch', 'exportnum', 'countrycode' ],
169               'formats' => { 'default' => [ 'state', $numsub ] },
170             };
171
172   FS::Record::process_batch_import( $job, $opt, @_ );
173
174 }
175
176 # Used by FS::Upgrade to migrate to a new database.
177 sub _upgrade_data {
178   my ($class, %opts) = @_;
179
180   warn "$me upgrading $class\n" if $DEBUG;
181
182   my $sth = dbh->prepare(
183     'UPDATE phone_avail SET svcnum = NULL
184        WHERE svcnum IS NOT NULL
185          AND 0 = ( SELECT COUNT(*) FROM svc_phone
186                      WHERE phone_avail.svcnum = svc_phone.svcnum )'
187   ) or die dbh->errstr;
188
189   $sth->execute or die $sth->errstr;
190
191 }
192
193 =back
194
195 =head1 BUGS
196
197 Sparse documentation.
198
199 =head1 SEE ALSO
200
201 L<FS::Record>, schema.html from the base documentation.
202
203 =cut
204
205 1;
206