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