4 use vars qw( @ISA $DEBUG $me $ignore_quantity );
6 #use Scalar::Util qw( blessed );
8 use FS::Record qw( qsearch qsearchs dbh str2time_sql );
13 use FS::domain_record;
17 #most FS::svc_ classes are autoloaded in svc_x emthod
18 use FS::svc_acct; #this one is used in the cache stuff
20 @ISA = qw( FS::cust_main_Mixin FS::option_Common ); #FS::Record );
29 my ( $hashref, $cache ) = @_;
30 if ( $hashref->{'username'} ) {
31 $self->{'_svc_acct'} = FS::svc_acct->new($hashref, '');
33 if ( $hashref->{'svc'} ) {
34 $self->{'_svcpart'} = FS::part_svc->new($hashref);
40 FS::cust_svc - Object method for cust_svc objects
46 $record = new FS::cust_svc \%hash
47 $record = new FS::cust_svc { 'column' => 'value' };
49 $error = $record->insert;
51 $error = $new_record->replace($old_record);
53 $error = $record->delete;
55 $error = $record->check;
57 ($label, $value) = $record->label;
61 An FS::cust_svc represents a service. FS::cust_svc inherits from FS::Record.
62 The following fields are currently supported:
66 =item svcnum - primary key (assigned automatically for new services)
68 =item pkgnum - Package (see L<FS::cust_pkg>)
70 =item svcpart - Service definition (see L<FS::part_svc>)
72 =item overlimit - date the service exceeded its usage limit
82 Creates a new service. To add the refund to the database, see L<"insert">.
83 Services are normally created by creating FS::svc_ objects (see
84 L<FS::svc_acct>, L<FS::svc_domain>, and L<FS::svc_forward>, among others).
88 sub table { 'cust_svc'; }
92 Adds this service to the database. If there is an error, returns the error,
93 otherwise returns false.
97 Deletes this service from the database. If there is an error, returns the
98 error, otherwise returns false. Note that this only removes the cust_svc
99 record - you should probably use the B<cancel> method instead.
103 Cancels the relevant service by calling the B<cancel> method of the associated
104 FS::svc_XXX object (i.e. an FS::svc_acct object or FS::svc_domain object),
105 deleting the FS::svc_XXX record and then deleting this record.
107 If there is an error, returns the error, otherwise returns false.
114 local $SIG{HUP} = 'IGNORE';
115 local $SIG{INT} = 'IGNORE';
116 local $SIG{QUIT} = 'IGNORE';
117 local $SIG{TERM} = 'IGNORE';
118 local $SIG{TSTP} = 'IGNORE';
119 local $SIG{PIPE} = 'IGNORE';
121 my $oldAutoCommit = $FS::UID::AutoCommit;
122 local $FS::UID::AutoCommit = 0;
125 my $part_svc = $self->part_svc;
127 $part_svc->svcdb =~ /^([\w\-]+)$/ or do {
128 $dbh->rollback if $oldAutoCommit;
129 return "Illegal svcdb value in part_svc!";
132 require "FS/$svcdb.pm";
134 my $svc = $self->svc_x;
137 my $error = $svc->cancel;
139 $dbh->rollback if $oldAutoCommit;
140 return "Error canceling service: $error";
142 $error = $svc->delete; #this deletes this cust_svc record as well
144 $dbh->rollback if $oldAutoCommit;
145 return "Error deleting service: $error";
151 warn "WARNING: no svc_ record found for svcnum ". $self->svcnum.
152 "; deleting cust_svc only\n";
154 my $error = $self->delete;
156 $dbh->rollback if $oldAutoCommit;
157 return "Error deleting cust_svc: $error";
162 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
168 =item overlimit [ ACTION ]
170 Retrieves or sets the overlimit date. If ACTION is absent, return
171 the present value of overlimit. If ACTION is present, it can
172 have the value 'suspend' or 'unsuspend'. In the case of 'suspend' overlimit
173 is set to the current time if it is not already set. The 'unsuspend' value
174 causes the time to be cleared.
176 If there is an error on setting, returns the error, otherwise returns false.
182 my $action = shift or return $self->getfield('overlimit');
184 local $SIG{HUP} = 'IGNORE';
185 local $SIG{INT} = 'IGNORE';
186 local $SIG{QUIT} = 'IGNORE';
187 local $SIG{TERM} = 'IGNORE';
188 local $SIG{TSTP} = 'IGNORE';
189 local $SIG{PIPE} = 'IGNORE';
191 my $oldAutoCommit = $FS::UID::AutoCommit;
192 local $FS::UID::AutoCommit = 0;
195 if ( $action eq 'suspend' ) {
196 $self->setfield('overlimit', time) unless $self->getfield('overlimit');
197 }elsif ( $action eq 'unsuspend' ) {
198 $self->setfield('overlimit', '');
200 die "unexpected action value: $action";
203 local $ignore_quantity = 1;
204 my $error = $self->replace;
206 $dbh->rollback if $oldAutoCommit;
207 return "Error setting overlimit: $error";
210 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
216 =item replace OLD_RECORD
218 Replaces the OLD_RECORD with this one in the database. If there is an error,
219 returns the error, otherwise returns false.
226 # my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
228 # : $new->replace_old;
229 my ( $new, $old ) = ( shift, shift );
230 $old = $new->replace_old unless defined($old);
232 local $SIG{HUP} = 'IGNORE';
233 local $SIG{INT} = 'IGNORE';
234 local $SIG{QUIT} = 'IGNORE';
235 local $SIG{TERM} = 'IGNORE';
236 local $SIG{TSTP} = 'IGNORE';
237 local $SIG{PIPE} = 'IGNORE';
239 my $oldAutoCommit = $FS::UID::AutoCommit;
240 local $FS::UID::AutoCommit = 0;
243 if ( $new->svcpart != $old->svcpart ) {
244 my $svc_x = $new->svc_x;
245 my $new_svc_x = ref($svc_x)->new({$svc_x->hash, svcpart=>$new->svcpart });
246 local($FS::Record::nowarn_identical) = 1;
247 my $error = $new_svc_x->replace($svc_x);
249 $dbh->rollback if $oldAutoCommit;
250 return $error if $error;
254 # #trigger a re-export on pkgnum changes?
255 # # (of prepaid packages), for Expiration RADIUS attribute
256 # if ( $new->pkgnum != $old->pkgnum && $new->cust_pkg->part_pkg->is_prepaid ) {
257 # my $svc_x = $new->svc_x;
258 # local($FS::Record::nowarn_identical) = 1;
259 # my $error = $svc_x->export('replace');
261 # $dbh->rollback if $oldAutoCommit;
262 # return $error if $error;
266 #my $error = $new->SUPER::replace($old, @_);
267 my $error = $new->SUPER::replace($old);
269 $dbh->rollback if $oldAutoCommit;
270 return $error if $error;
273 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
280 Checks all fields to make sure this is a valid service. If there is an error,
281 returns the error, otherwise returns false. Called by the insert and
290 $self->ut_numbern('svcnum')
291 || $self->ut_numbern('pkgnum')
292 || $self->ut_number('svcpart')
293 || $self->ut_numbern('overlimit')
295 return $error if $error;
297 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
298 return "Unknown svcpart" unless $part_svc;
300 if ( $self->pkgnum ) {
301 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
302 return "Unknown pkgnum" unless $cust_pkg;
303 ($part_svc) = grep { $_->svcpart == $self->svcpart } $cust_pkg->part_svc;
305 return "Already ". $part_svc->get('num_cust_svc'). " ". $part_svc->svc.
306 " services for pkgnum ". $self->pkgnum
307 if $part_svc->get('num_avail') == 0 and !$ignore_quantity;
315 Returns the definition for this service, as a FS::part_svc object (see
323 ? $self->{'_svcpart'}
324 : qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
329 Returns the package this service belongs to, as a FS::cust_pkg object (see
336 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
341 Returns the pkg_svc record for for this service, if applicable.
347 my $cust_pkg = $self->cust_pkg;
348 return undef unless $cust_pkg;
350 qsearchs( 'pkg_svc', { 'svcpart' => $self->svcpart,
351 'pkgpart' => $cust_pkg->pkgpart,
358 Returns the date this service was inserted.
364 $self->h_date('insert');
369 Returns a list consisting of:
370 - The name of this service (from part_svc)
371 - A meaningful identifier (username, domain, or mail alias)
372 - The table name (i.e. svc_domain) for this service
377 my($label, $value, $svcdb) = $cust_svc->label;
381 Like the B<label> method, except the second item in the list ("meaningful
382 identifier") may be longer - typically, a full name is included.
386 sub label { shift->_label('svc_label', @_); }
387 sub label_long { shift->_label('svc_label_long', @_); }
392 my $svc_x = $self->svc_x
393 or return "can't find ". $self->part_svc->svcdb. '.svcnum '. $self->svcnum;
395 $self->$method($svc_x);
398 sub svc_label { shift->_svc_label('label', @_); }
399 sub svc_label_long { shift->_svc_label('label_long', @_); }
402 my( $self, $method, $svc_x ) = ( shift, shift, shift );
405 $self->part_svc->svc,
407 $self->part_svc->svcdb,
415 Returns a listref of html elements associated with this service's exports.
421 my $svc_x = $self->svc_x
422 or return "can't find ". $self->part_svc->svcdb. '.svcnum '. $self->svcnum;
424 $svc_x->export_links;
427 =item export_getsettings
429 Returns two hashrefs of settings associated with this service's exports.
433 sub export_getsettings {
435 my $svc_x = $self->svc_x
436 or return "can't find ". $self->part_svc->svcdb. '.svcnum '. $self->svcnum;
438 $svc_x->export_getsettings;
444 Returns the FS::svc_XXX object for this service (i.e. an FS::svc_acct object or
445 FS::svc_domain object, etc.)
451 my $svcdb = $self->part_svc->svcdb;
452 if ( $svcdb eq 'svc_acct' && $self->{'_svc_acct'} ) {
453 $self->{'_svc_acct'};
455 require "FS/$svcdb.pm";
456 warn "$me svc_x: part_svc.svcpart ". $self->part_svc->svcpart.
457 ", so searching for $svcdb.svcnum ". $self->svcnum. "\n"
459 qsearchs( $svcdb, { 'svcnum' => $self->svcnum } );
463 =item seconds_since TIMESTAMP
465 See L<FS::svc_acct/seconds_since>. Equivalent to
466 $cust_svc->svc_x->seconds_since, but more efficient. Meaningless for records
467 where B<svcdb> is not "svc_acct".
471 #internal session db deprecated (or at least on hold)
472 sub seconds_since { 'internal session db deprecated'; };
473 ##note: implementation here, POD in FS::svc_acct
475 # my($self, $since) = @_;
477 # my $sth = $dbh->prepare(' SELECT SUM(logout-login) FROM session
480 # AND logout IS NOT NULL'
481 # ) or die $dbh->errstr;
482 # $sth->execute($self->svcnum, $since) or die $sth->errstr;
483 # $sth->fetchrow_arrayref->[0];
486 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
488 See L<FS::svc_acct/seconds_since_sqlradacct>. Equivalent to
489 $cust_svc->svc_x->seconds_since_sqlradacct, but more efficient. Meaningless
490 for records where B<svcdb> is not "svc_acct".
494 #note: implementation here, POD in FS::svc_acct
495 sub seconds_since_sqlradacct {
496 my($self, $start, $end) = @_;
498 my $mes = "$me seconds_since_sqlradacct:";
500 my $svc_x = $self->svc_x;
502 my @part_export = $self->part_svc->part_export_usage;
503 die "no accounting-capable exports are enabled for ". $self->part_svc->svc.
504 " service definition"
509 foreach my $part_export ( @part_export ) {
511 next if $part_export->option('ignore_accounting');
513 warn "$mes connecting to sqlradius database\n"
516 my $dbh = DBI->connect( map { $part_export->option($_) }
517 qw(datasrc username password) )
518 or die "can't connect to sqlradius database: ". $DBI::errstr;
520 warn "$mes connected to sqlradius database\n"
523 #select a unix time conversion function based on database type
524 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
526 my $username = $part_export->export_username($svc_x);
530 warn "$mes finding closed sessions completely within the given range\n"
535 if ($part_export->option('process_single_realm')) {
536 $realm = 'AND Realm = ?';
537 $realmparam = $part_export->option('realm');
540 my $sth = $dbh->prepare("SELECT SUM(acctsessiontime)
544 AND $str2time AcctStartTime) >= ?
545 AND $str2time AcctStopTime ) < ?
546 AND $str2time AcctStopTime ) > 0
547 AND AcctStopTime IS NOT NULL"
548 ) or die $dbh->errstr;
549 $sth->execute($username, ($realm ? $realmparam : ()), $start, $end)
551 my $regular = $sth->fetchrow_arrayref->[0];
553 warn "$mes finding open sessions which start in the range\n"
556 # count session start->range end
557 $query = "SELECT SUM( ? - $str2time AcctStartTime ) )
561 AND $str2time AcctStartTime ) >= ?
562 AND $str2time AcctStartTime ) < ?
563 AND ( ? - $str2time AcctStartTime ) ) < 86400
564 AND ( $str2time AcctStopTime ) = 0
565 OR AcctStopTime IS NULL )";
566 $sth = $dbh->prepare($query) or die $dbh->errstr;
569 ($realm ? $realmparam : ()),
573 or die $sth->errstr. " executing query $query";
574 my $start_during = $sth->fetchrow_arrayref->[0];
576 warn "$mes finding closed sessions which start before the range but stop during\n"
579 #count range start->session end
580 $sth = $dbh->prepare("SELECT SUM( $str2time AcctStopTime ) - ? )
584 AND $str2time AcctStartTime ) < ?
585 AND $str2time AcctStopTime ) >= ?
586 AND $str2time AcctStopTime ) < ?
587 AND $str2time AcctStopTime ) > 0
588 AND AcctStopTime IS NOT NULL"
589 ) or die $dbh->errstr;
590 $sth->execute( $start,
592 ($realm ? $realmparam : ()),
597 my $end_during = $sth->fetchrow_arrayref->[0];
599 warn "$mes finding closed sessions which start before the range but stop after\n"
602 # count range start->range end
603 # don't count open sessions anymore (probably missing stop record)
604 $sth = $dbh->prepare("SELECT COUNT(*)
608 AND $str2time AcctStartTime ) < ?
609 AND ( $str2time AcctStopTime ) >= ?
611 # OR AcctStopTime = 0
612 # OR AcctStopTime IS NULL )"
613 ) or die $dbh->errstr;
614 $sth->execute($username, ($realm ? $realmparam : ()), $start, $end )
616 my $entire_range = ($end-$start) * $sth->fetchrow_arrayref->[0];
618 $seconds += $regular + $end_during + $start_during + $entire_range;
620 warn "$mes done finding sessions\n"
629 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
631 See L<FS::svc_acct/attribute_since_sqlradacct>. Equivalent to
632 $cust_svc->svc_x->attribute_since_sqlradacct, but more efficient. Meaningless
633 for records where B<svcdb> is not "svc_acct".
637 #note: implementation here, POD in FS::svc_acct
638 #(false laziness w/seconds_since_sqlradacct above)
639 sub attribute_since_sqlradacct {
640 my($self, $start, $end, $attrib) = @_;
642 my $mes = "$me attribute_since_sqlradacct:";
644 my $svc_x = $self->svc_x;
646 my @part_export = $self->part_svc->part_export_usage;
647 die "no accounting-capable exports are enabled for ". $self->part_svc->svc.
648 " service definition"
654 foreach my $part_export ( @part_export ) {
656 next if $part_export->option('ignore_accounting');
658 warn "$mes connecting to sqlradius database\n"
661 my $dbh = DBI->connect( map { $part_export->option($_) }
662 qw(datasrc username password) )
663 or die "can't connect to sqlradius database: ". $DBI::errstr;
665 warn "$mes connected to sqlradius database\n"
668 #select a unix time conversion function based on database type
669 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
671 my $username = $part_export->export_username($svc_x);
673 warn "$mes SUMing $attrib sessions\n"
678 if ($part_export->option('process_single_realm')) {
679 $realm = 'AND Realm = ?';
680 $realmparam = $part_export->option('realm');
683 my $sth = $dbh->prepare("SELECT SUM($attrib)
687 AND $str2time AcctStopTime ) >= ?
688 AND $str2time AcctStopTime ) < ?
689 AND AcctStopTime IS NOT NULL"
690 ) or die $dbh->errstr;
691 $sth->execute($username, ($realm ? $realmparam : ()), $start, $end)
694 my $row = $sth->fetchrow_arrayref;
695 $sum += $row->[0] if defined($row->[0]);
697 warn "$mes done SUMing sessions\n"
706 =item get_session_history TIMESTAMP_START TIMESTAMP_END
708 See L<FS::svc_acct/get_session_history>. Equivalent to
709 $cust_svc->svc_x->get_session_history, but more efficient. Meaningless for
710 records where B<svcdb> is not "svc_acct".
714 sub get_session_history {
715 my($self, $start, $end, $attrib) = @_;
719 my @part_export = $self->part_svc->part_export_usage;
720 die "no accounting-capable exports are enabled for ". $self->part_svc->svc.
721 " service definition"
727 foreach my $part_export ( @part_export ) {
729 @{ $part_export->usage_sessions( $start, $end, $self->svc_x ) };
740 Behaviour of changing the svcpart of cust_svc records is undefined and should
741 possibly be prohibited, and pkg_svc records are not checked.
743 pkg_svc records are not checked in general (here).
745 Deleting this record doesn't check or delete the svc_* record associated
748 In seconds_since_sqlradacct, specifying a DATASRC/USERNAME/PASSWORD instead of
749 a DBI database handle is not yet implemented.
753 L<FS::Record>, L<FS::cust_pkg>, L<FS::part_svc>, L<FS::pkg_svc>,
754 schema.html from the base documentation