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
49 Creates a new region. To add the region to the database, see L<"insert">.
51 Note that this stores the hash reference, not a distinct copy of the hash it
52 points to. You can ask the object for a copy with the I<hash> method.
56 # the new method can be inherited from FS::Record, if a table method is defined
58 sub table { 'rate_region'; }
60 =item insert [ , OPTION => VALUE ... ]
62 Adds this record to the database. If there is an error, returns the error,
63 otherwise returns false.
65 Currently available options are: I<rate_prefix> and I<dest_detail>
67 If I<rate_prefix> is set to an array reference of FS::rate_prefix objects, the
68 objects will have their regionnum field set and will be inserted after this
71 If I<dest_detail> is set to an array reference of FS::rate_detail objects, the
72 objects will have their dest_regionnum field set and will be inserted after
82 local $SIG{HUP} = 'IGNORE';
83 local $SIG{INT} = 'IGNORE';
84 local $SIG{QUIT} = 'IGNORE';
85 local $SIG{TERM} = 'IGNORE';
86 local $SIG{TSTP} = 'IGNORE';
87 local $SIG{PIPE} = 'IGNORE';
89 my $oldAutoCommit = $FS::UID::AutoCommit;
90 local $FS::UID::AutoCommit = 0;
93 my $error = $self->check;
94 return $error if $error;
96 $error = $self->SUPER::insert;
98 $dbh->rollback if $oldAutoCommit;
102 if ( $options{'rate_prefix'} ) {
103 foreach my $rate_prefix ( @{$options{'rate_prefix'}} ) {
104 $rate_prefix->regionnum($self->regionnum);
105 $error = $rate_prefix->insert;
107 $dbh->rollback if $oldAutoCommit;
113 if ( $options{'dest_detail'} ) {
114 foreach my $rate_detail ( @{$options{'dest_detail'}} ) {
115 $rate_detail->dest_regionnum($self->regionnum);
116 $error = $rate_detail->insert;
118 $dbh->rollback if $oldAutoCommit;
124 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
131 Delete this record from the database.
135 # the delete method can be inherited from FS::Record
137 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
139 Replaces the OLD_RECORD with this one in the database. If there is an error,
140 returns the error, otherwise returns false.
142 Currently available options are: I<rate_prefix> and I<dest_detail>
144 If I<rate_prefix> is set to an array reference of FS::rate_prefix objects, the
145 objects will have their regionnum field set and will be inserted after this
146 record. Any existing rate_prefix records associated with this record will be
149 If I<dest_detail> is set to an array reference of FS::rate_detail objects, the
150 objects will have their dest_regionnum field set and will be inserted after
151 this record. Any existing rate_detail records associated with this record will
157 my ($new, $old) = (shift, shift);
160 local $SIG{HUP} = 'IGNORE';
161 local $SIG{INT} = 'IGNORE';
162 local $SIG{QUIT} = 'IGNORE';
163 local $SIG{TERM} = 'IGNORE';
164 local $SIG{TSTP} = 'IGNORE';
165 local $SIG{PIPE} = 'IGNORE';
167 my $oldAutoCommit = $FS::UID::AutoCommit;
168 local $FS::UID::AutoCommit = 0;
171 my @old_rate_prefix = ();
172 @old_rate_prefix = $old->rate_prefix if $options{'rate_prefix'};
173 my @old_dest_detail = ();
174 @old_dest_detail = $old->dest_detail if $options{'dest_detail'};
176 my $error = $new->SUPER::replace($old);
178 $dbh->rollback if $oldAutoCommit;
182 foreach my $old_rate_prefix ( @old_rate_prefix ) {
183 my $error = $old_rate_prefix->delete;
185 $dbh->rollback if $oldAutoCommit;
189 foreach my $old_dest_detail ( @old_dest_detail ) {
190 my $error = $old_dest_detail->delete;
192 $dbh->rollback if $oldAutoCommit;
197 foreach my $rate_prefix ( @{$options{'rate_prefix'}} ) {
198 $rate_prefix->regionnum($new->regionnum);
199 $error = $rate_prefix->insert;
201 $dbh->rollback if $oldAutoCommit;
205 foreach my $rate_detail ( @{$options{'dest_detail'}} ) {
206 $rate_detail->dest_regionnum($new->regionnum);
207 $error = $rate_detail->insert;
209 $dbh->rollback if $oldAutoCommit;
214 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
221 Checks all fields to make sure this is a valid region. If there is
222 an error, returns the error, otherwise returns false. Called by the insert
227 # the check method should currently be supplied - FS::Record contains some
228 # data checking routines
234 $self->ut_numbern('regionnum')
235 || $self->ut_text('regionname')
237 return $error if $error;
244 Returns all prefixes (see L<FS::rate_prefix>) for this region.
251 sort { $a->countrycode cmp $b->countrycode
252 or $a->npa cmp $b->npa
253 or $a->nxx cmp $b->nxx
255 qsearch( 'rate_prefix', { 'regionnum' => $self->regionnum } );
260 Returns all rate details (see L<FS::rate_detail>) for this region as a
267 qsearch( 'rate_detail', { 'dest_regionnum' => $self->regionnum, } );
272 Returns a string representing all the prefixes for this region.
279 my $countrycode = '';
282 foreach my $rate_prefix ( $self->rate_prefix ) {
283 if ( $countrycode ne $rate_prefix->countrycode ) {
285 $countrycode = $rate_prefix->countrycode;
286 $out.= " +$countrycode ";
288 my $npa = $rate_prefix->npa;
289 if ( $countrycode eq '1' ) {
290 $out .= '('. substr( $npa, 0, 3 ). ')';
291 $out .= ' '. substr( $npa, 3 ) if length($npa) > 3;
293 $out .= $rate_prefix->npa;
308 L<FS::Record>, schema.html from the base documentation.