4 use vars qw( @ISA $DEBUG );
7 use FS::Record qw( qsearch qsearchs dbh fields );
10 @ISA = qw(FS::Record);
16 FS::rate - Object methods for rate records
22 $record = new FS::rate \%hash;
23 $record = new FS::rate { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::rate object represents an rate plan. FS::rate inherits from
36 FS::Record. The following fields are currently supported:
40 =item ratenum - primary key
52 Creates a new rate plan. To add the rate plan 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
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_detail>
70 If I<rate_detail> is set to an array reference of FS::rate_detail objects, the
71 objects will have their ratenum field set and will be inserted after this
80 local $SIG{HUP} = 'IGNORE';
81 local $SIG{INT} = 'IGNORE';
82 local $SIG{QUIT} = 'IGNORE';
83 local $SIG{TERM} = 'IGNORE';
84 local $SIG{TSTP} = 'IGNORE';
85 local $SIG{PIPE} = 'IGNORE';
87 my $oldAutoCommit = $FS::UID::AutoCommit;
88 local $FS::UID::AutoCommit = 0;
91 my $error = $self->check;
92 return $error if $error;
94 $error = $self->SUPER::insert;
96 $dbh->rollback if $oldAutoCommit;
100 if ( $options{'rate_detail'} ) {
102 my( $num, $last, $min_sec ) = (0, time, 5); #progressbar foo
104 foreach my $rate_detail ( @{$options{'rate_detail'}} ) {
106 $rate_detail->ratenum($self->ratenum);
107 $error = $rate_detail->insert;
109 $dbh->rollback if $oldAutoCommit;
113 if ( $options{'job'} ) {
115 if ( time - $min_sec > $last ) {
116 my $error = $options{'job'}->update_statustext(
117 int( 100 * $num / scalar( @{$options{'rate_detail'}} ) )
120 $dbh->rollback if $oldAutoCommit;
130 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
139 Delete this record from the database.
143 # the delete method can be inherited from FS::Record
145 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
147 Replaces the OLD_RECORD with this one in the database. If there is an error,
148 returns the error, otherwise returns false.
150 Currently available options are: I<rate_detail>
152 If I<rate_detail> is set to an array reference of FS::rate_detail objects, the
153 objects will have their ratenum field set and will be inserted after this
154 record. Any existing rate_detail records associated with this record will be
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_detail = ();
175 # @old_rate_detail = $old->rate_detail if $options{'rate_detail'};
177 my $error = $new->SUPER::replace($old);
179 $dbh->rollback if $oldAutoCommit;
183 # foreach my $old_rate_detail ( @old_rate_detail ) {
185 # my $error = $old_rate_detail->delete;
187 # $dbh->rollback if $oldAutoCommit;
191 # if ( $options{'job'} ) {
193 # if ( time - $min_sec > $last ) {
194 # my $error = $options{'job'}->update_statustext(
195 # int( 50 * $num / scalar( @old_rate_detail ) )
198 # $dbh->rollback if $oldAutoCommit;
206 if ( $options{'rate_detail'} ) {
207 my $sth = $dbh->prepare('DELETE FROM rate_detail WHERE ratenum = ?') or do {
208 $dbh->rollback if $oldAutoCommit;
212 $sth->execute($old->ratenum) or do {
213 $dbh->rollback if $oldAutoCommit;
217 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
219 foreach my $rate_detail ( @{$options{'rate_detail'}} ) {
221 $rate_detail->ratenum($new->ratenum);
222 $error = $rate_detail->insert;
224 $dbh->rollback if $oldAutoCommit;
228 if ( $options{'job'} ) {
230 if ( time - $min_sec > $last ) {
231 my $error = $options{'job'}->update_statustext(
232 int( 100 * $num / scalar( @{$options{'rate_detail'}} ) )
235 $dbh->rollback if $oldAutoCommit;
246 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
253 Checks all fields to make sure this is a valid rate plan. If there is
254 an error, returns the error, otherwise returns false. Called by the insert
259 # the check method should currently be supplied - FS::Record contains some
260 # data checking routines
266 $self->ut_numbern('ratenum')
267 || $self->ut_text('ratename')
269 return $error if $error;
274 =item dest_detail REGIONNUM | RATE_REGION_OBJECTD
276 Returns the rate detail (see L<FS::rate_detail>) for this rate to the
277 specificed destination.
283 my $regionnum = ref($_[0]) ? shift->regionnum : shift;
284 qsearchs( 'rate_detail', { 'ratenum' => $self->ratenum,
285 'dest_regionnum' => $regionnum, } );
290 Returns all region-specific details (see L<FS::rate_detail>) for this rate.
296 qsearch( 'rate_detail', { 'ratenum' => $self->ratenum } );
308 Experimental job-queue processor for web interface adds/edits
319 #my %param = split(/[;=]/, $param);
321 my $param = thaw(decode_base64(shift));
322 warn Dumper($param) if $DEBUG;
324 my $old = qsearchs('rate', { 'ratenum' => $param->{'ratenum'} } )
325 if $param->{'ratenum'};
327 my @rate_detail = map {
329 my $regionnum = $_->regionnum;
330 if ( $param->{"sec_granularity$regionnum"} ) {
332 new FS::rate_detail {
333 'dest_regionnum' => $regionnum,
334 map { $_ => $param->{"$_$regionnum"} }
335 qw( min_included min_charge sec_granularity )
340 new FS::rate_detail {
341 'dest_regionnum' => $regionnum,
344 'sec_granularity' => '60'
349 } qsearch('rate_region', {} );
351 my $rate = new FS::rate {
352 map { $_ => $param->{$_} }
357 if ( $param->{'ratenum'} ) {
358 warn "$rate replacing $old (". $param->{'ratenum'}. ")\n" if $DEBUG;
359 $error = $rate->replace( $old,
360 'rate_detail' => \@rate_detail,
364 warn "inserting $rate\n" if $DEBUG;
365 $error = $rate->insert( 'rate_detail' => \@rate_detail,
368 #$ratenum = $rate->getfield('ratenum');
371 die $error if $error;
375 # begin JSRPC code...
377 package FS::rate::JSRPC;
378 use vars qw(@ISA $DEBUG);
379 use JavaScript::RPC::Server::CGI;
381 @ISA = qw( JavaScript::RPC::Server::CGI );
388 warn "FS::rate::JSRPC::process_rate\n".
389 join('', map " $_ => $param{$_}\n", keys %param )
392 #progressbar prototype code... should be generalized
394 #first get the CGI params shipped off to a job ASAP so an id can be returned
397 my $job = new FS::queue { 'job' => 'FS::rate::process' };
399 #too slow to insert all the cgi params as individual args..,?
400 #my $error = $queue->insert('_JOB', $cgi->Vars);
402 #my $bigstring = join(';', map { "$_=". scalar($cgi->param($_)) } $cgi->param );
403 my $bigstring = join(';', map { "$_=". $param{$_} } keys %param );
404 my $error = $job->insert('_JOB', $bigstring);
424 L<FS::Record>, schema.html from the base documentation.