1 package FS::rate_region;
5 use FS::Record qw( qsearch qsearchs dbh );
13 FS::rate_region - Object methods for rate_region records
19 $record = new FS::rate_region \%hash;
20 $record = new FS::rate_region { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::rate_region object represents an call rating region. FS::rate_region
33 inherits from FS::Record. The following fields are currently supported:
37 =item regionnum - primary key
39 =item regionname - name of the region
41 =item exact_match - 'Y' if "prefixes" in this region really represent
42 complete phone numbers. Null if they represent prefixes (the usual case).
52 Creates a new region. To add the region to the database, see L<"insert">.
54 Note that this stores the hash reference, not a distinct copy of the hash it
55 points to. You can ask the object for a copy with the I<hash> method.
59 # the new method can be inherited from FS::Record, if a table method is defined
61 sub table { 'rate_region'; }
63 =item insert [ , OPTION => VALUE ... ]
65 Adds this record to the database. If there is an error, returns the error,
66 otherwise returns false.
68 Currently available options are: I<rate_prefix> and I<dest_detail>
70 If I<rate_prefix> is set to an array reference of FS::rate_prefix objects, the
71 objects will have their regionnum field set and will be inserted after this
74 If I<dest_detail> is set to an array reference of FS::rate_detail objects, the
75 objects will have their dest_regionnum field set and will be inserted after
85 local $SIG{HUP} = 'IGNORE';
86 local $SIG{INT} = 'IGNORE';
87 local $SIG{QUIT} = 'IGNORE';
88 local $SIG{TERM} = 'IGNORE';
89 local $SIG{TSTP} = 'IGNORE';
90 local $SIG{PIPE} = 'IGNORE';
92 my $oldAutoCommit = $FS::UID::AutoCommit;
93 local $FS::UID::AutoCommit = 0;
96 my $error = $self->check;
97 return $error if $error;
99 $error = $self->SUPER::insert;
101 $dbh->rollback if $oldAutoCommit;
105 if ( $options{'rate_prefix'} ) {
106 foreach my $rate_prefix ( @{$options{'rate_prefix'}} ) {
107 $rate_prefix->regionnum($self->regionnum);
108 $error = $rate_prefix->insert;
110 $dbh->rollback if $oldAutoCommit;
116 if ( $options{'dest_detail'} ) {
117 foreach my $rate_detail ( @{$options{'dest_detail'}} ) {
118 $rate_detail->dest_regionnum($self->regionnum);
119 $error = $rate_detail->insert;
121 $dbh->rollback if $oldAutoCommit;
127 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
134 Delete this record from the database.
138 # the delete method can be inherited from FS::Record
140 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
142 Replaces the OLD_RECORD with this one in the database. If there is an error,
143 returns the error, otherwise returns false.
145 Currently available options are: I<rate_prefix> and I<dest_detail>
147 If I<rate_prefix> is set to an array reference of FS::rate_prefix objects, the
148 objects will have their regionnum field set and will be inserted after this
149 record. Any existing rate_prefix records associated with this record will be
152 If I<dest_detail> is set to an array reference of FS::rate_detail objects, the
153 objects will have their dest_regionnum field set and will be inserted after
154 this record. Any existing rate_detail records associated with this record will
160 my ($new, $old) = (shift, shift);
163 local $SIG{HUP} = 'IGNORE';
164 local $SIG{INT} = 'IGNORE';
165 local $SIG{QUIT} = 'IGNORE';
166 local $SIG{TERM} = 'IGNORE';
167 local $SIG{TSTP} = 'IGNORE';
168 local $SIG{PIPE} = 'IGNORE';
170 my $oldAutoCommit = $FS::UID::AutoCommit;
171 local $FS::UID::AutoCommit = 0;
174 my @old_rate_prefix = ();
175 @old_rate_prefix = $old->rate_prefix if $options{'rate_prefix'};
176 my @old_dest_detail = ();
177 @old_dest_detail = $old->dest_detail if $options{'dest_detail'};
179 my $error = $new->SUPER::replace($old);
181 $dbh->rollback if $oldAutoCommit;
185 foreach my $old_rate_prefix ( @old_rate_prefix ) {
186 my $error = $old_rate_prefix->delete;
188 $dbh->rollback if $oldAutoCommit;
192 foreach my $old_dest_detail ( @old_dest_detail ) {
193 my $error = $old_dest_detail->delete;
195 $dbh->rollback if $oldAutoCommit;
200 foreach my $rate_prefix ( @{$options{'rate_prefix'}} ) {
201 $rate_prefix->regionnum($new->regionnum);
202 $error = $rate_prefix->insert;
204 $dbh->rollback if $oldAutoCommit;
208 foreach my $rate_detail ( @{$options{'dest_detail'}} ) {
209 $rate_detail->dest_regionnum($new->regionnum);
210 $error = $rate_detail->insert;
212 $dbh->rollback if $oldAutoCommit;
217 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
224 Checks all fields to make sure this is a valid region. If there is
225 an error, returns the error, otherwise returns false. Called by the insert
230 # the check method should currently be supplied - FS::Record contains some
231 # data checking routines
237 $self->ut_numbern('regionnum')
238 || $self->ut_text('regionname')
239 || $self->ut_flag('exact_match')
241 return $error if $error;
248 Returns all prefixes (see L<FS::rate_prefix>) for this region.
255 map { $_ } #return $self->num_rate_prefix unless wantarray;
256 sort { $a->countrycode cmp $b->countrycode
257 or $a->npa cmp $b->npa
258 or $a->nxx cmp $b->nxx
260 qsearch( 'rate_prefix', { 'regionnum' => $self->regionnum } );
265 Returns all rate details (see L<FS::rate_detail>) for this region as a
272 qsearch( 'rate_detail', { 'dest_regionnum' => $self->regionnum } );
277 Returns a string representing all the prefixes for this region.
284 my $countrycode = '';
287 foreach my $rate_prefix ( $self->rate_prefix ) {
288 if ( $countrycode ne $rate_prefix->countrycode ) {
290 $countrycode = $rate_prefix->countrycode;
291 $out.= " +$countrycode ";
293 my $npa = $rate_prefix->npa;
294 if ( $countrycode eq '1' ) {
295 #$out .= '('. substr( $npa, 0, 3 ). ')';
296 $out .= substr( $npa, 0, 3 );
297 $out .= ' '. substr( $npa, 3 ) if length($npa) > 3;
299 $out .= $rate_prefix->npa;
301 $out .= '-'. $rate_prefix->nxx if $rate_prefix->nxx;
315 L<FS::Record>, schema.html from the base documentation.