This commit was generated by cvs2svn to compensate for changes in r10640,
[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_foreign_keyn('ordernum', 'did_order', 'ordernum' )
149     || $self->ut_textn('availbatch')
150     || $self->ut_textn('name')
151     || $self->ut_textn('rate_center_abbrev')
152   ;
153   return $error if $error;
154
155   $self->SUPER::check;
156 }
157
158 =item cust_svc
159
160 =cut
161
162 sub cust_svc {
163   my $self = shift;
164   return '' unless $self->svcnum;
165   qsearchs('cust_svc', { 'svcnum' => $self->svcnum });
166 }
167
168 =item part_export
169
170 =cut
171
172 sub part_export {
173   my $self = shift;
174   return '' unless $self->exportnum;
175   qsearchs('part_export', { 'exportnum' => $self->exportnum });
176 }
177
178
179 sub process_batch_import {
180   my $job = shift;
181
182   my $numsub = sub {
183     my( $phone_avail, $value ) = @_;
184     $value =~ s/\D//g;
185     $value =~ /^(\d{3})(\d{3})(\d+)$/ or die "unparsable number $value\n";
186     #( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
187     $phone_avail->npa($1);
188     $phone_avail->nxx($2);
189     $phone_avail->station($3);
190   };
191
192   my $opt = { 'table'   => 'phone_avail',
193               'params'  => [ 'availbatch', 'exportnum', 'countrycode', 'ordernum' ],
194               'formats' => { 'default' => [ 'state', $numsub, 'name' ],
195                              'bulk' => [ 'state', $numsub, 'name', 'rate_center_abbrev' ],
196                            },
197               'postinsert_callback' => sub {  
198                     my $record = shift;
199                     if($record->ordernum) {
200                         my $did_order = qsearchs('did_order', 
201                                                 { 'ordernum' => $record->ordernum } );
202                         if($did_order && !$did_order->received) {
203                             $did_order->received(time);
204                             $did_order->replace;
205                         }
206                     }
207                 }, 
208             };
209
210   FS::Record::process_batch_import( $job, $opt, @_ );
211
212 }
213
214 sub flush { # evil direct SQL
215     my $opt = shift;
216
217     if ( $opt->{'ratecenter'} =~ /^[\w\s]+$/
218             && $opt->{'state'} =~ /^[A-Z][A-Z]$/ 
219             && $opt->{'exportnum'} =~ /^\d+$/) {
220         my $sth = dbh->prepare('delete from phone_avail where exportnum = ? '.
221                     ' and state = ? and name = ?');
222         $sth->execute($opt->{'exportnum'},$opt->{'state'},$opt->{'ratecenter'})
223             or die $sth->errstr;
224     }
225
226     '';
227 }
228
229 # Used by FS::Upgrade to migrate to a new database.
230 sub _upgrade_data {
231   my ($class, %opts) = @_;
232
233   warn "$me upgrading $class\n" if $DEBUG;
234
235   my $sth = dbh->prepare(
236     'UPDATE phone_avail SET svcnum = NULL
237        WHERE svcnum IS NOT NULL
238          AND 0 = ( SELECT COUNT(*) FROM svc_phone
239                      WHERE phone_avail.svcnum = svc_phone.svcnum )'
240   ) or die dbh->errstr;
241
242   $sth->execute or die $sth->errstr;
243
244 }
245
246 =back
247
248 =head1 BUGS
249
250 Sparse documentation.
251
252 =head1 SEE ALSO
253
254 L<FS::Record>, schema.html from the base documentation.
255
256 =cut
257
258 1;
259