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