5 use FS::Record qw( qsearch qsearchs dbh );
12 FS::rate - Object methods for rate records
18 $record = new FS::rate \%hash;
19 $record = new FS::rate { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
31 An FS::rate object represents an rate plan. FS::rate inherits from
32 FS::Record. The following fields are currently supported:
36 =item ratenum - primary key
48 Creates a new rate plan. To add the rate plan to the database, see L<"insert">.
50 Note that this stores the hash reference, not a distinct copy of the hash it
51 points to. You can ask the object for a copy with the I<hash> method.
55 # the new method can be inherited from FS::Record, if a table method is defined
59 =item insert [ , OPTION => VALUE ... ]
61 Adds this record to the database. If there is an error, returns the error,
62 otherwise returns false.
64 Currently available options are: I<rate_detail>
66 If I<rate_detail> is set to an array reference of FS::rate_detail objects, the
67 objects will have their ratenum field set and will be inserted after this
76 local $SIG{HUP} = 'IGNORE';
77 local $SIG{INT} = 'IGNORE';
78 local $SIG{QUIT} = 'IGNORE';
79 local $SIG{TERM} = 'IGNORE';
80 local $SIG{TSTP} = 'IGNORE';
81 local $SIG{PIPE} = 'IGNORE';
83 my $oldAutoCommit = $FS::UID::AutoCommit;
84 local $FS::UID::AutoCommit = 0;
87 my $error = $self->check;
88 return $error if $error;
90 $error = $self->SUPER::insert;
92 $dbh->rollback if $oldAutoCommit;
96 if ( $options{'rate_detail'} ) {
97 foreach my $rate_detail ( @{$options{'rate_detail'}} ) {
98 $rate_detail->ratenum($self->ratenum);
99 $error = $rate_detail->insert;
101 $dbh->rollback if $oldAutoCommit;
107 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
116 Delete this record from the database.
120 # the delete method can be inherited from FS::Record
122 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
124 Replaces the OLD_RECORD with this one in the database. If there is an error,
125 returns the error, otherwise returns false.
127 Currently available options are: I<rate_detail>
129 If I<rate_detail> is set to an array reference of FS::rate_detail objects, the
130 objects will have their ratenum field set and will be inserted after this
131 record. Any existing rate_detail records associated with this record will be
137 my ($new, $old) = (shift, shift);
140 local $SIG{HUP} = 'IGNORE';
141 local $SIG{INT} = 'IGNORE';
142 local $SIG{QUIT} = 'IGNORE';
143 local $SIG{TERM} = 'IGNORE';
144 local $SIG{TSTP} = 'IGNORE';
145 local $SIG{PIPE} = 'IGNORE';
147 my $oldAutoCommit = $FS::UID::AutoCommit;
148 local $FS::UID::AutoCommit = 0;
151 my @old_rate_detail = ();
152 @old_rate_detail = $old->rate_detail if $options{'rate_detail'};
154 my $error = $new->SUPER::replace($old);
156 $dbh->rollback if $oldAutoCommit;
160 foreach my $old_rate_detail ( @old_rate_detail ) {
161 my $error = $old_rate_detail->delete;
163 $dbh->rollback if $oldAutoCommit;
168 foreach my $rate_detail ( @{$options{'rate_detail'}} ) {
169 $rate_detail->ratenum($new->ratenum);
170 $error = $rate_detail->insert;
172 $dbh->rollback if $oldAutoCommit;
177 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
184 Checks all fields to make sure this is a valid rate plan. If there is
185 an error, returns the error, otherwise returns false. Called by the insert
190 # the check method should currently be supplied - FS::Record contains some
191 # data checking routines
197 $self->ut_numbern('ratenum')
198 || $self->ut_text('ratename')
200 return $error if $error;
205 =item dest_detail REGIONNUM | RATE_REGION_OBJECTD
207 Returns the rate detail (see L<FS::rate_detail>) for this rate to the
208 specificed destination.
214 my $regionnum = ref($_[0]) ? shift->regionnum : shift;
215 qsearchs( 'rate_detail', { 'ratenum' => $self->ratenum,
216 'dest_regionnum' => $regionnum, } );
221 Returns all region-specific details (see L<FS::rate_detail>) for this rate.
227 qsearch( 'rate_detail', { 'ratenum' => $self->ratenum } );
237 L<FS::Record>, schema.html from the base documentation.