1 package FS::rate_region;
2 use base qw(FS::Record);
5 use FS::Record qw( qsearch qsearchs dbh );
11 FS::rate_region - Object methods for rate_region records
17 $record = new FS::rate_region \%hash;
18 $record = new FS::rate_region { 'column' => 'value' };
20 $error = $record->insert;
22 $error = $new_record->replace($old_record);
24 $error = $record->delete;
26 $error = $record->check;
30 An FS::rate_region object represents an call rating region. FS::rate_region
31 inherits from FS::Record. The following fields are currently supported:
35 =item regionnum - primary key
37 =item regionname - name of the region
39 =item exact_match - 'Y' if "prefixes" in this region really represent
40 complete phone numbers. Null if they represent prefixes (the usual case).
50 Creates a new region. To add the region to the database, see L<"insert">.
52 Note that this stores the hash reference, not a distinct copy of the hash it
53 points to. You can ask the object for a copy with the I<hash> method.
57 # the new method can be inherited from FS::Record, if a table method is defined
59 sub table { 'rate_region'; }
61 =item insert [ , OPTION => VALUE ... ]
63 Adds this record to the database. If there is an error, returns the error,
64 otherwise returns false.
66 Currently available options are: I<rate_prefix> and I<dest_detail>
68 If I<rate_prefix> is set to an array reference of FS::rate_prefix objects, the
69 objects will have their regionnum field set and will be inserted after this
72 If I<dest_detail> is set to an array reference of FS::rate_detail objects, the
73 objects will have their dest_regionnum field set and will be inserted after
83 local $SIG{HUP} = 'IGNORE';
84 local $SIG{INT} = 'IGNORE';
85 local $SIG{QUIT} = 'IGNORE';
86 local $SIG{TERM} = 'IGNORE';
87 local $SIG{TSTP} = 'IGNORE';
88 local $SIG{PIPE} = 'IGNORE';
90 my $oldAutoCommit = $FS::UID::AutoCommit;
91 local $FS::UID::AutoCommit = 0;
94 my $error = $self->check;
95 return $error if $error;
97 $error = $self->SUPER::insert;
99 $dbh->rollback if $oldAutoCommit;
103 if ( $options{'rate_prefix'} ) {
104 foreach my $rate_prefix ( @{$options{'rate_prefix'}} ) {
105 $rate_prefix->regionnum($self->regionnum);
106 $error = $rate_prefix->insert;
108 $dbh->rollback if $oldAutoCommit;
114 if ( $options{'dest_detail'} ) {
115 foreach my $rate_detail ( @{$options{'dest_detail'}} ) {
116 $rate_detail->dest_regionnum($self->regionnum);
117 $error = $rate_detail->insert;
119 $dbh->rollback if $oldAutoCommit;
125 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
132 Delete this record from the database.
136 # the delete method can be inherited from FS::Record
138 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
140 Replaces the OLD_RECORD with this one in the database. If there is an error,
141 returns the error, otherwise returns false.
143 Currently available options are: I<rate_prefix> and I<dest_detail>
145 If I<rate_prefix> is set to an array reference of FS::rate_prefix objects, the
146 objects will have their regionnum field set and will be inserted after this
147 record. Any existing rate_prefix records associated with this record will be
150 If I<dest_detail> is set to an array reference of FS::rate_detail objects, the
151 objects will have their dest_regionnum field set and will be inserted after
152 this record. Any existing rate_detail records associated with this record will
158 my ($new, $old) = (shift, shift);
161 local $SIG{HUP} = 'IGNORE';
162 local $SIG{INT} = 'IGNORE';
163 local $SIG{QUIT} = 'IGNORE';
164 local $SIG{TERM} = 'IGNORE';
165 local $SIG{TSTP} = 'IGNORE';
166 local $SIG{PIPE} = 'IGNORE';
168 my $oldAutoCommit = $FS::UID::AutoCommit;
169 local $FS::UID::AutoCommit = 0;
172 my @old_rate_prefix = ();
173 @old_rate_prefix = $old->rate_prefix if $options{'rate_prefix'};
174 my @old_dest_detail = ();
175 @old_dest_detail = $old->dest_detail if $options{'dest_detail'};
177 my $error = $new->SUPER::replace($old);
179 $dbh->rollback if $oldAutoCommit;
183 foreach my $old_rate_prefix ( @old_rate_prefix ) {
184 my $error = $old_rate_prefix->delete;
186 $dbh->rollback if $oldAutoCommit;
190 foreach my $old_dest_detail ( @old_dest_detail ) {
191 my $error = $old_dest_detail->delete;
193 $dbh->rollback if $oldAutoCommit;
198 foreach my $rate_prefix ( @{$options{'rate_prefix'}} ) {
199 $rate_prefix->regionnum($new->regionnum);
200 $error = $rate_prefix->insert;
202 $dbh->rollback if $oldAutoCommit;
206 foreach my $rate_detail ( @{$options{'dest_detail'}} ) {
207 $rate_detail->dest_regionnum($new->regionnum);
208 $error = $rate_detail->insert;
210 $dbh->rollback if $oldAutoCommit;
215 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
222 Checks all fields to make sure this is a valid region. If there is
223 an error, returns the error, otherwise returns false. Called by the insert
228 # the check method should currently be supplied - FS::Record contains some
229 # data checking routines
235 $self->ut_numbern('regionnum')
236 || $self->ut_text('regionname')
237 || $self->ut_flag('exact_match')
239 return $error if $error;
246 Returns all prefixes (see L<FS::rate_prefix>) for this region.
253 map { $_ } #return $self->num_rate_prefix unless wantarray;
254 sort { $a->countrycode cmp $b->countrycode
255 or $a->npa cmp $b->npa
256 or $a->nxx cmp $b->nxx
258 qsearch( 'rate_prefix', { 'regionnum' => $self->regionnum } );
263 Returns all rate details (see L<FS::rate_detail>) for this region as a
270 qsearch( 'rate_detail', { 'dest_regionnum' => $self->regionnum } );
275 Returns a string representing all the prefixes for this region.
282 my $countrycode = '';
285 foreach my $rate_prefix ( $self->rate_prefix ) {
286 if ( $countrycode ne $rate_prefix->countrycode ) {
288 $countrycode = $rate_prefix->countrycode;
289 $out.= " +$countrycode ";
291 my $npa = $rate_prefix->npa;
292 if ( $countrycode eq '1' ) {
293 #$out .= '('. substr( $npa, 0, 3 ). ')';
294 $out .= substr( $npa, 0, 3 );
295 $out .= ' '. substr( $npa, 3 ) if length($npa) > 3;
297 $out .= $rate_prefix->npa;
299 $out .= '-'. $rate_prefix->nxx if $rate_prefix->nxx;
307 sub prefixes_short_sql {
310 my $countrycode = '';
313 foreach my $prefix (sort split(',', $self->prefixes)) {
314 my($cc, $npa) = split(' ', $prefix);
316 if ( $countrycode ne $cc ) {
319 $out.= " +$countrycode ";
321 if ( $countrycode eq '1' ) {
322 #$out .= '('. substr( $npa, 0, 3 ). ')';
323 $out .= substr( $npa, 0, 3 );
324 $out .= ' '. substr( $npa, 3 ) if length($npa) > 3;
328 #XXX have to implement this here too if we ever actually used the nxx field
329 # $out .= '-'. $rate_prefix->nxx if $rate_prefix->nxx;
343 L<FS::Record>, schema.html from the base documentation.