backup the schema for tables we don't need the data from. RT#85959
[freeside.git] / FS / FS / rate_region.pm
1 package FS::rate_region;
2 use base qw(FS::Record);
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::rate_prefix;
7 use FS::rate_detail;
8
9 =head1 NAME
10
11 FS::rate_region - Object methods for rate_region records
12
13 =head1 SYNOPSIS
14
15   use FS::rate_region;
16
17   $record = new FS::rate_region \%hash;
18   $record = new FS::rate_region { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
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:
32
33 =over 4
34
35 =item regionnum - primary key
36
37 =item regionname - name of the region
38
39 =item exact_match - 'Y' if "prefixes" in this region really represent 
40 complete phone numbers.  Null if they represent prefixes (the usual case).
41
42 =back
43
44 =head1 METHODS
45
46 =over 4
47
48 =item new HASHREF
49
50 Creates a new region.  To add the region to the database, see L<"insert">.
51
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.
54
55 =cut
56
57 # the new method can be inherited from FS::Record, if a table method is defined
58
59 sub table { 'rate_region'; }
60
61 =item insert [ , OPTION => VALUE ... ]
62
63 Adds this record to the database.  If there is an error, returns the error,
64 otherwise returns false.
65
66 Currently available options are: I<rate_prefix> and I<dest_detail>
67
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
70 record.
71
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
74 this record.
75
76
77 =cut
78
79 sub insert {
80   my $self = shift;
81   my %options = @_;
82
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';
89
90   my $oldAutoCommit = $FS::UID::AutoCommit;
91   local $FS::UID::AutoCommit = 0;
92   my $dbh = dbh;
93
94   my $error = $self->check;
95   return $error if $error;
96
97   $error = $self->SUPER::insert;
98   if ( $error ) {
99     $dbh->rollback if $oldAutoCommit;
100     return $error;
101   }
102
103   if ( $options{'rate_prefix'} ) {
104     foreach my $rate_prefix ( @{$options{'rate_prefix'}} ) {
105       $rate_prefix->regionnum($self->regionnum);
106       $error = $rate_prefix->insert;
107       if ( $error ) {
108         $dbh->rollback if $oldAutoCommit;
109         return $error;
110       }
111     }
112   }
113
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;
118       if ( $error ) {
119         $dbh->rollback if $oldAutoCommit;
120         return $error;
121       }
122     }
123   }
124
125   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
126
127   '';
128 }
129
130 =item delete
131
132 Delete this record from the database.
133
134 =cut
135
136 # the delete method can be inherited from FS::Record
137
138 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
139
140 Replaces the OLD_RECORD with this one in the database.  If there is an error,
141 returns the error, otherwise returns false.
142
143 Currently available options are: I<rate_prefix> and I<dest_detail>
144
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
148 deleted.
149
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
153 be deleted.
154
155 =cut
156
157 sub replace {
158   my ($new, $old) = (shift, shift);
159   my %options = @_;
160
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';
167
168   my $oldAutoCommit = $FS::UID::AutoCommit;
169   local $FS::UID::AutoCommit = 0;
170   my $dbh = dbh;
171
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'};
176
177   my $error = $new->SUPER::replace($old);
178   if ($error) {
179     $dbh->rollback if $oldAutoCommit;
180     return $error;
181   }
182
183   foreach my $old_rate_prefix ( @old_rate_prefix ) {
184     my $error = $old_rate_prefix->delete;
185     if ($error) {
186       $dbh->rollback if $oldAutoCommit;
187       return $error;
188     }
189   }
190   foreach my $old_dest_detail ( @old_dest_detail ) {
191     my $error = $old_dest_detail->delete;
192     if ($error) {
193       $dbh->rollback if $oldAutoCommit;
194       return $error;
195     }
196   }
197
198   foreach my $rate_prefix ( @{$options{'rate_prefix'}} ) {
199     $rate_prefix->regionnum($new->regionnum);
200     $error = $rate_prefix->insert;
201     if ( $error ) {
202       $dbh->rollback if $oldAutoCommit;
203       return $error;
204     }
205   }
206   foreach my $rate_detail ( @{$options{'dest_detail'}} ) {
207     $rate_detail->dest_regionnum($new->regionnum);
208     $error = $rate_detail->insert;
209     if ( $error ) {
210       $dbh->rollback if $oldAutoCommit;
211       return $error;
212     }
213   }
214
215   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
216   '';
217
218 }
219
220 =item check
221
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
224 and replace methods.
225
226 =cut
227
228 # the check method should currently be supplied - FS::Record contains some
229 # data checking routines
230
231 sub check {
232   my $self = shift;
233
234   my $error =
235        $self->ut_numbern('regionnum')
236     || $self->ut_text('regionname')
237     || $self->ut_flag('exact_match')
238   ;
239   return $error if $error;
240
241   $self->SUPER::check;
242 }
243
244 =item rate_prefix
245
246 Returns all prefixes (see L<FS::rate_prefix>) for this region.
247
248 =cut
249
250 sub rate_prefix {
251   my $self = shift;
252
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
257        }
258        qsearch( 'rate_prefix', { 'regionnum' => $self->regionnum } );
259 }
260
261 =item dest_detail
262
263 Returns all rate details (see L<FS::rate_detail>) for this region as a
264 destionation.
265
266 =cut
267
268 sub dest_detail {
269   my $self = shift;
270   qsearch( 'rate_detail', { 'dest_regionnum' => $self->regionnum } );
271 }
272
273 =item prefixes_short
274
275 Returns a string representing all the prefixes for this region.
276
277 =cut
278
279 sub prefixes_short {
280   my $self = shift;
281
282   my $countrycode = '';
283   my $out = '';
284
285   foreach my $rate_prefix ( $self->rate_prefix ) {
286     if ( $countrycode ne $rate_prefix->countrycode ) {
287       $out =~ s/, $//;
288       $countrycode = $rate_prefix->countrycode;
289       $out.= " +$countrycode ";
290     }
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;
296     } else {
297       $out .= $rate_prefix->npa;
298     }
299     $out .= '-'. $rate_prefix->nxx if $rate_prefix->nxx;
300     $out .= ', ';
301   }
302   $out =~ s/, $//;
303
304   $out;
305 }
306
307 sub prefixes_short_sql {
308   my $self = shift;
309
310   my $countrycode = '';
311   my $out = '';
312
313   foreach my $prefix (sort split(',', $self->prefixes)) {
314     my($cc, $npa) = split(' ', $prefix);
315
316     if ( $countrycode ne $cc ) {
317       $out =~ s/, $//;
318       $countrycode = $cc;
319       $out.= " +$countrycode ";
320     }
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;
325     } else {
326       $out .= $npa;
327     }
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;
330     $out .= ', ';
331   }
332   $out =~ s/, $//;
333
334   $out;
335 }
336
337 =back
338
339 =head1 BUGS
340
341 =head1 SEE ALSO
342
343 L<FS::Record>, schema.html from the base documentation.
344
345 =cut
346
347 1;
348